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 ability to provide custom headers #27

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
5 changes: 5 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ provider "rabbitmq" {
endpoint = "http://127.0.0.1:15672"
username = "guest"
password = "guest"

headers = {
"X-Custom-Header" = "CustomValue"
}
}

# Create a virtual host
Expand Down Expand Up @@ -48,5 +52,6 @@ $ sudo rabbitmq-plugins enable rabbitmq_management
- `cacert_file` (String) The path to a custom CA / intermediate certificate. This can also be sourced from the `RABBITMQ_CACERT` Environment Variable.
- `clientcert_file` (String) The path to the X.509 client certificate. This can also be sourced from the `RABBITMQ_CLIENTCERT` Environment Variable.
- `clientkey_file` (String) The path to the private key. This can also be sourced from the `RABBITMQ_CLIENTKEY` Environment Variable.
- `headers` (Map of String) Custom headers to include in HTTP requests. This should be a map of header names to values.
- `insecure` (Boolean) Trust self-signed certificates. This can also be sourced from the `RABBITMQ_INSECURE` Environment Variable.
- `proxy` (String) The URL of a proxy through which to send HTTP requests to the RabbitMQ server. This can also be sourced from the `RABBITMQ_PROXY` Environment Variable. If not set, the default `HTTP_PROXY`/`HTTPS_PROXY` will be used instead.
36 changes: 32 additions & 4 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import (
"net/url"
"os"

rabbithole "github.com/michaelklishin/rabbit-hole/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
)

type customHeaderRoundTripper struct {
headers map[string]string
transport http.RoundTripper
}

func (c *customHeaderRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for key, value := range c.headers {
req.Header.Add(key, value)
}
return c.transport.RoundTrip(req)
}

func New() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -95,6 +106,13 @@ func New() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("RABBITMQ_PROXY", ""),
},

"headers": {
Description: "Custom headers to include in HTTP requests. This should be a map of header names to values.",
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -130,6 +148,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
var clientcertFile = d.Get("clientcert_file").(string)
var clientkeyFile = d.Get("clientkey_file").(string)
var proxy = d.Get("proxy").(string)
var headers = d.Get("headers").(map[string]interface{})

// Configure TLS/SSL:
// Ignore self-signed cert warnings
Expand Down Expand Up @@ -166,7 +185,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
}

// Connect to RabbitMQ management interface
customHeaders := make(map[string]string)
for k, v := range headers {
customHeaders[k] = v.(string)
}

transport := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: func(req *http.Request) (*url.URL, error) {
Expand All @@ -178,7 +201,12 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
},
}

rmqc, err := rabbithole.NewTLSClient(endpoint, username, password, transport)
customTransport := &customHeaderRoundTripper{
headers: customHeaders,
transport: transport,
}

rmqc, err := rabbithole.NewTLSClient(endpoint, username, password, customTransport)
if err != nil {
return nil, err
}
Expand Down
Loading