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

Feature/105 add ability to configure cas OIDC authenticator with terraform #106

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e75b44f
#105 add ability to configure cas oidc authonticator with terraform
meiserloh Jan 16, 2025
357ba54
#105 resolve terraform errors
meiserloh Jan 16, 2025
9063c46
#105 resolve terraform errors
meiserloh Jan 16, 2025
3b4e3f0
#105 join scopes list
meiserloh Jan 16, 2025
04eaf61
#105 join other lists
meiserloh Jan 16, 2025
f6f832a
#105 Adding defaults to all cas oidc variables
meiserloh Jan 27, 2025
cdc26ec
#105 Create keycloak module
jelemux Jan 27, 2025
57c4126
#105 Output client id and secret in keycloak module
jelemux Jan 28, 2025
6513d18
#105 Create example for CES with Keycloak and GKE
jelemux Jan 29, 2025
06de8b7
#105 Extend variable template
jelemux Jan 29, 2025
0de6bb9
#105 Document variables
jelemux Jan 29, 2025
e761a8d
#105 Remove provider to migrate from legacy module
jelemux Jan 29, 2025
115ea7b
#105 fix typo
meiserloh Jan 30, 2025
ac66576
#105 changed default value of scopes to reflect the standard naming
meiserloh Jan 30, 2025
f881d5b
#105 add instructions to keycloak module how to set up the keycloak
meiserloh Jan 30, 2025
89d9d24
#105 Simplify configuration and improve docs + rename keycloak module
jelemux Jan 30, 2025
8890e6a
#105 Add lifecycle blocks to ignore changes for randomly generated va…
jelemux Jan 30, 2025
9ac0b51
#105 Move docs to new directory
jelemux Jan 31, 2025
412da88
#105 Update changelog
jelemux Jan 31, 2025
6d78ee5
#105 Simplify keycloak GKE example
jelemux Jan 31, 2025
6369029
#105 Use latest setup version for example
jelemux Jan 31, 2025
856b7e7
#105 use an object for cas oidc configuration
meiserloh Jan 31, 2025
20df39b
#105 Refactor keycloak-client module
jelemux Jan 31, 2025
92369eb
Merge remote-tracking branch 'origin/feature/105-add-ability-to-confi…
jelemux Jan 31, 2025
fdd80cb
#105 use cas oidc config object in keycloak example
meiserloh Jan 31, 2025
a3b4856
#105 Improve default for description
jelemux Jan 31, 2025
2a25b52
#105 remove cas oidc client secret from config object to put it in se…
meiserloh Jan 31, 2025
24209d3
#105 remove quotes from setup.json object template
meiserloh Feb 3, 2025
d73c017
#105 change cas scopes to string
meiserloh Feb 3, 2025
610326e
#105 change cas allowedGroups and initialAdminUsernames to string
meiserloh Feb 3, 2025
69a0022
#105 change cas enabled and optional to string
meiserloh Feb 3, 2025
44b8b58
#105 revert change cas enabled and optional to string, where unnecessary
meiserloh Feb 3, 2025
43a1c9c
#105 introduce local variable to join lists in ces module
meiserloh Feb 5, 2025
7a2b2e7
#105 Fix typo
meiserloh Feb 5, 2025
c0b9bdf
#105 Add Readme description
meiserloh Feb 5, 2025
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- [#105] Terraform module to create Keycloak-Clients for CES delegated authentication
- [#105] Example for using the new `keycloak-client-module` with a CES in GKE
### Changed
- [#105] Extend Terraform CES module to configure CAS delegated authentication

## [v4.1.2] - 2024-12-19
### Added
Expand Down
30 changes: 23 additions & 7 deletions terraform/ces-module/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,36 @@ terraform {
locals {
split_fqdn = split(".", var.ces_fqdn)
# Top Level Domain extracted from fully qualified domain name. k3ces.local is used for development mode and empty fqdn.
topLevelDomain = var.ces_fqdn != "" ? "${element( split(".", var.ces_fqdn), length(local.split_fqdn) - 2)}.${element(local.split_fqdn, length(local.split_fqdn) - 1)}" : "k3ces.local"
topLevelDomain = var.ces_fqdn != "" ? "${element( split(".", var.ces_fqdn), length(local.split_fqdn) - 2)}.${element(local.split_fqdn, length(local.split_fqdn) - 1)}": "k3ces.local"
splitComponentNamespaces = [
for componentStr in var.components :
{
namespace = split("/", componentStr)[0]
rest = split("/", componentStr)[1] //provoke error here, so that the build fails if no namespace or name is given
rest = split("/", componentStr)[1]
//provoke error here, so that the build fails if no namespace or name is given
}
]
parsedComponents = [
for namespaceAndRest in local.splitComponentNamespaces :
{
namespace = namespaceAndRest.namespace
name = split(":", namespaceAndRest.rest)[0]
version = length(split(":", namespaceAndRest.rest)) == 2 ? split(":", namespaceAndRest.rest)[1] : "latest"
namespace = namespaceAndRest.namespace
name = split(":", namespaceAndRest.rest)[0]
version = length(split(":", namespaceAndRest.rest)) == 2 ? split(":", namespaceAndRest.rest)[1] : "latest"
deployNamespace = split(":", namespaceAndRest.rest)[0] != "k8s-longhorn" ? var.ces_namespace : "longhorn-system"
}
]
cas_oidc_config_formatted = {
enabled = var.cas_oidc_config.enabled
discovery_uri = var.cas_oidc_config.discovery_uri
client_id = var.cas_oidc_config.client_id
display_name = var.cas_oidc_config.display_name
optional = var.cas_oidc_config.optional
scopes = join(" ", var.cas_oidc_config.scopes)
principal_attribute = var.cas_oidc_config.principal_attribute
attribute_mapping = var.cas_oidc_config.attribute_mapping
allowed_groups = join(", ", var.cas_oidc_config.allowed_groups)
initial_admin_usernames = join(", ", var.cas_oidc_config.initial_admin_usernames)
}
}

resource "helm_release" "k8s-ces-setup" {
Expand Down Expand Up @@ -61,7 +74,7 @@ resource "helm_release" "k8s-ces-setup" {
"component_operator_chart" = var.component_operator_chart
"component_operator_crd_chart" = var.component_operator_crd_chart
"components" = local.parsedComponents
"setup_json" = yamlencode(templatefile(
"setup_json" = yamlencode(templatefile(
"${path.module}/setup.json.tftpl",
{
# https://docs.cloudogu.com/en/docs/system-components/ces-setup/operations/setup-json/
Expand All @@ -74,7 +87,10 @@ resource "helm_release" "k8s-ces-setup" {
"domain" = local.topLevelDomain
"certificateType" = var.ces_certificate_path == null ? "selfsigned" : "external"
"certificate" = var.ces_certificate_path != null ? replace(file(var.ces_certificate_path), "\n", "\\n") : ""
"certificateKey" = var.ces_certificate_key_path != null ? replace(file(var.ces_certificate_key_path), "\n", "\\n") : ""
"certificateKey" = var.ces_certificate_key_path != null ? replace(file(var.ces_certificate_key_path), "\n", "\\n") : ""

"cas_oidc_config" = jsonencode(local.cas_oidc_config_formatted)
"cas_oidc_client_secret" = var.cas_oidc_client_secret
}
))
"resource_patches" = var.resource_patches
Expand Down
12 changes: 12 additions & 0 deletions terraform/ces-module/setup.json.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,17 @@
"groupAttributeName": "",
"groupAttributeDescription": "",
"groupAttributeMember": ""
},
"registryConfig": {
"cas": {
"oidc": ${cas_oidc_config}
}
},
"registryConfigEncrypted": {
"cas": {
"oidc": {
"client_secret": "${cas_oidc_client_secret}"
}
}
}
}
35 changes: 35 additions & 0 deletions terraform/ces-module/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,39 @@ variable "is_setup_applied_matching_resource" {
api = "apiextensions.k8s.io/v1",
field_selector = "metadata.name==dogus.k8s.cloudogu.com"
}
}

variable "cas_oidc_config" {
description = "Configuration of an external cas oidc authenticator. For more information [see here](https://docs.cloudogu.com/en/docs/dogus/cas/operations/Configure_OIDC_Provider/)"
type = object({
enabled = string
discovery_uri = string
client_id = string
display_name = string
optional = string
scopes = list(string)
attribute_mapping = string
principal_attribute = string
allowed_groups = list(string)
initial_admin_usernames = list(string)
})
default = {
enabled = false
discovery_uri = ""
client_id = ""
display_name = "CAS oidc provider"
optional = false
scopes = ["openid", "email", "profile", "groups"]
attribute_mapping = "email:mail,family_name:surname,given_name:givenName,preferred_username:username,name:displayName,groups:externalGroups"
principal_attribute = "preferred_username"
allowed_groups = []
initial_admin_usernames = []
}
}

variable "cas_oidc_client_secret" {
description = "Contains the secret to be used together with the client ID to identify the CAS to the OIDC provider. Encrypted."
type = string
sensitive = true
default = ""
}
40 changes: 40 additions & 0 deletions terraform/examples/ces_keycloak_gke/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Cloudogu template files
!terraform.tfvars.template

# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log
crash.*.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json
*.gcs.tfbackend

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# Ignore CLI configuration files
.terraformrc
terraform.rc

secrets/**
107 changes: 107 additions & 0 deletions terraform/examples/ces_keycloak_gke/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions terraform/examples/ces_keycloak_gke/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# CES Keycloak GKE example

This is an example how to use the keycloak client module to configure delegated authentication
in a Cloudogu EcoSystem running on the Google Kubernetes Engine.

## Usage

### Secret configuration (IAM - service account)

List available gcloud projects.

`gcloud projects list`

Set variables.

```bash
PROJECT_ID=<insert_your_project_name>
SERVICE_ACCOUNT_NAME=<insert_your_sa_name>
```

Ensure you are in the correct project.

`gcloud config set project $PROJECT_ID`

You need to create a service account for the Google provider.

`gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME --description="DESCRIPTION" --display-name="$SERVICE_ACCOUNT_NAME" --project=$PROJECT_ID`

And assign the necessary Roles (only one role can be added each time with this command (see [here](https://www.googlecloudcommunity.com/gc/Developer-Tools/multiple-role-for-gcloud-iam-service-accounts-add-iam-policy/m-p/686863)))

`gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/editor"`
`gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com" --role="roles/container.serviceAgent"`

Get that service account and save it to `secrets/gcp_sa.json`:

`gcloud iam service-accounts keys create secrets/gcp_sa.json --iam-account=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com`

### General configuration
Use the `terraform.tfvars.template` file to create `terraform.tfvars` and set your GCP project and cluster name in it.

If you wish for example to create the cluster in another region you should template `terraform.tfvars.template`.
See `variables.tf` for possibilities.

Use the `secretVars.tfvars.template` file to create `secretVars.tfvars` and set sensible information like passwords in it.

If you wish to know more about how to use the keycloak-module, have a look at its [Readme](../../keycloak-client-module).

### Create cluster

Init with `terraform init -upgrade`

Check plan
`terraform plan -var-file=secretVars.tfvars`

Apply with
`terraform apply -var-file=secretVars.tfvars`

This takes up to 15 minutes.

### Get kubeconfig

```
gcloud container clusters get-credentials <cluster_name> --zone europe-west3-c --project $PROJECT_ID
```

### Delete cluster

```
terraform destroy -var-file=secretVars.tfvars
```
Loading