-
Notifications
You must be signed in to change notification settings - Fork 113
/
iam_v2_policies.go
289 lines (265 loc) · 8.28 KB
/
iam_v2_policies.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
package integration
import (
"fmt"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics/lbrequest"
)
const createV2PolicyTemplateStr = `
{
"id": "{{ .ID }}",
"name": "{{ .ID }} test policy",
"members": [
"token:{{ .TokenID }}",
"user:local:{{ .UserID }}",
"team:local:{{ .TeamID }}"
],
"statements": [
{
"effect": "ALLOW",
"role": "{{ .RoleID }}",
"projects": ["{{ .ProjectID }}"]
}
]
}
`
// V2PolicyInfo represents the nested response from the v2 Policy API.
type V2PolicyInfo struct {
Policy PolicyInfo
}
// PolicyParameters represents policy inputs
type PolicyParameters struct {
TokenID string
TeamID string
UserID string
RoleID string
ProjectID string
RuleID string
}
type generatedV2PolicyData struct {
TokenID string `json:"token_id"`
TokenValue string `json:"token_value"`
PolicyID string `json:"policy_id"`
TeamID string `json:"team_id"`
UserID string `json:"user_id"`
RoleID string `json:"role_id"`
ProjectID string `json:"project_id"`
RuleID string `json:"rule_id"`
Skipped bool `json:"skipped"`
}
// CreateV2Policy creates a policy using the given policy fields
func CreateV2Policy(tstCtx diagnostics.TestContext, pol PolicyParameters) (*PolicyInfo, error) {
policyInfo := PolicyInfo{}
v2PolicyInfo := V2PolicyInfo{Policy: policyInfo}
err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/apis/iam/v2/policies",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createV2PolicyTemplateStr, struct {
ID string
TokenID string
UserID string
TeamID string
RoleID string
ProjectID string
}{
ID: TimestampName(),
TokenID: pol.TokenID,
UserID: pol.UserID,
TeamID: pol.TeamID,
RoleID: pol.RoleID,
ProjectID: pol.ProjectID,
}),
)).WithValue(&v2PolicyInfo)
policyInfo = v2PolicyInfo.Policy
if err != nil {
return nil, errors.Wrap(err, "Could not create policy")
}
return &policyInfo, nil
}
// CreateIAMV2PoliciesDiagnostic creates the diagnostic struct for v2 policies.
func CreateIAMV2PoliciesDiagnostic() diagnostics.Diagnostic {
return diagnostics.Diagnostic{
Name: "iam-policies-v2",
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 := generatedV2PolicyData{}
err = tstCtx.GetValue("iam-policies-v2", &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-policies-v2", &generatedV2PolicyData{
Skipped: !isV2,
})
} else {
// the diagnostic has been run before, so we keep track of its saved values
tstCtx.SetValue("iam-policies-v2", &generatedV2PolicyData{
TokenID: loaded.TokenID,
TokenValue: loaded.TokenValue,
PolicyID: loaded.PolicyID,
TeamID: loaded.TeamID,
UserID: loaded.UserID,
RoleID: loaded.RoleID,
ProjectID: loaded.ProjectID,
RuleID: loaded.RuleID,
Skipped: loaded.Skipped,
})
}
return !isV2, "requires IAM v2", nil
},
Generate: func(tstCtx diagnostics.TestContext) error {
// use a specific ID prefix so there are no conflicts with other tests.
// different resources can have the same ID
id := fmt.Sprintf("iam-v2-%s", TimestampName())
// generate all the components of the policy
tokenInfo, err := CreateToken(tstCtx, id)
if err != nil {
return err
}
teamInfo, err := CreateTeam(tstCtx, id)
if err != nil {
return err
}
userInfo, err := CreateUser(tstCtx, id)
if err != nil {
return err
}
roleInfo, err := CreateRole(tstCtx, id, "system:serviceVersion:get")
if err != nil {
return err
}
projectInfo, err := CreateProjectWithRule(tstCtx, id)
if err != nil {
return err
}
pol := PolicyParameters{
TokenID: tokenInfo.ID,
TeamID: teamInfo.Team.ID,
UserID: userInfo.ID,
RoleID: roleInfo.Role.ID,
ProjectID: projectInfo.Project.ID,
RuleID: projectInfo.Project.Rule.ID,
}
policyInfo, err := CreateV2Policy(tstCtx, pol)
if err != nil {
return err
}
loaded := generatedV2PolicyData{}
err = tstCtx.GetValue("iam-policies-v2", &loaded)
if err != nil {
return errors.Wrap(err, "could not find generated context")
}
tstCtx.SetValue("iam-policies-v2", generatedV2PolicyData{
TokenID: pol.TokenID,
TokenValue: tokenInfo.Value,
PolicyID: policyInfo.ID,
TeamID: pol.TeamID,
UserID: pol.UserID,
RoleID: pol.RoleID,
ProjectID: pol.ProjectID,
RuleID: projectInfo.Project.Rule.ID,
Skipped: loaded.Skipped,
})
return nil
},
Verify: func(tstCtx diagnostics.VerificationTestContext) {
// in case there was an upgrade, check for v1 policy data
v1loaded := generatedV1PolicyData{}
err := tstCtx.GetValue("iam-policies-v1", &v1loaded)
// if generate was run while on v1 and cleanup was skipped,
// we can verify that the v1 policy was migrated to v2
if err == nil {
// the policy should exist as a migrated v2 policy
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/policies/%s", v1loaded.PolicyID),
)).WithValue(&struct{}{})
require.NoError(tstCtx, err, "Expected to be able to read gateway version")
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/api/v0/gateway/version",
lbrequest.WithAuthToken(v1loaded.TokenValue),
)).WithValue(&struct{}{})
require.NoError(tstCtx, err, "Expected to be able to read gateway version")
}
v2loaded := generatedV2PolicyData{}
err = tstCtx.GetValue("iam-policies-v2", &v2loaded)
require.NoError(tstCtx, err, "Could not find generated context")
if v2loaded.Skipped {
// in the v1->v2 force upgrade scenario:
// we only migrate v1 policy data,
// we do not generate v2 policies
return
}
// assert v2 policy is enforced by fetching the gateway version with the member token.
// this endpoint corresponds to the "system:serviceVersion:get" action
// (which is the policy's role's action).
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/api/v0/gateway/version",
lbrequest.WithAuthToken(v2loaded.TokenValue),
)).WithValue(&struct{}{})
require.NoError(tstCtx, err, "Expected to be able to read gateway version")
type Statement struct {
Role string
Resources []string
Projects []string
Effect string
}
resp := struct {
Policy struct {
ID, Type string
Members []string
Statements []Statement
}
}{}
expectedStmts := []Statement{
{
Role: v2loaded.RoleID,
Resources: []string{"*"},
Projects: []string{v2loaded.ProjectID},
Effect: "ALLOW",
},
}
err = MustJSONDecodeSuccess(tstCtx.DoLBRequest("/apis/iam/v2/policies/" + v2loaded.PolicyID)).
WithValue(&resp)
require.NoError(tstCtx, err, "Expected to be able to retrieve stored IAM v2 policy")
require.Equal(tstCtx, v2loaded.PolicyID, resp.Policy.ID)
require.Equal(tstCtx, "CUSTOM", resp.Policy.Type)
require.Contains(tstCtx, resp.Policy.Members, fmt.Sprintf("token:%s", v2loaded.TokenID))
require.ElementsMatch(tstCtx, expectedStmts, resp.Policy.Statements)
},
Cleanup: func(tstCtx diagnostics.TestContext) error {
loaded := generatedV2PolicyData{}
err := tstCtx.GetValue("iam-policies-v2", &loaded)
if loaded.Skipped {
// if diagnostic was run on v1, generating v2 policies was skipped
// so nothing to clean up here
return nil
}
if err != nil {
return errors.Wrap(err, "Could not load generated context")
}
return multierr.Combine(
DeletePolicy(tstCtx, loaded.PolicyID),
DeleteToken(tstCtx, loaded.TokenID),
DeleteTeam(tstCtx, loaded.TeamID),
DeleteUser(tstCtx, loaded.UserID),
DeleteRole(tstCtx, loaded.RoleID),
DeleteProjectWithRule(tstCtx, loaded.ProjectID, loaded.RuleID),
)
},
}
}
func init() {
diagnostics.RegisterDiagnostic(
CreateIAMV2PoliciesDiagnostic(),
)
}