Skip to content

Commit

Permalink
Add support for AWS credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jun 24, 2019
1 parent fa4ba0e commit f7ac876
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
2 changes: 2 additions & 0 deletions credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func ParseSingleCredentials(credentialsMap map[string]interface{}) (Credentials,
}

switch credentialsType {
case "aws":
credentials = NewAmazonWebServicesCredentials()
case "usernamepassword":
credentials = NewUsernamePassword()
case "secret":
Expand Down
47 changes: 47 additions & 0 deletions credentials/credentials_aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package credentials

import (
"fmt"

log "github.com/sirupsen/logrus"
)

// SecretTextCredentials represents credentials composed of a single string value
type AmazonWebServicesCredentials struct {
Base `mapstructure:",squash"`
AccessKey string `mapstructure:"access_key"`
SecretKey string `mapstructure:"secret_key"`
RoleARN string `mapstructure:"role_arn"`
MFASerialNumber string `mapstructure:"mfa_serial"`
}

// NewSecretText instantiates a SecretTextCredentials struct
func NewAmazonWebServicesCredentials() *AmazonWebServicesCredentials {
cred := &AmazonWebServicesCredentials{}
cred.CredType = "Amazon Web Services"
return cred
}

// ToString prints out the content of a SecretTextCredentials struct.
// If showSensitive is true, the secret text will be shown
func (cred *AmazonWebServicesCredentials) ToString(showSensitive bool) string {
secretKey := "********"
if showSensitive {
secretKey = cred.SecretKey
}
return fmt.Sprintf("%s - %s:%s", cred.BaseToString(), cred.AccessKey, secretKey)
}

// Validate verifies that the credentials is valid.
// A SecretTextCredentials is always considered valid, as empty values are accepted.
func (cred *AmazonWebServicesCredentials) Validate() bool {
if cred.AccessKey == "" {
log.Errorf("The credentials with ID %s does not define an access key", cred.ID)
return false
}
if cred.SecretKey == "" {
log.Errorf("The credentials with ID %s does not define an secret key", cred.ID)
return false
}
return true
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ require (
gopkg.in/yaml.v2 v2.2.2
)

replace github.com/bndr/gojenkins => github.com/julienduchesne/gojenkins v2.0.1+incompatible
replace github.com/bndr/gojenkins => github.com/julienduchesne/gojenkins v2.1.0+incompatible
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/julienduchesne/gojenkins v2.0.1+incompatible h1:EEsEPWaysnZyN+RYkLgKuF09Me50yhD5/nDsDAgr86I=
github.com/julienduchesne/gojenkins v2.0.1+incompatible/go.mod h1:vyTgrnBY+eN7w3FULQEfdrbSMecftCpXw9XGQmF76dk=
github.com/julienduchesne/gojenkins v2.1.0+incompatible h1:B5MG08Pwfjys8y/5i9x4psqCZOMJXf2+oBC4nhiistc=
github.com/julienduchesne/gojenkins v2.1.0+incompatible/go.mod h1:vyTgrnBY+eN7w3FULQEfdrbSMecftCpXw9XGQmF76dk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
Expand Down
10 changes: 10 additions & 0 deletions targets/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func (jenkins *JenkinsTarget) ValidateConfiguration() bool {

func toJenkinsCredential(creds credentials.Credentials) interface{} {
switch creds.(type) {
case *credentials.AmazonWebServicesCredentials:
castCreds := creds.(*credentials.AmazonWebServicesCredentials)
return &gojenkins.AmazonWebServicesCredentials{
ID: creds.GetID(),
Description: castCreds.GetDescriptionOrID(),
AccessKey: castCreds.AccessKey,
SecretKey: castCreds.SecretKey,
IAMRoleARN: castCreds.RoleARN,
IAMMFASerialNumber: castCreds.MFASerialNumber,
}
case *credentials.SecretTextCredentials:
castCreds := creds.(*credentials.SecretTextCredentials)
return &gojenkins.StringCredentials{
Expand Down

0 comments on commit f7ac876

Please sign in to comment.