-
Notifications
You must be signed in to change notification settings - Fork 30
enhance(secrets): verify casing of orgs and repos in SCM before adding secret #700
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8485106
enhance(secrets): verify casing and existence of orgs and repos in SC…
ecrupper ff2f36f
fix some comments
ecrupper f1dabe0
add clarifying comments
ecrupper 76bed2f
ditch equalfold check since it will always be true at that point
ecrupper 5c31e41
Merge branch 'main' into fix/repo-check-for-secrets
ecrupper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
enhance(secrets): verify casing and existence of orgs and repos in SC…
…M before adding secret
- Loading branch information
commit 848510660bc1ba139550d9c43ca55a123339aebc
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) 2022 Target Brands, Inc. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by the LICENSE file in this repository. | ||
|
|
||
| package github | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/sirupsen/logrus" | ||
|
|
||
| "github.com/go-vela/types/library" | ||
| ) | ||
|
|
||
| // GetOrg gets repo information from Github. | ||
| func (c *client) GetOrgName(u *library.User, o string) (string, error) { | ||
| c.Logger.WithFields(logrus.Fields{ | ||
| "org": o, | ||
| "user": u.GetName(), | ||
| }).Tracef("retrieving org information for %s", o) | ||
|
|
||
| // create GitHub OAuth client with user's token | ||
| client := c.newClientToken(u.GetToken()) | ||
|
|
||
| // send an API call to get the org info | ||
| orgInfo, resp, err := client.Organizations.Get(ctx, o) | ||
|
|
||
| orgName := orgInfo.GetLogin() | ||
|
|
||
| // if org is not found, return the personal org | ||
| if resp.StatusCode == http.StatusNotFound { | ||
| user, _, err := client.Users.Get(ctx, "") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| orgName = user.GetLogin() | ||
| } else if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return orgName, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright (c) 2022 Target Brands, Inc. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by the LICENSE file in this repository. | ||
|
|
||
| package github | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
|
|
||
| "github.com/go-vela/types/library" | ||
| ) | ||
|
|
||
| func TestGithub_GetOrgName(t *testing.T) { | ||
| // setup context | ||
| gin.SetMode(gin.TestMode) | ||
|
|
||
| resp := httptest.NewRecorder() | ||
| _, engine := gin.CreateTestContext(resp) | ||
|
|
||
| // setup mock server | ||
| engine.GET("/api/v3/orgs/:org", func(c *gin.Context) { | ||
| c.Header("Content-Type", "application/json") | ||
| c.Status(http.StatusOK) | ||
| c.File("testdata/get_org.json") | ||
| }) | ||
|
|
||
| s := httptest.NewServer(engine) | ||
| defer s.Close() | ||
|
|
||
| // setup types | ||
| u := new(library.User) | ||
| u.SetName("foo") | ||
| u.SetToken("bar") | ||
|
|
||
| want := "github" | ||
|
|
||
| client, _ := NewTest(s.URL) | ||
|
|
||
| // run test | ||
| got, err := client.GetOrgName(u, "github") | ||
|
|
||
| if resp.Code != http.StatusOK { | ||
| t.Errorf("GetOrgName returned %v, want %v", resp.Code, http.StatusOK) | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Errorf("GetOrgName returned err: %v", err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(got, want) { | ||
| t.Errorf("GetOrgName is %v, want %v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGithub_GetOrgName_Personal(t *testing.T) { | ||
| // setup context | ||
| gin.SetMode(gin.TestMode) | ||
|
|
||
| resp := httptest.NewRecorder() | ||
| _, engine := gin.CreateTestContext(resp) | ||
|
|
||
| // setup mock server | ||
| engine.GET("/api/v3/user", func(c *gin.Context) { | ||
| c.Header("Content-Type", "application/json") | ||
| c.Status(http.StatusOK) | ||
| c.File("testdata/user.json") | ||
| }) | ||
|
|
||
| s := httptest.NewServer(engine) | ||
| defer s.Close() | ||
|
|
||
| // setup types | ||
| u := new(library.User) | ||
| u.SetName("foo") | ||
| u.SetToken("bar") | ||
|
|
||
| want := "octocat" | ||
|
|
||
| client, _ := NewTest(s.URL) | ||
|
|
||
| // run test | ||
| got, err := client.GetOrgName(u, "octocat") | ||
|
|
||
| if resp.Code != http.StatusOK { | ||
| t.Errorf("GetOrgName returned %v, want %v", resp.Code, http.StatusOK) | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Errorf("GetOrgName returned err: %v", err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(got, want) { | ||
| t.Errorf("GetOrgName is %v, want %v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestGithub_GetOrgName_Fail(t *testing.T) { | ||
| // setup context | ||
| gin.SetMode(gin.TestMode) | ||
|
|
||
| resp := httptest.NewRecorder() | ||
| _, engine := gin.CreateTestContext(resp) | ||
|
|
||
| // setup mock server | ||
| engine.GET("/api/v3/orgs/:org", func(c *gin.Context) { | ||
| c.Header("Content-Type", "application/json") | ||
| c.Status(http.StatusNotFound) | ||
| }) | ||
|
|
||
| s := httptest.NewServer(engine) | ||
| defer s.Close() | ||
|
|
||
| // setup types | ||
| u := new(library.User) | ||
| u.SetName("foo") | ||
| u.SetToken("bar") | ||
|
|
||
| client, _ := NewTest(s.URL) | ||
|
|
||
| // run test | ||
| _, err := client.GetOrgName(u, "octocat") | ||
|
|
||
| if err == nil { | ||
| t.Error("GetOrgName should return error") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| { | ||
| "login": "github", | ||
| "id": 1, | ||
| "node_id": "MDEyOk9yZ2FuaXphdGlvbjE=", | ||
| "url": "https://api.github.com/orgs/github", | ||
| "repos_url": "https://api.github.com/orgs/github/repos", | ||
| "events_url": "https://api.github.com/orgs/github/events", | ||
| "hooks_url": "https://api.github.com/orgs/github/hooks", | ||
| "issues_url": "https://api.github.com/orgs/github/issues", | ||
| "members_url": "https://api.github.com/orgs/github/members{/member}", | ||
| "public_members_url": "https://api.github.com/orgs/github/public_members{/member}", | ||
| "avatar_url": "https://github.com/images/error/octocat_happy.gif", | ||
| "description": "A great organization", | ||
| "name": "github", | ||
| "company": "GitHub", | ||
| "blog": "https://github.com/blog", | ||
| "location": "San Francisco", | ||
| "email": "[email protected]", | ||
| "twitter_username": "github", | ||
| "is_verified": true, | ||
| "has_organization_projects": true, | ||
| "has_repository_projects": true, | ||
| "public_repos": 2, | ||
| "public_gists": 1, | ||
| "followers": 20, | ||
| "following": 0, | ||
| "html_url": "https://github.com/octocat", | ||
| "created_at": "2008-01-14T04:33:35Z", | ||
| "updated_at": "2014-03-03T18:58:10Z", | ||
| "type": "Organization", | ||
| "total_private_repos": 100, | ||
| "owned_private_repos": 100, | ||
| "private_gists": 81, | ||
| "disk_usage": 10000, | ||
| "collaborators": 8, | ||
| "billing_email": "[email protected]", | ||
| "plan": { | ||
| "name": "Medium", | ||
| "space": 400, | ||
| "private_repos": 20, | ||
| "filled_seats": 4, | ||
| "seats": 5 | ||
| }, | ||
| "default_repository_permission": "read", | ||
| "members_can_create_repositories": true, | ||
| "two_factor_requirement_enabled": true, | ||
| "members_allowed_repository_creation_type": "all", | ||
| "members_can_create_public_repositories": false, | ||
| "members_can_create_private_repositories": false, | ||
| "members_can_create_internal_repositories": false, | ||
| "members_can_create_pages": true, | ||
| "members_can_fork_private_repositories": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.