forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_iam_account_password_policy.go
168 lines (148 loc) · 4.93 KB
/
resource_aws_iam_account_password_policy.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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsIamAccountPasswordPolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsIamAccountPasswordPolicyUpdate,
Read: resourceAwsIamAccountPasswordPolicyRead,
Update: resourceAwsIamAccountPasswordPolicyUpdate,
Delete: resourceAwsIamAccountPasswordPolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"allow_users_to_change_password": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"expire_passwords": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"hard_expiry": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"max_password_age": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"minimum_password_length": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 6,
},
"password_reuse_prevention": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"require_lowercase_characters": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"require_numbers": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"require_symbols": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"require_uppercase_characters": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}
func resourceAwsIamAccountPasswordPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
input := &iam.UpdateAccountPasswordPolicyInput{}
if v, ok := d.GetOk("allow_users_to_change_password"); ok {
input.AllowUsersToChangePassword = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("hard_expiry"); ok {
input.HardExpiry = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("max_password_age"); ok {
input.MaxPasswordAge = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("minimum_password_length"); ok {
input.MinimumPasswordLength = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("password_reuse_prevention"); ok {
input.PasswordReusePrevention = aws.Int64(int64(v.(int)))
}
if v, ok := d.GetOk("require_lowercase_characters"); ok {
input.RequireLowercaseCharacters = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("require_numbers"); ok {
input.RequireNumbers = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("require_symbols"); ok {
input.RequireSymbols = aws.Bool(v.(bool))
}
if v, ok := d.GetOk("require_uppercase_characters"); ok {
input.RequireUppercaseCharacters = aws.Bool(v.(bool))
}
log.Printf("[DEBUG] Updating IAM account password policy: %s", input)
_, err := iamconn.UpdateAccountPasswordPolicy(input)
if err != nil {
return fmt.Errorf("Error updating IAM Password Policy: %s", err)
}
log.Println("[DEBUG] IAM account password policy updated")
d.SetId("iam-account-password-policy")
return resourceAwsIamAccountPasswordPolicyRead(d, meta)
}
func resourceAwsIamAccountPasswordPolicyRead(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
input := &iam.GetAccountPasswordPolicyInput{}
resp, err := iamconn.GetAccountPasswordPolicy(input)
if err != nil {
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == "NoSuchEntity" {
log.Printf("[WARN] IAM account password policy is gone (i.e. default)")
d.SetId("")
return nil
}
return fmt.Errorf("Error reading IAM account password policy: %s", err)
}
log.Printf("[DEBUG] Received IAM account password policy: %s", resp)
policy := resp.PasswordPolicy
d.Set("allow_users_to_change_password", policy.AllowUsersToChangePassword)
d.Set("expire_passwords", policy.ExpirePasswords)
d.Set("hard_expiry", policy.HardExpiry)
d.Set("max_password_age", policy.MaxPasswordAge)
d.Set("minimum_password_length", policy.MinimumPasswordLength)
d.Set("password_reuse_prevention", policy.PasswordReusePrevention)
d.Set("require_lowercase_characters", policy.RequireLowercaseCharacters)
d.Set("require_numbers", policy.RequireNumbers)
d.Set("require_symbols", policy.RequireSymbols)
d.Set("require_uppercase_characters", policy.RequireUppercaseCharacters)
return nil
}
func resourceAwsIamAccountPasswordPolicyDelete(d *schema.ResourceData, meta interface{}) error {
iamconn := meta.(*AWSClient).iamconn
log.Println("[DEBUG] Deleting IAM account password policy")
input := &iam.DeleteAccountPasswordPolicyInput{}
if _, err := iamconn.DeleteAccountPasswordPolicy(input); err != nil {
return fmt.Errorf("Error deleting IAM Password Policy: %s", err)
}
d.SetId("")
log.Println("[DEBUG] Deleted IAM account password policy")
return nil
}