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

feat(tls_activation): support mutual_authentication_id in domain activation at creation #875

Merged
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
10 changes: 9 additions & 1 deletion fastly/resource_fastly_tls_activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func resourceFastlyTLSActivation() *schema.Resource {

func resourceFastlyTLSActivationCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*APIClient).conn
var diags diag.Diagnostics

var configuration *fastly.TLSConfiguration
if v, ok := d.GetOk("configuration_id"); ok {
Expand All @@ -73,7 +74,14 @@ func resourceFastlyTLSActivationCreate(ctx context.Context, d *schema.ResourceDa

d.SetId(activation.ID)

return resourceFastlyTLSActivationRead(ctx, d, meta)
// Setting the mutual_authentication_id is only possible through an update via PATCH.
// See https://github.com/fastly/terraform-provider-fastly/issues/873
mtlsID := d.Get("mutual_authentication_id").(string)
if mtlsID != "" {
diags = append(diags, resourceFastlyTLSActivationUpdate(ctx, d, meta)...)
}

return append(diags, resourceFastlyTLSActivationRead(ctx, d, meta)...)
}

func resourceFastlyTLSActivationRead(_ context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
Expand Down
92 changes: 92 additions & 0 deletions fastly/resource_fastly_tls_activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,54 @@ func TestAccFastlyTLSActivation_basic(t *testing.T) {
})
}

func TestAccFastlyTLSActivation_mTLS(t *testing.T) {
domain := fmt.Sprintf("%s.com", acctest.RandomWithPrefix(testResourcePrefix))
key, cert, err := generateKeyAndCert(domain)
require.NoError(t, err)
_, mtlsCert, err := generateKeyAndCert(domain)
require.NoError(t, err)
key = strings.ReplaceAll(key, "\n", `\n`)
cert = strings.ReplaceAll(cert, "\n", `\n`)
name := acctest.RandomWithPrefix(testResourcePrefix)

resourceName := "fastly_tls_activation.mtls_test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccFastlyTLSActivationCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccFastlyTLSActivationMTLSConfig(name, name, key, name, cert, domain, mtlsCert),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "certificate_id"),
resource.TestCheckResourceAttrSet(resourceName, "configuration_id"),
resource.TestCheckResourceAttr(resourceName, "domain", domain),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
testAccFastlyTLSActivationCheckExists(resourceName),
),
},
{
Config: testAccFastlyTLSActivationMTLSConfig(name, name, key, name, cert, domain, mtlsCert),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "certificate_id"),
resource.TestCheckResourceAttrSet(resourceName, "configuration_id"),
resource.TestCheckResourceAttr(resourceName, "domain", domain),
resource.TestCheckResourceAttrSet(resourceName, "created_at"),
resource.TestCheckResourceAttrSet(resourceName, "mutual_authentication_id"),
testAccFastlyTLSActivationCheckExists(resourceName),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccFastlyTLSActivationConfig(serviceName, keyName, key, certName, cert, domain string) string {
return fmt.Sprintf(`
resource "fastly_service_vcl" "test" {
Expand Down Expand Up @@ -97,6 +145,50 @@ resource "fastly_tls_activation" "test" {
`, serviceName, domain, key, keyName, cert, certName, domain)
}

func testAccFastlyTLSActivationMTLSConfig(serviceName, keyName, key, certName, cert, domain, certBundle string) string {
return fmt.Sprintf(`
resource "fastly_service_vcl" "mtls_test" {
name = "%s"

domain {
name = "%s"
}

backend {
address = "127.0.0.1"
name = "localhost"
}

force_destroy = true
}

resource "fastly_tls_private_key" "mtls_test" {
key_pem = "%s"
name = "%s"
}

resource "fastly_tls_certificate" "mtls_test" {
certificate_body = "%s"
name = "%s"
depends_on = [fastly_tls_private_key.mtls_test]
}

resource "fastly_tls_activation" "mtls_test" {
certificate_id = fastly_tls_certificate.mtls_test.id
domain = "%s"
depends_on = [fastly_service_vcl.mtls_test]
mutual_authentication_id = fastly_tls_mutual_authentication.mtls_test.id
}

resource "fastly_tls_mutual_authentication" "mtls_test" {
cert_bundle = <<EOF
%s
EOF
name = "example_mtls"
}
`, serviceName, domain, key, keyName, cert, certName, domain, certBundle)
}

func testAccFastlyTLSActivationCheckExists(resourceName string) resource.TestCheckFunc {
return func(state *terraform.State) error {
conn := testAccProvider.Meta().(*APIClient).conn
Expand Down
Loading