Skip to content

Fix RDGW certificate deployment with proper key permissions - #25

Merged
gpas45 merged 1 commit into
mainfrom
claude/rdgw-certificate-acme-account-tzyyer
Jul 9, 2026
Merged

Fix RDGW certificate deployment with proper key permissions#25
gpas45 merged 1 commit into
mainfrom
claude/rdgw-certificate-acme-account-tzyyer

Conversation

@gpas45

@gpas45 gpas45 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Summary

This PR fixes a critical issue where the RD Gateway service fails to use newly imported certificates because the NETWORK SERVICE account lacks read permissions on the private key file.

Key Changes

  • New Grant-RDGWKeyAccess function: Grants the NETWORK SERVICE account read access to the certificate's private key file. This function handles both RSA-CNG and legacy RSA key storage locations in the Windows crypto key store.
  • Explicit certificate import: Added logic to import the PFX certificate into LocalMachine\My before applying it to RD Gateway, with permission setup immediately after import.
  • Enhanced error handling:
    • Added -ErrorAction Stop to Set-RDGWCertificate call to ensure errors are properly caught (the cmdlet suppresses errors by default)
    • Added verification that the certificate thumbprint was actually applied to the gateway, catching silent failures
  • Improved comments: Added Russian-language explanations of the issues being addressed

Implementation Details

The Grant-RDGWKeyAccess function:

  • Extracts the RSA private key from the X509 certificate
  • Determines the correct key file path based on whether the key is stored as RSA-CNG or legacy RSA
  • Validates the key file exists before attempting to modify permissions
  • Adds a FileSystemAccessRule granting Read access to the NETWORK SERVICE account
  • Applies the updated ACL to the key file

The main script flow now:

  1. Imports the certificate if not already present
  2. Grants key access to NETWORK SERVICE
  3. Applies the certificate to RD Gateway with proper error handling
  4. Verifies the thumbprint was actually applied

https://claude.ai/code/session_01FEgsWVRjt7q8d48YLXZKHY

…ateway

Set-RDGWCertificate импортирует pfx через Import-PfxCertificate, который
не выдаёт NETWORK SERVICE (учётная запись службы TSGateway) доступ к
закрытому ключу — из-за этого сертификат либо не применяется, либо
привязка вручную падает с "Отказано в доступе".

Также добавлена явная -ErrorAction Stop и проверка применённого
отпечатка: Set-RDGWCertificate гасит свои ошибки через trap {...; return}
и превращает их в non-terminating Write-Error, которая раньше проходила
мимо try/catch скрипта без единого сообщения.
@gpas45
gpas45 merged commit 89c7885 into main Jul 9, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants