Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
578 Fix gitlab basic auth with access tokens (#54)
trufflesteeeve9cb99e5aa4c0
diff --git a/pkg/sources/gitlab/gitlab.go b/pkg/sources/gitlab/gitlab.goindex 35445bce8..242744070 100644--- a/pkg/sources/gitlab/gitlab.go+++ b/pkg/sources/gitlab/gitlab.go@@ -89,6 +89,8 @@ func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64,s.authMethod = "BASIC_AUTH"s.user = cred.BasicAuth.Usernames.password = cred.BasicAuth.Password+ // We may need the password as a token if the user is using an access_token with basic auth.+ s.token = cred.BasicAuth.Passworddefault:return errors.Errorf("Invalid configuration given for source. Name: %s, Type: %s", name, s.Type())}@@ -117,33 +119,38 @@ func (s *Source) Init(aCtx context.Context, name string, jobId, sourceId int64,}func (s *Source) newClient() (*gitlab.Client, error) {-// initialize a new api instanceswitch s.authMethod {case "OAUTH":apiClient, err := gitlab.NewOAuthClient(s.token, gitlab.WithBaseURL(s.url))if err != nil {- return nil, fmt.Errorf("could not authenticate to Gitlab instance %s via OAUTH. Error: %v", s.url, err)+ return nil, fmt.Errorf("could not create Gitlab OAUTH client for %s. Error: %v", s.url, err)}return apiClient, nil+case "BASIC_AUTH":apiClient, err := gitlab.NewBasicAuthClient(s.user, s.password, gitlab.WithBaseURL(s.url))if err != nil {- return nil, fmt.Errorf("could not authenticate to Gitlab instance %s via BASICAUTH. Error: %v", s.url, err)+ return nil, fmt.Errorf("could not create Gitlab BASICAUTH client for %s. Error: %v", s.url, err)}- return apiClient, nil-+ // If the user is using an access_token rather than a username/password, then basic auth+ // will not work. In this case, we test to see if basic auth would work, and if it does not,+ // we proceed with an OAuth client using the access_token (s.password) as the token.+ // At this point, s.token is already set to s.password+ if s.basicAuthSuccessful(apiClient) {+ return apiClient, nil+ }+ fallthroughcase "TOKEN":apiClient, err := gitlab.NewOAuthClient(s.token, gitlab.WithBaseURL(s.url))if err != nil {- return nil, fmt.Errorf("could not authenticate to Gitlab instance %s via TOKEN Auth. Error: %v", s.url, err)+ return nil, fmt.Errorf("could not create Gitlab TOKEN client for %s. Error: %v", s.url, err)}return apiClient, nildefault:return nil, errors.New("Could not determine authMethod specified for GitLab")}-}func (s *Source) getAllProjects(apiClient *gitlab.Client) ([]*gitlab.Project, error) {@@ -381,3 +388,17 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk) errreturn nil}++func (s *Source) basicAuthSuccessful(apiClient *gitlab.Client) bool {+ user, resp, err := apiClient.Users.CurrentUser()+ if err != nil {+ return false+ }+ if resp.StatusCode <= 400 {+ return false+ }+ if user != nil {+ return true+ }+ return false+}