feat: Reconcile missing and updated OAuth2 Clients (#46)
This commit is contained in:
@ -117,66 +117,68 @@ func (r *OAuth2ClientReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error
|
||||
|
||||
}
|
||||
|
||||
if oauth2client.Generation != oauth2client.Status.ObservedGeneration {
|
||||
|
||||
var secret apiv1.Secret
|
||||
if err := r.Get(ctx, types.NamespacedName{Name: oauth2client.Spec.SecretName, Namespace: req.Namespace}, &secret); err != nil {
|
||||
if apierrs.IsNotFound(err) {
|
||||
if registerErr := r.registerOAuth2Client(ctx, &oauth2client, nil); registerErr != nil {
|
||||
return ctrl.Result{}, registerErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
var secret apiv1.Secret
|
||||
if err := r.Get(ctx, types.NamespacedName{Name: oauth2client.Spec.SecretName, Namespace: req.Namespace}, &secret); err != nil {
|
||||
if apierrs.IsNotFound(err) {
|
||||
if registerErr := r.registerOAuth2Client(ctx, &oauth2client, nil); registerErr != nil {
|
||||
return ctrl.Result{}, registerErr
|
||||
}
|
||||
return ctrl.Result{}, err
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
credentials, err := parseSecret(secret)
|
||||
if err != nil {
|
||||
r.Log.Error(err, fmt.Sprintf("secret %s/%s is invalid", secret.Name, secret.Namespace))
|
||||
if updateErr := r.updateReconciliationStatusError(ctx, &oauth2client, hydrav1alpha1.StatusInvalidSecret, err); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
hydraClient, err := r.getHydraClientForClient(oauth2client)
|
||||
if err != nil {
|
||||
r.Log.Error(err, fmt.Sprintf(
|
||||
"hydra address %s:%d%s is invalid",
|
||||
oauth2client.Spec.HydraAdmin.URL,
|
||||
oauth2client.Spec.HydraAdmin.Port,
|
||||
oauth2client.Spec.HydraAdmin.Endpoint,
|
||||
))
|
||||
if updateErr := r.updateReconciliationStatusError(ctx, &oauth2client, hydrav1alpha1.StatusInvalidHydraAddress, err); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
fetched, found, err := hydraClient.GetOAuth2Client(string(credentials.ID))
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
|
||||
}
|
||||
|
||||
if found {
|
||||
//conclude reconciliation if the client exists and has not been updated
|
||||
if oauth2client.Generation == oauth2client.Status.ObservedGeneration {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
credentials, err := parseSecret(secret)
|
||||
if err != nil {
|
||||
r.Log.Error(err, fmt.Sprintf("secret %s/%s is invalid", secret.Name, secret.Namespace))
|
||||
if updateErr := r.updateReconciliationStatusError(ctx, &oauth2client, hydrav1alpha1.StatusInvalidSecret, err); updateErr != nil {
|
||||
if fetched.Owner != fmt.Sprintf("%s/%s", oauth2client.Name, oauth2client.Namespace) {
|
||||
conflictErr := errors.Errorf("ID provided in secret %s/%s is assigned to another resource", secret.Name, secret.Namespace)
|
||||
if updateErr := r.updateReconciliationStatusError(ctx, &oauth2client, hydrav1alpha1.StatusInvalidSecret, conflictErr); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
hydraClient, err := r.getHydraClientForClient(oauth2client)
|
||||
if err != nil {
|
||||
r.Log.Error(err, fmt.Sprintf(
|
||||
"hydra address %s:%d%s is invalid",
|
||||
oauth2client.Spec.HydraAdmin.URL,
|
||||
oauth2client.Spec.HydraAdmin.Port,
|
||||
oauth2client.Spec.HydraAdmin.Endpoint,
|
||||
))
|
||||
if updateErr := r.updateReconciliationStatusError(ctx, &oauth2client, hydrav1alpha1.StatusInvalidHydraAddress, err); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
if updateErr := r.updateRegisteredOAuth2Client(ctx, &oauth2client, credentials); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
fetched, found, err := hydraClient.GetOAuth2Client(string(credentials.ID))
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
|
||||
}
|
||||
|
||||
if found {
|
||||
if fetched.Owner != fmt.Sprintf("%s/%s", oauth2client.Name, oauth2client.Namespace) {
|
||||
conflictErr := errors.Errorf("ID provided in secret %s/%s is assigned to another resource", secret.Name, secret.Namespace)
|
||||
if updateErr := r.updateReconciliationStatusError(ctx, &oauth2client, hydrav1alpha1.StatusInvalidSecret, conflictErr); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
if updateErr := r.updateRegisteredOAuth2Client(ctx, &oauth2client, credentials); updateErr != nil {
|
||||
return ctrl.Result{}, updateErr
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
if registerErr := r.registerOAuth2Client(ctx, &oauth2client, credentials); registerErr != nil {
|
||||
return ctrl.Result{}, registerErr
|
||||
}
|
||||
if registerErr := r.registerOAuth2Client(ctx, &oauth2client, credentials); registerErr != nil {
|
||||
return ctrl.Result{}, registerErr
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
|
@ -60,6 +60,7 @@ var _ = Describe("OAuth2Client Controller", func() {
|
||||
c := mgr.GetClient()
|
||||
|
||||
mch := &mocks.HydraClientInterface{}
|
||||
mch.On("GetOAuth2Client", Anything).Return(nil, false, nil)
|
||||
mch.On("DeleteOAuth2Client", Anything).Return(nil)
|
||||
mch.On("ListOAuth2Client", Anything).Return(nil, nil)
|
||||
mch.On("PostOAuth2Client", AnythingOfType("*hydra.OAuth2ClientJSON")).Return(func(o *hydra.OAuth2ClientJSON) *hydra.OAuth2ClientJSON {
|
||||
@ -139,6 +140,7 @@ var _ = Describe("OAuth2Client Controller", func() {
|
||||
c := mgr.GetClient()
|
||||
|
||||
mch := &mocks.HydraClientInterface{}
|
||||
mch.On("GetOAuth2Client", Anything).Return(nil, false, nil)
|
||||
mch.On("PostOAuth2Client", Anything).Return(nil, errors.New("error"))
|
||||
mch.On("DeleteOAuth2Client", Anything).Return(nil)
|
||||
mch.On("ListOAuth2Client", Anything).Return(nil, nil)
|
||||
@ -205,6 +207,7 @@ var _ = Describe("OAuth2Client Controller", func() {
|
||||
c := mgr.GetClient()
|
||||
|
||||
mch := mocks.HydraClientInterface{}
|
||||
mch.On("GetOAuth2Client", Anything).Return(nil, false, nil)
|
||||
mch.On("DeleteOAuth2Client", Anything).Return(nil)
|
||||
mch.On("ListOAuth2Client", Anything).Return(nil, nil)
|
||||
mch.On("GetOAuth2Client", Anything).Return(nil, false, nil)
|
||||
@ -300,6 +303,7 @@ var _ = Describe("OAuth2Client Controller", func() {
|
||||
c := mgr.GetClient()
|
||||
|
||||
mch := mocks.HydraClientInterface{}
|
||||
mch.On("GetOAuth2Client", Anything).Return(nil, false, nil)
|
||||
mch.On("DeleteOAuth2Client", Anything).Return(nil)
|
||||
mch.On("ListOAuth2Client", Anything).Return(nil, nil)
|
||||
|
||||
|
Reference in New Issue
Block a user