-
Notifications
You must be signed in to change notification settings - Fork 3
/
approval_requests_generated.go
363 lines (338 loc) · 10.4 KB
/
approval_requests_generated.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Copyright (c) Fortanix, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package sdkms
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/pkg/errors"
)
type ApprovalRequest struct {
AcctID UUID `json:"acct_id"`
Approvers []ReviewerPrincipal `json:"approvers"`
Body interface{} `json:"body,omitempty"`
CreatedAt Time `json:"created_at"`
DenialReason *string `json:"denial_reason,omitempty"`
Denier *ReviewerPrincipal `json:"denier,omitempty"`
Description *string `json:"description,omitempty"`
Expiry Time `json:"expiry"`
Method string `json:"method"`
Operation string `json:"operation"`
RequestID UUID `json:"request_id"`
Requester Principal `json:"requester"`
ResultViewed bool `json:"result_viewed"`
Reviewers *[]Reviewer `json:"reviewers,omitempty"`
Status ApprovalStatus `json:"status"`
Subjects *[]ApprovalSubject `json:"subjects,omitempty"`
}
type ApprovalRequestRequest struct {
Body interface{} `json:"body,omitempty"`
Description *string `json:"description,omitempty"`
Method *string `json:"method,omitempty"`
Operation *string `json:"operation,omitempty"`
}
// Approval request status.
type ApprovalStatus string
// List of supported ApprovalStatus values
const (
ApprovalStatusPending ApprovalStatus = "PENDING"
ApprovalStatusApproved ApprovalStatus = "APPROVED"
ApprovalStatusDenied ApprovalStatus = "DENIED"
ApprovalStatusFailed ApprovalStatus = "FAILED"
)
// Identifies an object acted upon by an approval request.
type ApprovalSubject struct {
Group *UUID
Sobject *UUID
App *UUID
Plugin *UUID
Account *UUID
NewAccount *struct{}
Role *UUID
}
func (x ApprovalSubject) MarshalJSON() ([]byte, error) {
if err := checkEnumPointers(
"ApprovalSubject",
[]bool{x.Group != nil,
x.Sobject != nil,
x.App != nil,
x.Plugin != nil,
x.Account != nil,
x.NewAccount != nil,
x.Role != nil}); err != nil {
return nil, err
}
switch {
case x.NewAccount != nil:
return []byte(`"newaccount"`), nil
}
var obj struct {
Group *UUID `json:"group,omitempty"`
Sobject *UUID `json:"sobject,omitempty"`
App *UUID `json:"app,omitempty"`
Plugin *UUID `json:"plugin,omitempty"`
Account *UUID `json:"account,omitempty"`
Role *UUID `json:"role,omitempty"`
}
obj.Group = x.Group
obj.Sobject = x.Sobject
obj.App = x.App
obj.Plugin = x.Plugin
obj.Account = x.Account
obj.Role = x.Role
return json.Marshal(obj)
}
func (x *ApprovalSubject) UnmarshalJSON(data []byte) error {
x.Group = nil
x.Sobject = nil
x.App = nil
x.Plugin = nil
x.Account = nil
x.NewAccount = nil
x.Role = nil
var str string
if err := json.Unmarshal(data, &str); err == nil {
switch str {
case "newaccount":
x.NewAccount = &struct{}{}
default:
return errors.Errorf("invalid value for ApprovalSubject: %v", str)
}
return nil
}
var obj struct {
Group *UUID `json:"group,omitempty"`
Sobject *UUID `json:"sobject,omitempty"`
App *UUID `json:"app,omitempty"`
Plugin *UUID `json:"plugin,omitempty"`
Account *UUID `json:"account,omitempty"`
Role *UUID `json:"role,omitempty"`
}
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
x.Group = obj.Group
x.Sobject = obj.Sobject
x.App = obj.App
x.Plugin = obj.Plugin
x.Account = obj.Account
x.Role = obj.Role
return nil
}
type ApproveRequest struct {
// Password is required if the approval policy requires password authentication.
Password *string `json:"password,omitempty"`
// U2F assertion is required if the approval policy requires two factor authentication.
U2f *U2fAuthRequest `json:"u2f,omitempty"`
// FIDO2 assertion is required if the approval policy requires two factor authentication.
Fido2AuthRequest *PublicKeyCredentialAuthenticatorAssertionResponse `json:"fido2_auth_request,omitempty"`
// Data associated with the approval
Body interface{} `json:"body,omitempty"`
}
type DenyRequest struct {
Reason *string `json:"reason,omitempty"`
}
type ListApprovalRequestsParams struct {
Requester *UUID `json:"requester,omitempty"`
Reviewer *UUID `json:"reviewer,omitempty"`
Subject *UUID `json:"subject,omitempty"`
Status *ApprovalStatus `json:"status,omitempty"`
}
func (x ListApprovalRequestsParams) urlEncode(v map[string][]string) error {
if x.Requester != nil {
v["requester"] = []string{fmt.Sprintf("%v", *x.Requester)}
}
if x.Reviewer != nil {
v["reviewer"] = []string{fmt.Sprintf("%v", *x.Reviewer)}
}
if x.Subject != nil {
v["subject"] = []string{fmt.Sprintf("%v", *x.Subject)}
}
if x.Status != nil {
v["status"] = []string{fmt.Sprintf("%v", *x.Status)}
}
return nil
}
// Reviewer of an approval request.
type Reviewer struct {
Entity ReviewerPrincipal `json:"entity"`
RequiresPassword *bool `json:"requires_password,omitempty"`
Requires2fa *bool `json:"requires_2fa,omitempty"`
}
func (x Reviewer) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{})
{ // x.Entity is flattened
b, err := json.Marshal(&x.Entity)
if err != nil {
return nil, err
}
f := make(map[string]interface{})
if err := json.Unmarshal(b, &f); err != nil {
return nil, err
}
for k, v := range f {
m[k] = v
}
}
if x.RequiresPassword != nil {
m["requires_password"] = x.RequiresPassword
}
if x.Requires2fa != nil {
m["requires_2fa"] = x.Requires2fa
}
return json.Marshal(&m)
}
func (x *Reviewer) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &x.Entity); err != nil {
return err
}
var r struct {
RequiresPassword *bool `json:"requires_password,omitempty"`
Requires2fa *bool `json:"requires_2fa,omitempty"`
}
if err := json.Unmarshal(data, &r); err != nil {
return err
}
x.RequiresPassword = r.RequiresPassword
x.Requires2fa = r.Requires2fa
return nil
}
// A Principal who can approve or deny an approval request.
type ReviewerPrincipal struct {
App *UUID
User *UUID
}
func (x ReviewerPrincipal) MarshalJSON() ([]byte, error) {
if err := checkEnumPointers(
"ReviewerPrincipal",
[]bool{x.App != nil,
x.User != nil}); err != nil {
return nil, err
}
var obj struct {
App *UUID `json:"app,omitempty"`
User *UUID `json:"user,omitempty"`
}
obj.App = x.App
obj.User = x.User
return json.Marshal(obj)
}
func (x *ReviewerPrincipal) UnmarshalJSON(data []byte) error {
x.App = nil
x.User = nil
var obj struct {
App *UUID `json:"app,omitempty"`
User *UUID `json:"user,omitempty"`
}
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
x.App = obj.App
x.User = obj.User
return nil
}
// Approve an approval request.
//
// If the quorum policy was configured to require extra things
// like 2FA, then, relevant info needs to be added to the request.
func (c *Client) ApproveRequest(ctx context.Context, id string, body ApproveRequest) (*ApprovalRequest, error) {
u := "/sys/v1/approval_requests/:id/approve"
u = strings.NewReplacer(":id", id).Replace(u)
var r ApprovalRequest
if err := c.fetch(ctx, http.MethodPost, u, &body, &r); err != nil {
return nil, err
}
return &r, nil
}
// Create a new approval request.
func (c *Client) CreateApprovalRequest(ctx context.Context, body ApprovalRequestRequest) (*ApprovalRequest, error) {
u := "/sys/v1/approval_requests"
var r ApprovalRequest
if err := c.fetch(ctx, http.MethodPost, u, &body, &r); err != nil {
return nil, err
}
return &r, nil
}
// Delete an approval request.
func (c *Client) DeleteApprovalRequest(ctx context.Context, id string) error {
u := "/sys/v1/approval_requests/:id"
u = strings.NewReplacer(":id", id).Replace(u)
if err := c.fetch(ctx, http.MethodDelete, u, nil, nil); err != nil {
return err
}
return nil
}
// Deny an approval request.
func (c *Client) DenyRequest(ctx context.Context, id string, body DenyRequest) (*ApprovalRequest, error) {
u := "/sys/v1/approval_requests/:id/deny"
u = strings.NewReplacer(":id", id).Replace(u)
var r ApprovalRequest
if err := c.fetch(ctx, http.MethodPost, u, &body, &r); err != nil {
return nil, err
}
return &r, nil
}
// Lookup a spcific approval request.
func (c *Client) GetApprovalRequest(ctx context.Context, id string) (*ApprovalRequest, error) {
u := "/sys/v1/approval_requests/:id"
u = strings.NewReplacer(":id", id).Replace(u)
var r ApprovalRequest
if err := c.fetch(ctx, http.MethodGet, u, nil, &r); err != nil {
return nil, err
}
return &r, nil
}
// Get the result for an approved or failed request.
func (c *Client) GetApprovalRequestResult(ctx context.Context, id string) (*ApprovableResult, error) {
u := "/sys/v1/approval_requests/:id/result"
u = strings.NewReplacer(":id", id).Replace(u)
var r ApprovableResult
if err := c.fetch(ctx, http.MethodPost, u, nil, &r); err != nil {
return nil, err
}
return &r, nil
}
// Get all approval requests.
func (c *Client) ListApprovalRequests(ctx context.Context, queryParameters *ListApprovalRequestsParams) ([]ApprovalRequest, error) {
u := "/sys/v1/approval_requests"
if queryParameters != nil {
q, err := encodeURLParams(queryParameters)
if err != nil {
return nil, err
}
u = fmt.Sprintf("%v?%v", u, q)
}
var r []ApprovalRequest
if err := c.fetch(ctx, http.MethodGet, u, nil, &r); err != nil {
return nil, err
}
return r, nil
}
// Creates a challenge for the FIDO2/U2F device to sign.
//
// If the quorum policy is configured to require 2FA, then a call to this API
// produces a challenge that needs to be signed by the respective FIDO2/U2F device.
// The signed data that U2F device provides can be then used with
// `POST /sys/v1/approval_requests/:id/approve` to successfully approve the
// request.
func (c *Client) MfaChallenge(ctx context.Context, id string, queryParameters *MfaChallengeParams) (*MfaChallengeResponse, error) {
u := "/sys/v1/approval_requests/:id/challenge"
u = strings.NewReplacer(":id", id).Replace(u)
if queryParameters != nil {
q, err := encodeURLParams(queryParameters)
if err != nil {
return nil, err
}
u = fmt.Sprintf("%v?%v", u, q)
}
var r MfaChallengeResponse
if err := c.fetch(ctx, http.MethodPost, u, nil, &r); err != nil {
return nil, err
}
return &r, nil
}