Skip to content

Commit

Permalink
Fix SAML import, allow for arbitrary headers
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudsmith-iduffy committed Jan 6, 2025
1 parent f77284a commit 520e10c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
9 changes: 8 additions & 1 deletion cloudsmith/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func Provider() *schema.Provider {
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("CLOUDSMITH_API_HOST", "https://api.cloudsmith.io/v1"),
},
"headers": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Additional HTTP headers to include in API requests",
Optional: true,
},
},
DataSourcesMap: map[string]*schema.Resource{
"cloudsmith_namespace": dataSourceNamespace(),
Expand Down Expand Up @@ -72,8 +78,9 @@ func Provider() *schema.Provider {
apiHost := requiredString(d, "api_host")
apiKey := requiredString(d, "api_key")
userAgent := fmt.Sprintf("(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, terraformVersion)
headers := d.Get("headers").(map[string]interface{})

return newProviderConfig(apiHost, apiKey, userAgent)
return newProviderConfig(apiHost, apiKey, headers, userAgent)
}

return p
Expand Down
21 changes: 19 additions & 2 deletions cloudsmith/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloudsmith
import (
"context"
"errors"
"fmt"
"net/http"

"github.com/cloudsmith-io/cloudsmith-api-go"
Expand All @@ -20,17 +21,21 @@ type providerConfig struct {
APIClient *cloudsmith.APIClient
}

func newProviderConfig(apiHost, apiKey, userAgent string) (*providerConfig, diag.Diagnostics) {
func newProviderConfig(apiHost string, apiKey string, headers map[string]interface{}, userAgent string) (*providerConfig, diag.Diagnostics) {
if apiKey == "" {
return nil, diag.FromErr(errMissingCredentials)
}

httpClient := http.DefaultClient
httpClient.Transport = logging.NewSubsystemLoggingHTTPTransport("Cloudsmith", http.DefaultTransport)
httpClient.Transport = logging.NewSubsystemLoggingHTTPTransport("Cloudsmith", &headerTransport{
headers: headers,
rt: http.DefaultTransport,
})

config := cloudsmith.NewConfiguration()
config.Debug = logging.IsDebugOrHigher()
config.HTTPClient = httpClient

config.Servers = cloudsmith.ServerConfigurations{
{URL: apiHost},
}
Expand Down Expand Up @@ -58,3 +63,15 @@ func (pc *providerConfig) GetAPIKey() string {
apiKeys, _ := pc.Auth.Value(cloudsmith.ContextAPIKeys).(map[string]cloudsmith.APIKey)
return apiKeys["apikey"].Key
}

type headerTransport struct {
headers map[string]interface{}
rt http.RoundTripper
}

func (t *headerTransport) RoundTrip(req *http.Request) (*http.Response, error) {
for k, v := range t.headers {
req.Header.Add(k, fmt.Sprint(v))
}
return t.rt.RoundTrip(req)
}
2 changes: 1 addition & 1 deletion cloudsmith/resource_saml_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func generateSAMLAuthID(organization string, samlAuth *cloudsmith.OrganizationSA
data += fmt.Sprintf("-%t", samlAuth.GetSamlAuthEnabled())
data += fmt.Sprintf("-%t", samlAuth.GetSamlAuthEnforced())

if url, hasURL := samlAuth.GetSamlMetadataUrlOk(); hasURL {
if url, hasURL := samlAuth.GetSamlMetadataUrlOk(); url != nil && hasURL {
data += fmt.Sprintf("-%s", *url)
}

Expand Down
2 changes: 1 addition & 1 deletion cloudsmith/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestAccService_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccServiceCheckExists("cloudsmith_service.test"),
resource.TestCheckResourceAttrSet("cloudsmith_service.test", "team.#"),
resource.TestMatchTypeSetElemNestedAttrs("cloudsmith_service.test", "team.*", map[string]*regexp.Regexp{
resource.TestMatchTypeSetElemNestedAttrs("cloudsmith_service.test", "team.*", map[string]*regexp.Regexp{
"slug": regexp.MustCompile("^tf-test-team-svc(-[^2].*)?$"),
"role": regexp.MustCompile("^Member$"),
}),
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkE
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudsmith-io/cloudsmith-api-go v0.0.41 h1:7bPbyD3FIN8PmmZQg43O/y8OTkFFZ2ZxcSbrMcdrjHE=
github.com/cloudsmith-io/cloudsmith-api-go v0.0.41/go.mod h1:xHTDgU8TyMXiWUZdn2OQR7AaZdz9M5SrNenyQU44zGQ=
github.com/cloudsmith-io/cloudsmith-api-go v0.0.42-0.20241129202450-bd5381591ce5 h1:vU6M9CeLmTU9xJj5G7pCQ9F8o+zl4pSfKnmGxB+miLE=
github.com/cloudsmith-io/cloudsmith-api-go v0.0.42-0.20241129202450-bd5381591ce5/go.mod h1:xHTDgU8TyMXiWUZdn2OQR7AaZdz9M5SrNenyQU44zGQ=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down

0 comments on commit 520e10c

Please sign in to comment.