Skip to content
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
5 changes: 5 additions & 0 deletions apps/docs/src/content/docs/terraform/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"icon": "layers",
"title": "Terraform",
"order": 6
}
110 changes: 110 additions & 0 deletions apps/docs/src/content/docs/terraform/default/en/authentication.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Authentication
description: "How the FerrisKey Terraform provider signs in: password and client credentials grants, environment variables, TLS, token refresh and retries."
icon: key-round
order: 3
---

# Authentication

The provider gets an access token from the token endpoint of one realm, `{url}/realms/{realm}/protocol/openid-connect/token`, and sends it as a bearer token on every API call. What the token allows is what the roles of the signed-in user, or of the client's service account, allow. See [roles and permissions](/en/discover/core-concepts/roles).

## Choosing the grant

The provider picks the grant from the credentials it finds:

| Credentials set | Grant | Use it for |
|---|---|---|
| `client_id`, `client_secret` | `client_credentials` | Every day: CI, pipelines, a workstation |
| `client_id`, `username`, `password` | `password` | Once, to [bootstrap](/en/terraform/getting-started) the service account |

Setting both `password` and `client_secret` is an error, and so is setting neither. The password grant sends no secret, so its `client_id` has to be a public client with **Direct access grants** on. The client credentials grant needs a confidential client with `service_account_enabled`.

