Skip to content

Commit

Permalink
Fixed ISO8601 format issue; Updated doucmentation and Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Lagerweij committed May 28, 2020
1 parent 63351ee commit 385904d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,24 @@ build:

run:
./dist/aws-sso-fetcher_darwin_amd64/aws-sso-fetcher hpydev_dev

VERSION := $(shell cat ./VERSION)

all: install


install:
go install -v

test:
go test ./... -v

fmt:
go fmt ./... -v

release:
git tag -a $(VERSION) -m "Release" || true
git push origin $(VERSION)
goreleaser --rm-dist

.PHONY: install test fmt release
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ sso_account_id = 0123456789
sso_role_name = AWSAdministratorAccess
region = us-east-2
output = json
```

And in your `~/.aws/credentials`, you'll need something like this:
```ini
[profile wrap_acme_dev]
credential_process = /Users/alice/bin/aws-sso-fetcher acme_dev
region = us-west-1
Expand All @@ -31,8 +34,7 @@ output = json
Once you get SSO credentials with:

```bash
export AWS_PROFILE=acme_dev
aws sso login
aws sso login --profile=acme_dev
```

You can then start using software with the other wrapper profile:
Expand Down
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type CredentialProcessJson struct {
AccessKeyID string `json:"AccessKeyId"`
SecretAccessKey string `json:"SecretAccessKey"`
SessionToken string `json:"SessionToken"`
Expiration time.Time `json:"Expiration"`
Expiration AWSTime `json:"Expiration"`
}

type Profile struct {
Expand All @@ -46,16 +46,20 @@ type AWSTime struct {
time.Time
}

func (t *AWSTime) UnmarshalJSON(buf []byte) error {

tt, err := time.Parse(time.RFC3339, strings.Trim(strings.Replace(string(buf), "UTC", "Z", 1), `"`))
if err != nil {
return err
func (it *AWSTime) UnmarshalJSON(data []byte) error {
t, err := time.Parse("2006-01-02T15:04:05Z07:00", strings.Trim(strings.Replace(string(data), "UTC", "Z", 1), `"`))
if err == nil {
*it = AWSTime{t}
}
t.Time = tt
return nil

return err
}

func (it AWSTime) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%sZ\"", it.Time.UTC().Format("2006-01-02T15:04:05"))), nil
}


func main(){
zerolog.SetGlobalLevel(zerolog.InfoLevel)
_, ok := os.LookupEnv("DEBUG")
Expand Down Expand Up @@ -152,7 +156,7 @@ func getCachedFile(awsSsoCachePath, awsSSOProfileName string) (*CredentialProces
if err != nil {
return nil, err
}
if time.Now().After(credentialProcessJson.Expiration) {
if time.Now().After(credentialProcessJson.Expiration.Time) {
log.Debug().Str("expire", credentialProcessJson.Expiration.String()).Msg("credentials expired")
return nil, nil
}
Expand Down Expand Up @@ -197,7 +201,7 @@ func getSsoRoleCredentials(profile Profile, awsSSOCredential AWSSSOCredential) (
AccessKeyID: *resp.RoleCredentials.AccessKeyId,
SecretAccessKey: *resp.RoleCredentials.SecretAccessKey,
SessionToken: *resp.RoleCredentials.SessionToken,
Expiration: aws.MillisecondsTimeValue(resp.RoleCredentials.Expiration),
Expiration: AWSTime{aws.MillisecondsTimeValue(resp.RoleCredentials.Expiration)},
}, nil
}

Expand Down

0 comments on commit 385904d

Please sign in to comment.