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

fix(tls_mutual_authentication): update activation after mtls creation #829

Merged
merged 13 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
214 changes: 208 additions & 6 deletions docs/resources/tls_mutual_authentication.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,217 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "fastly_tls_mutual_authentication Resource - terraform-provider-fastly"
subcategory: ""
layout: "fastly"
page_title: "Fastly: tls_mutual_authentication"
sidebar_current: "docs-fastly-resource-tls_mutual_authentication"
description: |-
Allows for client-to-server authentication using client-side X.509 authentication.
---

# fastly_tls_mutual_authentication (Resource)
# fastly_tls_mutual_authentication

The Mutual TLS API allows for client-to-server authentication using client-side X.509 authentication.

The main Mutual Authentication object represents the certificate bundle and other configurations which support Mutual TLS for your domains.

Mutual TLS can be added to existing TLS activations to allow for client-to-server authentication. In order to use mutual TLS, you must already have active server-side TLS using either custom certificates or an enabled Fastly-managed subscription.

The examples below demonstrate how to use Mutual Authentication along with a TLS Subscription. Refer to the `fastly_tls_subscription` resource documentation for a deeper explanation of that code.

## Example: Single Activation

The following example sets up a TLS Subscription for `www.example.com` and then adds Mutual Authentication.

```terraform
terraform {
required_providers {
dnsimple = {
source = "dnsimple/dnsimple"
version = "1.5.0"
}
fastly = {
source = "fastly/fastly"
version = "5.7.2"
}
}
}

variable "dnsimple_token" {
type = string
}

variable "dnsimple_account" {
type = string
}

provider "dnsimple" {
account = var.dnsimple_account
token = var.dnsimple_token
}

variable "zone" {
type = string
default = "example.com"
}

resource "fastly_service_vcl" "example" {
name = "example"
domain {
name = "www.${var.zone}"
}
backend {
address = "httpbin.org"
name = "httpbin"
}
force_destroy = true
}

resource "fastly_tls_subscription" "www" {
domains = [for domain in fastly_service_vcl.example.domain : domain.name if domain.name == "www.${var.zone}"]
certificate_authority = "lets-encrypt"
Integralist marked this conversation as resolved.
Show resolved Hide resolved
}

resource "dnsimple_zone_record" "www_acme_challenge" {
name = "_acme-challenge.www"
ttl = "60"
type = "CNAME"
value = one([for obj in fastly_tls_subscription.www.managed_dns_challenges : obj.record_value if obj.record_name == "_acme-challenge.www.${var.zone}"])
zone_name = var.zone
}

resource "fastly_tls_subscription_validation" "www" {
subscription_id = fastly_tls_subscription.www.id
depends_on = [dnsimple_zone_record.www_acme_challenge]
}

data "fastly_tls_configuration" "default" {
default = true
depends_on = [fastly_tls_subscription_validation.www]
}

resource "dnsimple_zone_record" "www" {
name = "www"
ttl = "60"
type = "CNAME"
value = one([for record in data.fastly_tls_configuration.default.dns_records : record.record_value if record.record_type == "CNAME"])
zone_name = var.zone
}

data "fastly_tls_activation" "www" {
domain = "www.example.com"
depends_on = [dnsimple_zone_record.www]
}

resource "fastly_tls_mutual_authentication" "www" {
activation_ids = [data.fastly_tls_activation.www.id]
cert_bundle = "-----BEGIN CERTIFICATE-----\n<REDACTED>\n-----END CERTIFICATE-----"
enforced = true
}
```

## Example: Multiple Activations

The following example sets up a TLS Subscription for `foo.example.com` and `bar.example.com` and then adds Mutual Authentication to each TLS Activation.

