Skip to content

Commit

Permalink
v0.0.24: Support mcma_api_key_auth
Browse files Browse the repository at this point in the history
* Update libs to v0.0.19
  • Loading branch information
evanverneyfink committed Aug 9, 2023
1 parent a349ed9 commit 02518d2
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ebu/terraform-provider-mcma
go 1.18

require (
github.com/ebu/mcma-libraries-go v0.0.16
github.com/ebu/mcma-libraries-go v0.0.19
github.com/hashicorp/terraform-plugin-docs v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.13.0
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ github.com/ebu/mcma-libraries-go v0.0.15 h1:+ruk6fGFtK6byTdLJLxnnqHz+mUp5Fgooepu
github.com/ebu/mcma-libraries-go v0.0.15/go.mod h1:RnT/sTbg7ICp6NHDxyj/xX9xI+bqbcm9SUkPTIJnqs4=
github.com/ebu/mcma-libraries-go v0.0.16 h1:NKmqnMlqGGrhhktzZ9/5coYVqp1kjnSRNXy00zccSZc=
github.com/ebu/mcma-libraries-go v0.0.16/go.mod h1:RnT/sTbg7ICp6NHDxyj/xX9xI+bqbcm9SUkPTIJnqs4=
github.com/ebu/mcma-libraries-go v0.0.17 h1:dX7AFX1nIPkoM/CYXGb9fXNBFtY/TayJv69FvEjRIgA=
github.com/ebu/mcma-libraries-go v0.0.17/go.mod h1:RnT/sTbg7ICp6NHDxyj/xX9xI+bqbcm9SUkPTIJnqs4=
github.com/ebu/mcma-libraries-go v0.0.18 h1:Bji40o9TY62lOCbOnhBwVnUKL8r8y+uP071m1ao0EaQ=
github.com/ebu/mcma-libraries-go v0.0.18/go.mod h1:RnT/sTbg7ICp6NHDxyj/xX9xI+bqbcm9SUkPTIJnqs4=
github.com/ebu/mcma-libraries-go v0.0.19 h1:O1lhCj1eFLbwUOOIO9sR8JUZ2SsXUFEgvXtl0n+WKfw=
github.com/ebu/mcma-libraries-go v0.0.19/go.mod h1:RnT/sTbg7ICp6NHDxyj/xX9xI+bqbcm9SUkPTIJnqs4=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down
16 changes: 16 additions & 0 deletions mcma/mcma_api_key_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mcma

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

mcmaclient "github.com/ebu/mcma-libraries-go/client"
)

