From 1bce65a760fad255cad034f3ec8050fdf6b2412f Mon Sep 17 00:00:00 2001 From: Shriyansh Singh Sengar Date: Wed, 18 Nov 2026 16:01:55 +0530 Subject: [PATCH] Azure Local: up.ps1 prepares Active Directory itself, after the cluster The README listed a prepared Active Directory as a prerequisite, while docs/authentication.md said to run ad-setup.ps1 once the cluster exists (it keeps the bind account's password and the directory's CA as Secrets in the cluster). Following the README, up.ps1 stopped at the Keycloak step. setup-platform.ps1 now runs ad-setup.ps1 as the ActiveDirectory step, right after the cluster and before Keycloak. It takes the domain controller from the Keycloak settings (AdServer, AdDomain, AdOuName, now aliases of ad-setup.ps1's parameters) and asks for a domain administrator. Once the cluster holds the bind account's password, later runs skip it. -Skip ActiveDirectory keeps the existing path for a directory prepared by hand (AdBindUpn and AdCaCertificatePath). Preflight checks that this machine reaches the domain controller over PowerShell remoting, which ad-setup.ps1 uses, before anything is created. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- samples/OrderManagementApp/README.md | 10 ++++-- .../OrderManagementApp/docs/authentication.md | 17 ++++++---- .../docs/azure-local-settings.md | 9 +++-- .../scripts/infra/azurelocal/ad-setup.ps1 | 11 +++++-- .../scripts/infra/azurelocal/preflight.ps1 | 23 +++++++++++-- .../scripts/lib/settings.ps1 | 13 ++++---- .../scripts/settings.example.json | 4 +-- .../scripts/setup-platform.ps1 | 33 +++++++++++++++---- samples/OrderManagementApp/scripts/up.ps1 | 8 +++-- 9 files changed, 92 insertions(+), 36 deletions(-) diff --git a/samples/OrderManagementApp/README.md b/samples/OrderManagementApp/README.md index 59cfaf2..978fe62 100644 --- a/samples/OrderManagementApp/README.md +++ b/samples/OrderManagementApp/README.md @@ -91,8 +91,10 @@ All commands run from this folder, `samples/OrderManagementApp`. rather than from the nodes. - Free addresses on the site network for the AKS cluster: its control plane, a pool for the nodes and a small range for the load balancer. -- The site's Active Directory, with the app's groups and a bind account for Keycloak. See - [Connect your Active Directory](./docs/authentication.md#connect-your-active-directory). +- The site's Active Directory, and a domain administrator: `up.ps1` asks for one to add the app's + groups and a bind account for Keycloak, over PowerShell remoting from the management machine. See + [Connect your Active Directory](./docs/authentication.md#connect-your-active-directory), which also + covers preparing the directory yourself. - Contributor and User Access Administrator on the subscription, and the right to create groups in Entra ID (or an existing group for the cluster's administrators). @@ -176,7 +178,9 @@ says where to find each value. Then run: ./scripts/up.ps1 -Target AzureLocal ``` -It checks the site, creates the AKS cluster and everything the app needs on it, and deploys the app. +It checks the site, creates the AKS cluster, asks for a domain administrator to add the app's groups +and Keycloak's bind account to Active Directory, sets up everything the app needs on the cluster, +and deploys the app. Open the address it prints and sign in with an AD account in one of the app's groups. The first time, on a machine outside the domain, trust the site CA with the command it printed. diff --git a/samples/OrderManagementApp/docs/authentication.md b/samples/OrderManagementApp/docs/authentication.md index fa1a357..eed386c 100644 --- a/samples/OrderManagementApp/docs/authentication.md +++ b/samples/OrderManagementApp/docs/authentication.md @@ -116,16 +116,19 @@ needs: - LDAPS on the domain controller, which needs a server certificate from a certificate authority. [ad-setup.ps1](../scripts/infra/azurelocal/ad-setup.ps1) creates all of this and hands the bind -account and the CA certificate to the cluster. Run it from the management machine as a -domain administrator, once the cluster exists: +account and the CA certificate to the cluster. `up.ps1` runs it for you, as the ActiveDirectory step, +right after it creates the cluster: it asks for a domain administrator's credentials and reaches the +domain controller, `Keycloak.AdServer`, over PowerShell remoting from the management machine. Once +the cluster holds the bind account's password, later runs skip the step. To change the directory +later, run it yourself: ```powershell ./scripts/infra/azurelocal/ad-setup.ps1 -ComputerName -DomainName ` -KubeContext ``` -If your directory already has these, put the bind account in `Keycloak.AdBindUpn` and the CA's -certificate file in `Keycloak.AdCaCertificatePath` ([settings](./azure-local-settings.md#active-directory)); -the first `up.ps1` run asks for the bind password and keeps it in the cluster. To change the bind -password, run `ad-setup.ps1 -ResetBindPassword`, then `keycloak.ps1`. To use an OU other than `OMS`, -pass `keycloak.ps1 -AdOuName`. +If your directory already has these, run `up.ps1 -Skip ActiveDirectory`, and put the bind account in +`Keycloak.AdBindUpn` and the CA's certificate file in `Keycloak.AdCaCertificatePath` +([settings](./azure-local-settings.md#active-directory)): the first run asks for the bind password +and keeps it in the cluster. To change the bind password, run `ad-setup.ps1 -ResetBindPassword`, then +`keycloak.ps1`. To use an OU other than `OMS`, set `Keycloak.AdOuName`. diff --git a/samples/OrderManagementApp/docs/azure-local-settings.md b/samples/OrderManagementApp/docs/azure-local-settings.md index 865c126..482ded0 100644 --- a/samples/OrderManagementApp/docs/azure-local-settings.md +++ b/samples/OrderManagementApp/docs/azure-local-settings.md @@ -54,10 +54,13 @@ free, and the three ranges must not overlap. | Setting | What to put | |---|---| -| `Keycloak.AdServer` | Your domain controller's full name, for example `dc01.contoso.local`. It must match the name on its LDAPS certificate. | +| `Keycloak.AdServer` | Your domain controller's full name, for example `dc01.contoso.local`. It must match the name on its LDAPS certificate. The ActiveDirectory step reaches it over PowerShell remoting (5985), so the management machine must resolve this name. | | `Keycloak.AdDomain` | Your Active Directory domain, for example `contoso.local` | -| `Keycloak.AdBindUpn` | The read-only account Keycloak reads the directory with, for example `svc-keycloak@contoso.local`. The first run asks for its password and keeps it in the cluster; it never goes in this file. | -| `Keycloak.AdCaCertificatePath` | The certificate (PEM) of the CA that issued your domain controller's LDAPS certificate. It's public; on a domain-joined machine export it with `certutil -ca.cert ca.cer` and convert it with `certutil -encode ca.cer ca.crt`. | +| `Keycloak.AdBindUpn` | Only if you prepared the directory yourself (`up.ps1 -Skip ActiveDirectory`): the read-only account Keycloak reads the directory with, for example `svc-keycloak@contoso.local`. The first run asks for its password and keeps it in the cluster; it never goes in this file. | +| `Keycloak.AdCaCertificatePath` | Only with `-Skip ActiveDirectory`: the certificate (PEM) of the CA that issued your domain controller's LDAPS certificate. It's public; on a domain-joined machine export it with `certutil -ca.cert ca.cer` and convert it with `certutil -encode ca.cer ca.crt`. | + +Otherwise leave the last two empty: the ActiveDirectory step creates the account and hands the CA to +the cluster ([Connect your Active Directory](./authentication.md#connect-your-active-directory)). ## Check before you deploy diff --git a/samples/OrderManagementApp/scripts/infra/azurelocal/ad-setup.ps1 b/samples/OrderManagementApp/scripts/infra/azurelocal/ad-setup.ps1 index c29e333..9465823 100644 --- a/samples/OrderManagementApp/scripts/infra/azurelocal/ad-setup.ps1 +++ b/samples/OrderManagementApp/scripts/infra/azurelocal/ad-setup.ps1 @@ -34,6 +34,10 @@ If you'd rather not run it, make the same objects with the Active Directory PowerShell module instead; docs/authentication.md lists them under "Connect your Active Directory". + setup-platform.ps1 (and so up.ps1) runs it as the ActiveDirectory step, right after it creates + the cluster, with the domain controller from the Keycloak settings: AdServer, AdDomain and + AdOuName. Run it yourself to change the directory later. + The script is safe to re-run. Accounts that already exist keep their passwords, so Keycloak's directory sign-in keeps working; pass -ResetBindPassword to give the bind account a new one, then run keycloak.ps1, which applies it to Keycloak. The bind account's limits cover the containers @@ -78,9 +82,10 @@ [CmdletBinding(DefaultParameterSetName = 'Remoting')] param( [Parameter(Mandatory = $true, ParameterSetName = 'Direct')][string]$VmName, - [Parameter(Mandatory = $true, ParameterSetName = 'Remoting')][string]$ComputerName, - [Parameter(Mandatory = $true)][string]$DomainName, - [string]$OuName = 'OMS', + # The aliases are the Keycloak settings' names, which setup-platform.ps1 passes. + [Parameter(Mandatory = $true, ParameterSetName = 'Remoting')][Alias('AdServer')][string]$ComputerName, + [Parameter(Mandatory = $true)][Alias('AdDomain')][string]$DomainName, + [Alias('AdOuName')][string]$OuName = 'OMS', [pscredential]$DomainAdminCredential, diff --git a/samples/OrderManagementApp/scripts/infra/azurelocal/preflight.ps1 b/samples/OrderManagementApp/scripts/infra/azurelocal/preflight.ps1 index 6335fa3..64fe69a 100644 --- a/samples/OrderManagementApp/scripts/infra/azurelocal/preflight.ps1 +++ b/samples/OrderManagementApp/scripts/infra/azurelocal/preflight.ps1 @@ -11,7 +11,8 @@ Azure signed in, the subscription, resource providers, the Azure Local instance's custom location and Arc resource bridge, the virtual switch Network addresses for the control plane, node pool and load balancer fit together - Directory the domain controller resolves through the site DNS and answers on LDAPS + Directory the domain controller resolves through the site DNS and answers on LDAPS; for + the ActiveDirectory step, this machine reaches it over PowerShell remoting Cluster if it already exists: reachable, nodes ready, the connected registry's storage class and ClusterIP, the AD Secrets Keycloak needs, a storage class for the observability volumes @@ -275,10 +276,24 @@ else { $ldaps = Test-TcpPort $ldapsTarget 636 if ($ldaps) { Add-Result Directory 'LDAPS (636)' PASS } else { Add-Result Directory 'LDAPS (636)' WARN "no answer from this machine; Keycloak in the cluster needs it" } + + # The ActiveDirectory step runs ad-setup.ps1 on this machine, which reaches the controller by + # name over PowerShell remoting (WinRM), so through this machine's own resolver. + if ('ActiveDirectory' -notin $Skip) { + $local = try { [Net.Dns]::GetHostAddresses($s.AdServer) | Where-Object AddressFamily -eq 'InterNetwork' | Select-Object -First 1 } catch { $null } + if (-not $local) { + Add-Result Directory 'PowerShell remoting (5985)' FAIL "this machine doesn't resolve $($s.AdServer), which the ActiveDirectory step connects to; use a management machine that resolves the site's domain, or prepare the directory yourself and -Skip ActiveDirectory" + } + elseif (Test-TcpPort $local.IPAddressToString 5985) { Add-Result Directory 'PowerShell remoting (5985)' PASS "$($s.AdServer) -> $($local.IPAddressToString)" } + else { Add-Result Directory 'PowerShell remoting (5985)' FAIL "no answer from $($s.AdServer) ($($local.IPAddressToString)); the ActiveDirectory step connects to it over PowerShell remoting" } + } } # Keycloak takes the directory's CA from this file the first time, so a wrong path only shows there. -if ($s.AdCaCertificatePath -and $s.AdCaCertificatePath -notlike '<*') { +if ('ActiveDirectory' -notin $Skip) { + Add-Result Directory 'Directory CA certificate' PASS 'the ActiveDirectory step hands it to the cluster' +} +elseif ($s.AdCaCertificatePath -and $s.AdCaCertificatePath -notlike '<*') { if ((Test-Path $s.AdCaCertificatePath) -and ((Get-Content $s.AdCaCertificatePath -Raw) -match '-----BEGIN CERTIFICATE-----')) { Add-Result Directory 'Directory CA certificate' PASS $s.AdCaCertificatePath } @@ -353,7 +368,9 @@ else { Add-Result Cluster "Secret identity/$secret" PASS } else { - $how = if ($secret -eq 'oms-ad-bind') { "the Keycloak step asks for the bind account's password" } else { 'the Keycloak step reads AdCaCertificatePath' } + $how = if ('ActiveDirectory' -notin $Skip) { 'the ActiveDirectory step creates it' } + elseif ($secret -eq 'oms-ad-bind') { "the Keycloak step asks for the bind account's password" } + else { 'the Keycloak step reads AdCaCertificatePath' } Add-Result Cluster "Secret identity/$secret" PASS "not there yet; $how" } } diff --git a/samples/OrderManagementApp/scripts/lib/settings.ps1 b/samples/OrderManagementApp/scripts/lib/settings.ps1 index c2b9a84..005192a 100644 --- a/samples/OrderManagementApp/scripts/lib/settings.ps1 +++ b/samples/OrderManagementApp/scripts/lib/settings.ps1 @@ -10,15 +10,16 @@ function Read-OmsSettings([string]$Target, [string]$Path) { $all[$Target] } -# 'Common' values go only to scripts that declare the parameter; the step's own section goes as is, -# so a misspelt name fails loudly instead of being ignored. -function Get-OmsStepArguments([hashtable]$Settings, [string]$Step, [string]$ScriptPath) { - $declared = (Get-Command $ScriptPath).Parameters.Keys +# 'Common' values, and those of any -Shared section, go only to scripts that declare the parameter, +# by name or alias; the step's own section goes as is, so a misspelt name fails loudly instead of +# being ignored. +function Get-OmsStepArguments([hashtable]$Settings, [string]$Step, [string]$ScriptPath, [string[]]$Shared = @()) { + $declared = foreach ($parameter in (Get-Command $ScriptPath).Parameters.Values) { $parameter.Name; $parameter.Aliases } $arguments = @{} - foreach ($section in 'Common', $Step) { + foreach ($section in @('Common') + @($Shared | Where-Object { $_ }) + $Step) { if (-not $Settings.ContainsKey($section)) { continue } foreach ($name in $Settings[$section].Keys) { - if ($section -eq 'Common' -and $name -notin $declared) { continue } + if ($section -ne $Step -and $name -notin $declared) { continue } $arguments[$name] = $Settings[$section][$name] } } diff --git a/samples/OrderManagementApp/scripts/settings.example.json b/samples/OrderManagementApp/scripts/settings.example.json index 1925914..b0a5eb2 100644 --- a/samples/OrderManagementApp/scripts/settings.example.json +++ b/samples/OrderManagementApp/scripts/settings.example.json @@ -62,8 +62,8 @@ "Keycloak": { "AdServer": "", "AdDomain": "", - "AdBindUpn": "svc-keycloak@", - "AdCaCertificatePath": "", + "AdBindUpn": "", + "AdCaCertificatePath": "", "AllowInClusterIssuer": true }, "Observability": { diff --git a/samples/OrderManagementApp/scripts/setup-platform.ps1 b/samples/OrderManagementApp/scripts/setup-platform.ps1 index 492d164..fec4097 100644 --- a/samples/OrderManagementApp/scripts/setup-platform.ps1 +++ b/samples/OrderManagementApp/scripts/setup-platform.ps1 @@ -16,6 +16,8 @@ AzureLocal Cluster infra/azurelocal/provision-infra.ps1 AKS Arc cluster, ACR, load balancer + ActiveDirectory infra/azurelocal/ad-setup.ps1 the app's OU, groups and Keycloak bind account in the site's + directory; asks for a domain administrator Images infra/azurelocal/import-images.ps1 third-party images mirrored into the ACR Registry infra/azurelocal/connected-registry.ps1 in-cluster registry that keeps serving offline KeyVault infra/azurelocal/key-vault.ps1 Key Vault and its Secret Store extension, for secrets from Azure @@ -27,15 +29,18 @@ Observability infra/azurelocal/provision-observability.ps1 metrics, logs, traces, alerts and Grafana, in the cluster Values come from the target's section of scripts/settings.json (see settings.example.json): 'Common' goes to every step that takes the parameter, a section named after the step to that - step only. Keep secrets out of it; the scripts read those from Kubernetes Secrets. + step only. The ActiveDirectory step also takes the domain controller from the Keycloak section. + Keep secrets out of it; the scripts read those from Kubernetes Secrets. On Azure Local, 'Common' Availability sets how the backing services run, for the platform and the app alike: 'high' (the default) replicates them across nodes and backs the databases up, with operators; 'standard' runs one copy of each, with no operators and no backups. - Azure Local needs a read-only bind account in the site's Active Directory for Keycloak: set - Keycloak.AdBindUpn and Keycloak.AdCaCertificatePath, and the Keycloak step asks for the - account's password the first time. infra/azurelocal/ad-setup.ps1 can create the account. + Azure Local needs a read-only bind account in the site's Active Directory for Keycloak. The + ActiveDirectory step creates it, with the app's OU and groups, and asks for a domain + administrator; once the cluster holds the bind account's password, later runs skip it. If your + directory is already prepared, -Skip ActiveDirectory and set Keycloak.AdBindUpn and + Keycloak.AdCaCertificatePath instead: the Keycloak step then asks for the account's password. On Azure Local, run it from a management machine that can sign in to Azure and reach the site network: some steps use both. @@ -54,13 +59,14 @@ param( [Parameter(Mandatory)] [ValidateSet('Azure', 'AzureLocal')] [string]$Target, - [ValidateSet('Cluster', 'Images', 'Registry', 'KeyVault', 'Operators', 'ObjectStore', 'Keycloak', 'Ingress', 'Identity', 'Observability')] + [ValidateSet('Cluster', 'ActiveDirectory', 'Images', 'Registry', 'KeyVault', 'Operators', 'ObjectStore', 'Keycloak', 'Ingress', 'Identity', 'Observability')] [string[]]$Skip = @(), [string]$SettingsPath ) $ErrorActionPreference = 'Stop' . "$PSScriptRoot/lib/settings.ps1" +. "$PSScriptRoot/lib/secrets.ps1" $steps = if ($Target -eq 'Azure') { [ordered]@{ @@ -72,6 +78,9 @@ $steps = if ($Target -eq 'Azure') { else { [ordered]@{ Cluster = 'infra/azurelocal/provision-infra.ps1' + # After the cluster, which keeps the bind account's password and the directory's CA as + # Secrets; before Keycloak, which reads them. + ActiveDirectory = 'infra/azurelocal/ad-setup.ps1' Images = 'infra/azurelocal/import-images.ps1' Registry = 'infra/azurelocal/connected-registry.ps1' KeyVault = 'infra/azurelocal/key-vault.ps1' @@ -87,6 +96,8 @@ else { } $settings = Read-OmsSettings $Target $SettingsPath +# Sections a step reads besides its own: the domain controller is in Keycloak's settings. +$shared = @{ ActiveDirectory = @('Keycloak') } # Only highly available backing services need the operators. $availability = if ($settings.ContainsKey('Common')) { $settings['Common']['Availability'] } if ($Target -eq 'AzureLocal' -and $availability -eq 'standard') { @@ -101,7 +112,17 @@ try { continue } $script = Join-Path $PSScriptRoot $steps[$step] - $arguments = Get-OmsStepArguments $settings $step $script + $arguments = Get-OmsStepArguments $settings $step $script $shared[$step] + # The directory is prepared once. When the cluster already holds the bind account's password, + # there is nothing to do and no reason to ask for a domain administrator again; run + # ad-setup.ps1 itself to change the directory later. + if ($step -eq 'ActiveDirectory') { + $bindPassword = try { Get-OmsSecretValue -KubeContext $arguments['KubeContext'] -Namespace identity -Name oms-ad-bind -Key bind-password } catch { $null } + if ($bindPassword) { + Write-Host "==> [$Target] $step already done: the cluster holds Secret identity/oms-ad-bind" -ForegroundColor DarkGray + continue + } + } $shown = ($arguments.GetEnumerator() | Sort-Object Key | ForEach-Object { "-$($_.Key) $(@($_.Value) -join ',')" }) -join ' ' if ($PSCmdlet.ShouldProcess("scripts/$($steps[$step]) $shown", "$Target $step")) { Write-Host "`n==> [$Target] $step : scripts/$($steps[$step])" -ForegroundColor Magenta diff --git a/samples/OrderManagementApp/scripts/up.ps1 b/samples/OrderManagementApp/scripts/up.ps1 index e641744..b461f8a 100644 --- a/samples/OrderManagementApp/scripts/up.ps1 +++ b/samples/OrderManagementApp/scripts/up.ps1 @@ -15,8 +15,10 @@ On Azure Local, run it from a management machine that can sign in to Azure and reach the site network. - Before the first run on Azure Local, the site's Active Directory needs the app's groups and a - bind account for Keycloak; see "Connect your Active Directory" in docs/authentication.md. + On Azure Local, once the cluster exists it asks for a domain administrator of the site's Active + Directory, to add the app's OU, groups and Keycloak's bind account (the ActiveDirectory step). + If your directory is already prepared, -Skip ActiveDirectory; see "Connect your Active + Directory" in docs/authentication.md. .EXAMPLE ./scripts/up.ps1 -Target AzureLocal @@ -31,7 +33,7 @@ param( [ValidateSet('Azure', 'AzureLocal')] [string]$Target, # Platform steps to leave out, as for setup-platform.ps1. - [ValidateSet('Cluster', 'Images', 'Registry', 'KeyVault', 'Operators', 'ObjectStore', 'Keycloak', 'Ingress', 'Identity', 'Observability')] + [ValidateSet('Cluster', 'ActiveDirectory', 'Images', 'Registry', 'KeyVault', 'Operators', 'ObjectStore', 'Keycloak', 'Ingress', 'Identity', 'Observability')] [string[]]$Skip = @(), [string]$SettingsPath, [switch]$SkipPreflight