Code Monkey home page Code Monkey logo

ingress-nginx-validate-jwt's Introduction

ingress-nginx-validate-jwt

codecov Artifact Hub Docker Hub GitHub

โ— Check out my new project OIDC-Guard, which supports JWT and Cookie Authentication!

What is this?

This project is an API server which is used along with the nginx.ingress.kubernetes.io/auth-url annotation for ingress-nginx and enables per Ingress customizable JWT validation.

Supports AMD64 and ARM64

Install

helm repo add ingress-nginx-validate-jwt https://ivanjosipovic.github.io/ingress-nginx-validate-jwt

helm repo update

helm install ingress-nginx-validate-jwt \
ingress-nginx-validate-jwt/ingress-nginx-validate-jwt \
--create-namespace \
--namespace ingress-nginx-validate-jwt \
--set openIdProviderConfigurationUrl="https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"

Options

  • openIdProviderConfigurationUrl
    • OpenID Provider Configuration Url for your Identity Provider
  • logLevel
    • Logging Level (Trace, Debug, Information, Warning, Error, Critical, and None)
  • Helm Values

Configure Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/auth-url: http://ingress-nginx-validate-jwt.ingress-nginx-validate-jwt.svc.cluster.local:8080/auth?tid=11111111-1111-1111-1111-111111111111&aud=22222222-2222-2222-2222-222222222222&aud=33333333-3333-3333-3333-333333333333
spec:

Parameters

The /auth endpoint supports configurable parameters in the format of {claim}={value}. In the case the same claim is called more than once, the traffic will have to match only one.

For example, using the following query string /auth?
tid=11111111-1111-1111-1111-111111111111
&aud=22222222-2222-2222-2222-222222222222
&aud=33333333-3333-3333-3333-333333333333

Along with validating the JWT token, the token must have a claim tid=11111111-1111-1111-1111-111111111111 and one of aud=22222222-2222-2222-2222-222222222222 or aud=33333333-3333-3333-3333-333333333333

How to query arrays

The /auth endpoint is able to query arrays. We'll use the following JWT token in the example.

{
  "email": "[email protected]",
  "groups": ["admin", "developers"],
}

Using the following query string we can limit this endpoint to only tokens with an admin group /auth?
groups=admin

Inject claims as headers

The /auth endpoint supports a custom parameter called "inject-claim". The value is the name of claim which will be added to the response headers.

For example, using the following query string /auth?
tid=11111111-1111-1111-1111-111111111111
&aud=22222222-2222-2222-2222-222222222222
&inject-claim=email

The /auth response will contain header email=[email protected]

Inject claims as headers with custom name

The value should be in the following format, "{claim name},{header name}".

For example, using the following query string /auth?
tid=11111111-1111-1111-1111-111111111111
&aud=22222222-2222-2222-2222-222222222222
&inject-claim=email,mail

The /auth response will contain header mail=[email protected]

Example Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: app
  annotations:
    nginx.ingress.kubernetes.io/auth-url: http://ingress-nginx-validate-jwt.ingress-nginx-validate-jwt.svc.cluster.local:8080/auth?aud=11111111-11111-1111111111&inject-claim=groups,JWT-Claim-Groups&inject-claim=scope,JWT-Claim-Scope
    nginx.ingress.kubernetes.io/auth-response-headers: JWT-Claim-Groups, JWT-Claim-Scope

Design

alt text

Metrics

Metrics are exposed on :8080/metrics

Metric Name Description
ingress_nginx_validate_jwt_authorized Number of Authorized operations ongoing
ingress_nginx_validate_jwt_unauthorized Number of Unauthorized operations ongoing
ingress_nginx_validate_jwt_duration_seconds Histogram of JWT validation durations

Building locally

cd src/ingress-nginx-validate-jwt
docker build -t ingress-nginx-validate-jwt -f Dockerfile .
docker run ingress-nginx-validate-jwt -e "OpenIdProviderConfigurationUrl=https://login.microsoftonline.com/common/v2.0/.well-known/openid-configuration"

ingress-nginx-validate-jwt's People

Contributors

alexgoris-kasparsolutions avatar ivanjosipovic avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ingress-nginx-validate-jwt's Issues

Extract claims into response headers?

Is there any way to extract the values of JWT claims and set them as response headers, so that the ingress controller can then pass them along to the actual backend using auth_request_set and add_header? With oauth2-proxy you can do this using the --set-xauthrequest flag for basic configuration, or using the injectResponseHeaders.*.values.*.claim alpha configuration for advanced configuration.

I'd love to see similar functionality here, because this tool seems much easier to use when different Ingress objects need different configurations; maybe something along the lines of:

nginx.ingress.kubernetes.io/auth-url: http://ingress-nginx-validate-jwt.ingress-nginx-validate-jwt.svc.cluster.local:8080/auth?tid=11111111-1111-1111-1111-111111111111&aud=22222222-2222-2222-2222-222222222222&aud=33333333-3333-3333-3333-333333333333&inject-claims=user,groups

Docker Image

By any chance would you be able to provider the Dockerfile? (perhaps place it in the root)

Thanks :)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
src/ingress-nginx-validate-jwt/Dockerfile
  • mcr.microsoft.com/dotnet/sdk 8.0-alpine@sha256:e269102a31758d5d6c16f4d11d0e9cf97140551fae8af91fb7e4c861beed5ddf
  • mcr.microsoft.com/dotnet/runtime-deps 8.0-alpine@sha256:90659eefcb847ac52d993f70f65e48db955af435d555d22c5af2368fa94989cd
