-
Notifications
You must be signed in to change notification settings - Fork 263
feat: Support OIDC Provider OIDC Clients #2147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bigkevmcd
wants to merge
1
commit into
rancher:main
Choose a base branch
from
bigkevmcd:support-oidcclients
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,3 +43,4 @@ destructuring | |
| yaml | ||
| ttl | ||
| pkce | ||
| oidcclients | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --- | ||
| page_title: "rancher2_oidc_client Resource" | ||
| --- | ||
|
|
||
| # rancher2\_oidc_client Resource | ||
|
|
||
| Provides a Rancher OIDC Client. This can be used to configure the OIDC Clients | ||
| available for the Rancher OIDC Provider. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ### Creating a Rancher OIDC Client. | ||
|
|
||
| ```hcl | ||
| resource "rancher2_oidc_client" "oidc-test-client" { | ||
| description = "Access for Test Client" | ||
| token_expiration_seconds = 600 # expiration of the id_token and access_token | ||
| refresh_token_expiration_seconds = 7200 # expiration of the refresh_token | ||
| redirect_uris = [ | ||
| "http://127.0.0.1:5556/auth/rancher/callback", | ||
| "http://127.0.0.1:33418/", | ||
| "https://vscode.dev/redirect" | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ## Argument Reference | ||
|
|
||
| The following arguments are supported: | ||
|
|
||
| * `description` - A human-readable description for the OIDC Client. | ||
| * `token_expiration_seconds` - ID Token and Access Token will only be valid for this many seconds. | ||
| * `refresh_token_expiration_seconds` - How long can the refresh token be used for? | ||
| * `redirect_uris` - Provides a list of allowed redirect URIs for this OIDC Client. | ||
|
bigkevmcd marked this conversation as resolved.
|
||
| * `annotations` - (Optional/Computed) Annotations for OIDC Client object (map) | ||
| * `labels` - (Optional/Computed) Labels for OIDC Client object (map) | ||
|
|
||
| ## Attributes Reference | ||
|
|
||
| The following attributes are exported: | ||
|
|
||
| * `id` - (Computed) The ID of the resource (string) | ||
| * `client_id` - (Computed) The ID to be used when authenticating as this OIDC Client. | ||
|
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| ## Import | ||
|
|
||
| OIDC Clients can be imported using the Client name in the format `<client_name>` | ||
|
|
||
|
bigkevmcd marked this conversation as resolved.
|
||
| ``` | ||
| $ terraform import rancher2_oidc_client.foo <CLIENT_NAME> | ||
|
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
|
||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package rancher2 | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "maps" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
| managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" | ||
| ) | ||
|
|
||
| func resourceRancher2OIDCClient() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Create: resourceRancher2OIDCClientCreate, | ||
| Read: resourceRancher2OIDCClientRead, | ||
| Update: resourceRancher2OIDCClientUpdate, | ||
| Delete: resourceRancher2OIDCClientDelete, | ||
|
bigkevmcd marked this conversation as resolved.
|
||
| Importer: &schema.ResourceImporter{ | ||
| State: resourceRancher2OIDCClientImport, | ||
| }, | ||
|
|
||
| Schema: oidcClientFields(), | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func oidcClientFields() map[string]*schema.Schema { | ||
| s := map[string]*schema.Schema{ | ||
| "token_expiration_seconds": { | ||
| Type: schema.TypeInt, | ||
| Optional: true, | ||
| Computed: true, | ||
| Description: "The duration (in seconds) before an access token and ID token expire.", | ||
| }, | ||
| "refresh_token_expiration_seconds": { | ||
| Type: schema.TypeInt, | ||
| Optional: true, | ||
| Computed: true, | ||
| Description: "The duration (in seconds) a refresh token remains valid before it expires.", | ||
| }, | ||
| "redirect_uris": { | ||
| Description: "List of allowed redirect_uris for this client.", | ||
| Required: true, | ||
| Type: schema.TypeList, | ||
|
bigkevmcd marked this conversation as resolved.
|
||
| Elem: &schema.Schema{ | ||
| Type: schema.TypeString, | ||
| }, | ||
| }, | ||
| "description": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Description: "OIDCClient description", | ||
| }, | ||
| "client_id": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: "The Client ID for OIDC Access.", | ||
| }, | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| maps.Copy(s, commonAnnotationLabelFields()) | ||
|
|
||
| return s | ||
|
bigkevmcd marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func resourceRancher2OIDCClientCreate(d *schema.ResourceData, meta any) error { | ||
| log.Printf("[INFO] Creating OIDCClient") | ||
| oidcClient, err := expandOIDCClient(d) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| client, err := meta.(managementClientGetter).ManagementClient() | ||
| if err != nil { | ||
| return fmt.Errorf("getting a client when creating OIDC Client: %w", err) | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| createdClient, err := client.OIDCClient.Create(oidcClient) | ||
| if err != nil { | ||
| return fmt.Errorf("creating OIDCClient: %w", err) | ||
| } | ||
|
|
||
| d.SetId(createdClient.ID) | ||
|
|
||
| return resourceRancher2OIDCClientRead(d, meta) | ||
| } | ||
|
|
||
| func resourceRancher2OIDCClientRead(d *schema.ResourceData, meta any) error { | ||
| log.Printf("[INFO] Refreshing OIDCClient ID %s", d.Id()) | ||
|
|
||
| client, err := meta.(managementClientGetter).ManagementClient() | ||
| if err != nil { | ||
| return fmt.Errorf("getting a client when reading OIDC Client: %w", err) | ||
| } | ||
| oidcClient, err := client.OIDCClient.ByID(d.Id()) | ||
| if err != nil { | ||
| if IsNotFound(err) || IsForbidden(err) || IsNotAccessibleByID(err) { | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
| return fmt.Errorf("reading OIDC Client %s: %w", d.Id(), err) | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| return flattenOIDCClient(d, oidcClient) | ||
| } | ||
|
|
||
| func resourceRancher2OIDCClientUpdate(d *schema.ResourceData, meta any) error { | ||
| log.Printf("[INFO] Updating OIDCClient ID %s", d.Id()) | ||
|
|
||
| client, err := meta.(managementClientGetter).ManagementClient() | ||
| if err != nil { | ||
| return fmt.Errorf("getting a client when updating OIDC Client: %w", err) | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| oidcClient, err := client.OIDCClient.ByID(d.Id()) | ||
| if err != nil { | ||
| return fmt.Errorf("getting OIDC Client %s for update: %w", d.Id(), err) | ||
| } | ||
|
|
||
| update := map[string]any{ | ||
| "labels": toMapString(d.Get("labels").(map[string]any)), | ||
| "annotations": toMapString(d.Get("annotations").(map[string]any)), | ||
| "description": d.Get("description"), | ||
| "redirectURIs": toArrayString(d.Get("redirect_uris").([]any)), | ||
|
bigkevmcd marked this conversation as resolved.
|
||
| "tokenExpirationSeconds": d.Get("token_expiration_seconds"), | ||
| "refreshTokenExpirationSeconds": d.Get("refresh_token_expiration_seconds"), | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| _, err = client.OIDCClient.Update(oidcClient, update) | ||
| if err != nil { | ||
| return fmt.Errorf("updating OIDC Client %s: %w", d.Id(), err) | ||
| } | ||
|
|
||
| return resourceRancher2OIDCClientRead(d, meta) | ||
| } | ||
|
|
||
| func resourceRancher2OIDCClientDelete(d *schema.ResourceData, meta any) error { | ||
| log.Printf("[INFO] Deleting OIDCClient ID %s", d.Id()) | ||
|
|
||
| client, err := meta.(managementClientGetter).ManagementClient() | ||
| if err != nil { | ||
| return fmt.Errorf("getting a client when deleting OIDC Client: %w", err) | ||
| } | ||
|
|
||
| oidcClient, err := client.OIDCClient.ByID(d.Id()) | ||
| if err != nil { | ||
| if IsNotFound(err) || IsForbidden(err) || IsNotAccessibleByID(err) { | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
| return fmt.Errorf("getting OIDC Client %s for deletion: %w", d.Id(), err) | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| err = client.OIDCClient.Delete(oidcClient) | ||
| if err != nil { | ||
| if !IsNotFound(err) { | ||
| return fmt.Errorf("deleting OIDC Client %s: %w", d.Id(), err) | ||
| } | ||
| } | ||
|
|
||
| d.SetId("") | ||
| return nil | ||
| } | ||
|
|
||
| func resourceRancher2OIDCClientImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
| err := resourceRancher2OIDCClientRead(d, meta) | ||
| if err != nil || d.Id() == "" { | ||
| return []*schema.ResourceData{}, err | ||
| } | ||
|
|
||
| return []*schema.ResourceData{d}, nil | ||
| } | ||
|
bigkevmcd marked this conversation as resolved.
|
||
|
|
||
| type managementClientGetter interface { | ||
| ManagementClient() (*managementClient.Client, error) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.