2015-09-09 17:58:24 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/kms"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsKmsKey() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsKmsKeyCreate,
|
|
|
|
Read: resourceAwsKmsKeyRead,
|
|
|
|
Update: resourceAwsKmsKeyUpdate,
|
|
|
|
Delete: resourceAwsKmsKeyDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"key_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"key_usage": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if !(value == "ENCRYPT_DECRYPT" || value == "") {
|
|
|
|
es = append(es, fmt.Errorf(
|
2015-11-14 21:28:19 +01:00
|
|
|
"%q must be ENCRYPT_DECRYPT or not specified", k))
|
2015-09-09 17:58:24 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-10-26 15:06:34 +01:00
|
|
|
},
|
2015-11-14 18:47:58 +01:00
|
|
|
"deletion_window_in_days": &schema.Schema{
|
2015-10-26 15:06:34 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(int)
|
2015-11-14 21:27:23 +01:00
|
|
|
if value > 30 || value < 7 {
|
2015-10-26 15:06:34 +01:00
|
|
|
es = append(es, fmt.Errorf(
|
2015-11-14 21:28:19 +01:00
|
|
|
"%q must be between 7 and 30 days inclusive", k))
|
2015-10-26 15:06:34 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
},
|
2015-09-09 17:58:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kmsconn
|
|
|
|
|
|
|
|
// Allow aws to chose default values if we don't pass them
|
|
|
|
var req kms.CreateKeyInput
|
|
|
|
if v, exists := d.GetOk("description"); exists {
|
|
|
|
req.Description = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
if v, exists := d.GetOk("key_usage"); exists {
|
|
|
|
req.KeyUsage = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
if v, exists := d.GetOk("policy"); exists {
|
|
|
|
req.Policy = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := conn.CreateKey(&req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resourceAwsKmsKeyReadResult(d, resp.KeyMetadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kmsconn
|
|
|
|
keyId := d.Get("key_id").(string)
|
|
|
|
|
|
|
|
req := &kms.DescribeKeyInput{
|
2015-10-26 15:06:34 +01:00
|
|
|
KeyId: aws.String(keyId),
|
2015-09-09 17:58:24 +02:00
|
|
|
}
|
|
|
|
resp, err := conn.DescribeKey(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return resourceAwsKmsKeyReadResult(d, resp.KeyMetadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyReadResult(d *schema.ResourceData, metadata *kms.KeyMetadata) error {
|
|
|
|
d.SetId(*metadata.KeyId)
|
|
|
|
|
2015-11-14 18:41:50 +01:00
|
|
|
d.Set("arn", metadata.Arn)
|
|
|
|
d.Set("key_id", metadata.KeyId)
|
|
|
|
d.Set("description", metadata.Description)
|
|
|
|
d.Set("key_usage", metadata.KeyUsage)
|
|
|
|
|
2015-09-09 17:58:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kmsconn
|
|
|
|
|
|
|
|
if d.HasChange("description") {
|
|
|
|
if err := resourceAwsKmsKeyDescriptionUpdate(conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.HasChange("policy") {
|
|
|
|
if err := resourceAwsKmsKeyPolicyUpdate(conn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resourceAwsKmsKeyRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyDescriptionUpdate(conn *kms.KMS, d *schema.ResourceData) error {
|
|
|
|
description := d.Get("description").(string)
|
|
|
|
keyId := d.Get("key_id").(string)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] KMS key: %s, update description: %s", keyId, description)
|
|
|
|
|
|
|
|
req := &kms.UpdateKeyDescriptionInput{
|
|
|
|
Description: aws.String(description),
|
|
|
|
KeyId: aws.String(keyId),
|
|
|
|
}
|
|
|
|
_, err := conn.UpdateKeyDescription(req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyPolicyUpdate(conn *kms.KMS, d *schema.ResourceData) error {
|
|
|
|
policy := d.Get("policy").(string)
|
|
|
|
keyId := d.Get("key_id").(string)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] KMS key: %s, update policy: %s", keyId, policy)
|
|
|
|
|
|
|
|
req := &kms.PutKeyPolicyInput{
|
2015-11-14 21:27:23 +01:00
|
|
|
KeyId: aws.String(keyId),
|
|
|
|
Policy: aws.String(policy),
|
|
|
|
PolicyName: aws.String("default"),
|
2015-09-09 17:58:24 +02:00
|
|
|
}
|
|
|
|
_, err := conn.PutKeyPolicy(req)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsKmsKeyDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).kmsconn
|
|
|
|
keyId := d.Get("key_id").(string)
|
|
|
|
|
2015-10-26 15:06:34 +01:00
|
|
|
req := &kms.ScheduleKeyDeletionInput{
|
2015-11-14 21:27:23 +01:00
|
|
|
KeyId: aws.String(keyId),
|
2015-10-26 15:06:34 +01:00
|
|
|
}
|
2015-11-14 18:47:58 +01:00
|
|
|
if v, exists := d.GetOk("deletion_window_in_days"); exists {
|
2015-10-26 15:06:34 +01:00
|
|
|
req.PendingWindowInDays = aws.Int64(int64(v.(int)))
|
2015-09-09 17:58:24 +02:00
|
|
|
}
|
2015-10-26 15:06:34 +01:00
|
|
|
_, err := conn.ScheduleKeyDeletion(req)
|
2015-11-14 18:41:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-09 17:58:24 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] KMS Key: %s deactivated.", keyId)
|
|
|
|
d.SetId("")
|
2015-11-14 18:41:50 +01:00
|
|
|
return nil
|
2015-09-09 17:58:24 +02:00
|
|
|
}
|