-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
feat(organizations): supports bearer token #869
Conversation
c52776b
to
eddbbb0
Compare
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") |
There was a problem hiding this comment.
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.
pkg/scim/scim_client.go
Outdated
func (t *bearerTokenTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
req.Header.Set("Authorization", "Bearer "+t.Token) | ||
return t.Next.RoundTrip(req) | ||
} | ||
|
There was a problem hiding this comment.
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.....
eddbbb0
to
b4115b6
Compare
9942ca4
to
a69ba14
Compare
Description
What type of PR is this? (check all applicable)
Related Tickets & Documents
Added tests?
I wrote new unit tests for organization webhook.
Added to documentation?
Checklist