/
iam.go
304 lines (266 loc) · 8.34 KB
/
iam.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"context"
"fmt"
"os"
"regexp"
"strings"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/spf13/cobra"
iam_common "github.com/chef/automate/api/external/iam/v2/common"
iam_req "github.com/chef/automate/api/external/iam/v2/request"
v2_constants "github.com/chef/automate/components/authz-service/constants"
"github.com/chef/automate/components/automate-cli/pkg/adminmgmt"
"github.com/chef/automate/components/automate-cli/pkg/client/apiclient"
"github.com/chef/automate/components/automate-cli/pkg/docs"
"github.com/chef/automate/components/automate-cli/pkg/status"
)
const adminsID = "admins"
var iamCmdFlags = struct {
dryRun bool
adminToken bool
tokenID string
betaVersion bool
skipLegacyUpgrade bool
}{}
func newIAMCommand() *cobra.Command {
return &cobra.Command{
Use: "iam COMMAND",
Short: "Chef Automate iam commands",
PersistentPreRunE: preIAMCmd,
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
}
func newIAMAdminAccessCommand() *cobra.Command {
return &cobra.Command{
Use: "admin-access COMMAND",
Short: "Manage and restore default admin access",
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
}
func newIAMTokensCommand() *cobra.Command {
return &cobra.Command{
Use: "token COMMAND",
Short: "Manage tokens",
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
}
func newIAMCreateTokenCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "create NAME",
Short: "Generate a token",
RunE: runCreateTokenCmd,
Args: cobra.ExactArgs(1),
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
cmd.PersistentFlags().BoolVar(
&iamCmdFlags.adminToken,
"admin",
false,
"Generate a token and add it to the chef-managed admin policy")
cmd.PersistentFlags().StringVar(
&iamCmdFlags.tokenID,
"id",
"",
"Specify a custom ID (if omitted, an ID will be generated based on NAME)")
return cmd
}
func newIAMRestoreDefaultAdminAccessCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "restore PASSWORD",
Short: "Restore the factory default admin user, team, and access",
Long: "Recreate the admin user, admin team, and related admin policy as needed " +
"to restore to factory default and update the admin user's password",
RunE: runRestoreDefaultAdminAccessAdminCmd,
Args: cobra.ExactArgs(1),
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
cmd.PersistentFlags().BoolVar(
&iamCmdFlags.dryRun,
"dry-run",
false,
"Show what would be updated by this command without performing any changes")
return cmd
}
func newIAMVersionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Retrieve IAM version in use",
RunE: runIAMVersionCmd,
Args: cobra.ExactArgs(0),
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
}
}
func init() {
iamCommand := newIAMCommand()
iamCommand.AddCommand(newIAMVersionCmd())
iamAdminAccessCommand := newIAMAdminAccessCommand()
iamCommand.AddCommand(iamAdminAccessCommand)
iamAdminAccessCommand.AddCommand(newIAMRestoreDefaultAdminAccessCmd())
iamTokensCommand := newIAMTokensCommand()
iamCommand.AddCommand(iamTokensCommand)
iamTokensCommand.AddCommand(newIAMCreateTokenCommand())
RootCmd.AddCommand(iamCommand)
}
type vsn struct {
Major iam_common.Version_VersionNumber
Minor iam_common.Version_VersionNumber
}
func display(v *iam_common.Version) string {
x := vsn{Minor: v.Minor, Major: v.Major}
switch x {
case vsn{Major: iam_common.Version_V2, Minor: iam_common.Version_V1}:
return "v2.1"
// this should not happen unless forced at the database level
case vsn{Major: iam_common.Version_V2, Minor: iam_common.Version_V0}:
return "v2.0"
default:
return "v1.0"
}
}
func runIAMVersionCmd(cmd *cobra.Command, args []string) error {
ctx := context.Background()
apiClient, err := apiclient.OpenConnection(ctx)
if err != nil {
return status.Wrap(err, status.APIUnreachableError,
"Failed to create a connection to the API")
}
resp, err := apiClient.PoliciesClient().GetPolicyVersion(ctx, &iam_req.GetPolicyVersionReq{})
if err != nil {
return status.Wrap(err, status.APIError, "Failed to retrieve IAM version")
}
writer.Printf("IAM %s\n", display(resp.Version))
return nil
}
func runRestoreDefaultAdminAccessAdminCmd(cmd *cobra.Command, args []string) error {
if iamCmdFlags.dryRun {
writer.Title("Dry run: showing all actions needed to restore default admin access without performing any changes\n")
} else {
writer.Title("Restoring all factory defaults for admin access\n")
}
newAdminPassword := args[0]
ctx := context.Background()
apiClient, err := apiclient.OpenConnection(ctx)
if err != nil {
return status.Wrap(err, status.APIUnreachableError, "Failed to create a connection to the API")
}
// restore admin user and team if needed
membershipID, adminUserFound, err := adminmgmt.CreateAdminUserOrUpdatePassword(ctx,
apiClient, newAdminPassword, iamCmdFlags.dryRun)
if err != nil {
return err
}
if adminUserFound {
writer.Success("Updated existing admin user's password")
} else {
writer.Success("Created new admin user with specified password")
}
adminsTeamFound, err := adminmgmt.CreateAdminTeamIfMissing(ctx, apiClient, iamCmdFlags.dryRun)
if err != nil {
return err
}
if adminsTeamFound {
writer.Skipped("Found admins team")
} else {
writer.Success("Recreated admins team")
}
// In dry-run mode, we might be missing some IDs that would have been created.
// We'll only hit this condition in dry-run mode.
if iamCmdFlags.dryRun && (membershipID == "" || !adminsTeamFound) {
writer.Success("Added admin user to admins team")
} else { // non-dry-run mode or dry-run mode where user and team already existed.
userAdded, err := adminmgmt.AddAdminUserToTeam(ctx,
apiClient, adminsID, membershipID, iamCmdFlags.dryRun)
if err != nil {
return err
}
if userAdded {
writer.Success("Added admin user to admins team")
} else {
writer.Skipped("Admin user already existed in admins team")
}
}
foundAdminsTeaminV2AdminPolicy, err := adminmgmt.UpdateAdminsPolicyIfNeeded(ctx,
apiClient, iamCmdFlags.dryRun)
if err != nil {
return err
}
if !foundAdminsTeaminV2AdminPolicy {
writer.Success("Added local 'admins' team to Chef-managed 'Administrator' policy")
}
writer.Skipped("Found local 'admins' team in Chef-managed 'Administrator' policy")
if err := apiClient.CloseConnection(); err != nil {
return status.Wrap(err, status.APIUnreachableError, "Failed to close connection to the API")
}
return nil
}
func runCreateTokenCmd(cmd *cobra.Command, args []string) error {
name := args[0]
ctx := context.Background()
apiClient, err := apiclient.OpenConnection(ctx)
if err != nil {
return status.Wrap(err, status.APIUnreachableError,
"Failed to create a connection to the API.")
}
var id string
if iamCmdFlags.tokenID == "" {
re := regexp.MustCompile(`[^a-z0-9]`)
id = strings.ToLower(name)
id = re.ReplaceAllString(id, "-")
} else {
id = iamCmdFlags.tokenID
}
tokenResp, err := apiClient.TokensClient().CreateToken(ctx, &iam_req.CreateTokenReq{
Id: id,
Name: name,
Active: &wrappers.BoolValue{Value: true},
// TODO (tc): Might want to let them specify a --projects list somehow eventually.
Projects: []string{},
})
if err != nil {
return status.Wrap(err, status.APIError, "Failed to generate new token.")
}
if iamCmdFlags.adminToken {
member := fmt.Sprintf("token:%s", tokenResp.Token.Id)
_, err = apiClient.PoliciesClient().AddPolicyMembers(ctx, &iam_req.AddPolicyMembersReq{
Id: v2_constants.AdminPolicyID,
Members: []string{member},
})
if err != nil {
return status.Wrap(err, status.APIError, "Failed to add token as a member of chef-managed Admin policy.")
}
}
status.GlobalResult = struct {
Token string `json:"token"`
}{Token: tokenResp.Token.Value}
writer.Println(tokenResp.Token.Value)
return nil
}
func preIAMCmd(cmd *cobra.Command, args []string) error {
err := commandPrePersistent(cmd)
if err != nil {
return status.Wrap(err, status.CommandExecutionError, "unable to set command parent settings")
}
if isA2HARBFileExist() {
output, err := RunCmdOnSingleAutomateNode(cmd, args)
if err != nil {
return err
}
writer.Print(output)
// NOTE: used os.exit as need to stop next lifecycle method to execute
os.Exit(0)
}
return nil
}