Skip to content

Commit eb788b2

Browse files
committed
fix: add specific stuff
1 parent 3948193 commit eb788b2

File tree

8 files changed

+84
-21
lines changed

8 files changed

+84
-21
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
NAME := aws-cli-auth
2-
VERSION := v0.3.0
2+
VERSION := v0.4.0
33
REVISION := $(shell git rev-parse --short HEAD)
44

55
LDFLAGS := -ldflags="-s -w -X \"github.com/dnitsch/aws-cli-auth/version.Version=$(VERSION)\" -X \"github.com/dnitsch/aws-cli-auth/version.Revision=$(REVISION)\" -extldflags -static"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ To give it a quick test.
117117
aws sts get-caller-identity --profile=nonprod_saml_admin
118118
```
119119

120-
### Integrate aws-cli
120+
### AWS Credential Process
121121

122122
[Sourcing credentials with an external process](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sourcing-external.html) describes how to integrate aws-cli with external tool.
123123
You can use `aws-cli-auth` as the external process. Add the following lines to your `.aws/config` file.

cmd/clear.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ func init() {
2222
}
2323

2424
func clear(cmd *cobra.Command, args []string) {
25+
web := web.New()
26+
2527
if force {
28+
2629
if err := web.ClearCache(); err != nil {
2730
util.Exit(err)
2831
}

cmd/specific.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func specific(cmd *cobra.Command, args []string) {
3535
if method != "" {
3636
switch method {
3737
case "WEB_ID":
38-
awsCreds, err = auth.LoginAwsWebToken(os.Getenv("USER"))
38+
awsCreds, err = auth.LoginAwsWebToken(os.Getenv("USER")) // TODO: redo this getUser implementation
3939
if err != nil {
4040
util.Exit(err)
4141
}
@@ -45,5 +45,12 @@ func specific(cmd *cobra.Command, args []string) {
4545
}
4646
config := config.SamlConfig{BaseConfig: config.BaseConfig{StoreInProfile: storeInProfile}}
4747

48+
if role != "" {
49+
awsCreds, err = auth.AssumeRoleWithCreds(awsCreds, os.Getenv("USER"), role)
50+
if err != nil {
51+
util.Exit(err)
52+
}
53+
}
54+
4855
util.SetCredentials(awsCreds, config)
4956
}

internal/auth/awssts.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66

77
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/credentials"
89
"github.com/aws/aws-sdk-go/aws/session"
910
"github.com/aws/aws-sdk-go/service/sts"
1011
"github.com/dnitsch/aws-cli-auth/internal/config"
@@ -85,3 +86,37 @@ func LoginAwsWebToken(username string) (*util.AWSCredentials, error) {
8586
Expires: resp.Credentials.Expiration.Local(),
8687
}, nil
8788
}
89+
90+
func AssumeRoleWithCreds(creds *util.AWSCredentials, username, role string) (*util.AWSCredentials, error) {
91+
sess, err := session.NewSession()
92+
if err != nil {
93+
return nil, errors.Wrap(err, "Failed to create session")
94+
}
95+
96+
specificCreds := credentials.NewStaticCredentialsFromCreds(credentials.Value{
97+
AccessKeyID: creds.AWSAccessKey,
98+
SecretAccessKey: creds.AWSSecretKey,
99+
SessionToken: creds.AWSSessionToken,
100+
})
101+
102+
svc := sts.New(sess, aws.NewConfig().WithCredentials(specificCreds))
103+
sessionName := util.SessionName(username, config.SELF_NAME)
104+
105+
input := &sts.AssumeRoleInput{
106+
RoleArn: &role,
107+
RoleSessionName: &sessionName,
108+
}
109+
roleCreds, err := svc.AssumeRole(input)
110+
111+
if err != nil {
112+
return nil, errors.Wrap(err, "Failed to retrieve STS credentials using Role Provided")
113+
}
114+
115+
return &util.AWSCredentials{
116+
AWSAccessKey: aws.StringValue(roleCreds.Credentials.AccessKeyId),
117+
AWSSecretKey: aws.StringValue(roleCreds.Credentials.SecretAccessKey),
118+
AWSSessionToken: aws.StringValue(roleCreds.Credentials.SessionToken),
119+
PrincipalARN: aws.StringValue(roleCreds.AssumedRoleUser.Arn),
120+
Expires: roleCreds.Credentials.Expiration.Local(),
121+
}, nil
122+
}

internal/auth/saml.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package auth
33
import (
44
"fmt"
55
"os/user"
6+
"runtime"
67

78
"github.com/dnitsch/aws-cli-auth/internal/config"
89
"github.com/dnitsch/aws-cli-auth/internal/util"
@@ -15,9 +16,13 @@ func GetSamlCreds(conf config.SamlConfig) {
1516
util.Exit(nil)
1617
}
1718

19+
web := web.New()
1820
var awsCreds *util.AWSCredentials
1921
var err error
2022

23+
os := runtime.GOOS
24+
util.Writeln("Is OS: %s\nAnd conf.BaseConfig.StoreInProfile: %v", os, conf.BaseConfig.StoreInProfile)
25+
2126
// Try to reuse stored credential in secret
2227
if !conf.BaseConfig.StoreInProfile {
2328
awsCreds, err = util.AWSCredential(conf.BaseConfig.Role)
@@ -27,11 +32,11 @@ func GetSamlCreds(conf config.SamlConfig) {
2732

2833
t, err := web.GetSamlLogin(conf)
2934
if err != nil {
30-
fmt.Printf("Err: %v", err)
35+
util.Writeln("Err: %v", err)
3136
}
3237
user, err := user.Current()
3338
if err != nil {
34-
fmt.Errorf(err.Error())
39+
util.Writeln(err.Error())
3540
}
3641

3742
roleObj := &util.AWSRole{RoleARN: conf.BaseConfig.Role, PrincipalARN: conf.PrincipalArn, Name: util.SessionName(user.Username, config.SELF_NAME), Duration: conf.Duration}

internal/util/secret.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func init() {
3030
}
3131

3232
Secret.AWSCredentials = make(map[string]string)
33-
Secret.Load()
3433
}
3534

3635
var secretService = config.SELF_NAME
@@ -126,7 +125,7 @@ func AWSCredential(roleArn string) (*AWSCredentials, error) {
126125

127126
jsonStr, ok := Secret.AWSCredentials[roleArn]
128127
if !ok {
129-
return nil, fmt.Errorf("Not found the credential for %s", roleArn)
128+
Exit(fmt.Errorf("Not found the credential for %s", roleArn))
130129
}
131130

132131
Writeln("Got credential from OS secret store for %s", roleArn)

internal/web/web.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,45 @@ import (
1717
ps "github.com/mitchellh/go-ps"
1818
)
1919

20-
var (
21-
datadir = path.Join(util.GetHomeDir(), fmt.Sprintf(".%s-data", config.SELF_NAME))
22-
)
20+
type Web struct {
21+
datadir *string
22+
launcher *launcher.Launcher
23+
browser *rod.Browser
24+
}
2325

24-
func GetSamlLogin(conf config.SamlConfig) (string, error) {
26+
func New() *Web {
27+
ddir := path.Join(util.GetHomeDir(), fmt.Sprintf(".%s-data", config.SELF_NAME))
2528

2629
l := launcher.New().
2730
Headless(false).
2831
Devtools(false)
2932

30-
// do not clean up userdata
31-
32-
// datadir := path.Join(util.GetHomeDir(), fmt.Sprintf(".%s-data", config.SELF_NAME))
33-
util.WriteDataDir(datadir)
34-
url := l.UserDataDir(datadir).MustLaunch()
33+
url := l.UserDataDir(ddir).MustLaunch()
3534

3635
browser := rod.New().
3736
ControlURL(url).
3837
MustConnect().NoDefaultDevice()
3938

40-
defer browser.MustClose()
39+
return &Web{
40+
datadir: &ddir,
41+
launcher: l,
42+
browser: browser,
43+
}
44+
45+
}
46+
47+
func (web *Web) GetSamlLogin(conf config.SamlConfig) (string, error) {
48+
49+
// do not clean up userdata
50+
51+
// datadir := path.Join(util.GetHomeDir(), fmt.Sprintf(".%s-data", config.SELF_NAME))
52+
util.WriteDataDir(*web.datadir)
53+
54+
defer web.browser.MustClose()
4155

42-
page := browser.MustPage(conf.ProviderUrl)
56+
page := web.browser.MustPage(conf.ProviderUrl)
4357

44-
router := browser.HijackRequests()
58+
router := web.browser.HijackRequests()
4559
defer router.MustStop()
4660

4761
router.MustAdd(conf.AcsUrl, func(ctx *rod.Hijack) {
@@ -62,9 +76,9 @@ func GetSamlLogin(conf config.SamlConfig) (string, error) {
6276

6377
}
6478

65-
func ClearCache() error {
79+
func (web *Web) ClearCache() error {
6680
errs := []error{}
67-
if err := os.Remove(datadir); err != nil {
81+
if err := os.Remove(*web.datadir); err != nil {
6882
errs = append(errs, err)
6983
}
7084
if err := checkRodProcess(); err != nil {

0 commit comments

Comments
 (0)