-
Notifications
You must be signed in to change notification settings - Fork 537
/
resource_aws_auth_backend_client.go
163 lines (143 loc) · 5.2 KB
/
resource_aws_auth_backend_client.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
package vault
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
)
func awsAuthBackendClientResource() *schema.Resource {
return &schema.Resource{
Create: awsAuthBackendWrite,
Read: awsAuthBackendRead,
Update: awsAuthBackendWrite,
Delete: awsAuthBackendDelete,
Exists: awsAuthBackendExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"backend": {
Type: schema.TypeString,
Optional: true,
Description: "Unique name of the auth backend to configure.",
ForceNew: true,
Default: "aws",
// standardise on no beginning or trailing slashes
StateFunc: func(v interface{}) string {
return strings.Trim(v.(string), "/")
},
},
"access_key": {
Type: schema.TypeString,
Optional: true,
Description: "AWS Access key with permissions to query AWS APIs.",
Sensitive: true,
},
"secret_key": {
Type: schema.TypeString,
Optional: true,
Description: "AWS Secret key with permissions to query AWS APIs.",
Sensitive: true,
},
"ec2_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: "URL to override the default generated endpoint for making AWS EC2 API calls.",
},
"iam_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: "URL to override the default generated endpoint for making AWS IAM API calls.",
},
"sts_endpoint": {
Type: schema.TypeString,
Optional: true,
Description: "URL to override the default generated endpoint for making AWS STS API calls.",
},
"iam_server_id_header_value": {
Type: schema.TypeString,
Optional: true,
Description: "The value to require in the X-Vault-AWS-IAM-Server-ID header as part of GetCallerIdentity requests that are used in the iam auth method.",
},
},
}
}
func awsAuthBackendWrite(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
// if backend comes from the config, it won't have the StateFunc
// applied yet, so we need to apply it again.
backend := d.Get("backend").(string)
accessKey := d.Get("access_key").(string)
secretKey := d.Get("secret_key").(string)
ec2Endpoint := d.Get("ec2_endpoint").(string)
iamEndpoint := d.Get("iam_endpoint").(string)
stsEndpoint := d.Get("sts_endpoint").(string)
iamServerIDHeaderValue := d.Get("iam_server_id_header_value").(string)
path := awsAuthBackendClientPath(backend)
data := map[string]interface{}{
"access_key": accessKey,
"secret_key": secretKey,
"endpoint": ec2Endpoint,
"iam_endpoint": iamEndpoint,
"sts_endpoint": stsEndpoint,
"iam_server_id_header_value": iamServerIDHeaderValue,
}
log.Printf("[DEBUG] Writing AWS auth backend client config to %q", path)
_, err := client.Logical().Write(path, data)
if err != nil {
return fmt.Errorf("error writing to %q: %s", path, err)
}
log.Printf("[DEBUG] Wrote AWS auth backend client config to %q", path)
d.SetId(path)
return awsAuthBackendRead(d, meta)
}
func awsAuthBackendRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
log.Printf("[DEBUG] Reading AWS auth backend client config")
secret, err := client.Logical().Read(d.Id())
if err != nil {
return fmt.Errorf("Error reading AWS auth backend client config from %q: %s", d.Id(), err)
}
log.Printf("[DEBUG] Read AWS auth backend client config")
if secret == nil {
log.Printf("[WARN] No info found at %q; removing from state.", d.Id())
d.SetId("")
return nil
}
idPieces := strings.Split(d.Id(), "/")
if len(idPieces) != 4 {
return fmt.Errorf("expected %q to have 4 pieces, has %d", d.Id(), len(idPieces))
}
d.Set("backend", idPieces[1])
d.Set("access_key", secret.Data["access_key"])
d.Set("secret_key", secret.Data["secret_key"])
d.Set("ec2_endpoint", secret.Data["endpoint"])
d.Set("iam_endpoint", secret.Data["iam_endpoint"])
d.Set("sts_endpoint", secret.Data["sts_endpoint"])
d.Set("iam_server_id_header_value", secret.Data["iam_server_id_header_value"])
return nil
}
func awsAuthBackendDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
log.Printf("[DEBUG] Deleting AWS auth backend client config from %q", d.Id())
_, err := client.Logical().Delete(d.Id())
if err != nil {
return fmt.Errorf("Error deleting AWS auth backend client config from %q: %s", d.Id(), err)
}
log.Printf("[DEBUG] Deleted AWS auth backend client config from %q", d.Id())
return nil
}
func awsAuthBackendExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*api.Client)
log.Printf("[DEBUG] Checking if AWS auth backend client is configured at %q", d.Id())
secret, err := client.Logical().Read(d.Id())
if err != nil {
return true, fmt.Errorf("Error checking if AWS auth backend client is configured at %q: %s", d.Id(), err)
}
log.Printf("[DEBUG] Checked if AWS auth backend client is configured at %q", d.Id())
return secret != nil, nil
}
func awsAuthBackendClientPath(path string) string {
return "auth/" + strings.Trim(path, "/") + "/config/client"
}