-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathauth.go
263 lines (247 loc) · 7.29 KB
/
auth.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
package main
import (
"fmt"
"github.com/influxdata/influx-cli/v2/api/extras"
"github.com/influxdata/influx-cli/v2/clients/auth"
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
"github.com/urfave/cli"
)
func newAuthCommand() cli.Command {
return cli.Command{
Name: "auth",
Usage: "Authorization management commands",
Aliases: []string{"authorization"},
Before: middleware.NoArgs,
Subcommands: []cli.Command{
newCreateCommand(),
newDeleteCommand(),
newListCommand(),
newSetActiveCommand(),
newSetInactiveCommand(),
},
}
}
func helpText(perm string) struct{ readHelp, writeHelp string } {
var helpOverrides = map[string]struct{ readHelp, writeHelp string }{
"user": {"perform read actions against organization users", "perform mutative actions against organization users"},
"buckets": {"perform read actions against organization buckets", "perform mutative actions against organization buckets"},
"telegrafs": {"read telegraf configs", "create telegraf configs"},
"orgs": {"read organizations", "create organizations"},
"dbrps": {"read database retention policy mappings", "create database retention policy mappings"},
}
help := helpOverrides[perm]
if help.readHelp == "" {
help.readHelp = fmt.Sprintf("read %s", perm)
}
if help.writeHelp == "" {
help.writeHelp = fmt.Sprintf("create or update %s", perm)
}
help.readHelp = "Grants the permission to " + help.readHelp
help.writeHelp = "Grants the permission to " + help.writeHelp
return help
}
func hidden(perm string) bool {
var hiddenTypes = map[string]struct{}{
"functions": {},
}
_, ok := hiddenTypes[perm]
return ok
}
func newCreateCommand() cli.Command {
var params auth.CreateParams
flags := append(commonFlags(), getOrgFlags(¶ms.OrgParams)...)
flags = append(flags,
&cli.StringFlag{
Name: "user, u",
Usage: "The user name",
Destination: ¶ms.User,
},
&cli.StringFlag{
Name: "description, d",
Usage: "Token description",
Destination: ¶ms.Description,
},
&cli.StringSliceFlag{
Name: "write-bucket",
Usage: "The bucket id",
},
&cli.StringSliceFlag{
Name: "read-bucket",
Usage: "The bucket id",
},
&cli.BoolFlag{
Name: "operator",
Usage: "Grants all permissions in all organizations",
Destination: ¶ms.OperatorPermission,
},
&cli.BoolFlag{
Name: "all-access",
Usage: "Grants all permissions in a single organization",
Destination: ¶ms.AllAccess,
},
)
params.ResourcePermissions = auth.BuildResourcePermissions()
for _, perm := range params.ResourcePermissions {
if perm.Name == string(extras.RESOURCEENUMOSS_INSTANCE) {
// Instance permissions are only set during setup
continue
}
help := helpText(perm.Name)
ossVsCloud := ""
if perm.IsCloud && !perm.IsOss {
ossVsCloud = " (Cloud only)"
}
if !perm.IsCloud && perm.IsOss {
ossVsCloud = " (OSS only)"
}
flags = append(flags,
&cli.BoolFlag{
Name: "read-" + perm.Name,
Usage: help.readHelp + ossVsCloud,
Destination: &perm.Read,
Hidden: hidden(perm.Name),
},
&cli.BoolFlag{
Name: "write-" + perm.Name,
Usage: help.writeHelp + ossVsCloud,
Destination: &perm.Write,
Hidden: hidden(perm.Name),
})
}
return cli.Command{
Name: "create",
Usage: "Create authorization",
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
if err := checkOrgFlags(¶ms.OrgParams); err != nil {
return err
}
params.WriteBucketIds = ctx.StringSlice("write-bucket")
params.ReadBucketIds = ctx.StringSlice("read-bucket")
api := getAPI(ctx)
client := auth.Client{
CLI: getCLI(ctx),
AuthorizationsAPITokensApi: api.AuthorizationsAPITokensApi,
UsersApi: api.UsersApi,
OrganizationsApi: api.OrganizationsApi,
ResourcesApi: api.ResourcesApi,
}
return client.Create(getContext(ctx), ¶ms)
},
}
}
func newDeleteCommand() cli.Command {
return cli.Command{
Name: "delete",
Usage: "Delete authorization",
Flags: append(
commonFlags(),
&cli.StringFlag{
Name: "id, i",
Usage: "The authorization ID (required)",
Required: true,
},
),
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{
CLI: getCLI(ctx),
AuthorizationsAPITokensApi: api.AuthorizationsAPITokensApi,
UsersApi: api.UsersApi,
OrganizationsApi: api.OrganizationsApi,
}
return client.Remove(getContext(ctx), ctx.String("id"))
},
}
}
func newListCommand() cli.Command {
var params auth.ListParams
flags := append(commonFlags(), getOrgFlags(¶ms.OrgParams)...)
flags = append(flags,
&cli.StringFlag{
Name: "id, i",
Usage: "The authorization ID",
Destination: ¶ms.Id,
},
&cli.StringFlag{
Name: "user, u",
Usage: "The user",
Destination: ¶ms.User,
},
&cli.StringFlag{
Name: "user-id",
Usage: "The user ID",
Destination: ¶ms.UserID,
},
)
return cli.Command{
Name: "list",
Usage: "List authorizations",
Aliases: []string{"find", "ls"},
Flags: flags,
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
if err := checkOrgFlags(¶ms.OrgParams); err != nil {
return err
}
api := getAPI(ctx)
client := auth.Client{
CLI: getCLI(ctx),
AuthorizationsAPITokensApi: api.AuthorizationsAPITokensApi,
UsersApi: api.UsersApi,
OrganizationsApi: api.OrganizationsApi,
}
return client.List(getContext(ctx), ¶ms)
},
}
}
func newSetActiveCommand() cli.Command {
return cli.Command{
Name: "active",
Usage: "Active authorization",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id, i",
Usage: "The authorization ID (required)",
Required: true,
},
},
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{
CLI: getCLI(ctx),
AuthorizationsAPITokensApi: api.AuthorizationsAPITokensApi,
UsersApi: api.UsersApi,
OrganizationsApi: api.OrganizationsApi,
}
return client.SetActive(getContext(ctx), ctx.String("id"), true)
},
}
}
func newSetInactiveCommand() cli.Command {
return cli.Command{
Name: "inactive",
Usage: "Inactive authorization",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id, i",
Usage: "The authorization ID (required)",
Required: true,
},
},
Before: middleware.WithBeforeFns(withCli(), withApi(true), middleware.NoArgs),
Action: func(ctx *cli.Context) error {
api := getAPI(ctx)
client := auth.Client{
CLI: getCLI(ctx),
AuthorizationsAPITokensApi: api.AuthorizationsAPITokensApi,
UsersApi: api.UsersApi,
OrganizationsApi: api.OrganizationsApi,
}
return client.SetActive(getContext(ctx), ctx.String("id"), false)
},
}
}