github-actions
.github/workflows/cicd.yml
  • actions/checkout v4
  • actions/setup-dotnet v4
  • docker/setup-buildx-action v3
  • codecov/codecov-action v4
  • actions/checkout v4
  • cycjimmy/semantic-release-action v3
  • docker/setup-buildx-action v3
  • azure/setup-helm v4
  • helm/chart-releaser-action v1
nuget
benchmarks/ingress-nginx-validate-jwt-benchmarks/ingress-nginx-validate-jwt-benchmarks.csproj
  • BenchmarkDotNet 0.14.0
global.json
  • dotnet-sdk 8.0.401
src/ingress-nginx-validate-jwt/ingress-nginx-validate-jwt.csproj
  • System.Private.Uri 4.3.2
  • System.IdentityModel.Tokens.Jwt 8.0.2
  • prometheus-net.AspNetCore 8.2.1
  • Microsoft.IdentityModel.Protocols.OpenIdConnect 8.0.2
tests/ingress-nginx-validate-jwt-tests/ingress-nginx-validate-jwt-tests.csproj
  • xunit.runner.visualstudio 2.8.2
  • xunit 2.9.0
  • Testcontainers 3.10.0
  • Moq 4.20.71
  • Microsoft.NET.Test.Sdk 17.11.1
  • Microsoft.Identity.Client 4.64.1
  • FluentAssertions 6.12.1
  • coverlet.collector 6.0.2
regex
src/ingress-nginx-validate-jwt/Dockerfile
  • alpine_3_19/ca-certificates-bundle 20240226-r0
  • alpine_3_19/busybox 1.36.1-r19
  • alpine_3_19/busybox-binsh 1.36.1-r19
  • alpine_3_19/libc-utils 0.7.2-r5
  • alpine_3_19/libcrypto3 3.1.7-r0
  • alpine_3_19/ssl_client 1.36.1-r19
  • alpine_3_19/libgcc 13.2.1_git20231014-r0
  • alpine_3_19/libssl3 3.1.7-r0
  • alpine_3_19/libstdc++ 13.2.1_git20231014-r0
  • alpine_3_19/zlib 1.3.1-r0

  • Check this box to trigger a request for Renovate to run again on this repository

Renovate Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
src/ingress-nginx-validate-jwt/Dockerfile
  • mcr.microsoft.com/dotnet/sdk 7.0-alpine@sha256:38f4916cfd8902b3c703f03aac547790621c0adc474f4cc46e21461653d94774
  • mcr.microsoft.com/dotnet/runtime-deps 7.0-alpine@sha256:201d5aa0b8659de4693fe1b75fa69f6137490719dae77bdfcc4c589e10c62305
github-actions
.github/workflows/cicd.yml
  • actions/checkout v4
  • actions/setup-dotnet v3
  • docker/setup-buildx-action v3
  • codecov/codecov-action v3
  • actions/checkout v4
  • cycjimmy/semantic-release-action v3
  • docker/setup-buildx-action v3
  • azure/setup-helm v3
  • helm/chart-releaser-action v1
nuget
benchmarks/ingress-nginx-validate-jwt-benchmarks/ingress-nginx-validate-jwt-benchmarks.csproj
  • BenchmarkDotNet 0.13.9
global.json
  • dotnet-sdk 7.0.401
src/ingress-nginx-validate-jwt/ingress-nginx-validate-jwt.csproj
  • System.Private.Uri 4.3.2
  • System.IdentityModel.Tokens.Jwt 7.0.2
  • prometheus-net.AspNetCore 8.0.1
  • Microsoft.IdentityModel.Protocols.OpenIdConnect 7.0.2
tests/ingress-nginx-validate-jwt-tests/ingress-nginx-validate-jwt-tests.csproj
  • xunit.runner.visualstudio 2.5.1
  • xunit 2.5.1
  • Testcontainers 3.5.0
  • Moq 4.20.69
  • Microsoft.NET.Test.Sdk 17.7.2
  • Microsoft.Identity.Client 4.56.0
  • FluentAssertions 6.12.0
  • coverlet.collector 6.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

custom jwt token header name

When the request comes in via the Google Identity Aware Proxy (IAP), the IAP adds the jwt token using the header name x-goog-iap-jwt-assertion. Can ingress-nginx-validate-jwt add support to read the jwt token from a custom header instead of the default Authorization header?

Here is the doc with sample code. The public key used for validating the token should probably be cached. Based on the discussion here, the key expiration may be infrequent, but IAP engineer suggested refresh the keyfile on lookup failure approach.

The audience should match configured aud parameter.

Inject claim email should work, but the email value may be prefixed with the namespace: if the third party (non-Google) IdP is used. So we probably want to strip. eg:

  "email": "securetoken.google.com/my-gcp-project-123:[email protected]"

guest/anonymous access

One of my applications would like to give authenticated users protected content, and unauthenticated users guest/anonymous content. It will check the special email_header.

Can ingress-nginx-validate-jwt let the request through even if jwt token validation fails?, but to blank out the email_header (to stop client from spoofing the email_header)? That will let backend application know this user is unauthenticated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.