2016-10-25 17:33:42 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-10-25 20:04:55 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2016-10-25 17:33:42 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/hashicorp/errwrap"
|
2016-12-06 21:24:21 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/encryption"
|
2016-10-25 17:33:42 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsIamUserLoginProfile() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsIamUserLoginProfileCreate,
|
|
|
|
Read: schema.Noop,
|
|
|
|
Update: schema.Noop,
|
|
|
|
Delete: schema.RemoveFromState,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"user": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"pgp_key": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"password_reset_required": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
|
|
|
"password_length": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 20,
|
|
|
|
ValidateFunc: validateAwsIamLoginProfilePasswordLength,
|
|
|
|
},
|
|
|
|
|
|
|
|
"key_fingerprint": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"encrypted_password": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateAwsIamLoginProfilePasswordLength(v interface{}, _ string) (_ []string, es []error) {
|
|
|
|
length := v.(int)
|
2016-10-25 19:13:15 +02:00
|
|
|
if length < 4 {
|
|
|
|
es = append(es, errors.New("minimum password_length is 4 characters"))
|
2016-10-25 17:33:42 +02:00
|
|
|
}
|
|
|
|
if length > 128 {
|
|
|
|
es = append(es, errors.New("maximum password_length is 128 characters"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 19:13:15 +02:00
|
|
|
// generatePassword generates a random password of a given length using
|
|
|
|
// characters that are likely to satisfy any possible AWS password policy
|
|
|
|
// (given sufficient length).
|
2016-10-25 17:33:42 +02:00
|
|
|
func generatePassword(length int) string {
|
2016-10-25 19:13:15 +02:00
|
|
|
charsets := []string{
|
|
|
|
"abcdefghijklmnopqrstuvwxyz",
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
|
|
|
"012346789",
|
|
|
|
"!@#$%^&*()_+-=[]{}|'",
|
|
|
|
}
|
2016-10-25 17:33:42 +02:00
|
|
|
|
2016-10-25 19:13:15 +02:00
|
|
|
// Use all character sets
|
2016-10-25 20:04:55 +02:00
|
|
|
random := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
|
2016-10-25 19:13:15 +02:00
|
|
|
components := make(map[int]byte, length)
|
2016-10-25 17:33:42 +02:00
|
|
|
for i := 0; i < length; i++ {
|
2016-10-25 19:13:15 +02:00
|
|
|
charset := charsets[i%len(charsets)]
|
|
|
|
components[i] = charset[random.Intn(len(charset))]
|
2016-10-25 17:33:42 +02:00
|
|
|
}
|
2016-10-25 19:13:15 +02:00
|
|
|
|
|
|
|
// Randomise the ordering so we don't end up with a predictable
|
|
|
|
// lower case, upper case, numeric, symbol pattern
|
|
|
|
result := make([]byte, length)
|
|
|
|
i := 0
|
|
|
|
for _, b := range components {
|
|
|
|
result[i] = b
|
|
|
|
i = i + 1
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:33:42 +02:00
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:24:21 +01:00
|
|
|
func resourceAwsIamUserLoginProfileCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
2016-10-25 17:33:42 +02:00
|
|
|
|
2016-12-06 21:24:21 +01:00
|
|
|
encryptionKey, err := encryption.RetrieveGPGKey(d.Get("pgp_key").(string))
|
2016-10-25 17:33:42 +02:00
|
|
|
if err != nil {
|
2016-12-06 21:24:21 +01:00
|
|
|
return err
|
2016-10-25 17:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
username := d.Get("user").(string)
|
|
|
|
passwordResetRequired := d.Get("password_reset_required").(bool)
|
|
|
|
passwordLength := d.Get("password_length").(int)
|
|
|
|
|
2016-12-06 21:24:21 +01:00
|
|
|
_, err = iamconn.GetLoginProfile(&iam.GetLoginProfileInput{
|
2016-10-25 23:14:00 +02:00
|
|
|
UserName: aws.String(username),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "NoSuchEntity" {
|
|
|
|
// If there is already a login profile, bring it under management (to prevent
|
|
|
|
// resource creation diffs) - we will never modify it, but obviously cannot
|
|
|
|
// set the password.
|
|
|
|
d.SetId(username)
|
|
|
|
d.Set("key_fingerprint", "")
|
|
|
|
d.Set("encrypted_password", "")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 17:33:42 +02:00
|
|
|
initialPassword := generatePassword(passwordLength)
|
2016-12-06 21:24:21 +01:00
|
|
|
fingerprint, encrypted, err := encryption.EncryptValue(encryptionKey, initialPassword, "Password")
|
2016-10-25 17:33:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &iam.CreateLoginProfileInput{
|
|
|
|
UserName: aws.String(username),
|
|
|
|
Password: aws.String(initialPassword),
|
|
|
|
PasswordResetRequired: aws.Bool(passwordResetRequired),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("[DEBUG] Create IAM User Login Profile request:", request)
|
|
|
|
createResp, err := iamconn.CreateLoginProfile(request)
|
|
|
|
if err != nil {
|
2016-10-25 20:04:55 +02:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "EntityAlreadyExists" {
|
|
|
|
// If there is already a login profile, bring it under management (to prevent
|
|
|
|
// resource creation diffs) - we will never modify it, but obviously cannot
|
|
|
|
// set the password.
|
2016-10-25 23:14:00 +02:00
|
|
|
d.SetId(username)
|
|
|
|
d.Set("key_fingerprint", "")
|
|
|
|
d.Set("encrypted_password", "")
|
2016-10-25 20:04:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-25 17:33:42 +02:00
|
|
|
return errwrap.Wrapf(fmt.Sprintf("Error creating IAM User Login Profile for %q: {{err}}", username), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*createResp.LoginProfile.UserName)
|
|
|
|
d.Set("key_fingerprint", fingerprint)
|
|
|
|
d.Set("encrypted_password", encrypted)
|
|
|
|
return nil
|
|
|
|
}
|