/
iam_users.go
214 lines (188 loc) · 5.18 KB
/
iam_users.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
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 createV1UserTemplate = `
{
"username":"{{ .Name }}",
"name":"{{ .Name }} test user",
"password":"{{ .Password }}"
}
`
const createV2UserTemplate = `
{
"id":"{{ .ID }}",
"name":"{{ .ID }} test user",
"password":"{{ .Password }}"
}
`
// UserInfo represents the user parameters, including v1 and v2 fields.
// Password will be provided only when creating, not when fetching.
type UserInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Password string
IsV2 bool
}
// V2UserInfo represents the nested response from the v2 User API.
type V2UserInfo struct {
User UserInfo
}
type generatedUserData struct {
ID string `json:"id"`
Username string `json:"username"`
IsV2 bool `json:"is_v2"`
}
// CreateUser creates a user with the given id
func CreateUser(tstCtx diagnostics.TestContext, id string) (*UserInfo, error) {
var err error
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return nil, err
}
userInfo := UserInfo{IsV2: isV2}
if isV2 {
v2UserInfo := V2UserInfo{User: userInfo}
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/apis/iam/v2/users",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createV2UserTemplate, struct {
ID string
Password string
}{
ID: id,
Password: Password(),
}),
)).WithValue(&v2UserInfo)
userInfo = v2UserInfo.User
} else {
err = MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
"/api/v0/auth/users",
lbrequest.WithMethod("POST"),
lbrequest.WithJSONStringTemplateBody(createV1UserTemplate, struct {
Name string
Password string
}{
Name: TimestampName(),
Password: Password(),
}),
)).WithValue(&userInfo)
}
if err != nil {
return nil, errors.Wrap(err, "Could not create user")
}
return &userInfo, nil
}
// GetUser fetches the given user
func GetUser(tstCtx diagnostics.TestContext, id string) (*UserInfo, error) {
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return nil, err
}
userInfo := UserInfo{}
if isV2 {
v2UserInfo := V2UserInfo{User: userInfo}
err = MustJSONDecodeSuccess(tstCtx.DoLBRequest(
fmt.Sprintf("/apis/iam/v2/users/%s", id),
)).WithValue(&v2UserInfo)
userInfo = v2UserInfo.User
} else {
err = MustJSONDecodeSuccess(tstCtx.DoLBRequest(
fmt.Sprintf("/api/v0/auth/users/%s", id),
)).WithValue(&userInfo)
}
if err != nil {
return nil, errors.Wrap(err, "Could not fetch user")
}
return &userInfo, nil
}
// DeleteUser deletes the given user
func DeleteUser(tstCtx diagnostics.TestContext, username string) error {
isV2, err := tstCtx.IsIAMV2()
if err != nil {
return err
}
var reqPath string
if isV2 {
reqPath = fmt.Sprintf("/apis/iam/v2/users/%s", username)
} else {
reqPath = fmt.Sprintf("/api/v0/auth/users/%s", username)
}
if err := MustJSONDecodeSuccess(
tstCtx.DoLBRequest(
reqPath,
lbrequest.WithMethod("DELETE"),
)).WithValue(&struct{}{}); err != nil {
return errors.Wrap(err, "Could not delete user")
}
return nil
}
// GetUserID determines which identifier to use when fetching the user, since
// the ID field is different between IAM v1 and v2
func GetUserID(user generatedUserData) (string, error) {
if !user.IsV2 {
// if the user was saved as a v1 user, its identifier is its Username
return user.Username, nil
}
return user.ID, nil
}
// CreateIAMUsersDiagnostic creates the diagnostic struct for iam users
func CreateIAMUsersDiagnostic() diagnostics.Diagnostic {
return diagnostics.Diagnostic{
Name: "iam-users",
Tags: diagnostics.Tags{"iam"},
Generate: func(tstCtx diagnostics.TestContext) error {
userInfo, err := CreateUser(tstCtx, fmt.Sprintf("iam-users-%s", TimestampName()))
if err != nil {
return err
}
tstCtx.SetValue("iam-users", &generatedUserData{
ID: userInfo.ID,
Username: userInfo.Username,
IsV2: userInfo.IsV2,
})
return nil
},
Verify: func(tstCtx diagnostics.VerificationTestContext) {
loaded := generatedUserData{}
err := tstCtx.GetValue("iam-users", &loaded)
require.NoError(tstCtx, err, "Could not find generated context")
id, err := GetUserID(loaded)
require.NoError(tstCtx, err, "Could not determine user identifier")
userInfo, err := GetUser(tstCtx, id)
require.NoError(tstCtx, err)
isV2, err := tstCtx.IsIAMV2()
require.NoError(tstCtx, err)
if isV2 {
assert.Equal(tstCtx, id, userInfo.ID)
} else {
assert.Equal(tstCtx, id, userInfo.Username)
}
},
Cleanup: func(tstCtx diagnostics.TestContext) error {
loaded := generatedUserData{}
err := tstCtx.GetValue("iam-users", &loaded)
if err != nil {
return errors.Wrap(err, "Could not find generated context")
}
id, err := GetUserID(loaded)
if err != nil {
return errors.Wrap(err, "Could not determine user identifier")
}
return DeleteUser(tstCtx, id)
},
}
}
func init() {
diagnostics.RegisterDiagnostic(
CreateIAMUsersDiagnostic(),
)
}