Skip to content

Commit a1dbe39

Browse files
authored
Add support for HTTP basic auth (#24)
* Add support for HTTP basic auth
1 parent 43e5608 commit a1dbe39

9 files changed

+100
-29
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Configure the provider directly, or set the ENV variable `KAFKA_CONNECT_URL`
1515
```hcl
1616
provider "kafka-connect" {
1717
url = "http://localhost:8083"
18+
basic_auth_username = "user" # Optional
19+
basic_auth_password = "password" # Optional
1820
}
1921
2022
resource "kafka-connect_connector" "sqlite-sink" {
@@ -36,6 +38,22 @@ resource "kafka-connect_connector" "sqlite-sink" {
3638
}
3739
```
3840

41+
## Provider Properties
42+
43+
| Property | Type | Example | Alternative environment variable name |
44+
|-----------------------|--------|-------------------------|---------------------------------------|
45+
| `url` | URL | "http://localhost:8083" | `KAFKA_CONNECT_URL` |
46+
| `basic_auth_username` | String | "user" | `KAFKA_CONNECT_BASIC_AUTH_USERNAME` |
47+
| `basic_auth_password` | String | "password" | `KAFKA_CONNECT_BASIC_AUTH_PASSWORD` |
48+
49+
## Resource Properties
50+
51+
| Property | Type | Description |
52+
|-----------------------|-----------|----------------------------------------------------------------------|
53+
| `name` | String | Connector name |
54+
| `config` | HCL Block | Connector configuration |
55+
| `config_sensitive` | HCL Block | Sensitive connector configuration. Will be masked in output. |
56+
3957
## Developing
4058

4159
0. [Install go][install-go]

bin/test

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ set -ex
44

55
go build
66
mv terraform-provider-kafka-connect ~/.terraform.d/plugins/terraform-provider-kafka-connect
7+
docker-compose down
8+
docker-compose up -d
9+
sleep 60
710
cd examples
11+
rm -rf .terraform terraform.tfstate*
812
terraform init
913
terraform plan
14+
terraform apply -auto-approve
15+
docker-compose down

connect/provider.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ func Provider() terraform.ResourceProvider {
1717
Optional: true,
1818
DefaultFunc: schema.EnvDefaultFunc("KAFKA_CONNECT_URL", ""),
1919
},
20+
"basic_auth_username": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
DefaultFunc: schema.EnvDefaultFunc("KAFKA_CONNECT_BASIC_AUTH_USERNAME", ""),
24+
},
25+
"basic_auth_password": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
DefaultFunc: schema.EnvDefaultFunc("KAFKA_CONNECT_BASIC_AUTH_PASSWORD", ""),
29+
},
2030
},
2131
ConfigureFunc: providerConfigure,
2232
ResourcesMap: map[string]*schema.Resource{
@@ -31,6 +41,10 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
3141
log.Printf("[INFO] Initializing KafkaConnect client")
3242
addr := d.Get("url").(string)
3343
c := kc.NewClient(addr)
34-
44+
user := d.Get("basic_auth_username").(string)
45+
pass := d.Get("basic_auth_password").(string)
46+
if user != "" && pass != "" {
47+
c.SetBasicAuth(user, pass)
48+
}
3549
return c, nil
3650
}

connect/resource_kafka_connector.go

