forked from felixb/swamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
129 lines (115 loc) · 4.19 KB
/
config.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"flag"
"fmt"
"strings"
"os"
)
const (
INTERMEDIATE_SESSION_TOKEN_DURATION = int64(12 * 60 * 60)
TARGET_SESSION_TOKEN_DURATION = int64(60 * 60)
VERSION = "0.7-snapshot"
)
type SwampConfig struct {
targetAccount string
intermediateProfile string
intermediateDuration int64
targetProfile string
targetRole string
targetDuration int64
profile string
region string
tokenSerialNumber string
useInstanceProfile bool
renew bool
exportProfile bool
exportFile string
}
func NewSwampConfig() *SwampConfig {
return &SwampConfig{
targetAccount: "",
intermediateProfile: "session-token",
intermediateDuration: INTERMEDIATE_SESSION_TOKEN_DURATION,
targetProfile: "",
targetRole: "",
targetDuration: TARGET_SESSION_TOKEN_DURATION,
profile: "default",
region: "eu-central-1",
tokenSerialNumber: "",
useInstanceProfile: false,
renew: false,
exportProfile: false,
exportFile: "/tmp/current_swamp_profile",
}
}
func (config *SwampConfig) isRoleArn() bool {
return strings.HasPrefix(config.targetRole, "arn:aws:iam::")
}
func (config *SwampConfig) GetRoleArn() *string {
if config.isRoleArn() {
return &config.targetRole
} else {
arn := fmt.Sprintf("arn:aws:iam::%s:role/%s", config.targetAccount, config.targetRole)
return &arn
}
}
func (config *SwampConfig) SetupFlags() {
flag.StringVar(&config.targetAccount, "account", config.targetAccount, "AWS account")
flag.StringVar(&config.intermediateProfile, "intermediate-profile", config.intermediateProfile, "Intermediate AWS CLI profile")
flag.Int64Var(&config.intermediateDuration, "intermediate-duration", config.intermediateDuration, "Token duration in seconds for intermediate profile")
flag.StringVar(&config.targetProfile, "target-profile", config.targetProfile, "Write this AWS CLI profile")
flag.StringVar(&config.targetRole, "target-role", config.targetRole, "AWS role to assume (can either be ARN or name)")
flag.Int64Var(&config.targetDuration, "target-duration", config.targetDuration, "Token duration in seconds for target profile")
flag.StringVar(&config.profile, "profile", config.profile, "AWS CLI profile")
flag.StringVar(&config.region, "region", config.region, "AWS region")
flag.StringVar(&config.tokenSerialNumber, "mfa-device", config.tokenSerialNumber, "MFA device arn")
flag.BoolVar(&config.useInstanceProfile, "instance", config.useInstanceProfile, "Use instance profile")
flag.BoolVar(&config.renew, "renew", config.renew, "renew token every duration/2")
flag.BoolVar(&config.exportProfile, "export-profile", config.exportProfile, "set AWS_PROFILE in environment")
flag.StringVar(&config.exportFile, "export-file", config.exportFile, "File to write AWS_PROFILE to, defaults to '/tmp/current_swamp_profile'")
flag.Usage = flagUsage
}
func (config *SwampConfig) Validate() {
checkStringFlagNotEmpty("target-profile", config.targetProfile)
checkStringFlagNotEmpty("target-role", config.targetRole)
checkStringFlagNotEmpty("profile", config.profile)
checkStringFlagNotEmpty("region", config.region)
if !config.isRoleArn() {
checkStringFlagNotEmpty("account", config.targetAccount)
}
if config.exportProfile && config.renew {
fmt.Fprintln(os.Stderr, "Using renew and export-profile is mutual exclusive")
flag.Usage()
os.Exit(1)
}
if config.useInstanceProfile {
if config.tokenSerialNumber != "" {
fmt.Fprintln(os.Stderr, "Using MFA and instance profile is mutual exclusive")
flag.Usage()
os.Exit(1)
}
if config.profile != "default" {
fmt.Fprintln(os.Stderr, "Using a profile and instance profile is mutual exclusive")
flag.Usage()
os.Exit(1)
}
}
if config.tokenSerialNumber != "" {
checkStringFlagNotEmpty("intermediate-profile", config.intermediateProfile)
}
if config.exportProfile {
checkStringFlagNotEmpty("export-file", config.exportFile)
}
}
func checkStringFlagNotEmpty(name string, f string) {
if f == "" {
fmt.Fprintf(os.Stderr, "Missing mandatory parameter: %s\n", name)
flag.Usage()
os.Exit(1)
}
}
func flagUsage() {
fmt.Fprintf(os.Stderr, "Version of %s: %s\n", os.Args[0], VERSION)
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}