/
iam_teams.go
204 lines (180 loc) · 5.07 KB
/
iam_teams.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
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 createV1TeamTemplate = `
{
"name": "{{ .Name }}",
"description": "{{ .Name }} V1 diagnostics test team"
}
`
const createV2TeamTemplate = `
{
"id": "{{ .ID }}",
"name": "{{ .ID }} V2 diagnostics test team"
}
`
// TeamInfo contains information about a team, including v1 and v2 fields. This
// is returned when creating a team and getting a team
type TeamInfo struct {
IsV2 bool
Team struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Projects []string `json:"projects"`
} `json:"team"`
}
type generatedTeamData struct {
ID string `json:"id"`
Name string `json:"name"`
IsV2 bool `json:"is_v2"`
}
// CreateTeam creates a team with the given identifier
func CreateTeam(tstCtx diagnostics.TestContext, id string) (*TeamInfo, error) {
var err error
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return nil, err
}
teamInfo := TeamInfo{IsV2: isV2}
if isV2 {
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/apis/iam/v2/teams",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createV2TeamTemplate, struct {
ID string
}{
ID: id,
}),
)).WithValue(&teamInfo)
} else {
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/api/v0/auth/teams",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createV1TeamTemplate, struct {
Name string
}{
Name: TimestampName(),
}),
)).WithValue(&teamInfo)
}
if err != nil {
return nil, errors.Wrap(err, "Could not create team")
}
return &teamInfo, nil
}
// GetTeam gets the team with the given id
func GetTeam(tstCtx diagnostics.TestContext, id string) (*TeamInfo, error) {
var err error
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return nil, err
}
var reqPath string
if isV2 {
reqPath = fmt.Sprintf("/apis/iam/v2/teams/%s", id)
} else {
reqPath = fmt.Sprintf("/api/v0/auth/teams/%s", id)
}
teamInfo := TeamInfo{}
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(reqPath)).WithValue(&teamInfo)
if err != nil {
return nil, errors.Wrap(err, "Could not fetch team")
}
return &teamInfo, nil
}
// DeleteTeam deletes the team with the given id
func DeleteTeam(tstCtx diagnostics.TestContext, id string) error {
var err error
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return err
}
var reqPath string
if isV2 {
reqPath = fmt.Sprintf("/apis/iam/v2/teams/%s", id)
} else {
reqPath = fmt.Sprintf("/api/v0/auth/teams/%s", id)
}
if err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
reqPath,
lbrequest.WithMethod("DELETE"),
)).WithValue(&struct{}{}); err != nil {
return errors.Wrap(err, "Could not delete team")
}
return nil
}
// GetTeamID determines which identifier to use when fetching the team, since
// the ID field is different between IAM v1 and v2
func GetTeamID(tstCtx diagnostics.TestContext, team generatedTeamData) (string, error) {
// if the team was saved as a v1 team, it has an auto-generated guid ID and unique name
if !team.IsV2 {
upgradedToV2, err := tstCtx.IsIAMV2()
if err != nil {
return "", err
}
if upgradedToV2 {
// after upgrade to v2, a v1 team's Name becomes its v2 team ID
return team.Name, nil
}
}
// if there was no change in IAM version between the generation and verify steps
// then we can use the same team ID
return team.ID, nil
}
// CreateIAMTeamsDiagnostic creates a diagnostic for iam teams
func CreateIAMTeamsDiagnostic() diagnostics.Diagnostic {
return diagnostics.Diagnostic{
Name: "iam-teams",
Tags: diagnostics.Tags{"iam"},
Generate: func(tstCtx diagnostics.TestContext) error {
teamInfo, err := CreateTeam(tstCtx, fmt.Sprintf("iam-teams-%s", TimestampName()))
if err != nil {
return err
}
tstCtx.SetValue("iam-teams", &generatedTeamData{
ID: teamInfo.Team.ID,
Name: teamInfo.Team.Name,
IsV2: teamInfo.IsV2,
})
return nil
},
Verify: func(tstCtx diagnostics.VerificationTestContext) {
loaded := generatedTeamData{}
err := tstCtx.GetValue("iam-teams", &loaded)
require.NoError(tstCtx, err, "Could not load generated context")
id, err := GetTeamID(tstCtx, loaded)
require.NoError(tstCtx, err, "Could not determine team identifier")
teamInfo, err := GetTeam(tstCtx, id)
require.NoError(tstCtx, err)
assert.Equal(tstCtx, id, teamInfo.Team.ID, "Received unexpected team ID")
},
Cleanup: func(tstCtx diagnostics.TestContext) error {
loaded := generatedTeamData{}
err := tstCtx.GetValue("iam-teams", &loaded)
if err != nil {
return errors.Wrap(err, "Could not load generated context")
}
id, err := GetTeamID(tstCtx, loaded)
if err != nil {
return errors.Wrap(err, "Could not determine team identifier")
}
return DeleteTeam(tstCtx, id)
},
}
}
func init() {
diagnostics.RegisterDiagnostic(
CreateIAMTeamsDiagnostic(),
)
}