forked from daryl-d/terraform-provider-cassandra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
204 lines (165 loc) · 4.86 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log"
"time"
"github.com/gocql/gocql"
"github.com/hashicorp/terraform/helper/schema"
)
var (
allowedTlsProtocols = map[string]uint16{
"SSL3.0": tls.VersionSSL30,
"TLS1.0": tls.VersionTLS10,
"TLS1.1": tls.VersionTLS11,
"TLS1.2": tls.VersionTLS12,
}
)
func Provider() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"cassandra_keyspace": resourceCassandraKeyspace(),
"cassandra_role": resourceCassandraRole(),
"cassandra_grant": resourceCassandraGrant(),
},
ConfigureFunc: configureProvider,
Schema: map[string]*schema.Schema{
"username": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Cassandra Username",
Sensitive: true,
},
"password": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Cassandra Password",
Sensitive: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 9042,
Description: "Cassandra CQL Port",
ValidateFunc: func(i interface{}, s string) (ws []string, errors []error) {
port := i.(int)
if port <= 0 || port >= 65535 {
errors = append(errors, fmt.Errorf("%d: invalid value - must be between 1 and 65535", port))
}
return
},
},
"hosts": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
MinItems: 1,
Required: true,
},
"connection_timeout": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1000,
Description: "Connection timeout in milliseconds",
},
"root_ca": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Use root CA to connect to Cluster. Applies only when useSSL is enabled",
ValidateFunc: func(i interface{}, s string) (ws []string, errors []error) {
rootCA := i.(string)
if rootCA == "" {
return
}
caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM([]byte(rootCA))
if !ok {
errors = append(errors, fmt.Errorf("%s: invalid PEM", rootCA))
}
return
},
},
"use_ssl": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Use SSL when connecting to cluster",
},
"min_tls_version": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "TLS1.2",
Description: "Minimum TLS Version used to connect to the cluster - allowed values are SSL3.0, TLS1.0, TLS1.1, TLS1.2. Applies only when useSSL is enabled",
ValidateFunc: func(i interface{}, s string) (ws []string, errors []error) {
minTlsVersion := i.(string)
if allowedTlsProtocols[minTlsVersion] == 0 {
errors = append(errors, fmt.Errorf("%s: invalid value - must be one of SSL3.0, TLS1.0, TLS1.1, TLS1.2", minTlsVersion))
}
return
},
},
"protocol_version": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 4,
Description: "CQL Binary Protocol Version",
},
},
}
}
func configureProvider(d *schema.ResourceData) (interface{}, error) {
log.Printf("Creating provider")
useSSL := d.Get("use_ssl").(bool)
username := d.Get("username").(string)
password := d.Get("password").(string)
port := d.Get("port").(int)
connectionTimeout := d.Get("connection_timeout").(int)
protocolVersion := d.Get("protocol_version").(int)
log.Printf("Using port %d", port)
log.Printf("Using use_ssl %v", useSSL)
log.Printf("Using username %s", username)
rawHosts := d.Get("hosts").([]interface{})
hosts := make([]string, len(rawHosts))
for _, value := range rawHosts {
hosts = append(hosts, value.(string))
log.Printf("Using host %v", value.(string))
}
cluster := gocql.NewCluster()
cluster.Hosts = hosts
cluster.Port = port
cluster.Authenticator = &gocql.PasswordAuthenticator{
Username: username,
Password: password,
}
cluster.ConnectTimeout = time.Millisecond * time.Duration(connectionTimeout)
cluster.Timeout = time.Minute * time.Duration(1)
cluster.CQLVersion = "3.0.0"
cluster.Keyspace = "system"
cluster.ProtoVersion = protocolVersion
cluster.HostFilter = gocql.WhiteListHostFilter(hosts...)
cluster.DisableInitialHostLookup = true
if useSSL {
rootCA := d.Get("root_ca").(string)
minTLSVersion := d.Get("min_tls_version").(string)
tlsConfig := &tls.Config{
MinVersion: allowedTlsProtocols[minTLSVersion],
}
if rootCA != "" {
caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM([]byte(rootCA))
if !ok {
return nil, errors.New("Unable to load rootCA")
}
tlsConfig.RootCAs = caPool
}
cluster.SslOpts = &gocql.SslOptions{
Config: tlsConfig,
}
}
return cluster, nil
}