Skip to content

Commit

Permalink
Force AWS usage of creds file (#11)
Browse files Browse the repository at this point in the history
* Forcing aws session to read the local file

* Updated token validation
  • Loading branch information
cameronnewman authored Jun 23, 2019
1 parent fa2be4e commit 11602b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
16 changes: 15 additions & 1 deletion internal/pkg/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ import (
"bytes"
"os/user"
"path/filepath"
"regexp"
"time"

"gopkg.in/ini.v1"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"

"github.com/spf13/afero"
)

const (
tokenValidationRegex string = "^[0-9]+$"
)

var (
appFs = afero.NewOsFs()

tokenValidationRegexComplied = regexp.MustCompilePOSIX(tokenValidationRegex)
)

//Credentials represents the set of attributes used to authenticate to AWS with a short lived session
Expand Down Expand Up @@ -58,7 +67,9 @@ func GenerateSTSCredentials(profile string, tokenCode string) (*Credentials, err
}

awsSession := session.Must(session.NewSessionWithOptions(session.Options{
Profile: profile,
Config: aws.Config{
Credentials: credentials.NewSharedCredentials(path, profile),
},
}))

iamInstance := iam.New(awsSession)
Expand Down Expand Up @@ -133,6 +144,9 @@ func validateToken(token string) error {
if len(token) <= 5 {
return ErrInvalidToken
}
if !tokenValidationRegexComplied.MatchString(token) {
return ErrInvalidToken
}

return nil
}
14 changes: 14 additions & 0 deletions internal/pkg/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ func Test_validateToken(t *testing.T) {
},
true,
},
{
"Invalid/NotNumbers",
args{
token: "23ss21",
},
true,
},
{
"Invalid/NotNumbersLong",
args{
token: "5364f'[73",
},
true,
},
{
"Valid/Token",
args{
Expand Down

0 comments on commit 11602b2

Please sign in to comment.