-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.go
More file actions
90 lines (79 loc) · 2.07 KB
/
config.go
File metadata and controls
90 lines (79 loc) · 2.07 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package awsmfa
import (
"errors"
"os"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/urfave/cli/v2"
)
// Config holds information about the configuration of awsmfa.
type Config struct {
// sts client
client *sts.Client
// sts config
profile string
mfaProfileName string
configPath string
credentialsPath string
durationSeconds int32
serialNumber string
mfaTokenCode string
// output
outConfigPath string
outCredentialsPath string
awsDir string
// print log config
quiet bool
}
// NewConfig generates the Config.
func NewConfig(c *cli.Context) (*Config, error) {
serialNumber := c.String("serial-number")
if serialNumber == "" {
return nil, errors.New("--serial-number is required")
}
mfaTokenCode := c.Args().First()
if mfaTokenCode == "" {
return nil, errors.New("[token-code] arguments is required")
}
cfg, err := config.LoadDefaultConfig(c.Context,
config.WithSharedConfigProfile(c.String("profile")),
config.WithRegion(os.Getenv("AWS_REGION")),
)
if err != nil {
return nil, err
}
client := sts.NewFromConfig(cfg)
var (
configPath string
credentialsPath string
)
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
if v := os.Getenv("AWS_CONFIG_FILE"); v != "" {
configPath = v
} else {
configPath = filepath.Join(homeDir, ".aws", "config")
}
if v := os.Getenv("AWS_SHARED_CREDENTIALS_FILE"); v != "" {
credentialsPath = v
} else {
credentialsPath = filepath.Join(homeDir, ".aws", "credentials")
}
return &Config{
client: client,
profile: c.String("profile"),
mfaProfileName: c.String("mfa-profile-name"),
configPath: configPath,
credentialsPath: credentialsPath,
durationSeconds: int32(c.Int("duration-seconds")),
serialNumber: serialNumber,
mfaTokenCode: mfaTokenCode,
outConfigPath: configPath,
outCredentialsPath: credentialsPath,
awsDir: filepath.Join(homeDir, ".aws"),
quiet: c.Bool("quiet"),
}, nil
}