Skip to content

Commit e1eb585

Browse files
committed
fix: manage_api_keys.ps1 CL token detection and OpenAI error UX
Two fixes: 1. CourtListener token showed 'Not set' even when stored: Get-CourtListenerToken/Set-CourtListenerToken ran Python with Path('src') as a relative path. If CWD was not the project root, the import failed silently (stderr suppressed), returning null. Fixed by wrapping all Python calls with Push-Location/Pop-Location to the project directory. 2. OpenAI key 'data is invalid' / confusing 'Not set' display: The .openai_api_key file was written by an older script using a different DPAPI entropy scheme (USPTO-style fixed entropy). DPAPI decryption fails for incompatible files. Fixed by: - Suppressing the internal [WARN] during status display (-Silent) - Showing '[!!] File exists but cannot be decrypted (incompatible format) - use option [2] to re-enter' instead of 'Not set' - Same pattern applied to Mistral for consistency Same Push-Location fix applied to Invoke-MigrateCourtListenerToken.
1 parent 1fc316f commit e1eb585

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

deploy/manage_api_keys.ps1

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ $ProjectDir = Split-Path -Parent $ScriptDir
4343
# ============================================================================
4444

4545
function Get-DpapiKeyFromFile {
46-
param([string]$FilePath)
46+
param([string]$FilePath, [switch]$Silent)
4747

4848
if (-not (Test-Path $FilePath)) { return $null }
4949

5050
try {
5151
$rawData = [System.IO.File]::ReadAllBytes($FilePath)
5252
if ($rawData.Length -le $DPAPI_ENTROPY_BYTES) {
53-
Write-Host "[WARN] Key file appears corrupt (too small): $FilePath" -ForegroundColor Yellow
53+
if (-not $Silent) { Write-Host "[WARN] Key file appears corrupt (too small): $FilePath" -ForegroundColor Yellow }
5454
return $null
5555
}
5656

@@ -65,7 +65,7 @@ function Get-DpapiKeyFromFile {
6565
return [System.Text.Encoding]::UTF8.GetString($decrypted)
6666
}
6767
catch {
68-
Write-Host "[WARN] Failed to decrypt key file ($FilePath): $_" -ForegroundColor Yellow
68+
if (-not $Silent) { Write-Host "[WARN] Failed to decrypt key file ($FilePath): $_" -ForegroundColor Yellow }
6969
return $null
7070
}
7171
}
@@ -116,13 +116,15 @@ try:
116116
except Exception as e:
117117
print('ERROR:' + str(e))
118118
'@
119+
Push-Location $ProjectDir
119120
$result = & $pythonExe -c $pythonCode 2>$null | Out-String
121+
Pop-Location
120122
if ($result -match "^TOKEN:(.*)") {
121123
$token = $matches[1].Trim()
122-
return if ($token) { $token } else { $null }
124+
if ($token) { return $token } else { return $null }
123125
}
124126
}
125-
catch { }
127+
catch { Pop-Location }
126128
return $null
127129
}
128130

@@ -149,7 +151,9 @@ try:
149151
except Exception as e:
150152
print('ERROR:' + str(e))
151153
'@
154+
Push-Location $ProjectDir
152155
$result = & $pythonExe -c $pythonCode 2>$null | Out-String
156+
Pop-Location
153157
Remove-Item Env:\_CL_MGR_TOKEN -ErrorAction SilentlyContinue
154158

155159
if ($result -match "SUCCESS") {
@@ -226,7 +230,8 @@ function Show-KeyStatus {
226230
Write-Host ""
227231

228232
# OpenAI key
229-
$oaiKey = Get-DpapiKeyFromFile -FilePath $OPENAI_KEY_PATH
233+
$oaiFileExists = Test-Path $OPENAI_KEY_PATH
234+
$oaiKey = Get-DpapiKeyFromFile -FilePath $OPENAI_KEY_PATH -Silent
230235
if ($oaiKey) {
231236
$masked = Hide-ApiKey -ApiKey $oaiKey
232237
Write-Host "[OK] OpenAI API Key: $masked" -ForegroundColor Green
@@ -236,13 +241,18 @@ function Show-KeyStatus {
236241
$masked = Hide-ApiKey -ApiKey $env:OPENAI_API_KEY
237242
Write-Host "[OK] OpenAI API Key: $masked (env var only)" -ForegroundColor Yellow
238243
}
244+
elseif ($oaiFileExists) {
245+
Write-Host "[!!] OpenAI API Key: File exists but cannot be decrypted (incompatible format)" -ForegroundColor Red
246+
Write-Host " Use option [2] to re-enter your key and overwrite the file" -ForegroundColor Yellow
247+
}
239248
else {
240249
Write-Host "[--] OpenAI API Key: Not set (optional)" -ForegroundColor Yellow
241250
}
242251
Write-Host ""
243252

244253
# Mistral key
245-
$mistralKey = Get-DpapiKeyFromFile -FilePath $MISTRAL_KEY_PATH
254+
$mistralFileExists = Test-Path $MISTRAL_KEY_PATH
255+
$mistralKey = Get-DpapiKeyFromFile -FilePath $MISTRAL_KEY_PATH -Silent
246256
if ($mistralKey) {
247257
$masked = Hide-ApiKey -ApiKey $mistralKey
248258
Write-Host "[OK] Mistral API Key: $masked" -ForegroundColor Green
@@ -252,6 +262,10 @@ function Show-KeyStatus {
252262
$masked = Hide-ApiKey -ApiKey $env:MISTRAL_API_KEY
253263
Write-Host "[OK] Mistral API Key: $masked (env var only)" -ForegroundColor Yellow
254264
}
265+
elseif ($mistralFileExists) {
266+
Write-Host "[!!] Mistral API Key: File exists but cannot be decrypted (incompatible format)" -ForegroundColor Red
267+
Write-Host " Use option [3] to re-enter your key and overwrite the file" -ForegroundColor Yellow
268+
}
255269
else {
256270
Write-Host "[--] Mistral API Key: Not set (optional)" -ForegroundColor Yellow
257271
}
@@ -393,7 +407,9 @@ except Exception as e:
393407
print('ERROR:' + str(e))
394408
'@
395409

410+
Push-Location $ProjectDir
396411
$result = & $pythonExe -c $pythonCode 2>$null | Out-String
412+
Pop-Location
397413
if ($result -match "SUCCESS") {
398414
Write-Host "[OK] Migration successful - token now in Credential Manager" -ForegroundColor Green
399415
Write-Host " DPAPI file retained as backup: $CL_TOKEN_PATH" -ForegroundColor Gray

0 commit comments

Comments
 (0)