forked from hashicorp/terraform-provider-vault
/
data_source_aws_access_credentials.go
188 lines (167 loc) · 5.47 KB
/
data_source_aws_access_credentials.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package vault
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/sts"
cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
)
func awsAccessCredentialsDataSource() *schema.Resource {
return &schema.Resource{
Read: awsAccessCredentialsDataSourceRead,
Schema: map[string]*schema.Schema{
"backend": {
Type: schema.TypeString,
Required: true,
Description: "AWS Secret Backend to read credentials from.",
},
"role": {
Type: schema.TypeString,
Required: true,
Description: "AWS Secret Role to read credentials from.",
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "creds",
Description: "Type of credentials to read. Must be either 'creds' for Access Key and Secret Key, or 'sts' for STS.",
ValidateFunc: func(v interface{}, k string) (ws []string, errs []error) {
value := v.(string)
if value != "sts" && value != "creds" {
errs = append(errs, fmt.Errorf("type must be creds or sts"))
}
return
},
},
"access_key": {
Type: schema.TypeString,
Computed: true,
Description: "AWS access key ID read from Vault.",
},
"secret_key": {
Type: schema.TypeString,
Computed: true,
Description: "AWS secret key read from Vault.",
},
"security_token": {
Type: schema.TypeString,
Computed: true,
Description: "AWS security token read from Vault. (Only returned if type is 'sts'.)",
},
"lease_id": {
Type: schema.TypeString,
Computed: true,
Description: "Lease identifier assigned by vault.",
},
"lease_duration": {
Type: schema.TypeInt,
Computed: true,
Description: "Lease duration in seconds relative to the time in lease_start_time.",
},
"lease_start_time": {
Type: schema.TypeString,
Computed: true,
Description: "Time at which the lease was read, using the clock of the system where Terraform was running",
},
"lease_renewable": {
Type: schema.TypeBool,
Computed: true,
Description: "True if the duration of this lease can be extended through renewal.",
},
},
}
}
func awsAccessCredentialsDataSourceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
backend := d.Get("backend").(string)
credType := d.Get("type").(string)
role := d.Get("role").(string)
path := backend + "/" + credType + "/" + role
log.Printf("[DEBUG] Reading %q from Vault", path)
secret, err := client.Logical().Read(path)
if err != nil {
return fmt.Errorf("error reading from Vault: %s", err)
}
log.Printf("[DEBUG] Read %q from Vault", path)
if secret == nil {
return fmt.Errorf("No role found at %q; are you sure you're using the right backend and role?", path)
}
accessKey := secret.Data["access_key"].(string)
secretKey := secret.Data["secret_key"].(string)
var securityToken string
if secret.Data["security_token"] != nil {
securityToken = secret.Data["security_token"].(string)
}
d.SetId(secret.LeaseID)
d.Set("access_key", secret.Data["access_key"])
d.Set("secret_key", secret.Data["secret_key"])
d.Set("security_token", secret.Data["security_token"])
d.Set("lease_id", secret.LeaseID)
d.Set("lease_duration", secret.LeaseDuration)
d.Set("lease_start_time", time.Now().Format(time.RFC3339))
d.Set("lease_renewable", secret.Renewable)
awsConfig := &aws.Config{
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, securityToken),
HTTPClient: cleanhttp.DefaultClient(),
}
sess, err := session.NewSession(awsConfig)
if err != nil {
return fmt.Errorf("Error creating AWS session: %s", err)
}
iamconn := iam.New(sess)
stsconn := sts.New(sess)
for successes := 0; successes < 3; successes++ {
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
if credType == "creds" {
log.Printf("[DEBUG] Checking if AWS creds %q are valid", secret.LeaseID)
_, err := iamconn.GetUser(nil)
if err != nil && isAWSAuthError(err) {
log.Printf("[DEBUG] AWS auth error checking if creds %q are valid, is retryable", secret.LeaseID)
return resource.RetryableError(err)
} else if err != nil {
log.Printf("[DEBUG] Error checking if creds %q are valid: %s", secret.LeaseID, err)
return resource.NonRetryableError(err)
}
log.Printf("[DEBUG] Checked if AWS creds %q are valid", secret.LeaseID)
} else {
log.Printf("[DEBUG] Checking if AWS sts token %q is valid", secret.LeaseID)
_, err := stsconn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil && isAWSAuthError(err) {
return resource.RetryableError(err)
} else if err != nil {
return resource.NonRetryableError(err)
}
log.Printf("[DEBUG] Checked if AWS sts token %q is valid", secret.LeaseID)
}
return nil
})
if err != nil {
return fmt.Errorf("Error checking if credentials are valid: %s", err)
}
}
return nil
}
func isAWSAuthError(err error) bool {
awsErr, ok := err.(awserr.Error)
if !ok {
return false
}
switch awsErr.Code() {
case "AccessDenied":
return true
case "ValidationError":
return true
case "InvalidClientTokenId":
return true
default:
return false
}
}