From cf6756af1f7c2b6253133928098a214912edc394 Mon Sep 17 00:00:00 2001 From: utkarsh-lambdatest <98745310+utkarsh-lambdatest@users.noreply.github.com> Date: Thu, 24 Mar 2022 21:50:37 +0530 Subject: [PATCH] Feature/synapse bitbucket (#80) * added refresh token api * added unit tests * added unit test for secret parser expired function * disable golangci for false positive case * added comments * added token type * added gitprovider condition in refresh oauth * added basic auth supprt in git init * fixed spelling * fixed unit tests * fixed unit test * Changes get oauth secret test --- .sample.synapse.json | 3 ++- config/synapsemodel.go | 3 ++- pkg/core/interfaces.go | 4 +-- pkg/core/lifecycle.go | 13 ++++----- pkg/core/models.go | 10 +++++++ pkg/diffmanager/setup.go | 16 +++++------ pkg/diffmanager/setup_test.go | 51 ++++++++++++++++++++--------------- pkg/gitmanager/setup.go | 30 +++++++++++++++------ pkg/gitmanager/setup_test.go | 24 ++++++++++++----- pkg/secret/secret.go | 6 ++++- pkg/secret/secret_test.go | 25 ++++++++--------- pkg/secrets/secrets.go | 1 + pkg/secrets/secrets_test.go | 2 +- pkg/tests/testutils.go | 3 ++- 14 files changed, 120 insertions(+), 71 deletions(-) diff --git a/.sample.synapse.json b/.sample.synapse.json index 988873f..e3f7d88 100644 --- a/.sample.synapse.json +++ b/.sample.synapse.json @@ -7,7 +7,8 @@ "SecretKey": "SecretKeyForLambdaTest" }, "Git": { - "Token": "" + "Token": "", + "TokenType": "Bearer" }, "ContainerRegistry": { "PullPolicy": "always", diff --git a/config/synapsemodel.go b/config/synapsemodel.go index 4bd3e1b..e5e36c6 100644 --- a/config/synapsemodel.go +++ b/config/synapsemodel.go @@ -24,7 +24,8 @@ type LambdatestConfig struct { // GitConfig contains git token type GitConfig struct { - Token string + Token string + TokenType string } // PullPolicyType defines when to pull docker image diff --git a/pkg/core/interfaces.go b/pkg/core/interfaces.go index a81f53c..8ba1e26 100644 --- a/pkg/core/interfaces.go +++ b/pkg/core/interfaces.go @@ -22,12 +22,12 @@ type TASConfigManager interface { // GitManager manages the cloning of git repositories type GitManager interface { // Clone repository from TAS config - Clone(ctx context.Context, payload *Payload, cloneToken string) error + Clone(ctx context.Context, payload *Payload, oauth *Oauth) error } // DiffManager manages the diff findings for the given payload type DiffManager interface { - GetChangedFiles(ctx context.Context, payload *Payload, cloneToken string) (map[string]int, error) + GetChangedFiles(ctx context.Context, payload *Payload, oauth *Oauth) (map[string]int, error) } // TestDiscoveryService services discovery of tests diff --git a/pkg/core/lifecycle.go b/pkg/core/lifecycle.go index 1315840..fc04ac9 100644 --- a/pkg/core/lifecycle.go +++ b/pkg/core/lifecycle.go @@ -71,7 +71,7 @@ func (pl *Pipeline) Start(ctx context.Context) (err error) { os.Exit(0) } - oauth, err := pl.getOauthSecret(payload.RepoID) + oauth, err := pl.getOauthSecret(payload.RepoID, payload.GitProvider) if err != nil { pl.Logger.Fatalf("failed to get oauth secret %v", err) } @@ -79,7 +79,7 @@ func (pl *Pipeline) Start(ctx context.Context) (err error) { // set payload on pipeline object pl.Payload = payload if pl.Cfg.ParseMode { - err = pl.GitManager.Clone(ctx, payload, oauth.Data.AccessToken) + err = pl.GitManager.Clone(ctx, payload, oauth) if err != nil { pl.Logger.Fatalf("failed to clone YML for build ID: %s, error: %v", payload.BuildID, err) } @@ -134,7 +134,7 @@ func (pl *Pipeline) Start(ctx context.Context) (err error) { if pl.Cfg.DiscoverMode { pl.Logger.Infof("Cloning repo ...") - err = pl.GitManager.Clone(ctx, pl.Payload, oauth.Data.AccessToken) + err = pl.GitManager.Clone(ctx, pl.Payload, oauth) if err != nil { pl.Logger.Errorf("Unable to clone repo '%s': %s", payload.RepoLink, err) errRemark = fmt.Sprintf("Unable to clone repo: %s", payload.RepoLink) @@ -249,7 +249,7 @@ func (pl *Pipeline) Start(ctx context.Context) (err error) { pl.Logger.Infof("Identifying changed files ...") diffExists := true - diff, err := pl.DiffManager.GetChangedFiles(ctx, payload, oauth.Data.AccessToken) + diff, err := pl.DiffManager.GetChangedFiles(ctx, payload, oauth) if err != nil { if errors.Is(err, errs.ErrGitDiffNotFound) { diffExists = false @@ -363,13 +363,13 @@ func (pl *Pipeline) sendStats(payload ExecutionResults) error { } // getOauthSecret returns a valid oauth token -func (pl *Pipeline) getOauthSecret(repoID string) (*Oauth, error) { +func (pl *Pipeline) getOauthSecret(repoID, gitProvider string) (*Oauth, error) { oauth, err := pl.SecretParser.GetOauthSecret(global.OauthSecretPath) if err != nil { return nil, err } - if !pl.SecretParser.Expired(oauth) { + if gitProvider != Bitbucket || !pl.SecretParser.Expired(oauth) { return oauth, nil } @@ -397,5 +397,6 @@ func (pl *Pipeline) getOauthSecret(repoID string) (*Oauth, error) { pl.Logger.Errorf("error while unmarshaling json to oauth for RepoID %s : %s", repoID, err) } + refreshedOauth.Data.Type = Bearer return refreshedOauth, nil } diff --git a/pkg/core/models.go b/pkg/core/models.go index 849142b..341acf9 100644 --- a/pkg/core/models.go +++ b/pkg/core/models.go @@ -268,12 +268,22 @@ const ( Bitbucket string = "bitbucket" ) +type TokenType string + +const ( + // Bearer as token type + Bearer TokenType = "Bearer" + // Basic as token type + Basic TokenType = "Basic" +) + // Oauth represents the sructure of Oauth type Oauth struct { Data struct { AccessToken string `json:"access_token"` Expiry time.Time `json:"expiry"` RefreshToken string `json:"refresh_token"` + Type TokenType `json:"token_type,omitempty"` } `json:"data"` } diff --git a/pkg/diffmanager/setup.go b/pkg/diffmanager/setup.go index c92813d..9dffaa4 100644 --- a/pkg/diffmanager/setup.go +++ b/pkg/diffmanager/setup.go @@ -62,7 +62,7 @@ func (dm *diffManager) updateWithOr(m map[string]int, key string, value int) { m[key] = m[key] | value } -func (dm *diffManager) getCommitDiff(gitprovider, repoURL string, cloneToken string, baseCommit, targetCommit string) ([]byte, error) { +func (dm *diffManager) getCommitDiff(gitprovider, repoURL string, oauth *core.Oauth, baseCommit, targetCommit string) ([]byte, error) { if baseCommit == "" { dm.logger.Debugf("basecommit is empty for gitprovider %v error %v", gitprovider, errs.ErrGitDiffNotFound) return nil, errs.ErrGitDiffNotFound @@ -85,8 +85,8 @@ func (dm *diffManager) getCommitDiff(gitprovider, repoURL string, cloneToken str if err != nil { return nil, err } - if cloneToken != "" { - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", cloneToken)) + if oauth.Data.AccessToken != "" { + req.Header.Add("Authorization", fmt.Sprintf("%s %s", oauth.Data.Type, oauth.Data.AccessToken)) } req.Header.Add("Accept", "application/vnd.github.v3.diff") resp, err := dm.client.Do(req) @@ -102,7 +102,7 @@ func (dm *diffManager) getCommitDiff(gitprovider, repoURL string, cloneToken str return ioutil.ReadAll(resp.Body) } -func (dm *diffManager) getPRDiff(gitprovider, repoURL string, prNumber int, cloneToken string) ([]byte, error) { +func (dm *diffManager) getPRDiff(gitprovider, repoURL string, prNumber int, oauth *core.Oauth) ([]byte, error) { parsedUrl, err := url.Parse(repoURL) if err != nil { return nil, err @@ -123,7 +123,7 @@ func (dm *diffManager) getPRDiff(gitprovider, repoURL string, prNumber int, clon dm.logger.Errorf("failed to create http request for changelist url error: %v", err) return nil, err } - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", cloneToken)) + req.Header.Add("Authorization", fmt.Sprintf("%s %s", oauth.Data.Type, oauth.Data.AccessToken)) req.Header.Set("Accept", "application/vnd.github.v3.diff") resp, err := dm.client.Do(req) @@ -197,20 +197,20 @@ func (dm *diffManager) parseGitDiff(gitprovider string, eventType core.EventType } // GetChangedFiles Figure out changed files -func (dm *diffManager) GetChangedFiles(ctx context.Context, payload *core.Payload, cloneToken string) (map[string]int, error) { +func (dm *diffManager) GetChangedFiles(ctx context.Context, payload *core.Payload, oauth *core.Oauth) (map[string]int, error) { // map to store file and type of change (added, removed, modified) var m map[string]int var diff []byte var err error if payload.EventType == core.EventPullRequest { - diff, err = dm.getPRDiff(payload.GitProvider, payload.RepoLink, payload.PullRequestNumber, cloneToken) + diff, err = dm.getPRDiff(payload.GitProvider, payload.RepoLink, payload.PullRequestNumber, oauth) if err != nil { dm.logger.Errorf("failed to parse pr diff for gitprovider: %s error: %v", payload.GitProvider, err) return nil, err } } else { - diff, err = dm.getCommitDiff(payload.GitProvider, payload.RepoLink, cloneToken, payload.BuildBaseCommit, payload.BuildTargetCommit) + diff, err = dm.getCommitDiff(payload.GitProvider, payload.RepoLink, oauth, payload.BuildBaseCommit, payload.BuildTargetCommit) if err != nil { dm.logger.Errorf("failed to get commit diff for gitprovider: %s error: %v", payload.GitProvider, err) return nil, err diff --git a/pkg/diffmanager/setup_test.go b/pkg/diffmanager/setup_test.go index 563e7cc..deaa7b3 100644 --- a/pkg/diffmanager/setup_test.go +++ b/pkg/diffmanager/setup_test.go @@ -8,12 +8,21 @@ import ( "net/http/httptest" "reflect" "testing" + "time" "github.com/LambdaTest/synapse/pkg/core" "github.com/LambdaTest/synapse/pkg/global" "github.com/LambdaTest/synapse/testutils" ) +//nolint unused +type oauthData struct { + AccessToken string `json:"access_token"` + Expiry time.Time `json:"expiry"` + RefreshToken string `json:"refresh_token"` + Type core.TokenType `json:"token_type,omitempty"` +} + func Test_updateWithOr(t *testing.T) { check := func(t *testing.T) { dm := &diffManager{} @@ -52,9 +61,9 @@ func Test_diffManager_GetChangedFiles_PRDiff(t *testing.T) { dm := NewDiffManager(config, logger) type args struct { - ctx context.Context - payload *core.Payload - cloneToken string + ctx context.Context + payload *core.Payload + oauth *core.Oauth } tests := []struct { name string @@ -63,19 +72,19 @@ func Test_diffManager_GetChangedFiles_PRDiff(t *testing.T) { wantErr bool }{ // expects to hit Server.URL/testdata/pulls/2 - {"Test GetChangedFile for PRdiff for github gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", GitProvider: "github", PrivateRepo: false, EventType: "pull-request", Diff: "xyz", PullRequestNumber: 2}, cloneToken: ""}, map[string]int{}, false}, + {"Test GetChangedFile for PRdiff for github gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", GitProvider: "github", PrivateRepo: false, EventType: "pull-request", Diff: "xyz", PullRequestNumber: 2}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, false}, // expects to hit Server.URL/testdata/merge_requests/2/changes - {"Test GetChangedFile for PRdiff for gitlab gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", GitProvider: "gitlab", PrivateRepo: false, EventType: "pull-request", Diff: "xyz", PullRequestNumber: 2}, cloneToken: ""}, map[string]int{}, false}, + {"Test GetChangedFile for PRdiff for gitlab gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", GitProvider: "gitlab", PrivateRepo: false, EventType: "pull-request", Diff: "xyz", PullRequestNumber: 2}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, false}, - {"Test GetChangedFile for Commitdiff for unsupported gitprovider", args{ctx: context.TODO(), payload: &core.Payload{GitProvider: "unsupported"}}, map[string]int{}, true}, + {"Test GetChangedFile for Commitdiff for unsupported gitprovider", args{ctx: context.TODO(), payload: &core.Payload{GitProvider: "unsupported"}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, true}, - {"Test GetChangedFile for PRdiff for unsupported gitprovider", args{ctx: context.TODO(), payload: &core.Payload{GitProvider: "unsupported", EventType: "pull-request"}}, map[string]int{}, true}, + {"Test GetChangedFile for PRdiff for unsupported gitprovider", args{ctx: context.TODO(), payload: &core.Payload{GitProvider: "unsupported", EventType: "pull-request"}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { global.APIHostURLMap[tt.args.payload.GitProvider] = server.URL - resp, err := dm.GetChangedFiles(tt.args.ctx, tt.args.payload, tt.args.cloneToken) + resp, err := dm.GetChangedFiles(tt.args.ctx, tt.args.payload, tt.args.oauth) if tt.wantErr { if err == nil { @@ -113,9 +122,9 @@ func Test_diffManager_GetChangedFiles_CommitDiff_Github(t *testing.T) { dm := NewDiffManager(config, logger) type args struct { - ctx context.Context - payload *core.Payload - cloneToken string + ctx context.Context + payload *core.Payload + oauth *core.Oauth } tests := []struct { name string @@ -124,18 +133,18 @@ func Test_diffManager_GetChangedFiles_CommitDiff_Github(t *testing.T) { wantErr bool }{ // expects to hit serverURL/testdata/compare/abc...xyz - {"Test GetChangedFile for CommitDiff for github gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", BuildTargetCommit: "xyz", BuildBaseCommit: "abc", GitProvider: "github", EventType: "push", Diff: "xyz", PullRequestNumber: 2}}, map[string]int{}, false}, + {"Test GetChangedFile for CommitDiff for github gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", BuildTargetCommit: "xyz", BuildBaseCommit: "abc", GitProvider: "github", EventType: "push", Diff: "xyz", PullRequestNumber: 2}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, false}, - {"Test GetChangedFile for CommitDiff for github provider and empty base commit", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", BuildBaseCommit: "", GitProvider: "gitlab", EventType: "push"}}, map[string]int{}, true}, + {"Test GetChangedFile for CommitDiff for github provider and empty base commit", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", BuildBaseCommit: "", GitProvider: "gitlab", EventType: "push"}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, true}, - {"Test GetChangedFile for CommitDiff for github provider for non 200 response", args{ctx: context.TODO(), payload: &core.Payload{RepoLink: server.URL + "/notfound/", BuildTargetCommit: "xyz", BuildBaseCommit: "abc", GitProvider: "gitlab", EventType: "push"}}, map[string]int{}, true}, + {"Test GetChangedFile for CommitDiff for github provider for non 200 response", args{ctx: context.TODO(), payload: &core.Payload{RepoLink: server.URL + "/notfound/", BuildTargetCommit: "xyz", BuildBaseCommit: "abc", GitProvider: "gitlab", EventType: "push"}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, true}, - {"Test GetChangedFile for CommitDiff for non supported git provider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/notfound/", RepoLink: server.URL + "/notfound/", BuildTargetCommit: "xyz", BuildBaseCommit: "abc", GitProvider: "gittest", EventType: "push"}}, map[string]int{}, false}, + {"Test GetChangedFile for CommitDiff for non supported git provider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/notfound/", RepoLink: server.URL + "/notfound/", BuildTargetCommit: "xyz", BuildBaseCommit: "abc", GitProvider: "gittest", EventType: "push"}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { global.APIHostURLMap[tt.args.payload.GitProvider] = server.URL - resp, err := dm.GetChangedFiles(tt.args.ctx, tt.args.payload, tt.args.cloneToken) + resp, err := dm.GetChangedFiles(tt.args.ctx, tt.args.payload, tt.args.oauth) // t.Errorf("") if tt.args.payload.GitProvider == "gittest" { if resp != nil || err == nil { @@ -188,9 +197,9 @@ func Test_diffManager_GetChangedFiles_CommitDiff_Gitlab(t *testing.T) { dm := NewDiffManager(config, logger) type args struct { - ctx context.Context - payload *core.Payload - cloneToken string + ctx context.Context + payload *core.Payload + oauth *core.Oauth } tests := []struct { name string @@ -198,12 +207,12 @@ func Test_diffManager_GetChangedFiles_CommitDiff_Gitlab(t *testing.T) { want map[string]int }{ // expects to hit serverURL/testdata/repository/compare?from=abc&to=abcd - {"Test GetChangedFile for CommitDiff for gitlab gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", BuildTargetCommit: "abcd", BuildBaseCommit: "abc", TaskID: "taskid", BranchName: "branchname", BuildID: "buildid", RepoID: "repoid", OrgID: "orgid", GitProvider: "gitlab", PrivateRepo: false, EventType: "push", Diff: "xyz", PullRequestNumber: 2}, cloneToken: ""}, map[string]int{}}, + {"Test GetChangedFile for CommitDiff for gitlab gitprovider", args{ctx: context.TODO(), payload: &core.Payload{RepoSlug: "/testdata", RepoLink: server.URL + "/testdata", BuildTargetCommit: "abcd", BuildBaseCommit: "abc", TaskID: "taskid", BranchName: "branchname", BuildID: "buildid", RepoID: "repoid", OrgID: "orgid", GitProvider: "gitlab", PrivateRepo: false, EventType: "push", Diff: "xyz", PullRequestNumber: 2}, oauth: &core.Oauth{Data: oauthData{}}}, map[string]int{}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { global.APIHostURLMap[tt.args.payload.GitProvider] = server.URL - resp, err := dm.GetChangedFiles(tt.args.ctx, tt.args.payload, tt.args.cloneToken) + resp, err := dm.GetChangedFiles(tt.args.ctx, tt.args.payload, tt.args.oauth) if err != nil { t.Errorf("error in getting changed files, error %v", err.Error()) diff --git a/pkg/gitmanager/setup.go b/pkg/gitmanager/setup.go index 825af14..1f4b413 100644 --- a/pkg/gitmanager/setup.go +++ b/pkg/gitmanager/setup.go @@ -3,6 +3,7 @@ package gitmanager import ( "context" + "encoding/base64" "fmt" "io" "net/http" @@ -36,7 +37,7 @@ func NewGitManager(logger lumber.Logger, execManager core.ExecutionManager) core } } -func (gm *gitManager) Clone(ctx context.Context, payload *core.Payload, cloneToken string) error { +func (gm *gitManager) Clone(ctx context.Context, payload *core.Payload, oauth *core.Oauth) error { repoLink := payload.RepoLink repoItems := strings.Split(repoLink, "/") repoName := repoItems[len(repoItems)-1] @@ -48,7 +49,7 @@ func (gm *gitManager) Clone(ctx context.Context, payload *core.Payload, cloneTok return err } gm.logger.Debugf("cloning from %s", archiveURL) - err = gm.downloadFile(ctx, archiveURL, commitID+".zip", cloneToken) + err = gm.downloadFile(ctx, archiveURL, commitID+".zip", oauth) if err != nil { gm.logger.Errorf("failed to download file %v", err) return err @@ -65,7 +66,7 @@ func (gm *gitManager) Clone(ctx context.Context, payload *core.Payload, cloneTok return err } - if err = gm.initGit(ctx, payload, cloneToken); err != nil { + if err = gm.initGit(ctx, payload, oauth); err != nil { gm.logger.Errorf("failed to initialize git, error %v", err) return err } @@ -74,13 +75,13 @@ func (gm *gitManager) Clone(ctx context.Context, payload *core.Payload, cloneTok } // downloadFile clones the archive from github and extracts the file if it is a zip file. -func (gm *gitManager) downloadFile(ctx context.Context, archiveURL, fileName, cloneToken string) error { +func (gm *gitManager) downloadFile(ctx context.Context, archiveURL, fileName string, oauth *core.Oauth) error { req, err := http.NewRequestWithContext(ctx, http.MethodGet, archiveURL, nil) if err != nil { return err } - if cloneToken != "" { - req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", cloneToken)) + if oauth.Data.AccessToken != "" { + req.Header.Add("Authorization", fmt.Sprintf("%s %s", oauth.Data.Type, oauth.Data.AccessToken)) } resp, err := gm.httpClient.Do(req) if err != nil { @@ -130,13 +131,26 @@ func (gm *gitManager) copyAndExtractFile(resp *http.Response, path string) error return err } -func (gm *gitManager) initGit(ctx context.Context, payload *core.Payload, cloneToken string) error { +func (gm *gitManager) initGit(ctx context.Context, payload *core.Payload, oauth *core.Oauth) error { branch := payload.BranchName repoURL, perr := url.Parse(payload.RepoLink) if perr != nil { return perr } - repoURL.User = url.UserPassword("x-token-auth", cloneToken) + + if oauth.Data.Type == core.Basic { + decodedToken, err := base64.StdEncoding.DecodeString(oauth.Data.AccessToken) + if err != nil { + gm.logger.Errorf("Failed to decode basic oauth token for RepoID %s: %s", payload.RepoID, err) + return err + } + + creds := strings.Split(string(decodedToken), ":") + repoURL.User = url.UserPassword(creds[0], creds[1]) + } else { + repoURL.User = url.UserPassword("x-token-auth", oauth.Data.AccessToken) + } + urlWithToken := repoURL.String() commands := []string{ "git init", diff --git a/pkg/gitmanager/setup_test.go b/pkg/gitmanager/setup_test.go index 8fd1965..4d3ca9b 100644 --- a/pkg/gitmanager/setup_test.go +++ b/pkg/gitmanager/setup_test.go @@ -11,14 +11,24 @@ import ( "os" "strings" "testing" + "time" "github.com/LambdaTest/synapse/pkg/command" + "github.com/LambdaTest/synapse/pkg/core" "github.com/LambdaTest/synapse/pkg/global" "github.com/LambdaTest/synapse/pkg/lumber" "github.com/LambdaTest/synapse/testutils" "github.com/LambdaTest/synapse/testutils/mocks" ) +//nolint unused +type data struct { + AccessToken string `json:"access_token"` + Expiry time.Time `json:"expiry"` + RefreshToken string `json:"refresh_token"` + Type core.TokenType `json:"token_type,omitempty"` +} + func CreateDirectory(path string) { if _, err := os.Stat(path); os.IsNotExist(err) { if err := os.MkdirAll(path, 0755); err != nil { @@ -42,9 +52,9 @@ func Test_downloadFile(t *testing.T) { } reqToken := r.Header.Get("Authorization") splitToken := strings.Split(reqToken, "Bearer ") - expectedCloneToken := "dummy" - if splitToken[1] != expectedCloneToken { - t.Errorf("Invalid clone token, expected: %v\nreceived: %v", expectedCloneToken, splitToken[1]) + expectedOauth := &core.Oauth{Data: data{AccessToken: "dummy", Type: core.Bearer}} + if splitToken[1] != expectedOauth.Data.AccessToken { + t.Errorf("Invalid clone token, expected: %v\nreceived: %v", expectedOauth.Data.AccessToken, splitToken[1]) w.WriteHeader(http.StatusUnauthorized) } else { w.WriteHeader(http.StatusOK) @@ -63,8 +73,8 @@ func Test_downloadFile(t *testing.T) { } archiveURL := server.URL + "/archive/zipfile.zip" fileName := "copyAndExtracted" - cloneToken := "dummy" - err2 := gm.downloadFile(context.TODO(), archiveURL, fileName, cloneToken) + oauth := &core.Oauth{Data: data{AccessToken: "dummy", Type: core.Bearer}} + err2 := gm.downloadFile(context.TODO(), archiveURL, fileName, oauth) defer removeFile(fileName) // remove the file created after downloading and extracting if err != nil { t.Errorf("Error: %v", err2) @@ -126,10 +136,10 @@ func TestClone(t *testing.T) { payload.RepoLink = server.URL payload.BuildTargetCommit = "testRepo" - cloneToken := "dummy" + oauth := &core.Oauth{Data: data{AccessToken: "dummy", Type: core.Bearer}} commitID := payload.BuildTargetCommit - err = gm.Clone(context.TODO(), payload, cloneToken) + err = gm.Clone(context.TODO(), payload, oauth) global.TestEnv = false expErr := "opening zip archive for reading: creating reader: zip: not a valid zip file" diff --git a/pkg/secret/secret.go b/pkg/secret/secret.go index 5d48824..40f9288 100644 --- a/pkg/secret/secret.go +++ b/pkg/secret/secret.go @@ -69,6 +69,11 @@ func (s *secretParser) GetOauthSecret(path string) (*core.Oauth, error) { return nil, err } + // If tokentype is not basic set it to bearer + if o.Data.Type != core.Basic { + o.Data.Type = core.Bearer + } + return o, err } @@ -94,7 +99,6 @@ func (s *secretParser) SubstituteSecret(command string, secretData map[string]st return result, nil } -// Expired reports whether the token is expired. func (s *secretParser) Expired(token *core.Oauth) bool { if len(token.Data.RefreshToken) == 0 { return false diff --git a/pkg/secret/secret_test.go b/pkg/secret/secret_test.go index b628fb2..cc2d9e9 100644 --- a/pkg/secret/secret_test.go +++ b/pkg/secret/secret_test.go @@ -14,6 +14,14 @@ import ( "github.com/LambdaTest/synapse/pkg/lumber" ) +//nolint unused +type data struct { + AccessToken string `json:"access_token"` + Expiry time.Time `json:"expiry"` + RefreshToken string `json:"refresh_token"` + Type core.TokenType `json:"token_type,omitempty"` +} + func Test_secretParser_GetRepoSecret(t *testing.T) { logger, err := lumber.NewLogger(lumber.LoggingConfig{EnableConsole: true}, true, lumber.InstanceZapLogger) if err != nil { @@ -63,16 +71,11 @@ func Test_secretParser_GetOauthSecret(t *testing.T) { log.Fatalf("Could not instantiate logger %s", err.Error()) } secretParser := New(logger) - type data struct { - AccessToken string `json:"access_token"` - Expiry time.Time `json:"expiry"` - RefreshToken string `json:"refresh_token"` - } time, err := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", "Tue, 22 Feb 2022 16:22:01 IST") if err != nil { log.Fatalf("Could not parse time, error: %v", err) } - Data := data{AccessToken: "token", Expiry: time, RefreshToken: "refresh"} + Data := data{AccessToken: "token", Expiry: time, RefreshToken: "refresh", Type: core.Bearer} type args struct { path string @@ -96,9 +99,9 @@ func Test_secretParser_GetOauthSecret(t *testing.T) { t.Errorf("secretParser.GetOauthSecret() error = %v, wantErr %v", err, tt.wantErr) return } - expected := "&{{token 2022-02-22 16:22:01 +0530 IST refresh}}" + expected := fmt.Sprintf("%v", tt.want) received := fmt.Sprintf("%v", got) - if got != nil && !(strings.HasPrefix(received, "&{{token") && strings.HasSuffix(received, "refresh}}")) { + if got != nil && !(strings.HasPrefix(received, "&{{token") && strings.HasSuffix(received, "Bearer}}")) { t.Errorf("Expected: %v, got: %v", expected, received) return } @@ -181,12 +184,6 @@ func Test_secretParser_Expired(t *testing.T) { type args struct { token *core.Oauth } - //nolint:unused - type data struct { - AccessToken string `json:"access_token"` - Expiry time.Time `json:"expiry"` - RefreshToken string `json:"refresh_token"` - } tests := []struct { name string fields fields diff --git a/pkg/secrets/secrets.go b/pkg/secrets/secrets.go index 3c30dd2..91ae390 100644 --- a/pkg/secrets/secrets.go +++ b/pkg/secrets/secrets.go @@ -40,6 +40,7 @@ func (s *secertManager) WriteGitSecrets(path string) error { "access_token": s.cfg.Git.Token, "expiry": "0001-01-01T00:00:00Z", "refresh_token": "", + "token_type": s.cfg.Git.TokenType, }, } gitSecretsJSON, err := json.Marshal(gitSecrets) diff --git a/pkg/secrets/secrets_test.go b/pkg/secrets/secrets_test.go index 4338d13..25e4296 100644 --- a/pkg/secrets/secrets_test.go +++ b/pkg/secrets/secrets_test.go @@ -25,7 +25,7 @@ func TestGetLambdatestSecrets(t *testing.T) { func TestWriteGitSecrets(t *testing.T) { expectedFile := fmt.Sprintf("%s/%s", testdDataDir, global.GitConfigFileName) defer removeCreatedPath(testdDataDir) - expectedFileContent := `{"data":{"access_token":"dummytoken","expiry":"0001-01-01T00:00:00Z","refresh_token":""}}` + expectedFileContent := `{"data":{"access_token":"dummytoken","expiry":"0001-01-01T00:00:00Z","refresh_token":"","token_type":"Bearer"}}` err := secretsManager.WriteGitSecrets(testdDataDir) if err != nil { t.Errorf("error while writing secrets: %v", err) diff --git a/pkg/tests/testutils.go b/pkg/tests/testutils.go index c86f257..5ebbf09 100644 --- a/pkg/tests/testutils.go +++ b/pkg/tests/testutils.go @@ -13,7 +13,8 @@ func MockConfig() *config.SynapseConfig { SecretKey: "dummysecretkey", }, Git: config.GitConfig{ - Token: "dummytoken", + Token: "dummytoken", + TokenType: "Bearer", }, ContainerRegistry: config.ContainerRegistryConfig{ Mode: config.PublicMode,