Skip to content

Commit

Permalink
add save command
Browse files Browse the repository at this point in the history
  • Loading branch information
winjer committed May 16, 2020
1 parent 39dadbe commit 4d38011
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
awsso2env
awssso2env
74 changes: 74 additions & 0 deletions cmd/creds.go
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
}
10 changes: 7 additions & 3 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(envCmd)
}

var addCmd = &cobra.Command{
var envCmd = &cobra.Command{
Use: "env",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("foo")
creds := getCacheFile()
fmt.Printf("export AWS_ACCESS_KEY_ID=%s\n", creds.Credentials.AccessKeyId)
fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s\n", creds.Credentials.SecretAccessKey)
fmt.Printf("export AWS_SESSION_TOKEN=%s\n", creds.Credentials.SessionToken)
},
}
41 changes: 41 additions & 0 deletions cmd/save.go
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)
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/winjer/awssso2env

go 1.14

require github.com/spf13/cobra v1.0.0
require (
github.com/spf13/cobra v1.0.0
gopkg.in/ini.v1 v1.56.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.56.0 h1:DPMeDvGTM54DXbPkVIZsp19fp/I2K7zwA/itHYHKo8Y=
gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit 4d38011

Please sign in to comment.