-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
119 lines (100 loc) · 2.77 KB
/
api.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
package integration
import (
"fmt"
"github.com/pkg/errors"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics/lbrequest"
)
const tokenCreateTemplateStr = `
{
"description": "{{ .Description }}",
"active": true
}
`
// TokenInfo holds information about a created token
type TokenInfo struct {
ID string `json:"id"`
Description string `json:"description"`
Active bool `json:"active"`
Token string `json:"value"`
}
// CreateRandomToken creates a token
func CreateRandomToken(tstCtx diagnostics.TestContext) (*TokenInfo, error) {
tokenInfo := TokenInfo{}
err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/api/v0/auth/tokens",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(tokenCreateTemplateStr, struct {
Description string
}{
Description: TimestampName(),
}),
)).WithValue(&tokenInfo)
if err != nil {
return nil, errors.Wrap(err, "Could not create token")
}
return &tokenInfo, nil
}
// DeleteToken deletes the token with the given id
func DeleteToken(tstCtx diagnostics.TestContext, id string) error {
err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
fmt.Sprintf("/api/v0/auth/tokens/%s", id),
lbrequest.WithMethod("DELETE"),
lbrequest.WithJSONStringTemplateBody(tokenCreateTemplateStr, struct {
Description string
}{
Description: TimestampName(),
}),
)).Error()
if err != nil {
return errors.Wrap(err, "Could not delete token")
}
return nil
}
const policyCreateTemplateStr = `
{
"action": "{{ .Action }}",
"subjects": ["token:{{ .TokenID }}"],
"resource": "{{ .Resource }}"
}
`
// PolicyInfo contains info about a created policy
type PolicyInfo struct {
ID string `json:"id"`
}
// CreatePolicyOnToken creates a policy on a given tokenID
func CreatePolicyOnToken(tstCtx diagnostics.TestContext, tokenID string, action string, resource string) (*PolicyInfo, error) {
policyInfo := PolicyInfo{}
err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/api/v0/auth/policies",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(policyCreateTemplateStr, struct {
Action string
TokenID string
Resource string
}{
Action: action,
TokenID: tokenID,
Resource: resource,
}),
)).WithValue(&policyInfo)
if err != nil {
return nil, errors.Wrap(err, "Could not create policy")
}
return &policyInfo, nil
}
// DeletePolicy deletes a policy with the given id
func DeletePolicy(tstCtx diagnostics.TestContext, id string) error {
err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
fmt.Sprintf("/api/v0/auth/policies/%s", id),
lbrequest.WithMethod("DELETE"),
)).Error()
if err != nil {
return errors.Wrap(err, "Could not create policy")
}
return nil
}