-
Notifications
You must be signed in to change notification settings - Fork 29
Automation for SCIM endpoints #570
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
dasarinaidu
wants to merge
1
commit into
rancher:main
Choose a base branch
from
dasarinaidu:scim-card-validations-with-openldap
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
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,164 @@ | ||
| package scim | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "fmt" | ||
|
|
||
| "github.com/rancher/shepherd/clients/rancher" | ||
| scimclient "github.com/rancher/shepherd/clients/rancher/auth/scim" | ||
| "github.com/rancher/shepherd/extensions/defaults" | ||
| "github.com/rancher/shepherd/pkg/clientbase" | ||
| "github.com/rancher/tests/actions/auth" | ||
| "github.com/rancher/tests/actions/features" | ||
| "github.com/sirupsen/logrus" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| kwait "k8s.io/apimachinery/pkg/util/wait" | ||
| ) | ||
|
|
||
| const ( | ||
| SCIMFeatureFlag = "scim" | ||
| SCIMSecretNamespace = "cattle-global-data" | ||
| SCIMSecretDataKey = "token" | ||
|
|
||
| scimSecretKindLabel = "cattle.io/kind" | ||
| scimSecretKindValue = "scim-auth-token" | ||
| scimAuthProviderLabel = "authn.management.cattle.io/provider" | ||
| ) | ||
|
|
||
| var errNoSCIMTokenSecret = fmt.Errorf("no SCIM token secret found") | ||
|
|
||
| // FetchSCIMBearerToken retrieves the SCIM bearer token for the given auth provider from the cattle-global-data namespace | ||
| func FetchSCIMBearerToken(client *rancher.Client, providerName string) (string, error) { | ||
| logrus.Infof("Fetching SCIM bearer token from %s by label %s=%s", | ||
| SCIMSecretNamespace, scimAuthProviderLabel, providerName) | ||
| selector := labels.SelectorFromSet(labels.Set{ | ||
| scimSecretKindLabel: scimSecretKindValue, | ||
| scimAuthProviderLabel: providerName, | ||
| }) | ||
| list, err := client.WranglerContext.Core.Secret().List( | ||
| SCIMSecretNamespace, | ||
| metav1.ListOptions{LabelSelector: selector.String()}, | ||
| ) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if len(list.Items) == 0 { | ||
| return "", errNoSCIMTokenSecret | ||
| } | ||
| newest := &list.Items[0] | ||
| for i := 1; i < len(list.Items); i++ { | ||
| if list.Items[i].CreationTimestamp.After(newest.CreationTimestamp.Time) { | ||
| newest = &list.Items[i] | ||
| } | ||
| } | ||
| if len(list.Items) > 1 { | ||
| logrus.Warnf("Multiple SCIM token secrets found for provider %s, using newest: %s", providerName, newest.Name) | ||
| } | ||
| token, ok := newest.Data[SCIMSecretDataKey] | ||
| if !ok || len(token) == 0 { | ||
| return "", fmt.Errorf("key %q not found or empty in secret %s/%s", | ||
| SCIMSecretDataKey, SCIMSecretNamespace, newest.Name) | ||
| } | ||
| logrus.Infof("Found SCIM token in secret %s/%s", SCIMSecretNamespace, newest.Name) | ||
| return string(token), nil | ||
| } | ||
|
|
||
| // CreateSCIMTokenSecret generates a new random bearer token and stores it as a Kubernetes secret in the cattle-global-data namespace | ||
| func CreateSCIMTokenSecret(client *rancher.Client, providerName string) (string, error) { | ||
| b := make([]byte, 32) | ||
| if _, err := rand.Read(b); err != nil { | ||
| return "", err | ||
| } | ||
| token := hex.EncodeToString(b) | ||
|
|
||
| secretName := fmt.Sprintf("scim-token-%s", providerName) | ||
| secret := &corev1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: secretName, | ||
| Namespace: SCIMSecretNamespace, | ||
| Labels: map[string]string{ | ||
| scimSecretKindLabel: scimSecretKindValue, | ||
| scimAuthProviderLabel: providerName, | ||
| }, | ||
| }, | ||
| Data: map[string][]byte{ | ||
| SCIMSecretDataKey: []byte(token), | ||
| }, | ||
| } | ||
|
|
||
| logrus.Infof("Creating SCIM token secret %s/%s", SCIMSecretNamespace, secretName) | ||
| _, err := client.WranglerContext.Core.Secret().Create(secret) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return token, nil | ||
| } | ||
|
|
||
| // SetupSCIMClient ensures the SCIM feature flag is enabled and the auth provider | ||
| // is active, then fetches or creates a bearer token secret and returns a configured | ||
| // SCIM client ready to make requests against the given provider. | ||
| func SetupSCIMClient(client *rancher.Client, providerName string) (*scimclient.Client, error) { | ||
| enabled, err := features.IsEnabled(client, SCIMFeatureFlag) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if !enabled { | ||
| if err = features.UpdateFeatureFlag(client, SCIMFeatureFlag, true); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| if err = auth.EnsureAuthProviderEnabled(client, providerName); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| token, err := FetchSCIMBearerToken(client, providerName) | ||
| if err != nil { | ||
| if err != errNoSCIMTokenSecret { | ||
| return nil, err | ||
| } | ||
| logrus.Infof("No SCIM token secret found for provider %s, creating one", providerName) | ||
| token, err = CreateSCIMTokenSecret(client, providerName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return scimclient.NewClient(&clientbase.ClientOpts{ | ||
| URL: fmt.Sprintf("https://%s", client.RancherConfig.Host), | ||
| TokenKey: token, | ||
| Insecure: true, | ||
| }, providerName), nil | ||
| } | ||
|
|
||
| // NewSCIMClientWithToken returns a SCIM client configured with the given host, provider, and bearer token | ||
| func NewSCIMClientWithToken(host, providerName, token string) *scimclient.Client { | ||
| return scimclient.NewClient(&clientbase.ClientOpts{ | ||
| URL: fmt.Sprintf("https://%s", host), | ||
| TokenKey: token, | ||
| Insecure: true, | ||
| }, providerName) | ||
| } | ||
|
|
||
|
|
||
| // WaitForSCIMResourceDeletion polls a SCIM GET endpoint until it returns 404. | ||
| func WaitForSCIMResourceDeletion(getFunc func() (int, error)) error { | ||
| return kwait.PollUntilContextTimeout( | ||
| context.Background(), | ||
| defaults.FiveSecondTimeout, | ||
| defaults.OneMinuteTimeout, | ||
| false, | ||
| func(ctx context.Context) (bool, error) { | ||
| status, err := getFunc() | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return status == 404, nil | ||
| }, | ||
| ) | ||
| } | ||
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
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
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,44 @@ | ||
| # SCIM Test Suite | ||
|
|
||
| This package contains Golang automation tests for Rancher's SCIM 2.0 integration. | ||
|
|
||
| ## Pre-requisites | ||
|
|
||
| - Ensure you have an existing cluster that the user has access to. If you do not have a downstream cluster in Rancher, create one first before running this test. | ||
| - Ensure OpenLDAP is configured in your Rancher instance. For full OpenLDAP configuration details see the [OpenLDAP auth provider README](../provider/openldap/README.md). | ||
|
|
||
| ## Test Setup | ||
|
|
||
| Your GO suite should be set to `-run ^Test<TestSuite>$` | ||
|
|
||
| - To run the scim_openldap_test.go, set the GO suite to `-run ^TestSCIMOpenLDAPSuite$` | ||
|
|
||
| In your config file, set the following: | ||
|
|
||
| ```yaml | ||
| rancher: | ||
| host: "rancher_server_address" | ||
| adminToken: "rancher_admin_token" | ||
| insecure: true | ||
| clusterName: "downstream_cluster_name" | ||
|
|
||
| openLDAP: | ||
| hostname: "open_ldap_host" | ||
| serviceAccount: | ||
| distinguishedName: "cn=admin,dc=qa,dc=example,dc=com" | ||
| password: "<service_account_password>" | ||
| users: | ||
| searchBase: "ou=users,dc=qa,dc=example,dc=com" | ||
| admin: | ||
| username: "<admin_username>" | ||
| password: "<admin_password>" | ||
| groups: | ||
| searchBase: "ou=groups,dc=qa,dc=example,dc=com" | ||
|
|
||
| openLdapAuthInput: | ||
| users: | ||
| - username: "<username1>" | ||
| password: "<password1>" | ||
| - username: "<username2>" | ||
| password: "<password2>" | ||
| ``` |
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.