Skip to content

Commit eea5504

Browse files
arielr-ltrsaksida
andauthored
Fix 500 on envelopes/download: read Argo token from mounted file (#1048)
## Summary - `GET /ce-registry/envelopes/download` returns 500 `key not found: "ARGO_WORKFLOWS_TOKEN"` in sandbox because the deployment provides `ARGO_WORKFLOWS_TOKEN_PATH` (mounted K8s secret) but the code only looks for `ARGO_WORKFLOWS_TOKEN` (env var). - Cherry-pick of `26bb12e` from `feature/k8s-argo-auth` — adds `ARGO_WORKFLOWS_TOKEN_PATH` support to `ArgoWorkflowsClient#configure_auth`, reading the bearer token from the mounted file. Co-authored-by: Rômulo Saksida <romulo@rsaksida.com>
1 parent aae893c commit eea5504

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

app/services/argo_workflows_client.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ def workflow_service_api
5959
end
6060

6161
def configure_auth(config)
62-
if env_present?('ARGO_WORKFLOWS_USERNAME', 'ARGO_WORKFLOWS_PASSWORD')
62+
if env_present?('ARGO_WORKFLOWS_TOKEN_PATH')
63+
config.api_key['Authorization'] = projected_service_account_token
64+
config.api_key_prefix['Authorization'] = 'Bearer'
65+
elsif env_present?('ARGO_WORKFLOWS_USERNAME', 'ARGO_WORKFLOWS_PASSWORD')
6366
config.api_key['Authorization'] = basic_auth_token
6467
config.api_key_prefix['Authorization'] = 'Basic'
6568
else
@@ -75,4 +78,8 @@ def env_present?(*keys)
7578
def basic_auth_token
7679
Base64.strict_encode64("#{ENV.fetch('ARGO_WORKFLOWS_USERNAME')}:#{ENV.fetch('ARGO_WORKFLOWS_PASSWORD')}")
7780
end
81+
82+
def projected_service_account_token
83+
File.read(ENV.fetch('ARGO_WORKFLOWS_TOKEN_PATH')).strip
84+
end
7885
end

docs/11_registry_changeset_sync.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,11 @@ key, workflow name, and namespace.
373373

374374
Authentication preference is:
375375

376-
1. Basic auth when `ARGO_WORKFLOWS_USERNAME` and `ARGO_WORKFLOWS_PASSWORD` are
376+
1. Bearer auth from a projected service account token file when
377+
`ARGO_WORKFLOWS_TOKEN_PATH` is present.
378+
2. Basic auth when `ARGO_WORKFLOWS_USERNAME` and `ARGO_WORKFLOWS_PASSWORD` are
377379
present.
378-
2. Bearer auth from `ARGO_WORKFLOWS_TOKEN`.
380+
3. Bearer auth from `ARGO_WORKFLOWS_TOKEN`.
379381

380382
SSL verification is disabled in the client because the app runs inside a trusted
381383
environment.
@@ -528,8 +530,8 @@ Required environment for S3/Argo sync:
528530
- `ARGO_WORKFLOWS_BASE_URL`
529531
- `ARGO_WORKFLOWS_NAMESPACE`
530532
- `ARGO_WORKFLOWS_TASK_IMAGE`
531-
- either `ARGO_WORKFLOWS_USERNAME` and `ARGO_WORKFLOWS_PASSWORD`, or
532-
`ARGO_WORKFLOWS_TOKEN`
533+
- `ARGO_WORKFLOWS_TOKEN_PATH`, `ARGO_WORKFLOWS_TOKEN`, or
534+
`ARGO_WORKFLOWS_USERNAME` and `ARGO_WORKFLOWS_PASSWORD`
533535

534536
Useful optional environment:
535537

spec/services/argo_workflows_client_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
allow(ENV).to receive(:fetch).and_call_original
1212
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_NAMESPACE').and_return('credreg-staging')
1313
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TOKEN').and_return('static-argo-token')
14+
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TOKEN_PATH', nil).and_return(nil)
1415
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_USERNAME', nil).and_return(nil)
1516
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_PASSWORD', nil).and_return(nil)
1617
unless configuration.nil?
@@ -65,11 +66,25 @@
6566
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TIMEOUT_SECONDS', 30).and_return(30)
6667
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_USERNAME', nil).and_return(nil)
6768
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_PASSWORD', nil).and_return(nil)
69+
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TOKEN_PATH', nil).and_return(nil)
6870
allow(ArgoWorkflowsApiClient::Configuration).to receive(:new).and_return(built_configuration)
6971
allow(ArgoWorkflowsApiClient::ApiClient).to receive(:new).with(built_configuration).and_return(api_client)
7072
allow(api_client).to receive(:config).and_return(built_configuration)
7173
end
7274

75+
it 'uses a projected service account token when ARGO_WORKFLOWS_TOKEN_PATH is configured' do
76+
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TOKEN_PATH', nil).and_return('/var/run/secrets/tokens/argo')
77+
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TOKEN_PATH').and_return('/var/run/secrets/tokens/argo')
78+
allow(File).to receive(:read).with('/var/run/secrets/tokens/argo').and_return("projected-argo-token\n")
79+
80+
allow(workflow_service_api).to receive(:workflow_service_get_workflow).and_return(workflow)
81+
82+
described_class.new.get_workflow(name: 'ce-registry-download-abc123')
83+
84+
expect(built_configuration.api_key['Authorization']).to eq('projected-argo-token')
85+
expect(built_configuration.api_key_prefix['Authorization']).to eq('Bearer')
86+
end
87+
7388
it 'uses ARGO_WORKFLOWS_TOKEN when Basic auth is not configured' do
7489
allow(ENV).to receive(:fetch).with('ARGO_WORKFLOWS_TOKEN').and_return('static-argo-token')
7590

0 commit comments

Comments
 (0)