2015-02-06 16:34:24 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-17 13:03:03 +01:00
|
|
|
"regexp"
|
2015-02-06 16:34:24 +01:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
2015-02-06 16:34:24 +01:00
|
|
|
|
2016-11-17 13:03:03 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-02-06 16:34:24 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsIamPolicy() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsIamPolicyCreate,
|
|
|
|
Read: resourceAwsIamPolicyRead,
|
|
|
|
Update: resourceAwsIamPolicyUpdate,
|
|
|
|
Delete: resourceAwsIamPolicyDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "/",
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"name": &schema.Schema{
|
2016-11-17 13:03:03 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ConflictsWith: []string{"name_prefix"},
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8329-L8334
|
|
|
|
value := v.(string)
|
|
|
|
if len(value) > 128 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be longer than 128 characters", k))
|
|
|
|
}
|
|
|
|
if !regexp.MustCompile("^[\\w+=,.@-]*$").MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q must match [\\w+=,.@-]", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"name_prefix": {
|
2015-02-06 16:34:24 +01:00
|
|
|
Type: schema.TypeString,
|
2016-11-17 13:03:03 +01:00
|
|
|
Optional: true,
|
2015-02-06 16:34:24 +01:00
|
|
|
ForceNew: true,
|
2016-11-17 13:03:03 +01:00
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
// https://github.com/boto/botocore/blob/2485f5c/botocore/data/iam/2010-05-08/service-2.json#L8329-L8334
|
|
|
|
value := v.(string)
|
|
|
|
if len(value) > 96 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot be longer than 96 characters, name is limited to 128", k))
|
|
|
|
}
|
|
|
|
if !regexp.MustCompile("^[\\w+=,.@-]*$").MatchString(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q must match [\\w+=,.@-]", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-02-06 16:34:24 +01:00
|
|
|
},
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamPolicyCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
2016-11-17 13:03:03 +01:00
|
|
|
|
|
|
|
var name string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
name = v.(string)
|
|
|
|
} else if v, ok := d.GetOk("name_prefix"); ok {
|
|
|
|
name = resource.PrefixedUniqueId(v.(string))
|
|
|
|
} else {
|
|
|
|
name = resource.UniqueId()
|
|
|
|
}
|
2015-02-06 16:34:24 +01:00
|
|
|
|
|
|
|
request := &iam.CreatePolicyInput{
|
|
|
|
Description: aws.String(d.Get("description").(string)),
|
|
|
|
Path: aws.String(d.Get("path").(string)),
|
|
|
|
PolicyDocument: aws.String(d.Get("policy").(string)),
|
|
|
|
PolicyName: aws.String(name),
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := iamconn.CreatePolicy(request)
|
|
|
|
if err != nil {
|
2015-06-08 03:56:41 +02:00
|
|
|
return fmt.Errorf("Error creating IAM policy %s: %s", name, err)
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return readIamPolicy(d, response.Policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamPolicyRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
|
|
|
|
request := &iam.GetPolicyInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PolicyArn: aws.String(d.Id()),
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := iamconn.GetPolicy(request)
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
|
2015-02-06 16:34:24 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-08 03:56:41 +02:00
|
|
|
return fmt.Errorf("Error reading IAM policy %s: %s", d.Id(), err)
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return readIamPolicy(d, response.Policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamPolicyUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
|
|
|
|
if err := iamPolicyPruneVersions(d.Id(), iamconn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !d.HasChange("policy") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
request := &iam.CreatePolicyVersionInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PolicyArn: aws.String(d.Id()),
|
2015-02-06 16:34:24 +01:00
|
|
|
PolicyDocument: aws.String(d.Get("policy").(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
SetAsDefault: aws.Bool(true),
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := iamconn.CreatePolicyVersion(request); err != nil {
|
2015-06-08 03:56:41 +02:00
|
|
|
return fmt.Errorf("Error updating IAM policy %s: %s", d.Id(), err)
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsIamPolicyDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
iamconn := meta.(*AWSClient).iamconn
|
|
|
|
|
|
|
|
if err := iamPolicyDeleteNondefaultVersions(d.Id(), iamconn); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &iam.DeletePolicyInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PolicyArn: aws.String(d.Id()),
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := iamconn.DeletePolicy(request)
|
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if iamerr, ok := err.(awserr.Error); ok && iamerr.Code() == "NoSuchEntity" {
|
2015-02-06 16:34:24 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Error reading IAM policy %s: %#v", d.Id(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// iamPolicyPruneVersions deletes the oldest versions.
|
|
|
|
//
|
|
|
|
// Old versions are deleted until there are 4 or less remaining, which means at
|
|
|
|
// least one more can be created before hitting the maximum of 5.
|
|
|
|
//
|
|
|
|
// The default version is never deleted.
|
|
|
|
|
|
|
|
func iamPolicyPruneVersions(arn string, iamconn *iam.IAM) error {
|
|
|
|
versions, err := iamPolicyListVersions(arn, iamconn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(versions) < 5 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldestVersion *iam.PolicyVersion
|
|
|
|
|
|
|
|
for _, version := range versions {
|
|
|
|
if *version.IsDefaultVersion {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if oldestVersion == nil ||
|
|
|
|
version.CreateDate.Before(*oldestVersion.CreateDate) {
|
|
|
|
oldestVersion = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
if err := iamPolicyDeleteVersion(arn, *oldestVersion.VersionId, iamconn); err != nil {
|
2015-02-06 16:34:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func iamPolicyDeleteNondefaultVersions(arn string, iamconn *iam.IAM) error {
|
|
|
|
versions, err := iamPolicyListVersions(arn, iamconn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, version := range versions {
|
|
|
|
if *version.IsDefaultVersion {
|
|
|
|
continue
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if err := iamPolicyDeleteVersion(arn, *version.VersionId, iamconn); err != nil {
|
2015-02-06 16:34:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func iamPolicyDeleteVersion(arn, versionID string, iamconn *iam.IAM) error {
|
|
|
|
request := &iam.DeletePolicyVersionInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PolicyArn: aws.String(arn),
|
|
|
|
VersionId: aws.String(versionID),
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := iamconn.DeletePolicyVersion(request)
|
|
|
|
if err != nil {
|
2015-06-08 03:56:41 +02:00
|
|
|
return fmt.Errorf("Error deleting version %s from IAM policy %s: %s", versionID, arn, err)
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func iamPolicyListVersions(arn string, iamconn *iam.IAM) ([]*iam.PolicyVersion, error) {
|
|
|
|
request := &iam.ListPolicyVersionsInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
PolicyArn: aws.String(arn),
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := iamconn.ListPolicyVersions(request)
|
|
|
|
if err != nil {
|
2015-06-08 03:56:41 +02:00
|
|
|
return nil, fmt.Errorf("Error listing versions for IAM policy %s: %s", arn, err)
|
2015-02-06 16:34:24 +01:00
|
|
|
}
|
|
|
|
return response.Versions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readIamPolicy(d *schema.ResourceData, policy *iam.Policy) error {
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*policy.Arn)
|
2015-02-06 16:34:24 +01:00
|
|
|
if policy.Description != nil {
|
|
|
|
// the description isn't present in the response to CreatePolicy.
|
|
|
|
if err := d.Set("description", *policy.Description); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := d.Set("path", *policy.Path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := d.Set("name", *policy.PolicyName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
if err := d.Set("arn", *policy.Arn); err != nil {
|
2015-02-06 16:34:24 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: set policy
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|