func GetMcmaApiKeyAuthenticator(authData map[string]interface{}) (mcmaclient.Authenticator, diag.Diagnostics) {
apiKey, d := GetAuthDataString(authData, "api_key", true)
if d != nil {
return nil, d
}

return mcmaclient.NewMcmaApiKeyAuthenticator(apiKey), nil
}
19 changes: 17 additions & 2 deletions mcma/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ func Provider() *schema.Provider {
},
},
},
"mcma_api_key_auth": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"api_key": {
Type: schema.TypeString,
Description: "The MCMA API key (header = 'x-mcma-api-key') to use for authentication",
Required: true,
},
},
},
},
},
ResourcesMap: map[string]*schema.Resource{
"mcma_service": resourceService(),
Expand All @@ -71,9 +84,10 @@ func addAuthToMap(
authMap map[string]mcmaclient.Authenticator,
resourceData *schema.ResourceData,
authType string,
authKey string,
authFactory func(map[string]interface{}) (mcmaclient.Authenticator, diag.Diagnostics),
) diag.Diagnostics {
blocks := resourceData.Get(authType + "_auth").(*schema.Set).List()
blocks := resourceData.Get(authKey + "_auth").(*schema.Set).List()
switch len(blocks) {
case 0:
return nil
Expand All @@ -97,7 +111,8 @@ func configure(d *schema.ResourceData) (interface{}, diag.Diagnostics) {
serviceRegistryAuthType := d.Get("service_registry_auth_type").(string)

authMap := make(map[string]mcmaclient.Authenticator)
addAuthToMap(authMap, d, "aws4", GetAWS4Authenticator)
addAuthToMap(authMap, d, "AWS4", "aws4", GetAWS4Authenticator)
addAuthToMap(authMap, d, "McmaApiKey", "mcma_api_key", GetMcmaApiKeyAuthenticator)

if len(authMap) == 1 && serviceRegistryAuthType == "" {
for s := range authMap {
Expand Down
66 changes: 50 additions & 16 deletions mcma/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,65 @@ func init() {
}
}

type authBlock interface {
GetText() string
}

type aws4AuthBlock struct {
region string
profile string
accessKey string
secretKey string
}

func getProviderConfig(serviceRegistryUrl string, serviceRegistryAuthType string, authBlocks []aws4AuthBlock) string {
func (authBlock aws4AuthBlock) GetText() string {
authBlockText := " aws4_auth {\n"
if authBlock.region != "" {
authBlockText += " region = \"" + authBlock.region + "\"\n"
}
if authBlock.profile != "" {
authBlockText += " profile = \"" + authBlock.profile + "\"\n"
}
if authBlock.accessKey != "" {
authBlockText += " access_key = \"" + authBlock.accessKey + "\"\n"
}
if authBlock.secretKey != "" {
authBlockText += " secret_key = \"" + authBlock.secretKey + "\"\n"
}
authBlockText += " }\n"
return authBlockText
}

type mcmaApiKeyAuthBlock struct {
apiKey string
}

func (authBlock mcmaApiKeyAuthBlock) GetText() string {
authBlockText := " mcma_api_key_auth {\n"
if authBlock.apiKey != "" {
authBlockText += " api_key = \"" + authBlock.apiKey + "\"\n"
}
authBlockText += " }\n"
return authBlockText
}

func getProviderConfig(serviceRegistryUrl string, serviceRegistryAuthType string, authBlocks []authBlock) string {
providerConfig := "provider \"mcma\" {\n"
providerConfig += " service_registry_url = \"" + serviceRegistryUrl + "\"\n"
if serviceRegistryAuthType != "" {
serviceRegistryAuthType += " service_registry_auth_type = \"" + serviceRegistryAuthType + "\"\n"
}
if authBlocks != nil && len(authBlocks) > 0 {
for _, authBlock := range authBlocks {
providerConfig += " aws4_auth {\n"
if authBlock.region != "" {
providerConfig += " region = \"" + authBlock.region + "\"\n"
}
if authBlock.profile != "" {
providerConfig += " profile = \"" + authBlock.profile + "\"\n"
}
if authBlock.accessKey != "" {
providerConfig += " access_key = \"" + authBlock.accessKey + "\"\n"
}
if authBlock.secretKey != "" {
providerConfig += " secret_key = \"" + authBlock.secretKey + "\"\n"
}
providerConfig += " }\n"
providerConfig += authBlock.GetText()
}
}
providerConfig += "}\n"
return providerConfig
}

func getAwsProfileProviderConfig(serviceRegistryUrl string, region string, profile string) string {
authBlocks := make([]aws4AuthBlock, 1)
authBlocks := make([]authBlock, 1)
authBlocks[0] = aws4AuthBlock{
region: region,
profile: profile,
Expand All @@ -63,3 +85,15 @@ func getAwsProfileProviderConfig(serviceRegistryUrl string, region string, profi
func getAwsProfileProviderConfigFromEnvVars() string {
return getAwsProfileProviderConfig(os.Getenv("MCMA_AWS_SERVICE_REGISTRY_URL"), os.Getenv("MCMA_AWS_REGION"), os.Getenv("MCMA_AWS_PROFILE"))
}

func getMcmaApiKeyProviderConfig(serviceRegistryUrl, apiKey string) string {
authBlocks := make([]authBlock, 1)
authBlocks[0] = mcmaApiKeyAuthBlock{
apiKey: apiKey,
}
return getProviderConfig(serviceRegistryUrl, "", authBlocks)
}

func getMcmaApiKeyProviderConfigFromEnvVars() string {
return getMcmaApiKeyProviderConfig(os.Getenv("MCMA_AWS_SERVICE_REGISTRY_URL"), os.Getenv("MCMA_API_KEY"))
}
2 changes: 1 addition & 1 deletion mcma/resource_job_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestAccMcmaJobProfile_basic(t *testing.T) {
}
}
//resource.Test(t, createTestCase(getKubernetesProviderConfigFromEnvVars()))
resource.Test(t, createTestCase(getAwsProfileProviderConfigFromEnvVars()))
resource.Test(t, createTestCase(getMcmaApiKeyProviderConfigFromEnvVars()))
}

func testAccCheckMcmaJobProfileDestroy(s *terraform.State) error {
Expand Down
2 changes: 1 addition & 1 deletion mcma/resource_mcma_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAccMcmaResource_basic(t *testing.T) {
},
}
}
resource.Test(t, createTestCase(getAwsProfileProviderConfigFromEnvVars()))
resource.Test(t, createTestCase(getMcmaApiKeyProviderConfigFromEnvVars()))
}

func testAccCheckMcmaResourceDestroy(s *terraform.State) error {
Expand Down
2 changes: 1 addition & 1 deletion mcma/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAccMcmaService_basic(t *testing.T) {
}
}
//resource.Test(t, createTestCase(getKubernetesProviderConfigFromEnvVars()))
resource.Test(t, createTestCase(getAwsProfileProviderConfigFromEnvVars()))
resource.Test(t, createTestCase(getMcmaApiKeyProviderConfigFromEnvVars()))
}

func testAccCheckMcmaServiceDestroy(s *terraform.State) error {
Expand Down

0 comments on commit 02518d2

Please sign in to comment.