Skip to content

Commit 24417d6

Browse files
committed
fix: ensure environment variable precedence for auth tokens
1 parent 596232f commit 24417d6

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

cliv2/cmd/cliv2/configuration.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
// !!! This import needs to be the first import, please do not change this !!!
4+
import _ "github.com/snyk/go-application-framework/pkg/networking/fips_enable"
5+
6+
import (
7+
"os"
8+
9+
"github.com/snyk/go-application-framework/pkg/auth"
10+
"github.com/snyk/go-application-framework/pkg/configuration"
11+
)
12+
13+
func defaultOAuthFF(config configuration.Configuration) configuration.DefaultValueFunction {
14+
return func(existingValue interface{}) interface{} {
15+
if _, ok := os.LookupEnv(auth.CONFIG_KEY_OAUTH_TOKEN); ok {
16+
return true
17+
}
18+
19+
keysThatMightDisableOAuth := config.GetAllKeysThatContainValues(configuration.AUTHENTICATION_BEARER_TOKEN)
20+
alternativeTokenKeys := config.GetAllKeysThatContainValues(configuration.AUTHENTICATION_TOKEN)
21+
keysThatMightDisableOAuth = append(keysThatMightDisableOAuth, alternativeTokenKeys...)
22+
23+
for _, key := range keysThatMightDisableOAuth {
24+
keyType := config.GetKeyType(key)
25+
if keyType == configuration.EnvVarKeyType {
26+
return false
27+
}
28+
}
29+
30+
return true
31+
}
32+
}

cliv2/cmd/cliv2/main.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/snyk/container-cli/pkg/container"
2323
"github.com/snyk/go-application-framework/pkg/analytics"
2424
"github.com/snyk/go-application-framework/pkg/app"
25-
"github.com/snyk/go-application-framework/pkg/auth"
2625
"github.com/snyk/go-application-framework/pkg/configuration"
2726
"github.com/snyk/go-application-framework/pkg/instrumentation"
2827
"github.com/spf13/cobra"
@@ -92,25 +91,6 @@ func initApplicationConfiguration(config configuration.Configuration) {
9291
config.AddAlternativeKeys(configuration.ORGANIZATION, []string{"snyk_cfg_org"})
9392
config.AddAlternativeKeys(configuration.PREVIEW_FEATURES_ENABLED, []string{"snyk_preview"})
9493
config.AddAlternativeKeys(configuration.LOG_LEVEL, []string{debug_level_flag})
95-
96-
// if the CONFIG_KEY_OAUTH_TOKEN is specified as env var, we don't apply any additional logic
97-
_, ok := os.LookupEnv(auth.CONFIG_KEY_OAUTH_TOKEN)
98-
if !ok {
99-
alternativeBearerKeys := config.GetAlternativeKeys(configuration.AUTHENTICATION_BEARER_TOKEN)
100-
alternativeBearerKeys = append(alternativeBearerKeys, configuration.AUTHENTICATION_BEARER_TOKEN)
101-
for _, key := range alternativeBearerKeys {
102-
hasPrefix := strings.HasPrefix(key, "snyk_")
103-
if hasPrefix {
104-
formattedKey := strings.ToUpper(key)
105-
_, ok := os.LookupEnv(formattedKey)
106-
if ok {
107-
globalLogger.Printf("Found environment variable %s, disabling OAuth flow", formattedKey)
108-
config.Set(configuration.FF_OAUTH_AUTH_FLOW_ENABLED, false)
109-
break
110-
}
111-
}
112-
}
113-
}
11494
}
11595

11696
func getFullCommandString(cmd *cobra.Command) string {
@@ -480,6 +460,8 @@ func MainWithErrorCode() int {
480460

481461
globalEngine = app.CreateAppEngineWithOptions(app.WithZeroLogger(globalLogger), app.WithConfiguration(globalConfiguration), app.WithRuntimeInfo(rInfo))
482462

463+
globalConfiguration.AddDefaultValue(configuration.FF_OAUTH_AUTH_FLOW_ENABLED, defaultOAuthFF(globalConfiguration))
464+
483465
if noProxyAuth := globalConfiguration.GetBool(basic_workflows.PROXY_NOAUTH); noProxyAuth {
484466
globalConfiguration.Set(configuration.PROXY_AUTHENTICATION_MECHANISM, httpauth.StringFromAuthenticationMechanism(httpauth.NoAuth))
485467
}

test/jest/acceptance/cli-token-precedence.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe('cli token precedence', () => {
110110
);
111111
});
112112

113-
describe('when oauth env vars are set', () => {
113+
describe('when env vars are set', () => {
114114
it('SNYK_OAUTH_TOKEN should override config', async () => {
115115
env = {
116116
...env,
@@ -134,6 +134,18 @@ describe('cli token precedence', () => {
134134
const authHeader = server.popRequest().headers?.authorization;
135135
expect(authHeader).toEqual(`Bearer ${env.SNYK_DOCKER_TOKEN}`);
136136
});
137+
138+
it('SNYK_TOKEN should override config', async () => {
139+
env = {
140+
...env,
141+
SNYK_TOKEN: 'SnykApiTokenEnvVar',
142+
};
143+
144+
await runSnykCLI(`-d`, { env });
145+
146+
const authHeader = server.popRequest().headers?.authorization;
147+
expect(authHeader).toEqual(`token ${env.SNYK_TOKEN}`);
148+
});
137149
});
138150

139151
if (snykOAuthConfig.name != auth.name) {

0 commit comments

Comments
 (0)