-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
now makes an aws sdk call to get access keys
- Loading branch information
Showing
6 changed files
with
145 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
type CacheFile struct { | ||
StartUrl string `json:startUrl` | ||
Region string `json:region` | ||
AccessToken string `json:accessToken` | ||
ExpiresAt string `json:expiresAt` | ||
Expiration time.Time | ||
} | ||
|
||
func parseDate(expiration string) time.Time { | ||
t, err := time.Parse(time.RFC3339Nano, expiration) | ||
if err != nil { | ||
t, err = time.Parse("2006-01-02T15:04:05MST", expiration) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
return t | ||
} | ||
|
||
func getCacheFile() CacheFile { | ||
homedir, err := os.UserHomeDir() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cachepath := fmt.Sprintf("%s/.aws/sso/cache", homedir) | ||
files, err := ioutil.ReadDir(cachepath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var latestCache *CacheFile | ||
for _, f := range files { | ||
path := fmt.Sprintf("%s/%s", cachepath, f.Name()) | ||
jsonFile, err := os.Open(path) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer jsonFile.Close() | ||
value, err := ioutil.ReadAll(jsonFile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var cache CacheFile | ||
json.Unmarshal(value, &cache) | ||
if cache.AccessToken != "" { | ||
cache.Expiration = parseDate(cache.ExpiresAt) | ||
if latestCache == nil || latestCache.Expiration.Before(cache.Expiration) { | ||
latestCache = &cache | ||
} | ||
} | ||
} | ||
if latestCache == nil { | ||
log.Fatal("No cached credentials. You need to run aws sso login.") | ||
} | ||
if latestCache.Expiration.Before(time.Now()) { | ||
log.Fatal("Cached credentials have expired. You need to run aws sso login.") | ||
} | ||
return *latestCache | ||
} |
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 |
---|---|---|
@@ -1,74 +1,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"time" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/sso" | ||
) | ||
|
||
type Credentials struct { | ||
AccessKeyId string `json:AccessKeyId` | ||
SecretAccessKey string `json:SecretAccessKey` | ||
SessionToken string `json:SessionToken` | ||
Expiration string `json:Expiration` | ||
type Creds struct { | ||
AccessKeyId string `json:accessKeyId` | ||
SecretAccessKey string `json:secretAccessKey` | ||
SessionToken string `json:sessionToken` | ||
} | ||
|
||
type CacheFile struct { | ||
ProviderType string `json:ProviderType` | ||
Credentials Credentials `json:Credentials` | ||
Expiration time.Time | ||
} | ||
|
||
func parseDate(expiration string) time.Time { | ||
t, err := time.Parse(time.RFC3339Nano, expiration) | ||
if err != nil { | ||
t, err = time.Parse("2006-01-02T15:04:05MST", expiration) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
return t | ||
} | ||
|
||
func getCacheFile() CacheFile { | ||
homedir, err := os.UserHomeDir() | ||
func getCredentials(cache CacheFile, accountId string, roleName string) (*Creds, error) { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String(cache.Region), | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
cachepath := fmt.Sprintf("%s/.aws/cli/cache", homedir) | ||
files, err := ioutil.ReadDir(cachepath) | ||
svc := sso.New(sess) | ||
output, err := svc.GetRoleCredentials(&sso.GetRoleCredentialsInput{ | ||
AccessToken: aws.String(cache.AccessToken), | ||
AccountId: aws.String(accountId), | ||
RoleName: aws.String(roleName), | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var latestCache *CacheFile | ||
for _, f := range files { | ||
path := fmt.Sprintf("%s/%s", cachepath, f.Name()) | ||
jsonFile, err := os.Open(path) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer jsonFile.Close() | ||
value, err := ioutil.ReadAll(jsonFile) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
var cache CacheFile | ||
json.Unmarshal(value, &cache) | ||
if cache.ProviderType == "sso" { | ||
cache.Expiration = parseDate(cache.Credentials.Expiration) | ||
if latestCache == nil || latestCache.Expiration.Before(cache.Expiration) { | ||
latestCache = &cache | ||
} | ||
} | ||
} | ||
if latestCache == nil { | ||
log.Fatal("No cached credentials. You need to run aws sso login.") | ||
return nil, err | ||
} | ||
if latestCache.Expiration.Before(time.Now()) { | ||
log.Fatal("Cached credentials have expired. You need to run aws sso login.") | ||
} | ||
return *latestCache | ||
} | ||
return &Creds{ | ||
AccessKeyId: *output.RoleCredentials.AccessKeyId, | ||
SecretAccessKey: *output.RoleCredentials.SecretAccessKey, | ||
SessionToken: *output.RoleCredentials.SessionToken, | ||
}, 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
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