-
Notifications
You must be signed in to change notification settings - Fork 111
/
iam_projects.go
243 lines (213 loc) · 6.5 KB
/
iam_projects.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
package integration
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pkg/errors"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics/lbrequest"
)
const createProjectTemplate = `
{
"id":"{{ .ID }}",
"name":"{{ .ID }} test project",
"skip_policies":true
}
`
const createRuleTemplate = `
{
"id":"{{ .ID }}",
"name":"{{ .ID }} test rule",
"type": "NODE",
"project_id": "{{ .ProjectID }}",
"conditions": [
{
"attribute": "CHEF_SERVER",
"operator": "EQUALS",
"values": ["testing"]
}
]
}
`
// ProjectInfo represents the project parameters
type ProjectInfo struct {
Project struct {
ID string `json:"id"`
Name string `json:"name"`
Rule Rule
}
}
// Rule represents the rule parameters
type Rule struct {
ID string `json:"id"`
Name string `json:"name"`
}
// RuleInfo represents the nested rule response
type RuleInfo struct {
Rule Rule
}
// RulesInfo represents the nested rules response
type RulesInfo struct {
Rules []Rule
}
type generatedProjectData struct {
ID string `json:"id"`
RuleID string `json:"rule_id"`
Skipped bool `json:"skipped"`
}
// CreateProjectWithRule creates a project using the given id with an associated rule
func CreateProjectWithRule(tstCtx diagnostics.TestContext, id string) (*ProjectInfo, error) {
projectInfo := ProjectInfo{}
err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/apis/iam/v2/projects",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createProjectTemplate, struct {
ID string
}{
ID: id,
}),
)).WithValue(&projectInfo)
if err != nil {
return nil, errors.Wrap(err, "Could not create project")
}
ruleResp := RuleInfo{}
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/projects/%s/rules", id),
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createRuleTemplate, struct {
ID string
ProjectID string
}{
ID: fmt.Sprintf("%s-rule-1", id),
ProjectID: projectInfo.Project.ID,
}),
)).WithValue(&ruleResp)
if err != nil {
return nil, errors.Wrap(err, "Could not create rule")
}
projectInfo.Project.Rule = ruleResp.Rule
return &projectInfo, nil
}
// GetProject fetches the given project and its associated rule
func GetProject(tstCtx diagnostics.TestContext, id string) (*ProjectInfo, error) {
projectInfo := ProjectInfo{}
err := MustJSONDecodeSuccess(tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/projects/%s", id),
)).WithValue(&projectInfo)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch project")
}
rulesInfo := RulesInfo{}
err = MustJSONDecodeSuccess(tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/projects/%s/rules", id),
)).WithValue(&rulesInfo)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch rules")
}
// there's only one created rule
projectInfo.Project.Rule = rulesInfo.Rules[0]
return &projectInfo, nil
}
// DeleteProjectWithRule deletes the project with the given id and its associated rule
func DeleteProjectWithRule(tstCtx diagnostics.TestContext, projectID, ruleID string) error {
if err := MustJSONDecodeSuccess(tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/projects/%s/rules/%s", projectID, ruleID),
lbrequest.WithMethod("DELETE"),
)).WithValue(&struct{}{}); err != nil {
return errors.Wrap(err, "Could not delete rule")
}
if err := MustJSONDecodeSuccess(tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/projects/%s", projectID),
lbrequest.WithMethod("DELETE"),
)).WithValue(&struct{}{}); err != nil {
return errors.Wrap(err, "Could not delete project")
}
return nil
}
// CreateIAMProjectsDiagnostic creates the diagnostic struct for iam projects
func CreateIAMProjectsDiagnostic() diagnostics.Diagnostic {
return diagnostics.Diagnostic{
Name: "iam-projects",
Tags: diagnostics.Tags{"iam"},
Skip: func(tstCtx diagnostics.TestContext) (bool, string, error) {
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return false, "", err
}
// the Skip function is run before each step: Generate, Verify, and Cleanup
loaded := generatedProjectData{}
err = tstCtx.GetValue("iam-projects", &loaded)
if err != nil {
// this is the first time running this diagnostic,
// so we save whether or not we're skipping it
tstCtx.SetValue("iam-projects", &generatedProjectData{
Skipped: !isV2,
})
} else {
// the diagnostic has been run before, so we keep track of its saved values
tstCtx.SetValue("iam-projects", &generatedProjectData{
ID: loaded.ID,
RuleID: loaded.RuleID,
Skipped: loaded.Skipped,
})
}
return !isV2, "requires IAM v2", nil
},
Generate: func(tstCtx diagnostics.TestContext) error {
projectInfo, err := CreateProjectWithRule(tstCtx,
fmt.Sprintf("iam-projects-%s", TimestampName()))
if err != nil {
return err
}
loaded := generatedProjectData{}
err = tstCtx.GetValue("iam-projects", &loaded)
if err != nil {
return errors.Wrap(err, "could not find generated context")
}
tstCtx.SetValue("iam-projects", &generatedProjectData{
ID: projectInfo.Project.ID,
RuleID: projectInfo.Project.Rule.ID,
Skipped: loaded.Skipped,
})
return nil
},
Verify: func(tstCtx diagnostics.VerificationTestContext) {
loaded := generatedProjectData{}
err := tstCtx.GetValue("iam-projects", &loaded)
require.NoError(tstCtx, err, "Could not find generated context")
if loaded.Skipped {
// This happens in the v1->v2 force upgrade scenario:
// when we run "Generate diagnostic data" while on v1,
// then force-upgrade to v2,
// then run Verify and Cleanup on that data.
// Since the Generate step was skipped,
// there is nothing to verify on v2.
return
}
projectInfo, err := GetProject(tstCtx, loaded.ID)
require.NoError(tstCtx, err)
assert.Equal(tstCtx, loaded.ID, projectInfo.Project.ID)
assert.Equal(tstCtx, loaded.RuleID, projectInfo.Project.Rule.ID)
},
Cleanup: func(tstCtx diagnostics.TestContext) error {
loaded := generatedProjectData{}
err := tstCtx.GetValue("iam-projects", &loaded)
if loaded.Skipped {
// if diagnostic was run on v1, generating projects was skipped
// so nothing to clean up here
return nil
}
if err != nil {
return errors.Wrap(err, "Could not find generated context")
}
return DeleteProjectWithRule(tstCtx, loaded.ID, loaded.RuleID)
},
}
}
func init() {
diagnostics.RegisterDiagnostic(
CreateIAMProjectsDiagnostic(),
)
}