forked from openshift/rosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
155 lines (126 loc) · 3.72 KB
/
flags.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package breakglasscredential
import (
"bytes"
"encoding/json"
"fmt"
"time"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/openshift/rosa/pkg/interactive"
"github.com/openshift/rosa/pkg/rosa"
)
const (
usernameFlag = "username"
expirationFlag = "expiration"
)
type BreakGlassCredentialArgs struct {
username string
expirationDuration time.Duration
}
func AddBreakGlassCredentialFlags(cmd *cobra.Command) *BreakGlassCredentialArgs {
args := &BreakGlassCredentialArgs{}
cmd.Flags().StringVar(
&args.username,
usernameFlag,
"",
"Username for the break glass credential.",
)
cmd.Flags().DurationVar(
&args.expirationDuration,
expirationFlag,
0,
fmt.Sprintf("Expire the break glass credential after a relative duration like 2h, 8h. "+
"The expiration duration needs to be at least 10 minutes from now and to be at maximum 24 hours."),
)
return args
}
func GetBreakGlassCredentialOptions(cmd *pflag.FlagSet, args *BreakGlassCredentialArgs) (
*BreakGlassCredentialArgs, error) {
var err error
result := &BreakGlassCredentialArgs{}
result.username = args.username
result.expirationDuration = args.expirationDuration
if !IsBreakGlassCredentialSetViaCLI(cmd) {
if !interactive.Enabled() {
return nil, nil
}
}
if interactive.Enabled() && !cmd.Changed(usernameFlag) {
result.username, err = interactive.GetString(interactive.Input{
Question: "Username",
Default: result.username,
Help: cmd.Lookup(usernameFlag).Usage,
})
if err != nil {
return nil, err
}
}
if interactive.Enabled() && !cmd.Changed(expirationFlag) {
inputString, err := interactive.GetString(interactive.Input{
Question: "Expiration duration",
Default: result.expirationDuration,
Help: cmd.Lookup(expirationFlag).Usage,
})
if err != nil {
return nil, err
}
if inputString != "" {
duration, err := time.ParseDuration(inputString)
if err != nil {
return nil, err
}
result.expirationDuration = duration
}
}
return result, nil
}
func CreateBreakGlass(cluster *cmv1.Cluster,
clusterKey string,
args *BreakGlassCredentialArgs, r *rosa.Runtime) (*cmv1.BreakGlassCredential, error) {
breakGlassConfig, err := CreateBreakGlassConfig(args)
if err != nil {
return &cmv1.BreakGlassCredential{}, fmt.Errorf("failed to create a break glass credential for cluster '%s': %s",
clusterKey, err)
}
credential, err := r.OCMClient.CreateBreakGlassCredential(cluster.ID(), breakGlassConfig)
if err != nil {
return &cmv1.BreakGlassCredential{}, fmt.Errorf("failed to create a break glass credential for cluster '%s': %s",
clusterKey, err)
}
return credential, nil
}
func CreateBreakGlassConfig(args *BreakGlassCredentialArgs) (*cmv1.BreakGlassCredential, error) {
breakGlassBuilder := cmv1.NewBreakGlassCredential()
if args != nil {
if args.username != "" {
breakGlassBuilder.Username(args.username)
}
if args.expirationDuration != 0 {
expirationTimeStamp := time.Now().Add(args.expirationDuration).Round(time.Second)
breakGlassBuilder.ExpirationTimestamp(expirationTimeStamp)
}
}
return breakGlassBuilder.Build()
}
func FormatBreakGlassCredentialOutput(breakGlassCredential *cmv1.BreakGlassCredential) (map[string]interface{}, error) {
var b bytes.Buffer
err := cmv1.MarshalBreakGlassCredential(breakGlassCredential, &b)
if err != nil {
return nil, err
}
ret := make(map[string]interface{})
err = json.Unmarshal(b.Bytes(), &ret)
if err != nil {
return nil, err
}
return ret, nil
}
func IsBreakGlassCredentialSetViaCLI(cmd *pflag.FlagSet) bool {
for _, parameter := range []string{usernameFlag, expirationFlag} {
if cmd.Changed(parameter) {
return true
}
}
return false
}