Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bearer token auth, additional arbitrary HTTP headers #81

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ resource "kafka-connect_connector" "sqlite-sink" {

## Provider Properties

| Property | Type | Example | Alternative environment variable name |
|-----------------------|--------|-------------------------|---------------------------------------|
| `url` | URL | "http://localhost:8083" | `KAFKA_CONNECT_URL` |
| `basic_auth_username` | String | "user" | `KAFKA_CONNECT_BASIC_AUTH_USERNAME` |
| `basic_auth_password` | String | "password" | `KAFKA_CONNECT_BASIC_AUTH_PASSWORD` |
| Property | Type | Example | Alternative environment variable name |
|-----------------------|-------------------|-------------------------|---------------------------------------|
| `url` | URL | "http://localhost:8083" | `KAFKA_CONNECT_URL` |
| `basic_auth_username` | String | "user" | `KAFKA_CONNECT_BASIC_AUTH_USERNAME` |
| `basic_auth_password` | String | "password" | `KAFKA_CONNECT_BASIC_AUTH_PASSWORD` |
| `bearer_token` | String | "27A43420FCFF497498B3" | `KAFKA_CONNECT_BEARER_TOKEN` |
| `extra_headers` | Map[String]String | {foo = "bar"} | N/A |

## Resource Properties

Expand Down
36 changes: 33 additions & 3 deletions connect/provider.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package connect

import (
"context"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
kc "github.com/ricardo-ch/go-kafka-connect/lib/connectors"

kc "github.com/ricardo-ch/go-kafka-connect/v3/lib/connectors"
)

func Provider() *schema.Provider {
Expand All @@ -26,8 +30,22 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KAFKA_CONNECT_BASIC_AUTH_PASSWORD", ""),
},
"bearer_token": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("KAFKA_CONNECT_BEARER_TOKEN", ""),
},
"extra_headers": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
// No DefaultFunc here to read from the env on account of this issue:
// https://github.com/hashicorp/terraform-plugin-sdk/issues/142
},
},
ConfigureFunc: providerConfigure,
ConfigureContextFunc: providerConfigure,
ResourcesMap: map[string]*schema.Resource{
"kafka-connect_connector": kafkaConnectorResource(),
},
Expand All @@ -36,7 +54,7 @@ func Provider() *schema.Provider {
return &provider
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
log.Printf("[INFO] Initializing KafkaConnect client")
addr := d.Get("url").(string)
c := kc.NewClient(addr)
Expand All @@ -45,5 +63,17 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
if user != "" && pass != "" {
c.SetBasicAuth(user, pass)
}

if token := d.Get("bearer_token").(string); token != "" {
c.SetHeader("Authorization", fmt.Sprintf("Bearer %s", token))
}

extraHeaders := d.Get("extra_headers").(map[string]interface{})
if extraHeaders != nil {
for k, v := range extraHeaders {
c.SetHeader(k, v.(string))
}
}

return c, nil
}
2 changes: 1 addition & 1 deletion connect/resource_kafka_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
kc "github.com/ricardo-ch/go-kafka-connect/lib/connectors"
kc "github.com/ricardo-ch/go-kafka-connect/v3/lib/connectors"
)

func kafkaConnectorResource() *schema.Resource {
Expand Down
2 changes: 1 addition & 1 deletion connect/resource_kafka_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

r "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
kc "github.com/ricardo-ch/go-kafka-connect/lib/connectors"
kc "github.com/ricardo-ch/go-kafka-connect/v3/lib/connectors"
)

func TestAccConnectorConfigUpdate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
bou.ke/monkey v1.0.2 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/ricardo-ch/go-kafka-connect v0.0.0-20200928094249-af7817721cb5
github.com/ricardo-ch/go-kafka-connect/v3 v3.0.0-20220613085032-a69a6c33b847
)

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