forked from 99designs/aws-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
68 lines (57 loc) · 1.58 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"os"
"github.com/99designs/aws-vault/keyring"
"github.com/99designs/aws-vault/prompt"
"github.com/aws/aws-sdk-go/aws/credentials"
"gopkg.in/alecthomas/kingpin.v2"
)
type AddCommandInput struct {
Profile string
Keyring keyring.Keyring
FromEnv bool
}
func AddCommand(app *kingpin.Application, input AddCommandInput) {
var accessKeyId, secretKey string
if input.FromEnv {
if accessKeyId = os.Getenv("AWS_ACCESS_KEY_ID"); accessKeyId == "" {
app.Fatalf("Missing value for AWS_ACCESS_KEY_ID")
return
}
if secretKey = os.Getenv("AWS_SECRET_ACCESS_KEY"); secretKey == "" {
app.Fatalf("Missing value for AWS_SECRET_ACCESS_KEY")
return
}
} else {
var err error
if accessKeyId, err = prompt.TerminalPrompt("Enter Access Key ID: "); err != nil {
app.Fatalf(err.Error())
return
}
if secretKey, err = prompt.TerminalPrompt("Enter Secret Access Key: "); err != nil {
app.Fatalf(err.Error())
return
}
}
creds := credentials.Value{AccessKeyID: accessKeyId, SecretAccessKey: secretKey}
provider := &KeyringProvider{Keyring: input.Keyring, Profile: input.Profile}
if err := provider.Store(creds); err != nil {
app.Fatalf(err.Error())
return
}
fmt.Printf("Added credentials to profile %q in vault\n", input.Profile)
profiles, err := awsConfigFile.Parse()
if err != nil {
app.Fatalf("%v", err)
return
}
sessions, err := NewKeyringSessions(input.Keyring, profiles)
if err != nil {
app.Fatalf(err.Error())
return
}
if n, _ := sessions.Delete(input.Profile); n > 0 {
fmt.Printf("Deleted %d existing sessions.\n", n)
}
}