Skip to content
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

feat(organizations): supports bearer token #869

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

gciezkowski-acc
Copy link
Contributor

@gciezkowski-acc gciezkowski-acc commented Jan 29, 2025

Description

What type of PR is this? (check all applicable)

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Documentation Update
  • 🎨 Style
  • πŸ§‘β€πŸ’» Code Refactor
  • πŸ”₯ Performance Improvements
  • βœ… Test
  • πŸ€– Build
  • πŸ” CI
  • πŸ“¦ Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

Added tests?

  • πŸ‘ yes
  • πŸ™… no, because they aren't needed
  • πŸ™‹ no, because I need help
  • Separate ticket for tests # (issue/pr)

I wrote new unit tests for organization webhook.

Added to documentation?

  • πŸ“œ README.md
  • 🀝 Documentation pages updated
  • πŸ™… no documentation needed
  • (if applicable) generated OpenAPI docs for CRD changes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

@github-actions github-actions bot added feature core-apis helm-charts documentation Improvements or additions to documentation labels Jan 29, 2025
@gciezkowski-acc gciezkowski-acc changed the title feat(orgranizations) supports bearer token feat(orgranizations): supports bearer token Jan 29, 2025
@gciezkowski-acc gciezkowski-acc changed the title feat(orgranizations): supports bearer token feat(organizations): supports bearer token Jan 29, 2025
@gciezkowski-acc gciezkowski-acc force-pushed the feat/805_orgranization_supports_bearer_token branch from c52776b to eddbbb0 Compare January 29, 2025 14:06
@gciezkowski-acc gciezkowski-acc marked this pull request as ready for review January 29, 2025 14:19
@gciezkowski-acc gciezkowski-acc requested a review from a team as a code owner January 29, 2025 14:19
Comment on lines 260 to 294
var authType *scim.AuthType
var basicAuthUser, basicAuthPw, bearerToken string
var err error
if scimConfig.BasicAuthUser.Secret != nil && scimConfig.BasicAuthPw.Secret != nil { // Conditions to avoid panic
basicAuthUser, err = clientutil.GetSecretKeyFromSecretKeyReference(ctx, r.Client, namespace, *scimConfig.BasicAuthUser.Secret)
if err != nil {
return greenhousesapv1alpha1.FalseCondition(greenhousesapv1alpha1.SCIMAPIAvailableCondition, greenhousesapv1alpha1.SecretNotFoundReason, "BasicAuthUser missing")
}
basicAuthPw, err = clientutil.GetSecretKeyFromSecretKeyReference(ctx, r.Client, namespace, *scimConfig.BasicAuthPw.Secret)
if err != nil {
return greenhousesapv1alpha1.FalseCondition(greenhousesapv1alpha1.SCIMAPIAvailableCondition, greenhousesapv1alpha1.SecretNotFoundReason, "BasicAuthPw missing")
}
newAuthType := scim.Basic
authType = &newAuthType
}
basicAuthPw, err := clientutil.GetSecretKeyFromSecretKeyReference(ctx, r.Client, namespace, *scimConfig.BasicAuthPw.Secret)
if err != nil {
return greenhousesapv1alpha1.FalseCondition(greenhousesapv1alpha1.SCIMAPIAvailableCondition, greenhousesapv1alpha1.SecretNotFoundReason, "BasicAuthPw missing")
if scimConfig.BearerToken.Secret != nil {
bearerToken, err = clientutil.GetSecretKeyFromSecretKeyReference(ctx, r.Client, namespace, *scimConfig.BearerToken.Secret)
if err != nil {
return greenhousesapv1alpha1.FalseCondition(greenhousesapv1alpha1.SCIMAPIAvailableCondition, greenhousesapv1alpha1.SecretNotFoundReason, "BearerToken missing")
}
newAuthType := scim.BearerToken
authType = &newAuthType
}
if authType == nil {
return greenhousesapv1alpha1.FalseCondition(greenhousesapv1alpha1.SCIMAPIAvailableCondition, greenhousesapv1alpha1.SCIMConfigNotProvidedReason, "SCIM config is not provided")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to avoid this. The auth type must come from spec and should not be guessed. This way we can switch case between multiple auth types and plus you won't need any validating webhooks.

In order to not break our apis you can do for example in organization_types -

type SCIMConfig struct {
	// URL to the SCIM server.
	BaseURL string `json:"baseURL"`

	// AuthType is the type of authentication to be used.
        // +kubebuilder:validation:Enum=basic;token
	// +kubebuilder:default="basic"
	AuthType string `json:"authType,omitempty"` // You can also make the string as type ScimAuthType string

	// User to be used for basic authentication.
	BasicAuthUser ValueFromSource `json:"basicAuthUser,omitempty"`

	// Password to be used for basic authentication.
	BasicAuthPw ValueFromSource `json:"basicAuthPw,omitempty"`
        .....
}

now you can switch case the authType and error out if the secrets for a particular auth type is not found. For our existing resources in the cluster they will all get basic by default since we supported only basic auth.

Comment on lines 62 to 84
func (t *bearerTokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", "Bearer "+t.Token)
return t.Next.RoundTrip(req)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we extended the transport to include customizable header + prefix as some providers might have custom auth headers - example -

	if t.Header == "" {
		t.Header = "Authorization"
	}
	if t.Prefix == "" {
		t.Prefix = "Bearer"
	}
	req.Header.Set(t.Header, fmt.Sprintf("%s %s", t.Prefix, t.Token))
	return t.Next.RoundTrip(req)

This way if no custom headers or prefix is not provided then we do a default fallback to Authorization: Bearer ey.....

@gciezkowski-acc gciezkowski-acc force-pushed the feat/805_orgranization_supports_bearer_token branch from eddbbb0 to b4115b6 Compare February 7, 2025 12:04
@gciezkowski-acc gciezkowski-acc force-pushed the feat/805_orgranization_supports_bearer_token branch from 9942ca4 to a69ba14 Compare February 10, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core-apis documentation Improvements or additions to documentation feature helm-charts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEAT] - SCIM Access with Bearer Token
3 participants