add: metadata encoding error propagation

This commit is contained in:
Roman Lytvyn 2021-06-04 13:10:08 +02:00
parent 1bd0d394b0
commit 8029224178
2 changed files with 20 additions and 6 deletions

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"github.com/ory/hydra-maester/hydra" "github.com/ory/hydra-maester/hydra"
"github.com/pkg/errors"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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 // ToOAuth2ClientJSON converts an OAuth2Client into a OAuth2ClientJSON object that represents an OAuth2 client digestible by ORY Hydra
func (c *OAuth2Client) ToOAuth2ClientJSON() *hydra.OAuth2ClientJSON { func (c *OAuth2Client) ToOAuth2ClientJSON() (*hydra.OAuth2ClientJSON, error) {
meta, _ := json.Marshal(c.Spec.Metadata) 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{ return &hydra.OAuth2ClientJSON{
ClientName: c.Spec.ClientName, ClientName: c.Spec.ClientName,
@ -201,7 +205,7 @@ func (c *OAuth2Client) ToOAuth2ClientJSON() *hydra.OAuth2ClientJSON {
Owner: fmt.Sprintf("%s/%s", c.Name, c.Namespace), Owner: fmt.Sprintf("%s/%s", c.Name, c.Namespace),
TokenEndpointAuthMethod: string(c.Spec.TokenEndpointAuthMethod), TokenEndpointAuthMethod: string(c.Spec.TokenEndpointAuthMethod),
Metadata: meta, Metadata: meta,
} }, nil
} }
func responseToStringSlice(rt []ResponseType) []string { func responseToStringSlice(rt []ResponseType) []string {

View File

@ -205,8 +205,13 @@ func (r *OAuth2ClientReconciler) registerOAuth2Client(ctx context.Context, c *hy
return err return err
} }
oauth2client, err := c.ToOAuth2ClientJSON()
if err != nil {
return err
}
if credentials != nil { 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 { if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusRegistrationFailed, err); updateErr != nil {
return updateErr return updateErr
} }
@ -214,7 +219,7 @@ func (r *OAuth2ClientReconciler) registerOAuth2Client(ctx context.Context, c *hy
return r.ensureEmptyStatusError(ctx, c) return r.ensureEmptyStatusError(ctx, c)
} }
created, err := hydra.PostOAuth2Client(c.ToOAuth2ClientJSON()) created, err := hydra.PostOAuth2Client(oauth2client)
if err != nil { if err != nil {
if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusRegistrationFailed, err); updateErr != nil { if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusRegistrationFailed, err); updateErr != nil {
return updateErr return updateErr
@ -257,7 +262,12 @@ func (r *OAuth2ClientReconciler) updateRegisteredOAuth2Client(ctx context.Contex
return err 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 { if updateErr := r.updateReconciliationStatusError(ctx, c, hydrav1alpha1.StatusUpdateFailed, err); updateErr != nil {
return updateErr return updateErr
} }