From 80292241784587e9254ff30e14f45b010352353e Mon Sep 17 00:00:00 2001 From: Roman Lytvyn Date: Fri, 4 Jun 2021 13:10:08 +0200 Subject: [PATCH] add: metadata encoding error propagation --- api/v1alpha1/oauth2client_types.go | 10 +++++++--- controllers/oauth2client_controller.go | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/api/v1alpha1/oauth2client_types.go b/api/v1alpha1/oauth2client_types.go index 5da7aaa..b4324d3 100644 --- a/api/v1alpha1/oauth2client_types.go +++ b/api/v1alpha1/oauth2client_types.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/ory/hydra-maester/hydra" + "github.com/pkg/errors" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -186,8 +187,11 @@ func init() { } // ToOAuth2ClientJSON converts an OAuth2Client into a OAuth2ClientJSON object that represents an OAuth2 client digestible by ORY Hydra -func (c *OAuth2Client) ToOAuth2ClientJSON() *hydra.OAuth2ClientJSON { - meta, _ := json.Marshal(c.Spec.Metadata) +func (c *OAuth2Client) ToOAuth2ClientJSON() (*hydra.OAuth2ClientJSON, error) { + meta, err := json.Marshal(c.Spec.Metadata) + if err != nil { + return nil, errors.WithMessage(err, "unable to encode `metadata` property value to json") + } return &hydra.OAuth2ClientJSON{ ClientName: c.Spec.ClientName, @@ -201,7 +205,7 @@ func (c *OAuth2Client) ToOAuth2ClientJSON() *hydra.OAuth2ClientJSON { Owner: fmt.Sprintf("%s/%s", c.Name, c.Namespace), TokenEndpointAuthMethod: string(c.Spec.TokenEndpointAuthMethod), Metadata: meta, - } + }, nil } func responseToStringSlice(rt []ResponseType) []string { diff --git a/controllers/oauth2client_controller.go b/controllers/oauth2client_controller.go index 7863f8a..06e2e7d 100644 --- a/controllers/oauth2client_controller.go +++ b/controllers/oauth2client_controller.go @@ -205,8 +205,13 @@ func (r *OAuth2ClientReconciler) registerOAuth2Client(ctx context.Context, c *hy return err } + oauth2client, err := c.ToOAuth2ClientJSON() + if err != nil { + return err + } + if credentials != nil { - if _, err := hydra.PostOAuth2Client(c.ToOAuth2ClientJSON().WithCredentials(credentials)); err != nil { + if _, err := hydra.PostOAuth2Client(oauth2client.WithCredentials(credentials)); err != nil { if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusRegistrationFailed, err); updateErr != nil { return updateErr } @@ -214,7 +219,7 @@ func (r *OAuth2ClientReconciler) registerOAuth2Client(ctx context.Context, c *hy return r.ensureEmptyStatusError(ctx, c) } - created, err := hydra.PostOAuth2Client(c.ToOAuth2ClientJSON()) + created, err := hydra.PostOAuth2Client(oauth2client) if err != nil { if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusRegistrationFailed, err); updateErr != nil { return updateErr @@ -257,7 +262,12 @@ func (r *OAuth2ClientReconciler) updateRegisteredOAuth2Client(ctx context.Contex return err } - if _, err := hydra.PutOAuth2Client(c.ToOAuth2ClientJSON().WithCredentials(credentials)); err != nil { + oauth2client, err := c.ToOAuth2ClientJSON() + if err != nil { + return err + } + + if _, err := hydra.PutOAuth2Client(oauth2client.WithCredentials(credentials)); err != nil { if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusUpdateFailed, err); updateErr != nil { return updateErr }