Skip to content

Conversation

@jindraj
Copy link
Contributor

@jindraj jindraj commented May 30, 2025

Notes

The OIDC applications need to be setup in advance

the oidc applications are configured via module's oidc variable - list of objects. Multiple oidc applications can be provided, you can select which provider to authenticate by appending ?auth=PROVIDER_NAME to url

  • suggestion: create s3 object /login.html with links <a href=/?auth=PROVIDER_NAME>PROVIDER_NAME and redirect user there if session is missing. Use can pick which application to use for authentication without need to change URL manually.

changes to the original main module:

new resources:

  • aws_cloudfront_cache_policy.oidc
  • aws_cloudfront_origin_request_policy.oidc

changed blocks:

  • custom_error_response in aws_cloudfront_distribution.this is now conditionaly removed if oidc is enabled, to prevent serving content on error
  • default_cache_behavior in aws_cloudfront_distribution.this conditionally enables forwarding all cookies if oidc is enabled

added blocks:

  • origin for api gateway in aws_cloudfront_distribution.this - dynamically created if oidc is enabled
  • lambda_function_association in aws_cloudfront_distribution.this dynamically created if oidc is enabled for edge lambda handling redirects to oidc provider
  • ordered_cache_behavior in aws_cloudfront_distribution.this - dynamically created if oidc is enabled, configuring /callback route to point to api gateway origin handling with callbacks

Example

  1. Create OIDC app just for the reference, gitlab_application is server wide and not ideal for use e.g. on gitlab.com, there's no support for the group applications
resource "gitlab_application" "fridges_list" {
  name         = "gitlab-app"
  redirect_url = "www.example.com"
  scopes = [
    "openid",
    "read_user",
    "profile",
    "email",
  ]
}
  1. use module
module "static_site" {
  source  = "cookielab/static-site/aws"
  version = "4.9.0"

  providers = {
    aws           = aws
    aws.us_east_1 = aws.vir
  }

  domains        = ["www.exmaple.com"]
  domain_zone_id = "aws_route53_zone.example_com.zone_id"
  s3_bucket_name = "example-com-web"

  gitlab_project_id  = data.gitlab_project.app_fridges_list.id
  gitlab_environment = var.environment

  enable_deploy_role = true
  enable_deploy_user = false

  oidc = [
    { # first oidc provider
      application_name = "gitlab"
      application_id   = gitlab_application.gitlab_application.application_id
      client_secret    = gitlab_application.gitlab_application.secret
      auth_url         = "https://gitlab.com/oauth/authorize"
      token_url        = "https://gitlab.com/oauth/token"
    },
    { # second oidc provider
      application_name = "second"
      application_id   = "second-oidc-app-id"
      client_secret    = "second-oidc-client-secret"
      auth_url         = "https://another-oidc-provicer.example.com/oauth/authorize"
      token_url        = "https://another-oidc-provicer.example.com/oauth/token"
    }
  ]

Then https://www.example.com/?auth=APPLICATION_NAME forces auth with specified provider, e.g.:

This is messy. Perhaps we can get along with a single OIDC auth for the application. Or constructed html file from the oidc list, put it on S3 and redirect if no session cookie is present:

<h1>Choose authentication method</h1>
<ul>
  <li>
    <a href="/?auth=gitlab">Gitlab</a>
  </li>
  <li>
    <a href="/?auth=second">Second</a>
  </li>
</ul>

@joli-sys
Copy link
Contributor

joli-sys commented Jun 2, 2025

LGTM, move OIDC to separate module under aws cloud provider submodule, to have opportunity extend OIDC to different cloud providers in future.

variables.tf Outdated
}

variable "oidc" {
description = "Seznam OIDC providerů"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List

const [value, signature] = session.split('.');
console.log('Edge Lambda - Session cookie parts - Value:', value, 'Signature:', signature);

// Pokud je providerKey přítomen, validujeme jen pro konkrétního providera
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English comments

// Pokud máme session cookie, zkusíme ji validovat
if (session) {
console.log('Edge Lambda - Found session cookie:', session);
// Kontrola formátu session cookie (měla by být ve formátu value.signature)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English comments

console.log('Edge Lambda - No cookie header present or not an array');
}

// Pokud máme session cookie, zkusíme ji validovat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English comments

\# Example
1. Create OIDC app
just for the reference, gitlab_application is server wide and not ideal for use e.g. on gitlab.com, there's no support for the group applications
```tf
resource "gitlab_application" "fridges_list" {
  name         = "gitlab-app"
  redirect_url = "www.example.com"
  scopes = [
    "openid",
    "read_user",
    "profile",
    "email",
  ]
}
```

2. use module
```tf
module "static_site" {
  source  = "cookielab/static-site/aws"
  version = "4.9.0"

  providers = {
    aws           = aws
    aws.us_east_1 = aws.vir
  }

  domains        = ["www.exmaple.com"]
  domain_zone_id = "aws_route53_zone.example_com.zone_id"
  s3_bucket_name = "example-com-web"

  gitlab_project_id  = data.gitlab_project.app_fridges_list.id
  gitlab_environment = var.environment

  enable_deploy_role = true
  enable_deploy_user = false

  oidc = [
    { # first oidc provider
      application_name = "gitlab"
      application_id   = gitlab_application.gitlab_application.application_id
      client_secret    = gitlab_application.gitlab_application.secret
      auth_url         = "https://gitlab.com/oauth/authorize"
      token_url        = "https://gitlab.com/oauth/token"
    },
    { # second oidc provider
      application_name = "second"
      application_id   = "second-oidc-app-id"
      client_secret    = "second-oidc-client-secret"
      auth_url         = "https://another-oidc-provicer.example.com/oauth/authorize"
      token_url        = "https://another-oidc-provicer.example.com/oauth/token"
    }
  ]
```

Then https://www.example.com/?auth=APPLICATION_NAME forces auth with specified provider, e.g.:
- https://www.example.com/?auth=gitlab
- https://www.example.com/?auth=second

This is messy. Perhaps we can get along with a single OIDC auth for the application or if no session cookie is present redirect to a url hosted on s3 bucket with constructed html from the oidc list.
```
<h1>Choose authentication method</h1>
<ul>
  <li>
    <a href=/?auth=gitlab>gitlab</a>
  </li>
  <li>
    <a href=/?auth=second>gitlab</a>
  </li>
</ul>
```

fmt

cleanup

review: update lambda engine version nodejs18.x -> nodejs22.x

replace apigateway with lambda_function_url

update docs + fix duplicate blocks of outdated documentation due to missing BEGIN_TF_DOCS comment

review

parametrize session_duration per provider, fix cookie domain

comment sensitive log messages in lambdas
@jindraj jindraj merged commit 453b063 into main Jun 19, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants