Skip to content

Commit

Permalink
Add providerClusterName in authorize url
Browse files Browse the repository at this point in the history
Signed-off-by: rasel <[email protected]>
  • Loading branch information
Superm4n97 committed Feb 28, 2024
1 parent efe42b7 commit e0f353b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 17 deletions.
8 changes: 0 additions & 8 deletions apis/kubebind/v1alpha1/bindingprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ import (
type BindingProvider struct {
metav1.TypeMeta `json:",inline"`

// providerClusterName is the name of the provider that is displayed to the user, e.g:
// MangoDB Inc.
//
// +required
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
ProviderClusterName string `json:"providerClusterName"`

// version is the kube-bind.appscode.com version of the provider. The kubectl bind will check
// this for compatibility.
//
Expand Down
3 changes: 1 addition & 2 deletions contrib/example-backend/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ func (h *handler) handleServiceExport(w http.ResponseWriter, r *http.Request) {
APIVersion: v1alpha1.GroupVersion,
Kind: "BindingProvider",
},
Version: ver,
ProviderPrettyName: "example-backend",
Version: ver,
AuthenticationMethods: []v1alpha1.AuthenticationMethod{
{
Method: "OAuth2CodeGrant",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/klog/v2"
)

func CreateClusterBinding(ctx context.Context, client bindclient.Interface, ns, secretName, providerPrettyName string) error {
func CreateClusterBinding(ctx context.Context, client bindclient.Interface, ns, secretName, clusterName string) error {
logger := klog.FromContext(ctx)

clusterBinding := &v1alpha1.ClusterBinding{
Expand All @@ -35,7 +35,7 @@ func CreateClusterBinding(ctx context.Context, client bindclient.Interface, ns,
Namespace: ns,
},
Spec: v1alpha1.ClusterBindingSpec{
ProviderPrettyName: providerPrettyName,
ProviderClusterName: clusterName,
KubeconfigSecretRef: v1alpha1.LocalSecretKeyRef{
Name: secretName,
Key: "kubeconfig",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r *reconciler) reconcile(ctx context.Context, binding *v1alpha1.APIService
errs = append(errs, err)
}

if err := r.ensurePrettyName(ctx, binding); err != nil {
if err := r.ensureClusterName(ctx, binding); err != nil {
errs = append(errs, err)
}

Expand Down Expand Up @@ -197,7 +197,7 @@ func (r *reconciler) ensureCRDs(ctx context.Context, binding *v1alpha1.APIServic
return utilerrors.NewAggregate(errs)
}

func (r *reconciler) ensurePrettyName(ctx context.Context, binding *v1alpha1.APIServiceBinding) error {
func (r *reconciler) ensureClusterName(ctx context.Context, binding *v1alpha1.APIServiceBinding) error {
binding.Status.Providers = []v1alpha1.Provider{}
for _, provider := range r.providerInfos {
clusterBinding, err := r.getClusterBinding(ctx, provider)
Expand All @@ -211,7 +211,10 @@ func (r *reconciler) ensurePrettyName(ctx context.Context, binding *v1alpha1.API
LocalSecretKeyRef: clusterBinding.Spec.KubeconfigSecretRef,
Namespace: clusterBinding.Namespace,
}
prov.PrettyName = clusterBinding.Spec.ProviderPrettyName
if clusterBinding.Status.Provider != nil {
prov.ClusterIdentity.ClusterName = clusterBinding.Spec.ProviderClusterName
prov.ClusterIdentity.ClusterUID = clusterBinding.Status.Provider.ClusterUID
}
binding.Status.Providers = append(binding.Status.Providers, prov)
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/kubectl/bind/plugin/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
clientgoversion "k8s.io/client-go/pkg/version"
)

// getProvider calls for /export url and returns BindingProvider which contains the oidc authentication method
func getProvider(url string) (*kubebindv1alpha1.BindingProvider, error) {
resp, err := http.Get(url)
if err != nil {
Expand Down Expand Up @@ -90,7 +91,7 @@ func validateProviderVersion(providerVersion string) error {
return nil
}

func (b *BindOptions) authenticate(provider *kubebindv1alpha1.BindingProvider, callback, sessionID, clusterID string, urlCh chan<- string) error {
func (b *BindOptions) authenticate(provider *kubebindv1alpha1.BindingProvider, callback, sessionID, clusterID, clusterName string, urlCh chan<- string) error {
var oauth2Method *kubebindv1alpha1.OAuth2CodeGrant
for _, m := range provider.AuthenticationMethods {
if m.Method == "OAuth2CodeGrant" {
Expand Down Expand Up @@ -120,6 +121,7 @@ func (b *BindOptions) authenticate(provider *kubebindv1alpha1.BindingProvider, c
values.Add("p", cbPort)
values.Add("s", sessionID)
values.Add("c", clusterID)
values.Add("n", clusterName)
u.RawQuery = values.Encode()

fmt.Fprintf(b.Options.ErrOut, "\nTo authenticate, visit in your browser:\n\n\t%s\n", u.String()) // nolint: errcheck
Expand Down
4 changes: 3 additions & 1 deletion pkg/kubectl/bind/plugin/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
return err // should never happen because we test this in Validate()
}

providerClusterName := exportURL.Query().Get("cluster")

provider, err := getProvider(exportURL.String())
if err != nil {
return fmt.Errorf("failed to fetch authentication url %q: %v", exportURL, err)
Expand Down Expand Up @@ -182,7 +184,7 @@ func (b *BindOptions) Run(ctx context.Context, urlCh chan<- string) error {
}

sessionID := SessionID()
if err := b.authenticate(provider, auth.Endpoint(), sessionID, ClusterID(ns), urlCh); err != nil {
if err := b.authenticate(provider, auth.Endpoint(), sessionID, ClusterID(ns), providerClusterName, urlCh); err != nil {
return err
}

Expand Down

0 comments on commit e0f353b

Please sign in to comment.