diff --git a/README.md b/README.md index 313d018..c3fde13 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Common issues and solutions: | Name | Version | |------|---------| | [helm](#provider\_helm) | 2.17.0 | -| [keycloak](#provider\_keycloak) | >= 5.0.0 | +| [keycloak](#provider\_keycloak) | 5.1.1 | | [kubernetes](#provider\_kubernetes) | 2.35.1 | ## Modules @@ -90,42 +90,41 @@ Common issues and solutions: | [keycloak_openid_client.client](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/openid_client) | resource | | [keycloak_realm.realm](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/realm) | resource | | [keycloak_role.developer_role](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/role) | resource | -| [keycloak_user.admin_user](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/user) | resource | -| [keycloak_user_roles.admin_roles](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/user_roles) | resource | -| [kubernetes_secret.keycloak_admin_creds](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret) | resource | +| [keycloak_user.user](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/user) | resource | +| [keycloak_user_roles.user_roles](https://registry.terraform.io/providers/keycloak/keycloak/latest/docs/resources/user_roles) | resource | +| [kubernetes_secret.keycloak_realm_creds](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret) | resource | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| [admin\_email](#input\_admin\_email) | Email for the realm admin user | `string` | `"admin@example.com"` | no | -| [admin\_password](#input\_admin\_password) | Password for the realm admin user | `string` | `""` | no | -| [admin\_username](#input\_admin\_username) | Username for the realm admin user | `string` | `"admin"` | no | | [base\_url](#input\_base\_url) | Base URL for the OpenID client | `string` | `""` | no | | [client\_id](#input\_client\_id) | Client ID for the OpenID client | `string` | `"app-client"` | no | | [client\_secret](#input\_client\_secret) | Client secret for the OpenID client | `string` | `""` | no | | [cluster\_domain](#input\_cluster\_domain) | The domain name used to reference ingresses for the cluster. | `string` | n/a | yes | | [cluster\_name](#input\_cluster\_name) | EKS cluster name name component used through out the EKS cluster describing its purpose (ex: dice-dev) | `string` | n/a | yes | | [default\_storage\_class](#input\_default\_storage\_class) | The default storage class to use for persistent volumes | `string` | `"gp3-encrypted"` | no | -| [keycloak\_admin\_secret](#input\_keycloak\_admin\_secret) | Secret key for Keycloak admin client | `string` | `""` | no | -| [keycloak\_database](#input\_keycloak\_database) | The name of the database to create in the RDS instance. | `string` | `"bitnami_keycloak"` | no | -| [keycloak\_hostname](#input\_keycloak\_hostname) | The hostname for Keycloak without the domain suffix | `string` | `"keycloak-service"` | no | -| [keycloak\_password](#input\_keycloak\_password) | The initial password for the database. | `string` | `"bn_keycloak"` | no | +| [keycloak\_database](#input\_keycloak\_database) | The name of the database to create in the RDS instance. | `string` | `"keycloak"` | no | +| [keycloak\_password](#input\_keycloak\_password) | The initial password for the database. | `string` | `"keycloak"` | no | | [keycloak\_tag](#input\_keycloak\_tag) | The image tag associated with the keycloak\_chart\_version | `string` | `"22.0.1-debian-11-r30"` | no | -| [keycloak\_user](#input\_keycloak\_user) | The username for the database. | `string` | `"bn_keycloak"` | no | +| [keycloak\_user](#input\_keycloak\_user) | The username for the database. | `string` | `"keycloak"` | no | | [namespace](#input\_namespace) | The namespace which will be created and into which keycloak will be deployed. | `string` | `"keycloak"` | no | | [postgresql\_tag](#input\_postgresql\_tag) | The tag of the postgresql image to use. | `string` | `"17.4.0"` | no | | [profile](#input\_profile) | AWS config profile used to upload images into ECR | `string` | `""` | no | +| [realm\_email](#input\_realm\_email) | Email for the realm user | `string` | `"admin@example.com"` | no | | [realm\_name](#input\_realm\_name) | Name of the Keycloak realm to create | `string` | `"default"` | no | +| [realm\_password](#input\_realm\_password) | Password for the realm user | `string` | `""` | no | +| [realm\_user](#input\_realm\_user) | Username for the realm admin user | `string` | `"user"` | no | | [redirect\_uris](#input\_redirect\_uris) | Valid redirect URIs for the OpenID client | `list(string)` |
[| no | | [root\_url](#input\_root\_url) | Root URL for the OpenID client | `string` | `""` | no | -| [service\_name](#input\_service\_name) | The name of the service to be deployed in the EKS cluster. | `string` | `"keycloak-service"` | no | +| [service\_name](#input\_service\_name) | The name of the service to be deployed in the EKS cluster. | `string` | `"service"` | no | | [telemetry\_namespace](#input\_telemetry\_namespace) | The namespace used for telemetry. | `string` | `"namespace"` | no | ## Outputs | Name | Description | |------|-------------| +| [admin\_internal\_endpoint](#output\_admin\_internal\_endpoint) | internal endpoint for admin | | [client\_id](#output\_client\_id) | The client ID for accessing keycloak. | | [client\_secret](#output\_client\_secret) | The client secret for accessing keycloak. | | [discovery\_url](#output\_discovery\_url) | The URL for discovering keycloak services. | diff --git a/kc_bootstrap.tf b/kc_bootstrap.tf index fd03ca1..60e4861 100644 --- a/kc_bootstrap.tf +++ b/kc_bootstrap.tf @@ -1,9 +1,8 @@ # Configure provider provider "keycloak" { - depends_on = [module.ingress_resources] - client_id = "admin-cli" - client_secret = var.keycloak_admin_secret - url = "https://${var.keycloak_hostname}.${var.cluster_domain}/auth" + client_id = var.keycloak_user + client_secret = var.keycloak_password + url = "https://${module.ingress_resources.service_url}/auth" } # Create realm @@ -23,24 +22,24 @@ resource "keycloak_role" "developer_role" { } # Create admin user -resource "keycloak_user" "admin_user" { +resource "keycloak_user" "user" { depends_on = [module.ingress_resources] realm_id = keycloak_realm.realm.id - username = var.admin_username - email = var.admin_email + username = var.realm_user + email = var.realm_email enabled = true initial_password { - value = var.admin_password + value = var.realm_password temporary = false } } # Assign role to user -resource "keycloak_user_roles" "admin_roles" { +resource "keycloak_user_roles" "user_roles" { depends_on = [module.ingress_resources] realm_id = keycloak_realm.realm.id - user_id = keycloak_user.admin_user.id + user_id = keycloak_user.user.id role_ids = [ keycloak_role.developer_role.id @@ -59,3 +58,20 @@ resource "keycloak_openid_client" "client" { root_url = var.root_url base_url = var.base_url } + +# Create secret with realm creds +resource "kubernetes_secret" "keycloak_realm_creds" { + metadata { + name = "keycloak-${var.realm_name}-creds" + namespace = var.namespace + } + + data = { + username = var.realm_user + password = var.realm_password + client-id = var.client_id + client-secret = var.client_secret + realm-name = var.realm_name + } + +} diff --git a/main.tf b/main.tf index f67b7ed..c3f7417 100644 --- a/main.tf +++ b/main.tf @@ -1,6 +1,7 @@ # main.tf locals { - internal_port_number = "8080" + internal_port_number = "8080" + admin_internal_port_number = "9000" } resource "helm_release" "keycloak" { @@ -13,19 +14,19 @@ resource "helm_release" "keycloak" { namespace = var.namespace values = [templatefile("${path.module}/keycloak-values.yaml.tmpl", { + cluster_name = var.cluster_name, + default_storage_class = var.default_storage_class, image_registry = module.images.images[local.keycloak_key].dest_registry, image_repository = module.images.images[local.keycloak_key].dest_repository, image_tag = module.images.images[local.keycloak_key].tag, - default_storage_class = var.default_storage_class, - serviceDnsName = format("https://%s.%s", var.service_name, var.cluster_domain), - telemetry_namespace = var.telemetry_namespace, + keycloak_database = var.keycloak_database, + keycloak_password = var.keycloak_password, + keycloak_user = var.keycloak_user, postgresql_image_registry = module.images.images[local.postgresql_key].dest_registry, postgresql_image_repository = module.images.images[local.postgresql_key].dest_repository, postgresql_image_tag = module.images.images[local.postgresql_key].tag, - keycloak_database = var.keycloak_database, - keycloak_user = var.keycloak_user, - keycloak_password = var.keycloak_password, - cluster_name = var.cluster_name, + serviceDnsName = format("https://%s.%s", var.service_name, var.cluster_domain), + telemetry_namespace = var.telemetry_namespace, })] depends_on = [ @@ -33,22 +34,6 @@ resource "helm_release" "keycloak" { ] } -resource "kubernetes_secret" "keycloak_admin_creds" { - metadata { - name = "keycloak-admin-creds" - namespace = var.namespace - } - - data = { - "keycloak-username" = var.keycloak_user - "keycloak-password" = var.keycloak_password - } - - depends_on = [ - helm_release.keycloak - ] -} - module "ingress_resources" { depends_on = [helm_release.keycloak] # tflint-ignore: terraform_module_pinned_source diff --git a/outputs.tf b/outputs.tf index 416c173..0d8f42c 100644 --- a/outputs.tf +++ b/outputs.tf @@ -3,6 +3,15 @@ output "public_endpoint" { value = module.ingress_resources.service_url } +output "admin_internal_endpoint" { + description = "internal endpoint for admin" + value = { + hostname = format("%v.%v.svc.cluster.local", var.service_name, var.namespace) + port_number = local.admin_internal_port_number + url = format("http://%v:%v", var.service_name, local.admin_internal_port_number) + } +} + output "realm_id" { value = keycloak_realm.realm.id } diff --git a/variables.tf b/variables.tf index 96caf71..7c5091c 100644 --- a/variables.tf +++ b/variables.tf @@ -29,19 +29,19 @@ variable "cluster_domain" { variable "keycloak_database" { description = "The name of the database to create in the RDS instance." type = string - default = "bitnami_keycloak" + default = "keycloak" } variable "keycloak_user" { description = "The username for the database." type = string - default = "bn_keycloak" + default = "keycloak" } variable "keycloak_password" { description = "The initial password for the database." type = string - default = "bn_keycloak" + default = "keycloak" } variable "default_storage_class" { @@ -53,7 +53,7 @@ variable "default_storage_class" { variable "service_name" { description = "The name of the service to be deployed in the EKS cluster." type = string - default = "keycloak-service" + default = "service" } variable "telemetry_namespace" { @@ -68,39 +68,26 @@ variable "postgresql_tag" { default = "17.4.0" } -variable "keycloak_admin_secret" { - description = "Secret key for Keycloak admin client" - type = string - default = "" - sensitive = true -} - -variable "keycloak_hostname" { - description = "The hostname for Keycloak without the domain suffix" - type = string - default = "keycloak-service" -} - variable "realm_name" { description = "Name of the Keycloak realm to create" type = string default = "default" } -variable "admin_username" { +variable "realm_user" { description = "Username for the realm admin user" type = string - default = "admin" + default = "user" } -variable "admin_email" { - description = "Email for the realm admin user" +variable "realm_email" { + description = "Email for the realm user" type = string default = "admin@example.com" } -variable "admin_password" { - description = "Password for the realm admin user" +variable "realm_password" { + description = "Password for the realm user" type = string default = "" sensitive = true
"*"
]