This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
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
4 changed files
with
160 additions
and
3 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
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,67 @@ | ||
package tokencache | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
) | ||
|
||
type content struct { | ||
Credentials contentCredentials `json:"Credentials"` | ||
} | ||
|
||
type contentCredentials struct { | ||
AccessKeyID string `json:"AccessKeyId"` | ||
SecretAccessKey string `json:"SecretAccessKey"` | ||
SessionToken string `json:"SessionToken"` | ||
Expiration time.Time `json:"Expiration"` | ||
} | ||
|
||
func newFromAWSCredentials(credentials aws.Credentials) content { | ||
return content{ | ||
Credentials: contentCredentials{ | ||
AccessKeyID: credentials.AccessKeyID, | ||
SecretAccessKey: credentials.SecretAccessKey, | ||
SessionToken: credentials.SessionToken, | ||
Expiration: credentials.Expires, | ||
}, | ||
} | ||
} | ||
|
||
func (c *content) awsCredentials() aws.Credentials { | ||
return aws.Credentials{ | ||
AccessKeyID: c.Credentials.AccessKeyID, | ||
SecretAccessKey: c.Credentials.SecretAccessKey, | ||
SessionToken: c.Credentials.SessionToken, | ||
Expires: c.Credentials.Expiration, | ||
CanExpire: true, | ||
} | ||
} | ||
|
||
func decodeFrom(name string) (*content, error) { | ||
f, err := os.Open(name) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot open %s: %w", name, err) | ||
} | ||
defer f.Close() | ||
var c content | ||
if err := json.NewDecoder(f).Decode(&c); err != nil { | ||
return nil, fmt.Errorf("cannot decode json from %s: %w", name, err) | ||
} | ||
return &c, nil | ||
} | ||
|
||
func encodeTo(name string, c content) error { | ||
f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) | ||
if err != nil { | ||
return fmt.Errorf("cannot create %s: %w", name, err) | ||
} | ||
defer f.Close() | ||
if err := json.NewEncoder(f).Encode(&c); err != nil { | ||
return fmt.Errorf("cannot encode json to %s: %w", name, err) | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Package tokencache provides access to the token cache in ~/.aws/cli/cache. | ||
// This is interoperable with the token cache of AWS CLI. | ||
package tokencache | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"fmt" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/aws/external" | ||
) | ||
|
||
// Load reads the token cache file and returns the credentials. | ||
func Load(cfg external.SharedConfig) (*aws.Credentials, error) { | ||
name := computeFilename(cfg) | ||
cache, err := decodeFrom(name) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot load the credentials from cache: %w", err) | ||
} | ||
credentials := cache.awsCredentials() | ||
return &credentials, nil | ||
} | ||
|
||
// Save writes the credentials to the token cache file. | ||
func Save(cfg external.SharedConfig, credentials aws.Credentials) error { | ||
name := computeFilename(cfg) | ||
if err := encodeTo(name, newFromAWSCredentials(credentials)); err != nil { | ||
return fmt.Errorf("cannot save the credentials to %s: %w", name, err) | ||
} | ||
return nil | ||
} | ||
|
||
func computeFilename(cfg external.SharedConfig) string { | ||
key := computeKey(cfg) | ||
dir := filepath.Join(filepath.Dir(external.DefaultSharedConfigFilename()), "cli", "cache") | ||
return filepath.Join(dir, key+".json") | ||
} | ||
|
||
// computeKey returns a key for the config. | ||
// This is based on https://github.com/boto/botocore/blob/9f5afe26cc4a2695dcb62db453f9db7c299f3ffc/botocore/credentials.py#L714 | ||
func computeKey(cfg external.SharedConfig) string { | ||
var a []string | ||
// keys must be sorted | ||
if cfg.RoleDurationSeconds != nil { | ||
a = append(a, fmt.Sprintf(`"DurationSeconds": %d`, int(cfg.RoleDurationSeconds.Seconds()))) | ||
} | ||
a = append(a, `"RoleArn": `+strconv.Quote(cfg.RoleARN)) | ||
if cfg.RoleSessionName != "" { | ||
a = append(a, `"RoleSessionName": `+strconv.Quote(cfg.RoleSessionName)) | ||
} | ||
a = append(a, `"SerialNumber": `+strconv.Quote(cfg.MFASerial)) | ||
s := sha1.Sum([]byte(fmt.Sprintf("{%s}", strings.Join(a, ", ")))) | ||
return hex.EncodeToString(s[:]) | ||
} |