|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform/helper/schema" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/service/kms" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceAwsKmsKey() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceAwsKmsKeyCreate, |
| 16 | + Read: resourceAwsKmsKeyRead, |
| 17 | + Update: resourceAwsKmsKeyUpdate, |
| 18 | + Delete: resourceAwsKmsKeyDelete, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "arn": &schema.Schema{ |
| 22 | + Type: schema.TypeString, |
| 23 | + Computed: true, |
| 24 | + }, |
| 25 | + "key_id": &schema.Schema{ |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + }, |
| 29 | + "enabled": &schema.Schema{ |
| 30 | + Type: schema.TypeBool, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "description": &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Computed: true, |
| 37 | + ForceNew: false, |
| 38 | + }, |
| 39 | + "key_usage": &schema.Schema{ |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Computed: true, |
| 43 | + ForceNew: true, |
| 44 | + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { |
| 45 | + value := v.(string) |
| 46 | + if !(value == "ENCRYPT_DECRYPT" || value == "") { |
| 47 | + es = append(es, fmt.Errorf( |
| 48 | + "key_usage must be ENCRYPT_DECRYPT or not specified")) |
| 49 | + } |
| 50 | + return |
| 51 | + }, |
| 52 | + }, |
| 53 | + "policy": &schema.Schema{ |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + Computed: true, |
| 57 | + ForceNew: false, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func resourceAwsKmsKeyCreate(d *schema.ResourceData, meta interface{}) error { |
| 64 | + conn := meta.(*AWSClient).kmsconn |
| 65 | + |
| 66 | + // Allow aws to chose default values if we don't pass them |
| 67 | + var req kms.CreateKeyInput |
| 68 | + if v, exists := d.GetOk("description"); exists { |
| 69 | + req.Description = aws.String(v.(string)) |
| 70 | + } |
| 71 | + if v, exists := d.GetOk("key_usage"); exists { |
| 72 | + req.KeyUsage = aws.String(v.(string)) |
| 73 | + } |
| 74 | + if v, exists := d.GetOk("policy"); exists { |
| 75 | + req.Policy = aws.String(v.(string)) |
| 76 | + } |
| 77 | + |
| 78 | + resp, err := conn.CreateKey(&req) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + return resourceAwsKmsKeyReadResult(d, resp.KeyMetadata) |
| 83 | +} |
| 84 | + |
| 85 | +func resourceAwsKmsKeyRead(d *schema.ResourceData, meta interface{}) error { |
| 86 | + conn := meta.(*AWSClient).kmsconn |
| 87 | + keyId := d.Get("key_id").(string) |
| 88 | + |
| 89 | + req := &kms.DescribeKeyInput{ |
| 90 | + KeyId: aws.String(keyId), |
| 91 | + } |
| 92 | + resp, err := conn.DescribeKey(req) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + return resourceAwsKmsKeyReadResult(d, resp.KeyMetadata) |
| 97 | +} |
| 98 | + |
| 99 | +func resourceAwsKmsKeyReadResult(d *schema.ResourceData, metadata *kms.KeyMetadata) error { |
| 100 | + d.SetId(*metadata.KeyId) |
| 101 | + |
| 102 | + if err := d.Set("arn", metadata.Arn); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + if err := d.Set("key_id", metadata.KeyId); err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + if err := d.Set("enabled", metadata.Enabled); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + if err := d.Set("description", metadata.Description); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + if err := d.Set("key_usage", metadata.KeyUsage); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func resourceAwsKmsKeyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 121 | + conn := meta.(*AWSClient).kmsconn |
| 122 | + |
| 123 | + if d.HasChange("description") { |
| 124 | + if err := resourceAwsKmsKeyDescriptionUpdate(conn, d); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + } |
| 128 | + if d.HasChange("policy") { |
| 129 | + if err := resourceAwsKmsKeyPolicyUpdate(conn, d); err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + } |
| 133 | + return resourceAwsKmsKeyRead(d, meta) |
| 134 | +} |
| 135 | + |
| 136 | +func resourceAwsKmsKeyDescriptionUpdate(conn *kms.KMS, d *schema.ResourceData) error { |
| 137 | + description := d.Get("description").(string) |
| 138 | + keyId := d.Get("key_id").(string) |
| 139 | + |
| 140 | + log.Printf("[DEBUG] KMS key: %s, update description: %s", keyId, description) |
| 141 | + |
| 142 | + req := &kms.UpdateKeyDescriptionInput{ |
| 143 | + Description: aws.String(description), |
| 144 | + KeyId: aws.String(keyId), |
| 145 | + } |
| 146 | + _, err := conn.UpdateKeyDescription(req) |
| 147 | + return err |
| 148 | +} |
| 149 | + |
| 150 | +func resourceAwsKmsKeyPolicyUpdate(conn *kms.KMS, d *schema.ResourceData) error { |
| 151 | + policy := d.Get("policy").(string) |
| 152 | + keyId := d.Get("key_id").(string) |
| 153 | + |
| 154 | + log.Printf("[DEBUG] KMS key: %s, update policy: %s", keyId, policy) |
| 155 | + |
| 156 | + req := &kms.PutKeyPolicyInput{ |
| 157 | + KeyId: aws.String(keyId), |
| 158 | + Policy: aws.String(policy), |
| 159 | + PolicyName: aws.String("default"), |
| 160 | + } |
| 161 | + _, err := conn.PutKeyPolicy(req) |
| 162 | + return err |
| 163 | +} |
| 164 | + |
| 165 | +func resourceAwsKmsKeyDelete(d *schema.ResourceData, meta interface{}) error { |
| 166 | + conn := meta.(*AWSClient).kmsconn |
| 167 | + keyId := d.Get("key_id").(string) |
| 168 | + |
| 169 | + req := &kms.DisableKeyInput{ |
| 170 | + KeyId: aws.String(keyId), |
| 171 | + } |
| 172 | + _, err := conn.DisableKey(req) |
| 173 | + |
| 174 | + log.Printf("[DEBUG] KMS Key: %s deactivated.", keyId) |
| 175 | + d.SetId("") |
| 176 | + return err |
| 177 | +} |
0 commit comments