forked from terraform-mars/terraform-provider-credstash
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprovider.go
115 lines (106 loc) · 3.29 KB
/
provider.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
package main
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
awsbase "github.com/hashicorp/aws-sdk-go-base"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/joshuamorris3/terraform-provider-credstash/credstash"
)
var _ terraform.ResourceProvider = provider()
const defaultAWSProfile = "default"
func provider() terraform.ResourceProvider {
return &schema.Provider{
DataSourcesMap: map[string]*schema.Resource{
"credstash_secret": dataSourceSecret(),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"AWS_REGION",
"AWS_DEFAULT_REGION",
}, nil),
Description: "The region where AWS operations will take place. Examples\n" +
"are us-east-1, us-west-2, etc.",
},
"table": {
Type: schema.TypeString,
Optional: true,
Description: "The DynamoDB table where the secrets are stored.",
Default: "credential-store",
},
"profile": {
Type: schema.TypeString,
Optional: true,
Default: defaultAWSProfile,
Description: "The profile that should be used to connect to AWS",
},
"assume_role": assumeRoleSchema(),
"key_id": {
Type: schema.TypeString,
Optional: true,
Description: "the KMS key ID, ARN, alias or alias ARN used to encrypt/decrypt the secret",
Sensitive: true,
},
},
ConfigureFunc: providerConfig,
}
}
func providerConfig(d *schema.ResourceData) (interface{}, error) {
region := d.Get("region").(string)
table := d.Get("table").(string)
profile := d.Get("profile").(string)
keyId := d.Get("key_id").(string)
var sess *session.Session
var err error
if profile != defaultAWSProfile {
log.Printf("[DEBUG] creating a session for profile: %s", profile)
sess, err = session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String(region)},
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
})
} else if l, ok := d.Get("assume_role").([]interface{}); ok && len(l) > 0 && l[0] != nil {
m := l[0].(map[string]interface{})
log.Println("[DEBUG] creating a session with assume role")
cfg := &awsbase.Config{Region: region}
if v, ok := m["duration_seconds"].(int); ok && v != 0 {
cfg.AssumeRoleDurationSeconds = v
}
if v, ok := m["role_arn"].(string); ok && v != "" {
cfg.AssumeRoleARN = v
}
sess, err = awsbase.GetSession(cfg)
} else {
sess, err = session.NewSession(&aws.Config{Region: aws.String(region)})
}
if err != nil {
return nil, err
}
log.Printf("[DEBUG] configured credstash for table %s", table)
return credstash.New(table, keyId, sess), nil
}
func assumeRoleSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"duration_seconds": {
Type: schema.TypeInt,
Optional: true,
Description: "Seconds to restrict the assume role session duration.",
},
"role_arn": {
Type: schema.TypeString,
Optional: true,
Description: "Amazon Resource Name of an IAM Role to assume prior to making API calls.",
},
},
},
}
}