iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🛡️

Handling ALB OIDC with go-amzn-oidc - fujiwara-ware 2024 Day 14

に公開

This article is for day 14 of the fujiwara-ware advent calendar 2024.

What is go-amzn-oidc

https://github.com/fujiwara/go-amzn-oidc

go-amzn-oidc is a library for validating authentication information sent from an AWS ALB (Application Load Balancer) to a backend when OpenID Connect (OIDC) authentication is performed by the ALB.

When OIDC authentication is performed on an ALB, once authentication succeeds, a JWT is passed to the backend in the x-amzn-oidc-data header. This library/server is for validating whether this JWT is legitimate and extracting the information contained in its claims.

Why I made it

The JWTs sent by ALB after authentication used to be something similar to, yet different from, standard JWTs. While the JWT specification dictates that base64 padding (the trailing =) should not be included, for some reason, ALB was including =. This could not be validated using standard JWT libraries.[1]

Additionally, the public key required for signature verification must be retrieved by accessing the ALB endpoint https://public-keys.auth.elb.{region}.amazonaws.com/{keyid_included_in_jwt} via HTTPS.

Since it is inefficient to retrieve the public key for every ALB request (at the time signature verification is required), it needs to be cached appropriately. I created this library/server with those considerations in mind.

Validating something like a JWT

If you use common JWT libraries as-is for values that are like JWTs but aren't quite JWTs, validation will fail.

However, by chance, golang-jwt/jwt/v4 has an option called DecodePaddingAllowed=true. Enabling this allows validation to pass even if padding is present. Good for us.

Using as a library

go-amzn-oidc can be used as a Go library. Validating the JWT after OIDC authentication and extracting claims is as simple as follows:

import "github.com/fujiwara/go-amzn-oidc/validator"

claims, err := validator.Validate(r.Header.Get("x-amzn-oidc-data"))
if err != nil {
    // Signature verification error
    http.Error(w, "Unauthorized", http.StatusUnauthorized)
    return
}
fmt.Fprintln(w, claims.Email())

Using as a server

go-amzn-oidc can also be started as a standalone server. It starts a server that receives requests from ALB, performs signature verification, and extracts and returns claims.

By using it in combination with nginx's auth_request, you can offload signature verification and claim extraction to go-amzn-oidc.

Applications created by users can perform access control by trusting the headers passed from there. The application does not need to know about the OIDC authentication performed by ALB, and it does not need to be implemented in Go.

# Validate x-amzn-oidc-data with go-amzn-oidc
location = /oidc_validate {
  proxy_pass http://127.0.0.1:8080;
  proxy_set_header X-Amzn-OIDC-Data $http_x_amzn_oidc_data;
  proxy_set_header Content-Length "";
  proxy_pass_request_body off;
  internal;
}

location / {
  # Send request to go-amzn-oidc for validation
  auth_request /oidc_validate;

  # If go-amzn-oidc succeeds in validation, extract claims and set them in the X-Email header
  auth_request_set $email $upstream_http_x_auth_request_email;
  proxy_set_header X-Email $email;
  
  # Actual application processing (proxy_pass, etc.)
}

Summary

go-amzn-oidc is a library for validating authentication information sent from an ALB to the backend when ALB OIDC authentication is performed. It can be used as both a library and a server.

It is very convenient, so please give it a try.

Look forward to tomorrow's post!

脚注
  1. It seems they have become proper JWTs now, but that wasn't the case back then. ↩︎

Discussion