Skip to content

Commit 049cd1c

Browse files
authored
fix: use default api when auto detection fails (#415)
1 parent ea6d613 commit 049cd1c

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

pkg/app/app_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func Test_CreateAppEngine_config_PAT_autoRegionDetection(t *testing.T) {
121121
assert.Equal(t, fmt.Sprintf("https://%s", apiUrl), actualApiUrl)
122122
})
123123

124-
t.Run("invalid PAT reverts to default API URL", func(t *testing.T) {
124+
t.Run("invalid PAT reverts to default API URL (with wrong payload)", func(t *testing.T) {
125125
patWithExtraSegments := "snyk_uat.12345678.payload.signature.extra"
126126
engine := CreateAppEngine()
127127
config := engine.GetConfiguration()
@@ -130,6 +130,16 @@ func Test_CreateAppEngine_config_PAT_autoRegionDetection(t *testing.T) {
130130
actualApiUrl := config.GetString(configuration.API_URL)
131131
assert.Equal(t, constants.SNYK_DEFAULT_API_URL, actualApiUrl)
132132
})
133+
134+
t.Run("invalid PAT reverts to default API URL (with no hostname in claim)", func(t *testing.T) {
135+
pat := createMockPAT(t, `{}`)
136+
engine := CreateAppEngine()
137+
config := engine.GetConfiguration()
138+
config.Set(configuration.AUTHENTICATION_TOKEN, pat)
139+
140+
actualApiUrl := config.GetString(configuration.API_URL)
141+
assert.Equal(t, constants.SNYK_DEFAULT_API_URL, actualApiUrl)
142+
})
133143
}
134144

135145
func Test_CreateAppEngine_config_OauthAudHasPrecedence(t *testing.T) {

pkg/auth/tokenauthenticator.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
// Claims represents the structure of the PATs claims, it does not represent all the claims; only the ones we need
2525
type Claims struct {
2626
// Hostname PAT is valid for
27-
Hostname string `json:"h,omitempty"`
27+
Hostname string `json:"h"`
2828
}
2929

3030
var _ Authenticator = (*tokenAuthenticator)(nil)
@@ -101,6 +101,10 @@ func GetApiUrlFromPAT(pat string) (string, error) {
101101
}
102102

103103
hostname := claims.Hostname
104+
if len(hostname) == 0 {
105+
return "", fmt.Errorf("hostname is empty")
106+
}
107+
104108
if !strings.HasPrefix(hostname, "http") {
105109
hostname = fmt.Sprintf("https://%s", hostname)
106110
}

pkg/auth/tokenauthenticator_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,17 @@ func TestGetApiUrlFromPAT(t *testing.T) {
9090

9191
t.Run("PAT with scheme", func(t *testing.T) {
9292
pat := createMockPAT(t, `{"h":"http://api.snyk.io"}`)
93-
fmt.Println("pat", pat)
9493
apiUrl, err := GetApiUrlFromPAT(pat)
9594
assert.NoError(t, err)
9695
assert.Equal(t, "http://api.snyk.io", apiUrl)
9796
})
9897

98+
t.Run("PAT without hostname in claims", func(t *testing.T) {
99+
pat := createMockPAT(t, `{}`)
100+
_, err := GetApiUrlFromPAT(pat)
101+
assert.Error(t, err)
102+
})
103+
99104
t.Run("Invalid PAT", func(t *testing.T) {
100105
patTooManySegments := "snyk_test.12345678.payload.signature.extra"
101106
apiUrl, err := GetApiUrlFromPAT(patTooManySegments)

0 commit comments

Comments
 (0)