-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bearer token auth #21462
base: master
Are you sure you want to change the base?
feat: add bearer token auth #21462
Changes from all commits
92245f3
bc9c011
fc32d98
4bcb3ae
4a9ec45
fb41ecc
c34f1ac
5d2abec
fcff88e
0c70dee
2895760
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,39 @@ | ||
package util | ||
|
||
import ( | ||
stderrors "errors" | ||
) | ||
|
||
var ( | ||
LogFormat string | ||
LogLevel string | ||
) | ||
|
||
func ValidateBearerTokenForHTTPSRepoOnly(bearerToken string, isHTTPS bool) error { | ||
// Bearer token is only valid for HTTPS repositories | ||
if bearerToken != "" { | ||
if !isHTTPS { | ||
err := stderrors.New("--bearer-token is only supported for HTTPS repositories") | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateBearerTokenForGitOnly(bearerToken string, repoType string) error { | ||
// Bearer token is only valid for Git repositories | ||
if bearerToken != "" && repoType != "git" { | ||
err := stderrors.New("--bearer-token is only supported for Git repositories") | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func ValidateBearerTokenAndPasswordCombo(bearerToken string, password string) error { | ||
// Either the password or the bearer token must be set, but not both | ||
if bearerToken != "" && password != "" { | ||
err := stderrors.New("only --bearer-token or --password is allowed, not both") | ||
return err | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidateBearerTokenAndPasswordCombo(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bearerToken string | ||
password string | ||
expectError bool | ||
errorMsg string | ||
}{ | ||
{ | ||
name: "Both token and password set", | ||
bearerToken: "some-token", | ||
password: "some-password", | ||
expectError: true, | ||
errorMsg: "only --bearer-token or --password is allowed, not both", | ||
}, | ||
{ | ||
name: "Only token set", | ||
bearerToken: "some-token", | ||
password: "", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Only password set", | ||
bearerToken: "", | ||
password: "some-password", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Neither token nor password set", | ||
bearerToken: "", | ||
password: "", | ||
expectError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateBearerTokenAndPasswordCombo(tt.bearerToken, tt.password) | ||
if tt.expectError { | ||
require.ErrorContains(t, err, tt.errorMsg) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateBearerTokenForGitOnly(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bearerToken string | ||
repoType string | ||
expectError bool | ||
errorMsg string | ||
}{ | ||
{ | ||
name: "Bearer token with helm repo", | ||
bearerToken: "some-token", | ||
repoType: "helm", | ||
expectError: true, | ||
errorMsg: "--bearer-token is only supported for Git repositories", | ||
}, | ||
{ | ||
name: "Bearer token with git repo", | ||
bearerToken: "some-token", | ||
repoType: "git", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "No bearer token with helm repo", | ||
bearerToken: "", | ||
repoType: "helm", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "No bearer token with git repo", | ||
bearerToken: "", | ||
repoType: "git", | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Bearer token with empty repo", | ||
bearerToken: "some-token", | ||
repoType: "", | ||
expectError: true, | ||
errorMsg: "--bearer-token is only supported for Git repositories", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateBearerTokenForGitOnly(tt.bearerToken, tt.repoType) | ||
if tt.expectError { | ||
require.ErrorContains(t, err, tt.errorMsg) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateBearerTokenForHTTPSRepoOnly(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bearerToken string | ||
isHTTPS bool | ||
expectError bool | ||
errorMsg string | ||
}{ | ||
{ | ||
name: "Bearer token with HTTPS repo", | ||
bearerToken: "some-token", | ||
isHTTPS: true, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "Bearer token with non-HTTPS repo", | ||
bearerToken: "some-token", | ||
isHTTPS: false, | ||
expectError: true, | ||
errorMsg: "--bearer-token is only supported for HTTPS repositories", | ||
}, | ||
{ | ||
name: "No bearer token with HTTPS repo", | ||
bearerToken: "", | ||
isHTTPS: true, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "No bearer token with non-HTTPS repo", | ||
bearerToken: "", | ||
isHTTPS: false, | ||
expectError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ValidateBearerTokenForHTTPSRepoOnly(tt.bearerToken, tt.isHTTPS) | ||
if tt.expectError { | ||
require.ErrorContains(t, err, tt.errorMsg) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we change it to bearer token to the repository, so it will be consistent with username and password?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is on purpose, since It is only relevant for Git, at least at the moment. When support for Helm would be added, this would be a good time to make it consistent.