+11-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package connect
33
import (
44
"fmt"
55
"log"
6-
"time"
76

87
"github.com/hashicorp/terraform/helper/schema"
98
kc "github.com/ricardo-ch/go-kafka-connect/lib/connectors"
@@ -52,19 +51,11 @@ func setNameFromID(d *schema.ResourceData, meta interface{}) ([]*schema.Resource
5251
}
5352

5453
func connectorCreate(d *schema.ResourceData, meta interface{}) error {
55-
c := meta.(kc.Client)
54+
c := meta.(kc.HighLevelClient)
5655
name := nameFromRD(d)
5756

5857
config, sensitiveCache := configFromRD(d)
59-
if !kc.TryUntil(
60-
func() bool {
61-
_, err := c.GetAll()
62-
return err == nil
63-
},
64-
5*time.Minute,
65-
) {
66-
return fmt.Errorf("timed out trying to connect to kafka-connect server at %s", c.URL)
67-
}
58+
6859
req := kc.CreateConnectorRequest{
6960
ConnectorRequest: kc.ConnectorRequest{
7061
Name: name,
@@ -87,7 +78,7 @@ func connectorCreate(d *schema.ResourceData, meta interface{}) error {
8778
}
8879

8980
func connectorDelete(d *schema.ResourceData, meta interface{}) error {
90-
c := meta.(kc.Client)
81+
c := meta.(kc.HighLevelClient)
9182

9283
name := nameFromRD(d)
9384
req := kc.ConnectorRequest{
@@ -105,7 +96,7 @@ func connectorDelete(d *schema.ResourceData, meta interface{}) error {
10596
}
10697

10798
func connectorUpdate(d *schema.ResourceData, meta interface{}) error {
108-
c := meta.(kc.Client)
99+
c := meta.(kc.HighLevelClient)
109100

110101
name := nameFromRD(d)
111102

@@ -135,7 +126,7 @@ func connectorUpdate(d *schema.ResourceData, meta interface{}) error {
135126
}
136127

137128
func connectorRead(d *schema.ResourceData, meta interface{}) error {
138-
c := meta.(kc.Client)
129+
c := meta.(kc.HighLevelClient)
139130

140131
config, sensitiveCache := configFromRD(d)
141132
name := d.Get("name").(string)
@@ -166,7 +157,7 @@ func connectorRead(d *schema.ResourceData, meta interface{}) error {
166157
// The first is intended to be passed to CreateConnectorRequest
167158
// The second is intended to preserve knowledge of which keys are sensitive information in the incoming
168159
// ConnectorResponse.Config
169-
func configFromRD(d *schema.ResourceData) (map[string]string, map[string]string) {
160+
func configFromRD(d *schema.ResourceData) (map[string]interface{}, map[string]interface{}) {
170161
cfg := mapFromRD(d, "config")
171162
scfg := mapFromRD(d, "config_sensitive")
172163
config := combineMaps(cfg, scfg)
@@ -177,18 +168,13 @@ func nameFromRD(d *schema.ResourceData) string {
177168
return d.Get("name").(string)
178169
}
179170

180-
func mapFromRD(d *schema.ResourceData, key string) map[string]string {
181-
mapToBe := d.Get(key).(map[string]interface{})
182-
realMap := make(map[string]string)
183-
for k, v := range mapToBe {
184-
realMap[k] = v.(string)
185-
}
186-
return realMap
171+
func mapFromRD(d *schema.ResourceData, key string) map[string]interface{} {
172+
return d.Get(key).(map[string]interface{})
187173
}
188174

189175
// if there are duplicate keys this will always take the kv from second!!!
190-
func combineMaps(first map[string]string, second map[string]string) map[string]string {
191-
union := make(map[string]string)
176+
func combineMaps(first map[string]interface{}, second map[string]interface{}) map[string]interface{} {
177+
union := make(map[string]interface{})
192178
for k, v := range first {
193179
union[k] = v
194180
}
@@ -198,7 +184,7 @@ func combineMaps(first map[string]string, second map[string]string) map[string]s
198184
return union
199185
}
200186

201-
func removeSecondKeysFromFirst(first map[string]string, second map[string]string) map[string]string {
187+
func removeSecondKeysFromFirst(first map[string]interface{}, second map[string]interface{}) map[string]interface{} {
202188
for k := range second {
203189
delete(first, k)
204190
}

connect/resource_kafka_connector_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func testResourceConnector_initialCheck(s *terraform.State) error {
4949
return fmt.Errorf("id doesn't match name")
5050
}
5151

52-
client := testProvider.Meta().(kc.Client)
52+
client := testProvider.Meta().(kc.HighLevelClient)
5353

5454
c, err := client.GetConnector(kc.ConnectorRequest{Name: "sqlite-sink"})
5555
if err != nil {
@@ -66,7 +66,7 @@ func testResourceConnector_initialCheck(s *terraform.State) error {
6666
}
6767

6868
func testResourceConnector_updateCheck(s *terraform.State) error {
69-
client := testProvider.Meta().(kc.Client)
69+
client := testProvider.Meta().(kc.HighLevelClient)
7070

7171
c, err := client.GetConnector(kc.ConnectorRequest{Name: "sqlite-sink"})
7272
if err != nil {

docker-compose.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ services:
4949
CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1
5050
CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1
5151
CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1
52+
nginx:
53+
image: xscys/nginx-sidecar-basic-auth
54+
ports:
55+
- 8087:8087
56+
depends_on:
57+
- kafka-connect
58+
environment:
59+
FORWARD_HOST: kafka-connect
60+
FORWARD_PORT: 8083
61+
BASIC_AUTH_USERNAME: testuser
62+
BASIC_AUTH_PASSWORD: testpassword

examples/main.tf

+26
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,29 @@ resource "kafka-connect_connector" "sqlite-sink" {
1919
"connection.password" = "this-should-never-appear-unmasked"
2020
}
2121
}
22+
23+
provider "kafka-connect" {
24+
alias = "with-basic-auth"
25+
url = "http://localhost:8087"
26+
basic_auth_username = "testuser"
27+
basic_auth_password = "testpassword"
28+
}
29+
30+
resource "kafka-connect_connector" "sqlite-sink-with-auth" {
31+
provider = kafka-connect.with-basic-auth
32+
name = "sqlite-sink-with-auth"
33+
34+
config = {
35+
"name" = "sqlite-sink-with-auth"
36+
"connector.class" = "io.confluent.connect.jdbc.JdbcSinkConnector"
37+
"tasks.max" = 1
38+
"topics" = "orders"
39+
"connection.url" = "jdbc:sqlite:test.db"
40+
"auto.create" = "true"
41+
"connection.user" = "admin"
42+
}
43+
44+
config_sensitive = {
45+
"connection.password" = "this-should-never-appear-unmasked"
46+
}
47+
}

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ go 1.12
44

55
require (
66
github.com/hashicorp/terraform v0.12.1
7-
github.com/ricardo-ch/go-kafka-connect v0.0.0-20180209135343-132b7b7ad380
7+
github.com/ricardo-ch/go-kafka-connect v0.0.0-20200403115642-f7b66cb04ed7
8+
gopkg.in/resty.v1 v1.12.0 // indirect
89
)
910

1011
replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ github.com/mitchellh/panicwrap v0.0.0-20190213213626-17011010aaa4/go.mod h1:YYMf
226226
github.com/mitchellh/prefixedio v0.0.0-20190213213902-5733675afd51/go.mod h1:kB1naBgV9ORnkiTVeyJOI1DavaJkG4oNIq0Af6ZVKUo=
227227
github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
228228
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
229+
github.com/mmajis/go-kafka-connect v0.0.0-20200328184024-6284b2164d53 h1:Yk+eTGgUgsl3mdjSeEEQ0R5pP/4ytJ7gK/1JY/J1BV8=
230+
github.com/mmajis/go-kafka-connect v0.0.0-20200328184024-6284b2164d53/go.mod h1:qmte3Nxjt2qkpVCRCzqKmD5HVqHolDYSiJ+I3dYf2wo=
229231
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
230232
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
231233
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
@@ -252,6 +254,10 @@ github.com/ricardo-ch/go-kafka-connect v0.0.0-20180209135343-132b7b7ad380 h1:aNt
252254
github.com/ricardo-ch/go-kafka-connect v0.0.0-20180209135343-132b7b7ad380/go.mod h1:1wqdL63J2nVaqWpJuc1SH1nc+yuEYZrYI6JpxGhY3F4=
253255
github.com/ricardo-ch/go-kafka-connect v0.0.0-20190603085745-132b7b7ad380a6c860fecc0ca9b0a2af9bf45471 h1:72zdRhATkqj0AF2EgQpXK3rL77+hGy7rGNmzHNDXB7U=
254256
github.com/ricardo-ch/go-kafka-connect v0.0.0-20190603085745-132b7b7ad380a6c860fecc0ca9b0a2af9bf45471/go.mod h1:1wqdL63J2nVaqWpJuc1SH1nc+yuEYZrYI6JpxGhY3F4=
257+
github.com/ricardo-ch/go-kafka-connect v0.0.0-20190603085745-7ed69492c725 h1:GEYixMvGK8NsbWtTe+pMpYeqA7taxTWRTuurKmesv3M=
258+
github.com/ricardo-ch/go-kafka-connect v0.0.0-20190603085745-7ed69492c725/go.mod h1:1wqdL63J2nVaqWpJuc1SH1nc+yuEYZrYI6JpxGhY3F4=
259+
github.com/ricardo-ch/go-kafka-connect v0.0.0-20200403115642-f7b66cb04ed7 h1:0X6qZzZMIBLjw5GQKgBQiYJYozAcOYCTQqBv9tdwn78=
260+
github.com/ricardo-ch/go-kafka-connect v0.0.0-20200403115642-f7b66cb04ed7/go.mod h1:1wqdL63J2nVaqWpJuc1SH1nc+yuEYZrYI6JpxGhY3F4=
255261
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
256262
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
257263
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
@@ -337,6 +343,7 @@ golang.org/x/net v0.0.0-20181029044818-c44066c5c816/go.mod h1:mL1N/T3taQHkDXs73r
337343
golang.org/x/net v0.0.0-20181106065722-10aee1819953/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
338344
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
339345
golang.org/x/net v0.0.0-20181129055619-fae4c4e3ad76/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
346+
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
340347
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
341348
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
342349
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -404,6 +411,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
404411
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
405412
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
406413
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
414+
gopkg.in/resty.v1 v1.12.0 h1:CuXP0Pjfw9rOuY6EP+UvtNvt5DSqHpIxILZKT/quCZI=
415+
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
407416
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
408417
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
409418
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 commit comments

Comments
 (0)