```terraform
terraform {
required_providers {
dnsimple = {
source = "dnsimple/dnsimple"
version = "1.5.0"
}
fastly = {
source = "fastly/fastly"
version = "5.7.2"
}
}
}

variable "dnsimple_token" {
type = string
}

variable "dnsimple_account" {
type = string
}

provider "dnsimple" {
account = var.dnsimple_account
token = var.dnsimple_token
}

variable "zone" {
type = string
default = "example.com"
}

resource "fastly_service_vcl" "example" {
name = "example"
domain {
name = "foo.${var.zone}"
}
domain {
name = "bar.${var.zone}"
}
backend {
address = "httpbin.org"
name = "httpbin"
}
force_destroy = true
}

resource "fastly_tls_subscription" "example" {
domains = [for domain in fastly_service_vcl.example.domain : domain.name]
certificate_authority = "lets-encrypt"
}

resource "dnsimple_zone_record" "example_acme_challenge" {
for_each = {
for domain in fastly_tls_subscription.example.domains : domain => one([
for obj in fastly_tls_subscription.example.managed_dns_challenges : obj if obj.record_name == "_acme-challenge.${domain}"
])
}
name = each.value.record_name
ttl = "60"
type = each.value.record_type
value = each.value.record_value
zone_name = var.zone
}

resource "fastly_tls_subscription_validation" "example" {
subscription_id = fastly_tls_subscription.example.id
depends_on = [dnsimple_zone_record.example_acme_challenge]
}

data "fastly_tls_configuration" "default" {
default = true
depends_on = [fastly_tls_subscription_validation.example]
}

resource "dnsimple_zone_record" "foo" {
name = "foo"
ttl = "60"
type = "CNAME"
value = one([for record in data.fastly_tls_configuration.default.dns_records : record.record_value if record.record_type == "CNAME"])
zone_name = var.zone
}

resource "dnsimple_zone_record" "bar" {
name = "bar"
ttl = "60"
type = "CNAME"
value = one([for record in data.fastly_tls_configuration.default.dns_records : record.record_value if record.record_type == "CNAME"])
zone_name = var.zone
}

data "fastly_tls_activation_ids" "example" {
certificate_id = fastly_tls_subscription.example.certificate_id
}

resource "fastly_tls_mutual_authentication" "example" {
activation_ids = data.fastly_tls_activation_ids.example.ids
cert_bundle = "-----BEGIN CERTIFICATE-----\n<REDACTED>\n-----END CERTIFICATE-----"
enforced = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand All @@ -21,8 +222,9 @@ description: |-

### Optional

- `activation_ids` (Set of String) List of TLS Activation IDs
- `enforced` (Boolean) Determines whether Mutual TLS will fail closed (enforced) or fail open. A true value will require a successful Mutual TLS handshake for the connection to continue and will fail closed if unsuccessful. A false value will fail open and allow the connection to proceed (if this attribute is not set we default to `false`).
- `include` (String) Comma-separated list of related objects to include (e.g. `tls_activations` will provide you with the TLS domain names that are related to your Mutual TLS authentication).
- `include` (String) A comma-separated list used by the Terraform provider during a state refresh to return more data related to your mutual authentication from the Fastly API (permitted values: `tls_activations`).
- `name` (String) A custom name for your mutual authentication. If name is not supplied we will auto-generate one.

### Read-Only
Expand Down
84 changes: 84 additions & 0 deletions examples/resources/tls_mutual_authentication_basic_usage.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
terraform {
required_providers {
dnsimple = {
source = "dnsimple/dnsimple"
version = "1.5.0"
}
fastly = {
source = "fastly/fastly"
version = "5.7.2"
}
}
}

variable "dnsimple_token" {
type = string
}

variable "dnsimple_account" {
type = string
}

provider "dnsimple" {
account = var.dnsimple_account
token = var.dnsimple_token
}

variable "zone" {
type = string
default = "example.com"
}

resource "fastly_service_vcl" "example" {
name = "example"
domain {
name = "www.${var.zone}"
}
backend {
address = "httpbin.org"
name = "httpbin"
}
force_destroy = true
}

resource "fastly_tls_subscription" "www" {
domains = [for domain in fastly_service_vcl.example.domain : domain.name if domain.name == "www.${var.zone}"]
certificate_authority = "lets-encrypt"
Integralist marked this conversation as resolved.
Show resolved Hide resolved
}

resource "dnsimple_zone_record" "www_acme_challenge" {
name = "_acme-challenge.www"
ttl = "60"
type = "CNAME"
value = one([for obj in fastly_tls_subscription.www.managed_dns_challenges : obj.record_value if obj.record_name == "_acme-challenge.www.${var.zone}"])
zone_name = var.zone
}

resource "fastly_tls_subscription_validation" "www" {
subscription_id = fastly_tls_subscription.www.id
depends_on = [dnsimple_zone_record.www_acme_challenge]
}

data "fastly_tls_configuration" "default" {
default = true
depends_on = [fastly_tls_subscription_validation.www]
}

resource "dnsimple_zone_record" "www" {
name = "www"
ttl = "60"
type = "CNAME"
value = one([for record in data.fastly_tls_configuration.default.dns_records : record.record_value if record.record_type == "CNAME"])
zone_name = var.zone
}

data "fastly_tls_activation" "www" {
domain = "www.example.com"
depends_on = [dnsimple_zone_record.www]
}

resource "fastly_tls_mutual_authentication" "www" {
activation_ids = [data.fastly_tls_activation.www.id]
cert_bundle = "-----BEGIN CERTIFICATE-----\n<REDACTED>\n-----END CERTIFICATE-----"
enforced = true
}
Loading
Loading