For Authlete 2.x documentation, see 2.x version.
Managing OAuth clients with Terraform
Before we dig into the matter, a contextualization is required here as client management on the OAuth/OpenID Connect world has multiple angles and scenarios. The scenarios and use cases for the Authlete provider for Terraform is managing the clients that are structurally required on the Authorization Server. For example: clients that will be used by a web portal or API server, mobile apps of the company domain, or clients that are required to be transposed between environments.Support for Authlete Client
As with the support for services, Authlete provider allows creating, changing, and deleting OAuth clients on services, but as the clients exists within a service, that service needs to be configured either on the provider or in the client configuration, as we will see on this document. To represent an Authlete OAuth client, anauthlete_client Terraform resource is defined and the supported properties of the object can be found on the provider documentation page.
Configuring the provider
For most use cases, where you’re provisioning clients for a single service, the simplest way to configure the provider is by using the environment variablesAUTHLETE_API_KEY and AUTHLETE_API_SECRET. This will specify the service to be used when provisioning the client.
In Authlete 3.0, the AUTHLETE_API_SECRET should be set to an Organization token, which corresponds to the organization where the service exists.
The following examples assume you have a service already created in Authlete. You can use the service from the simple_service example, or create a new service via the Authlete Management Console.
If you are continuing from the simple_service example, retrieve the output from the terraform output command:
Declaring the clients and provisioning
As we have done for service, we start by declaring the dependency to the Authlete Provider and initializing the working env. You can check the section Declaring the dependency on Creating a project from scratch. The source of this example can be found onhttps://github.com/authlete/authlete-terraform-samples under
client_management directory.
After the initialization, create a main.tf file with the content as below:
portal identifier in Terraform and some
properties for a client like those that a Web Portal OIDC client would use.
Note: When using PAR, depending on the timing and the implementation of the authorization server, unused records may remain in request_uris, which could be unintentionally deleted by Terraform. Therefore, in this tutorial, we explicitly ignore request_uris.The third and fourth blocks are declaring output variables that refer to OAuth client attributes. The
sensitive attribute of
the client_secret instructs Terraform to never echo its value in the logs, but make it available when queried.
To create that client, run the command below in the same directory.
After the creation of the client, you can validate the creation in the Management Console and/or query its client_id and
secret via output commands like below:
Changes on Client definition
Now any changes done on the client definition can be applied to Authlete by the provider. Challenged by requirements changes of refresh tokens for the portal client, for instance, you can go ahead and change the client definition to match the content like below:terraform apply command as below:
Note that Terraform has a copy of the state of the client locally and the checking for differences to be changed on the server is done
locally, which means that if you change the client definition on the server, the changes might be overwritten by
Terraform when updating the client.
Configuring service on the client
If you need to manage clients of different Authlete services in the same workspace, you can specify the service’s API key and API secret on the client resource itself. This can be done using references to variables or other resources objects. As an example, we will use the same portal client from previous section and declare it for test and production services and the api key and secret will be references to variables. You can check the source of the example onhttps://github.com/authlete/authlete-terraform-samples under
client_services folder.
The input variables are defined, by convention, in a variables.tf file using a structure as below. Keep in mind that
this variable approach is used extensively on Terraform projects that are multi modules, so it can be a powerful mechanism
for defining very large deployments with multiple components.
service_api_key and service_api_secret
properties’ values are prefixed with a var. to scope the reference to input variables.
localhost:3000 for debugging purpose, while the production
client can redirect only to the production portal.
To run this example, you can define a variables file and provide it on command line when applying, as in the execution shown below: