-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DT-3190 [GitHub] Add support for "GitHub" credentials type in credent…
…ials-sync (#87) * DT-3190 [GitHub] Add support for "GitHub" credentials type in credentials-sync Main changes: * Added new source type "github_app" * Updated the documentation in the README Other changes: * Ran go fmt and golint on the codebase * Fixed warnings reported by the IDE * (go.mod) Updated gojenkins library to latest released version * (credentials.go) Better error messages when parsing of config fails * (credentials_test.go) Added more tests to cover all valid source types Co-authored-by: Denis Blanchette <dblanchette@coveo.com>
- Loading branch information
1 parent
8dbd023
commit 4ef0e65
Showing
9 changed files
with
369 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,47 @@ | ||
package credentials | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// GithubAppCredentials represents credentials composed of an App ID, private key, and owner | ||
type GithubAppCredentials struct { | ||
Base `mapstructure:",squash"` | ||
AppID int `mapstructure:"app_id"` | ||
PrivateKey string `mapstructure:"private_key"` | ||
Owner string `mapstructure:"owner"` | ||
} | ||
|
||
// NewGithubAppCredentials instantiates a GithubAppCredentials struct | ||
func NewGithubAppCredentials() *GithubAppCredentials { | ||
cred := &GithubAppCredentials{} | ||
cred.CredType = "Github App" | ||
return cred | ||
} | ||
|
||
// ToString prints out the content of a GithubAppCredentials struct. | ||
func (cred *GithubAppCredentials) ToString(showSensitive bool) string { | ||
privateKeyText := "********" | ||
if showSensitive { | ||
privateKeyText = cred.PrivateKey | ||
} | ||
|
||
appIDOwner := fmt.Sprintf("%d", cred.AppID) | ||
if len(cred.Owner) > 0 { | ||
appIDOwner = fmt.Sprintf("%s(%s)", appIDOwner, cred.Owner) | ||
} | ||
return fmt.Sprintf("%s - %s:%s", cred.BaseToString(), appIDOwner, privateKeyText) | ||
} | ||
|
||
// Validate verifies that the credentials is valid. | ||
// A GithubAppCredentials must have an app id and a private key. Owner is optional. | ||
func (cred *GithubAppCredentials) Validate() error { | ||
switch { | ||
case cred.AppID == 0: | ||
return fmt.Errorf("the credentials with ID %s does not define an app ID", cred.ID) | ||
case len(cred.PrivateKey) == 0: | ||
return fmt.Errorf("the credentials with ID %s does not define a private key", cred.ID) | ||
default: | ||
return nil | ||
} | ||
} |
This file contains 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,70 @@ | ||
package credentials | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGithubAppCredentials(t *testing.T) { | ||
tests := map[string]struct { | ||
givenID string | ||
givenAppID int | ||
givenOwner string | ||
givenPrivate string | ||
givenShowSensitive bool | ||
expectString string | ||
}{ | ||
"without owner": {givenID: "test", givenAppID: 1, givenOwner: "", givenPrivate: "private", givenShowSensitive: false, expectString: "test -> Type: Github App - 1:********"}, | ||
"with owner": {givenID: "test", givenAppID: 2, givenOwner: "owner", givenPrivate: "private", givenShowSensitive: false, expectString: "test -> Type: Github App - 2(owner):********"}, | ||
"without owner showSensitive": {givenID: "test", givenAppID: 1, givenOwner: "", givenPrivate: "private", givenShowSensitive: true, expectString: "test -> Type: Github App - 1:private"}, | ||
"with owner showSensitive": {givenID: "test", givenAppID: 2, givenOwner: "owner", givenPrivate: "private", givenShowSensitive: true, expectString: "test -> Type: Github App - 2(owner):private"}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
cred := NewGithubAppCredentials() | ||
cred.ID = test.givenID | ||
cred.AppID = test.givenAppID | ||
cred.Owner = test.givenOwner | ||
cred.PrivateKey = test.givenPrivate | ||
assert.Equal(t, test.expectString, cred.ToString(test.givenShowSensitive)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGithubAppCredentialsValidation(t *testing.T) { | ||
tests := map[string]struct { | ||
givenCred GithubAppCredentials | ||
expectError bool | ||
}{ | ||
"valid": {givenCred: GithubAppCredentials{ | ||
AppID: 12345, | ||
PrivateKey: "private", | ||
Owner: "Me", | ||
}, expectError: false}, | ||
"valid no owner": {givenCred: GithubAppCredentials{ | ||
AppID: 12345, | ||
PrivateKey: "private", | ||
}, expectError: false}, | ||
"missing app id": {givenCred: GithubAppCredentials{ | ||
PrivateKey: "private", | ||
}, expectError: true}, | ||
"missing private key": {givenCred: GithubAppCredentials{ | ||
AppID: 12345, | ||
}, expectError: true}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
cred := NewGithubAppCredentials() | ||
cred.AppID = test.givenCred.AppID | ||
cred.PrivateKey = test.givenCred.PrivateKey | ||
cred.Owner = test.givenCred.Owner | ||
err := cred.Validate() | ||
if test.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.