From 2db7a838aa02460606a15813fa82b68151ddc95b Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier <51448674+AndreasBergmeier6176@users.noreply.github.com> Date: Fri, 24 Sep 2021 15:50:52 +0200 Subject: [PATCH 1/3] Add proxy_auth to Provider For example for usage like this: https://cloud.google.com/iap/docs/authentication-howto#authenticating_from_proxy-authorization_header --- grafana/provider.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/grafana/provider.go b/grafana/provider.go index bd06baa26..69c7d709c 100644 --- a/grafana/provider.go +++ b/grafana/provider.go @@ -46,6 +46,13 @@ func Provider(version string) func() *schema.Provider { DefaultFunc: schema.EnvDefaultFunc("GRAFANA_AUTH", nil), Description: "API token or basic auth username:password. May alternatively be set via the `GRAFANA_AUTH` environment variable.", }, + "proxy_auth": { + Type: schema.TypeString, + Required: false, + Sensitive: true, + DefaultFunc: schema.EnvDefaultFunc("GRAFANA_PROXY_AUTH", nil), + Description: "API token or basic auth username:password. May alternatively be set via the `GRAFANA_PROXY_AUTH` environment variable.", + }, "org_id": { Type: schema.TypeInt, Required: true, @@ -130,6 +137,10 @@ func Provider(version string) func() *schema.Provider { type client struct { gapi *gapi.Client smapi *smapi.Client + Proxy struct { + BasicAuth string + APIKey string + } } func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) { @@ -140,6 +151,11 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema c := &client{} auth := strings.SplitN(d.Get("auth").(string), ":", 2) + pa := d.Get("proxy_auth") + var proxyAuth []string + if pa != nil { + proxyAuth = strings.SplitN(d.Get("auth").(string), ":", 2) + } cli := cleanhttp.DefaultClient() transport := cleanhttp.DefaultTransport() transport.TLSClientConfig = &tls.Config{} @@ -179,6 +195,11 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema } else { cfg.APIKey = auth[0] } + if len(proxyAuth) == 2 { + c.Proxy.BasicAuth = url.UserPassword(proxyAuth[0], proxyAuth[1]) + } else { + c.Proxy.APIKey = proxyAuth[0] + } gclient, err := gapi.New(d.Get("url").(string), cfg) if err != nil { return nil, diag.FromErr(err) From d73a8a29497a0bc853a6b1e2c17eed5d22aa989a Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier <51448674+AndreasBergmeier6176@users.noreply.github.com> Date: Fri, 24 Sep 2021 16:56:32 +0200 Subject: [PATCH 2/3] Move implementation to golang bindings --- grafana/provider.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/grafana/provider.go b/grafana/provider.go index 69c7d709c..3258e1193 100644 --- a/grafana/provider.go +++ b/grafana/provider.go @@ -137,10 +137,6 @@ func Provider(version string) func() *schema.Provider { type client struct { gapi *gapi.Client smapi *smapi.Client - Proxy struct { - BasicAuth string - APIKey string - } } func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) { @@ -152,9 +148,9 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema auth := strings.SplitN(d.Get("auth").(string), ":", 2) pa := d.Get("proxy_auth") - var proxyAuth []string + var proxyAuth string if pa != nil { - proxyAuth = strings.SplitN(d.Get("auth").(string), ":", 2) + proxyAuth = d.Get("proxy_auth").(string) } cli := cleanhttp.DefaultClient() transport := cleanhttp.DefaultTransport() @@ -195,10 +191,8 @@ func configure(version string, p *schema.Provider) func(context.Context, *schema } else { cfg.APIKey = auth[0] } - if len(proxyAuth) == 2 { - c.Proxy.BasicAuth = url.UserPassword(proxyAuth[0], proxyAuth[1]) - } else { - c.Proxy.APIKey = proxyAuth[0] + if proxyAuth != "" { + cfg.Proxy.APIKey = proxyAuth } gclient, err := gapi.New(d.Get("url").(string), cfg) if err != nil { From 4d8711887b420d0540054e972e69d9440be9b7a3 Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier <51448674+AndreasBergmeier6176@users.noreply.github.com> Date: Fri, 24 Sep 2021 16:58:55 +0200 Subject: [PATCH 3/3] Correct description --- grafana/provider.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grafana/provider.go b/grafana/provider.go index 3258e1193..ec97eeb37 100644 --- a/grafana/provider.go +++ b/grafana/provider.go @@ -51,7 +51,7 @@ func Provider(version string) func() *schema.Provider { Required: false, Sensitive: true, DefaultFunc: schema.EnvDefaultFunc("GRAFANA_PROXY_AUTH", nil), - Description: "API token or basic auth username:password. May alternatively be set via the `GRAFANA_PROXY_AUTH` environment variable.", + Description: "API token. May alternatively be set via the `GRAFANA_PROXY_AUTH` environment variable.", }, "org_id": { Type: schema.TypeInt,