/
global.go
55 lines (44 loc) · 1.5 KB
/
global.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
package cli
import (
"github.com/99designs/keyring"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
// GlobalConfig used for defaults and command line arguments
type GlobalConfig struct {
//Region in AWS used by KMSAuth and BLESS
Region string
Profile string
AuthProvider []AuthConfig
Session *session.Session
Keyring *keyring.Keyring
}
// ConfigureGlobal application arguments and flags
func ConfigureGlobal(app *kingpin.Application, config *GlobalConfig) {
app.Flag("region", "The region in AWS").
Default(config.Region).
Envar("AWS_REGION").
StringVar(&config.Region)
app.Flag("profile", "The profile to use as defined in the AWS config file").
Default(config.Profile).
Envar("AWS_PROFILE").
StringVar(&config.Profile)
app.PreAction(func(c *kingpin.ParseContext) (err error) {
// Attempt to open the aws-vault keychain
keychain, err := keyring.Open(keyring.Config{
KeychainName: "aws-oidc",
ServiceName: "aws-oidc",
AllowedBackends: []keyring.BackendType{keyring.KeychainBackend},
KeychainTrustApplication: true,
})
kingpin.FatalIfError(err, "Could not open aws-vault keychain")
config.Keyring = &keychain
config.Session = session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(config.Region)},
Profile: config.Profile,
SharedConfigState: session.SharedConfigEnable,
}))
return nil
})
}