Skip to content

Commit 2f07203

Browse files
WEB-4891: make the discovery key optional for the per-user onboard cron (#206)
Per-user onboarding now scans with the user's own API key, so the scheduled `onboard` run no longer needs a separate discovery key. Stop requiring it at install time and in the cron wrapper (bash + PowerShell); forward it only when one was stored, for older/MDM-style setups. MDM/all-users scheduled scans use the `discover` command and are unchanged. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent c5f4b60 commit 2f07203

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

setup-scheduled-scan.ps1

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ Unbound Scheduled Run Setup (Windows)
9797
9898
Usage:
9999
Install (discover): .\setup-scheduled-scan.ps1 -ApiKey <key> -Domain <url>
100-
Install (onboard): .\setup-scheduled-scan.ps1 -Command onboard -ApiKey <key> -DiscoveryKey <key> [-Domain <url>]
100+
Install (onboard): .\setup-scheduled-scan.ps1 -Command onboard -ApiKey <key> [-Domain <url>]
101101
Uninstall: .\setup-scheduled-scan.ps1 -Uninstall
102102
103103
Options:
104104
-Command <name> 'discover' (default) or 'onboard'
105105
-ApiKey <key> User API key (or discovery key when -Command discover)
106-
-DiscoveryKey <k> Discovery key (required for -Command onboard)
106+
-DiscoveryKey <k> Org discovery key (optional; only for sudo/MDM all-users scans)
107107
-Domain <url> Backend URL
108108
-Uninstall Remove the scheduled task
109109
'@
@@ -252,10 +252,8 @@ switch ($Command) {
252252
Write-Log ("Discover exited with code {0}" -f $ec)
253253
}
254254
'onboard' {
255-
if ([string]::IsNullOrEmpty($DiscoveryKey)) {
256-
Write-Log "ERROR: discovery_key missing from Credential Manager (required for onboard command)"
257-
exit 1
258-
}
255+
# Per-user onboarding scans with the user's own API key (WEB-4891), so a
256+
# discovery key is optional — forward it only when one was stored.
259257
$unbound = (Get-Command unbound -ErrorAction SilentlyContinue).Source
260258
if (-not $unbound) {
261259
Write-Log "ERROR: 'unbound' CLI not found in PATH. Install with: npm install -g unbound-cli"
@@ -264,7 +262,7 @@ switch ($Command) {
264262
# Credentials go via env vars — Win32_Process.CommandLine is readable by any
265263
# authenticated user and Windows Event Log 4688 captures full command lines.
266264
$env:UNBOUND_API_KEY = $ApiKey
267-
$env:UNBOUND_DISCOVERY_KEY = $DiscoveryKey
265+
if (-not [string]::IsNullOrEmpty($DiscoveryKey)) { $env:UNBOUND_DISCOVERY_KEY = $DiscoveryKey }
268266
$cmdArgs = @('onboard')
269267
if (-not [string]::IsNullOrEmpty($Domain)) { $cmdArgs += @('--domain', $Domain) }
270268
Write-Log "Executing: unbound onboard (credentials via env vars) ..."
@@ -401,10 +399,8 @@ if ([string]::IsNullOrEmpty($ApiKey)) {
401399
Write-Host "Error: -ApiKey is required"
402400
Show-Usage
403401
}
404-
if ($Command -eq 'onboard' -and [string]::IsNullOrEmpty($DiscoveryKey)) {
405-
Write-Host "Error: -DiscoveryKey is required when -Command onboard"
406-
Show-Usage
407-
}
402+
# -DiscoveryKey is optional for onboard: per-user onboarding scans with the
403+
# user's own API key (WEB-4891). It is only needed for sudo/MDM all-users scans.
408404
if ($Command -eq 'discover' -and [string]::IsNullOrEmpty($Domain)) {
409405
Write-Host "Error: -Domain is required when -Command discover"
410406
Show-Usage

setup-scheduled-scan.sh

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ usage() {
6767
echo ""
6868
echo "Usage:"
6969
echo " Install (discover): $0 [--command discover] --api-key <key> --domain <url>"
70-
echo " Install (onboard): $0 --command onboard --api-key <key> --discovery-key <key> [--domain <url>]"
70+
echo " Install (onboard): $0 --command onboard --api-key <key> [--domain <url>]"
7171
echo " Uninstall: $0 --uninstall"
7272
echo ""
7373
echo "Options:"
7474
echo " --command <name> Subcommand to schedule: 'discover' (default) or 'onboard'"
7575
echo " --api-key <key> User API key (or discovery key when --command discover)"
76-
echo " --discovery-key <k> Discovery key (required for --command onboard)"
76+
echo " --discovery-key <k> Org discovery key (optional; only for sudo/MDM all-users scans)"
7777
echo " --domain <url> Backend URL (e.g., https://backend.getunbound.ai)"
7878
echo " --uninstall Remove the scheduled job"
7979
echo " --help Show this help message"
@@ -309,11 +309,9 @@ case "\$COMMAND" in
309309
fi
310310
;;
311311
onboard)
312-
# Re-run the full unbound onboard flow daily.
313-
if [ -z "\$DISCOVERY_KEY" ]; then
314-
log "ERROR: discovery_key missing from stored credentials (required for onboard)"
315-
exit 1
316-
fi
312+
# Re-run the full unbound onboard flow daily. Per-user onboarding scans
313+
# with the user's own API key (WEB-4891), so a separate discovery key is
314+
# optional here — it is forwarded only when one was stored (older setups).
317315
# Use the path that was resolved at setup time first. This survives nvm version
318316
# switches, non-standard npm prefix layouts, and any other case where the
319317
# scheduler's minimal PATH wouldn't find the binary. Fall back to a fresh PATH
@@ -331,9 +329,13 @@ case "\$COMMAND" in
331329
ARGS=(onboard)
332330
[ -n "\$DOMAIN" ] && ARGS+=(--domain "\$DOMAIN")
333331
log "Executing: unbound onboard (keys via env vars) \${DOMAIN:+--domain \$DOMAIN}"
334-
UNBOUND_API_KEY="\$API_KEY" UNBOUND_DISCOVERY_KEY="\$DISCOVERY_KEY" "\$UNBOUND_BIN" "\${ARGS[@]}" >> "\$LOG_DIR/scheduled.log" 2>&1 || EXIT_CODE=\$?
332+
if [ -n "\$DISCOVERY_KEY" ]; then
333+
UNBOUND_API_KEY="\$API_KEY" UNBOUND_DISCOVERY_KEY="\$DISCOVERY_KEY" "\$UNBOUND_BIN" "\${ARGS[@]}" >> "\$LOG_DIR/scheduled.log" 2>&1 || EXIT_CODE=\$?
334+
else
335+
UNBOUND_API_KEY="\$API_KEY" "\$UNBOUND_BIN" "\${ARGS[@]}" >> "\$LOG_DIR/scheduled.log" 2>&1 || EXIT_CODE=\$?
336+
fi
335337
if [ \$EXIT_CODE -ne 0 ] && tail -40 "\$LOG_DIR/scheduled.log" | grep -qiE "401|[Ii]nvalid.*(api.?key|key)|[Uu]nauthorized"; then
336-
log "HINT: Auth error detected — your API key may have been rotated in the Unbound dashboard. Re-run to update stored credentials: unbound onboard --set-cron --api-key <NEW_KEY> --discovery-key <NEW_DISCOVERY_KEY>"
338+
log "HINT: Auth error detected — your API key may have been rotated in the Unbound dashboard. Re-run to update stored credentials: unbound onboard --set-cron --api-key <NEW_KEY>"
337339
fi
338340
;;
339341
*)
@@ -647,10 +649,9 @@ if [ -z "$API_KEY" ]; then
647649
echo "Error: --api-key is required"
648650
usage
649651
fi
650-
if [ "$COMMAND" = "onboard" ] && [ -z "$DISCOVERY_KEY" ]; then
651-
echo "Error: --discovery-key is required when --command onboard"
652-
usage
653-
fi
652+
# --discovery-key is optional for onboard: per-user onboarding scans with the
653+
# user's own API key (WEB-4891). It is only needed for sudo/MDM (all-users)
654+
# enrollment, which is scheduled via --command discover, not onboard.
654655
if [ "$COMMAND" = "discover" ] && [ -z "$DOMAIN" ]; then
655656
echo "Error: --domain is required when --command discover"
656657
usage

0 commit comments

Comments
 (0)