-
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.
- Loading branch information
Showing
6 changed files
with
129 additions
and
5 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 |
---|---|---|
@@ -1 +1 @@ | ||
awsso2env | ||
awssso2env |
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,74 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
type Credentials struct { | ||
AccessKeyId string `json:AccessKeyId` | ||
SecretAccessKey string `json:SecretAccessKey` | ||
SessionToken string `json:SessionToken` | ||
Expiration string `json:Expiration` | ||
} | ||
|
||
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() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
cachepath := fmt.Sprintf("%s/.aws/cli/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.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.") | ||
} | ||
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
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,41 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/ini.v1" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(saveCmd) | ||
} | ||
|
||
var saveCmd = &cobra.Command{ | ||
Use: "save", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
creds := getCacheFile() | ||
UpdateIni(creds) | ||
}, | ||
} | ||
|
||
func UpdateIni(creds CacheFile) { | ||
homedir, err := os.UserHomeDir() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
credspath := fmt.Sprintf("%s/.aws/credentials", homedir) | ||
cfg, err := ini.Load(credspath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
d := cfg.Section("default") | ||
d.Key("aws_access_key_id").SetValue(creds.Credentials.AccessKeyId) | ||
d.Key("aws_secret_access_key").SetValue(creds.Credentials.SecretAccessKey) | ||
d.Key("aws_session_token").SetValue(creds.Credentials.SessionToken) | ||
d.Key("aws_security_token").SetValue(creds.Credentials.SessionToken) | ||
d.Key("aws_session_expiration").SetValue(creds.Credentials.Expiration) | ||
cfg.SaveTo(credspath) | ||
} |
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