-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_passwordprofile.go
295 lines (263 loc) · 8.92 KB
/
resource_passwordprofile.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
290
291
292
293
294
295
package centrify
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
logger "github.com/marcozj/golang-sdk/logging"
vault "github.com/marcozj/golang-sdk/platform"
"github.com/marcozj/golang-sdk/restapi"
)
func resourcePasswordProfile() *schema.Resource {
return &schema.Resource{
Create: resourcePasswordProfileCreate,
Read: resourcePasswordProfileRead,
Update: resourcePasswordProfileUpdate,
Delete: resourcePasswordProfileDelete,
Exists: resourcePasswordProfileExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the password profile",
},
"description": {
Type: schema.TypeString,
Required: true,
Description: "Description of password profile",
},
"minimum_password_length": {
Type: schema.TypeInt,
Required: true,
Description: "Minimum password length",
ValidateFunc: validation.IntBetween(4, 128),
},
"maximum_password_length": {
Type: schema.TypeInt,
Required: true,
Description: "Maximum password length",
ValidateFunc: validation.IntBetween(8, 128),
},
"at_least_one_lowercase": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "At least one lower-case alpha character",
},
"at_least_one_uppercase": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "At least one upper-case alpha character",
},
"at_least_one_digit": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "At least one digit",
},
"no_consecutive_repeated_char": {
Type: schema.TypeBool,
Optional: true,
Description: "No consecutive repeated characters",
},
"at_least_one_special_char": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "At least one special character",
},
"maximum_char_occurrence_count": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum character occurrence count",
ValidateFunc: validation.IntBetween(1, 128),
},
"special_charset": {
Type: schema.TypeString,
Required: true,
Description: "Special Characters",
},
"first_character_type": {
Type: schema.TypeString,
Optional: true,
Default: "AnyChar",
Description: "A leading alpha or alphanumeric character",
ValidateFunc: validation.StringInSlice([]string{
"AnyChar",
"AlphaOnly",
"AlphaNumericOnly",
}, false),
},
"last_character_type": {
Type: schema.TypeString,
Optional: true,
Default: "AnyChar",
Description: "A trailing alpha or alphanumeric character",
ValidateFunc: validation.StringInSlice([]string{
"AnyChar",
"AlphaOnly",
"AlphaNumericOnly",
}, false),
},
"minimum_alphabetic_character_count": {
Type: schema.TypeInt,
Optional: true,
Description: "Min number of alpha characters",
ValidateFunc: validation.IntBetween(1, 128),
},
"minimum_non_alphabetic_character_count": {
Type: schema.TypeInt,
Optional: true,
Description: "Min number of non-alpha characters",
ValidateFunc: validation.IntBetween(1, 128),
},
},
}
}
func resourcePasswordProfileExists(d *schema.ResourceData, m interface{}) (bool, error) {
logger.Infof("Checking password profile exist: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
object := vault.NewPasswordProfile(client)
object.ID = d.Id()
err := object.Read()
if err != nil {
if strings.Contains(err.Error(), "not exist") || strings.Contains(err.Error(), "not found") {
return false, nil
}
return false, err
}
logger.Infof("Authentication password exists in tenant: %s", object.ID)
return true, nil
}
func resourcePasswordProfileRead(d *schema.ResourceData, m interface{}) error {
logger.Infof("Reading password profile: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
// Create a password profile object and populate ID attribute
object := vault.NewPasswordProfile(client)
object.ID = d.Id()
err := object.Read()
// If the resource does not exist, inform Terraform. We want to immediately
// return here to prevent further processing.
if err != nil {
d.SetId("")
return fmt.Errorf(" Error reading password profile: %v", err)
}
//logger.Debugf("password profile from tenant: %v", object)
schemamap, err := vault.GenerateSchemaMap(object)
if err != nil {
return err
}
logger.Debugf("Generated Map for resourcePasswordProfileRead(): %+v", schemamap)
for k, v := range schemamap {
d.Set(k, v)
}
logger.Infof("Completed reading password profile: %s", object.Name)
return nil
}
func resourcePasswordProfileDelete(d *schema.ResourceData, m interface{}) error {
logger.Infof("Beginning deletion of password profile: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
object := vault.NewPasswordProfile(client)
object.ID = d.Id()
resp, err := object.Delete()
// If the resource does not exist, inform Terraform. We want to immediately
// return here to prevent further processing.
if err != nil {
return fmt.Errorf(" Error deleting password profile: %v", err)
}
if resp.Success {
d.SetId("")
}
logger.Infof("Deletion of password profile completed: %s", ResourceIDString(d))
return nil
}
func resourcePasswordProfileCreate(d *schema.ResourceData, m interface{}) error {
logger.Infof("Beginning password profile creation: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
// Create a password profile object and populate all attributes
object := vault.NewPasswordProfile(client)
createUpateGetPasswordProfileData(d, object)
resp, err := object.Create()
if err != nil {
return fmt.Errorf(" Error creating password profile: %v", err)
}
id := resp.Result
if id == "" {
return fmt.Errorf(" Password profile ID is not set")
}
d.SetId(id)
// Need to populate ID attribute for subsequence processes
object.ID = id
// Creation completed
logger.Infof("Creation of password profile completed: %s", object.Name)
return resourcePasswordProfileRead(d, m)
}
func resourcePasswordProfileUpdate(d *schema.ResourceData, m interface{}) error {
logger.Infof("Beginning password profile update: %s", ResourceIDString(d))
client := m.(*restapi.RestClient)
object := vault.NewPasswordProfile(client)
object.ID = d.Id()
createUpateGetPasswordProfileData(d, object)
// Deal with normal attribute changes first
if d.HasChanges("name", "description", "minimum_password_length", "maximum_password_length", "at_least_one_lowercase", "at_least_one_uppercase",
"at_least_one_digit", "no_consecutive_repeated_char", "at_least_one_special_char", "maximum_char_occurrence_count", "special_charset",
"first_character_type", "last_character_type", "minimum_alphabetic_character_count", "minimum_non_alphabetic_character_count") {
resp, err := object.Update()
if err != nil || !resp.Success {
return fmt.Errorf(" Error updating password profile attribute: %v", err)
}
logger.Debugf("Updated attributes to: %+v", object)
}
logger.Infof("Updating of password profile completed: %s", object.Name)
return resourcePasswordProfileRead(d, m)
}
func createUpateGetPasswordProfileData(d *schema.ResourceData, object *vault.PasswordProfile) error {
object.Name = d.Get("name").(string)
if v, ok := d.GetOk("description"); ok && d.HasChange("description") {
object.Description = v.(string)
}
if v, ok := d.GetOk("minimum_password_length"); ok {
object.MinimumPasswordLength = v.(int)
}
if v, ok := d.GetOk("maximum_password_length"); ok {
object.MaximumPasswordLength = v.(int)
}
if v, ok := d.GetOk("at_least_one_lowercase"); ok {
object.AtLeastOneLowercase = v.(bool)
}
if v, ok := d.GetOk("at_least_one_uppercase"); ok {
object.AtLeastOneUppercase = v.(bool)
}
if v, ok := d.GetOk("at_least_one_digit"); ok {
object.AtLeastOneDigit = v.(bool)
}
if v, ok := d.GetOk("no_consecutive_repeated_char"); ok {
object.ConsecutiveCharRepeatAllowed = v.(bool)
}
if v, ok := d.GetOk("at_least_one_special_char"); ok {
object.AtLeastOneSpecial = v.(bool)
}
if v, ok := d.GetOk("maximum_char_occurrence_count"); ok {
object.MaximumCharOccurrenceCount = v.(int)
}
if v, ok := d.GetOk("special_charset"); ok {
object.SpecialCharSet = v.(string)
}
if v, ok := d.GetOk("first_character_type"); ok {
object.FirstCharacterType = v.(string)
}
if v, ok := d.GetOk("last_character_type"); ok {
object.LastCharacterType = v.(string)
}
if v, ok := d.GetOk("minimum_alphabetic_character_count"); ok {
object.MinimumAlphabeticCharacterCount = v.(int)
}
if v, ok := d.GetOk("minimum_non_alphabetic_character_count"); ok {
object.MinimumNonAlphabeticCharacterCount = v.(int)
}
return nil
}