The password grant is [deprecated](/en/discover/core-concepts/authentication#password-resource-owner), and it fails for an admin who owes a required action or signs in with a passkey or a federated identity provider. Keep it for the bootstrap.

## Provider arguments

| Argument | Environment variable | Description |
|---|---|---|
| `url` | `FERRISKEY_URL` | Base URL of the API, with no `/realms` suffix. Required |
| `realm` | `FERRISKEY_REALM` | Realm the provider signs in to, often `master`. Required |
| `client_id` | `FERRISKEY_CLIENT_ID` | Client the token is requested for. Required |
| `client_secret` | `FERRISKEY_CLIENT_SECRET` | Selects the client credentials grant. Sensitive |
| `username` | `FERRISKEY_USERNAME` | User for the password grant |
| `password` | `FERRISKEY_PASSWORD` | Selects the password grant. Sensitive |
| `scope` | `FERRISKEY_SCOPE` | Scope sent with the token request. Optional |
| `ca_cert` | `FERRISKEY_CA_CERT` | PEM certificate or bundle to trust, for an instance behind a private CA |
| `tls_insecure_skip_verify` | `FERRISKEY_TLS_INSECURE` | Skip certificate verification. Development only |

An argument set in the configuration wins over its environment variable.

`url` is where the API answers, not where the console is. With the Helm chart, the API is served under `api.server.rootPath`, `/api` by default, so `url` is `https://auth.example.com/api`. A local server started on port 3333 with no root path is `http://localhost:3333`.

`realm` is only where the provider signs in. Resources name their own realm, so one provider signed in to `master` can manage every realm of the instance.

## Environment variables

Keep credentials out of `.tf` files. A CI job sets the variables and the provider block only names what is not secret:

```hcl
provider "ferriskey" {
url = "https://auth.example.com/api"
realm = "master"
client_id = "terraform-runner"
}
```

```bash
export FERRISKEY_CLIENT_SECRET='<secret>'
terraform plan
```

All of the provider block can come from the environment, in which case `provider "ferriskey" {}` is enough.

## Several instances

Use an alias per instance, and pass the alias to the resources that belong to it:

```hcl
provider "ferriskey" {
alias = "staging"
url = "https://auth.staging.example.com/api"
realm = "master"
client_id = "terraform-runner"
client_secret = var.staging_runner_secret
}

resource "ferriskey_realm" "acme_staging" {
provider = ferriskey.staging
name = "acme"
}
```

## TLS

For an instance whose certificate comes from a private CA, pass the CA:

```hcl
provider "ferriskey" {
url = "https://auth.internal.example/api"
realm = "master"
ca_cert = file("${path.module}/ca.pem")
}
```

`tls_insecure_skip_verify = true` turns verification off. It sends the client secret to whoever answers, so use it only against a local instance with a self-signed certificate.

## Tokens and retries

The provider requests a token on the first API call, keeps it, and gets a new one shortly before it expires: with the refresh token when it has one, with a new grant otherwise. A `401` makes it drop the token and retry the call once. A long `apply` does not need longer token lifetimes.

Throttled and failing calls are retried up to three times with exponential backoff, honoring `Retry-After`:

| Failure | Retried for |
|---|---|
| `429 Too Many Requests` | Every method |
| `5xx`, connection error | `GET`, `HEAD`, `PUT` and `DELETE` only |

A `POST` or `PATCH` that failed with a `5xx` is not retried, because the change may have been applied. Run `terraform plan` to see where the server stands, then apply again.

Set `TF_LOG=DEBUG` to see token requests and retries in the output.
57 changes: 57 additions & 0 deletions apps/docs/src/content/docs/terraform/default/en/data-sources.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Data sources
description: "Read existing FerrisKey objects from Terraform: a realm and its settings, a role by name, and a realm's OpenID Connect discovery document."
icon: search
order: 5
---

# Data sources

Data sources read objects Terraform does not manage, such as the `master` realm or the roles FerrisKey creates on its own.

## ferriskey_realm

```hcl
data "ferriskey_realm" "master" {
name = "master"
}

output "access_token_lifetime" {
value = data.ferriskey_realm.master.settings.access_token_lifetime
}
```

Takes `name` and returns `realm_id`, `created_at`, `updated_at` and `settings`, the same [realm settings](/en/discover/core-concepts/realms#realm-settings) that `ferriskey_realm_settings` manages.

## ferriskey_role

```hcl
data "ferriskey_role" "realm_admin" {
realm = "master"
name = "master-realm"
}

resource "ferriskey_user_role" "runner_realm_admin" {
realm = "master"
user_id = ferriskey_client.terraform_runner.service_account_user_id
role_id = data.ferriskey_role.realm_admin.id
}
```

Looks a role up by `realm` and `name`, and returns its UUID as `id`, its `description` and its `permissions`. Use it to assign a role that FerrisKey created, such as `master-realm` in the [bootstrap](/en/terraform/getting-started), or one that another state manages.

## ferriskey_openid_configuration

```hcl
data "ferriskey_openid_configuration" "acme" {
realm = "acme"
}

output "token_endpoint" {
value = data.ferriskey_openid_configuration.acme.token_endpoint
}
```

Reads the realm's [discovery document](/en/discover/core-concepts/authentication#openid-connect-discovery), which needs no authentication. It returns `issuer`, `authorization_endpoint`, `token_endpoint`, `userinfo_endpoint`, `jwks_uri`, `introspection_endpoint`, `revocation_endpoint`, `end_session_endpoint`, `grant_types_supported` and `token_endpoint_auth_methods_supported`.

Pass these values to the applications you configure in the same run, for example as environment variables of a Kubernetes deployment, instead of building the URLs by hand.
176 changes: 176 additions & 0 deletions apps/docs/src/content/docs/terraform/default/en/getting-started.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
---
title: Getting started
description: "Bootstrap a fresh FerrisKey instance with Terraform: create a service account with the admin password, then manage everything else with it."
icon: rocket
order: 2
---

# Getting started

A fresh FerrisKey instance has an admin user and nothing else. The credentials Terraform should use day to day, a confidential client with a service account, do not exist yet. The recommended setup therefore runs in two phases, each with its own state:

```text
FerrisKey deployed (admin user from ADMIN_USERNAME / ADMIN_PASSWORD)
└─> phase 1 auth: password grant, admin user
└─> creates the terraform-runner client and gives its service account a role
└─> phase 2 auth: client credentials, terraform-runner
└─> creates realms, clients, users, roles, ...
```

Keep the two phases in **separate states**, in separate directories or backends. If they shared one, a `terraform destroy` of your configuration could delete the client that runs the destroy.

## Prerequisites

- Terraform 1.0 or later
- A running FerrisKey instance. To start one locally, follow the [deployment getting-started](/en/discover/getting-started)
- The admin username and password of the `master` realm
- A public client in `master` that allows the password grant, such as `admin-cli`. To create one, open **Clients**, choose **New client**, set **Client authentication** to **Public** and turn on **Direct access grants** under **Capability config**

The password grant is [deprecated](/en/discover/core-concepts/authentication#password-resource-owner). Phase 1 uses it once, to create a client that does not need it. You can turn **Direct access grants** off again after phase 1.

## Phase 1: the service account

::::step-group
:::step{title="Write the bootstrap configuration"}
```hcl title="01-bootstrap/main.tf"
terraform {
required_providers {
ferriskey = {
source = "ferriskey/ferriskey"
}
}
}

provider "ferriskey" {
url = "https://auth.example.com"
realm = "master"
client_id = "admin-cli"
username = "admin"
# password comes from FERRISKEY_PASSWORD
}

resource "ferriskey_client" "terraform_runner" {
realm = "master"
client_id = "terraform-runner"
name = "Terraform Runner"
client_type = "confidential"
service_account_enabled = true
direct_access_grants_enabled = false
}

data "ferriskey_role" "realm_admin" {
realm = "master"
name = "master-realm"
}

resource "ferriskey_user_role" "runner_realm_admin" {
realm = "master"
user_id = ferriskey_client.terraform_runner.service_account_user_id
role_id = data.ferriskey_role.realm_admin.id
}

output "runner_client_id" {
value = ferriskey_client.terraform_runner.client_id
}

output "runner_client_secret" {
value = ferriskey_client.terraform_runner.secret
sensitive = true
}
```

`service_account_user_id` is the user behind the client's service account. A role given to that user is what the client can do once it authenticates with `client_credentials`. The `master-realm` role carries `manage_realm`, which grants everything. To give less, create a role with only the [permissions](/en/discover/core-concepts/roles#permissions-reference) your configuration needs and assign that one instead.
:::

:::step{title="Apply it"}
```bash
cd 01-bootstrap
export FERRISKEY_PASSWORD='<admin password>'
terraform init
terraform apply
```
:::

:::step{title="Store the secret"}
```bash
terraform output -raw runner_client_secret
```

The secret is also in this phase's state, in clear text, like every value Terraform reads. Store the state in an encrypted backend, and copy the secret to your secrets manager or your CI variables for phase 2.
:::
::::

## Phase 2: everything else

::::step-group
:::step{title="Write the configuration"}
```hcl title="02-resources/main.tf"
terraform {
required_providers {
ferriskey = {
source = "ferriskey/ferriskey"
}
}
}

provider "ferriskey" {
url = "https://auth.example.com"
realm = "master"
client_id = "terraform-runner"
# client_secret comes from FERRISKEY_CLIENT_SECRET
}

resource "ferriskey_realm" "acme" {
name = "acme"
}

resource "ferriskey_realm_settings" "acme" {
realm = ferriskey_realm.acme.name
access_token_lifetime = 900
user_registration_enabled = true
passkey_enabled = true
}

resource "ferriskey_client" "web" {
realm = ferriskey_realm.acme.name
client_id = "acme-web"
name = "Acme Web App"
client_type = "confidential"
redirect_uris = ["https://app.acme.example/callback"]
}
```

`realm` in the provider block is the realm Terraform signs in to, `master` here. It does not restrict where resources live: each resource names its own realm.
:::

:::step{title="Apply it"}
```bash
cd 02-resources
export FERRISKEY_CLIENT_SECRET='<runner secret>'
terraform init
terraform apply
```
:::
::::

Instead of copying the secret by hand, phase 2 can read the outputs of phase 1 with a `terraform_remote_state` data source. Phase 2 only reads that state and never writes it, so the two lifecycles stay apart:

```hcl
data "terraform_remote_state" "bootstrap" {
backend = "local"
config = {
path = "${path.module}/../01-bootstrap/terraform.tfstate"
}
}

provider "ferriskey" {
url = "https://auth.example.com"
realm = "master"
client_id = data.terraform_remote_state.bootstrap.outputs.runner_client_id
client_secret = data.terraform_remote_state.bootstrap.outputs.runner_client_secret
}
```

## Taking over an existing instance

On an instance that was configured by hand, write the resources that match what exists, then [import](/en/terraform/resources) them before the first `apply`. A resource that is not imported is created from scratch, next to or in conflict with the one that already exists. `terraform plan` after the imports shows the differences between your files and the server.
Loading
Loading