With OpenFaaS Identity and Access Management (IAM) you can secure function endpoints without having to write any additional code. Access permissions for functions can be configured using IAM Roles and Policies.
OpenFaaS Identity and Access Management needs to be enabled and configured in order to use function authentication, see: Identity and Access Management (IAM)
To enable function authentication your functions need to be using a compatible version of the OpenFaaS watchdog. For the classic-watchdog this is any version > 0.2.3. The of-watchdog support function authentication for releases > 0.9.15.
Authentication can be enabled on a per function basis by setting the jwt_auth environment variable to true in the function configuration, e.g:
Existing functions using an older watchdog version and functions without the jwt_auth environment variable are not affected if you start using built-in function authentication. By default all functions can be invoked without authentication.
To invoke a function that has authentication enabled you need to obtain a function access token. Function access token can be obtained by exchanging a valid ID token or OpenFaaS API access token for a function access token through the OpenFaaS Gateways token exchange endpoint.
Function authentication flow from IdP to function invocation.
Obtain an OIDC ID token
Obtain an initial token that can be exchanged for a function access token. This can either be an OIDC ID token from an identity provider registered with OpenFaaS or an OpenFaaS API access token.
This example uses the OAuth2 client credentials flow. This flow can be used for machine to machine interactions and does not need human interaction. Any other OAuth flow or mechanism supported by your identity provider can be used to obtain the initial ID token.
Call the token exchange endpoint to exchange the ID token or OpenFaaS API access token obtained in step 1 for a function access token.
Permission attenuation
The optional audience parameter can be used to reduce the permissions of a function access token so that it can only be used to invoke a single function, or a subset of functions.
The faas-cli invoke command can be used to invoke authenticated functions. You will only be able to invoke authenticated functions if the CLI is authenticated with the gateway, faas-cli pro auth.
echo"OpenFaaS"|faas-cliinvokefiglet
For more info on using the CLI with IAM for OpenFaaS, see: SSO with CLI
Auto-detection for authenticated functions
The CLI detects if a function needs authentication by calling the function a first time without any authorization header. If it receives a 401 response code the response is inspected to check if authentication with a function access token is required. If this is the case the CLI automatically retries the request with an access token.
If you know that a function needs authentication you can provide the optional --auth flag to skip the detection step. The CLI will immediately include the access token in the first call.