Skip to content

Commit

Permalink
updated discoveryconfig logic for secret auth
Browse files Browse the repository at this point in the history
Signed-off-by: dislbenn <[email protected]>
  • Loading branch information
dislbenn committed Aug 26, 2024
1 parent df5bb4b commit 2f775b4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
27 changes: 27 additions & 0 deletions controllers/discoveryconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (r *DiscoveryConfigReconciler) updateDiscoveredClusters(ctx context.Context
return err
}

// Update secret to include default authentication method if the field is missing.
if _, found := ocmSecret.Data["auth_method"]; !found {
if err := r.AddDefaultAuthMethodToSecret(ctx, ocmSecret); err != nil {
return err
}
}

// Parse user token from ocm secret.
authRequest, err := parseSecretForAuth(ocmSecret)

Expand Down Expand Up @@ -271,6 +278,26 @@ func parseSecretForAuth(secret *corev1.Secret) (auth.AuthRequest, error) {
return credentials, nil
}

func (r *DiscoveryConfigReconciler) AddDefaultAuthMethodToSecret(ctx context.Context, secret *corev1.Secret) error {
// Set the default auth_method to "offline-token"
secret.Data["auth_method"] = []byte("offline-token")

// Check if both client_id and client_secret are present in the secret data
if _, idOK := secret.Data["client_id"]; idOK {
if _, secretOk := secret.Data["client_secret"]; secretOk {
secret.Data["auth_method"] = []byte("service-account")
}
}

// Update the secret
if err := r.Client.Update(ctx, secret); err != nil {
logf.Error(err, "failed to update Secret with default auth_method: 'offline-token'", "Name", secret.GetName())
return err
}

return nil
}

// assignManagedStatus marks clusters in the discovered map as managed if they are in the managed list
func assignManagedStatus(discovered map[string]discovery.DiscoveredCluster, managed []metav1.PartialObjectMetadata) {
for _, mc := range managed {
Expand Down
3 changes: 2 additions & 1 deletion pkg/ocm/subscription/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package subscription

import (
"errors"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (client *subscriptionClient) GetSubscriptions() ([]Subscription, error) {

if err != nil {
if err.Error == nil && err.Reason != "" {
err.Error = fmt.Errorf(err.Reason)
err.Error = errors.New(err.Reason)
}

logf.Error(err.Error, "Failed to retrieve subscriptions", "Page", request.Page,
Expand Down

0 comments on commit 2f775b4

Please sign in to comment.