@@ -203,89 +203,7 @@ func (r *PostgresUserReconciler) Reconcile(ctx context.Context, req ctrl.Request
203203 }
204204 } else if awsIamRequested {
205205 reqLogger .WithValues ("role" , role ).Info ("IAM Auth requested while we are not running with AWS cloud provider config" )
206- // Reconcile logic for changes in group membership
207- // This is only applicable if user role is already created
208- // and privileges are changed in spec
209- if instance .Status .PostgresRole != "" {
210-
211- // We need to get the Postgres CR to get the group role name
212- database , err := r .getPostgresCR (ctx , instance )
213- if err != nil {
214- return r .requeue (ctx , instance , errors .NewInternalError (err ))
215- }
216-
217- // Determine desired group role
218- var desiredGroup string
219- switch instance .Spec .Privileges {
220- case "READ" :
221- desiredGroup = database .Status .Roles .Reader
222- case "WRITE" :
223- desiredGroup = database .Status .Roles .Writer
224- default :
225- desiredGroup = database .Status .Roles .Owner
226- }
227-
228- // Ability user to be reassigned to another group role
229- currentGroup := instance .Status .PostgresGroup
230- if desiredGroup != "" && currentGroup != desiredGroup {
231-
232- // Remove the old group membership if present
233- if currentGroup != "" {
234- err = r .pg .RevokeRole (currentGroup , role )
235- if err != nil {
236- return r .requeue (ctx , instance , errors .NewInternalError (err ))
237- }
238- }
239-
240- // Grant the new group role
241- err = r .pg .GrantRole (desiredGroup , role )
242- if err != nil {
243- return r .requeue (ctx , instance , errors .NewInternalError (err ))
244- }
245-
246- // Ensure objects created by the user are owned by the new group
247- err = r .pg .AlterDefaultLoginRole (role , desiredGroup )
248- if err != nil {
249- return r .requeue (ctx , instance , errors .NewInternalError (err ))
250- }
251-
252- instance .Status .PostgresGroup = desiredGroup
253- err = r .Status ().Update (ctx , instance )
254- if err != nil {
255- return r .requeue (ctx , instance , err )
256- }
257- }
258- } else {
259- role = instance .Status .PostgresRole
260- login = instance .Status .PostgresLogin
261- awsConfig := instance .Spec .AWS
262- awsIamRequested := awsConfig != nil && awsConfig .EnableIamAuth
263-
264- if r .cloudProvider == "AWS" {
265- if awsIamRequested && ! instance .Status .EnableIamAuth {
266- if err := r .pg .GrantRole ("rds_iam" , role ); err != nil {
267- reqLogger .WithValues ("role" , role ).Error (err , "failed to grant rds_iam role" )
268- } else {
269- instance .Status .EnableIamAuth = true
270- if sErr := r .Status ().Update (ctx , instance ); sErr != nil {
271- reqLogger .WithValues ("role" , role ).Error (sErr , "failed to update status after IAM grant" )
272- }
273- }
274- }
275-
276- // Revoke aws_iam role on transition: spec=false, status=true
277- if ! awsIamRequested && instance .Status .EnableIamAuth {
278- if err := r .pg .RevokeRole ("rds_iam" , role ); err != nil {
279- reqLogger .WithValues ("role" , role ).Error (err , "failed to revoke rds_iam role" )
280- } else {
281- instance .Status .EnableIamAuth = false
282- if sErr := r .Status ().Update (ctx , instance ); sErr != nil {
283- reqLogger .WithValues ("role" , role ).Error (sErr , "failed to update status after IAM revoke" )
284- }
285- }
286- }
287- } else if awsIamRequested {
288- reqLogger .WithValues ("role" , role ).Info ("IAM Auth requested while we are not running with AWS cloud provider config" )
206+ }
289207
290208 // Reconcile logic for changes in group membership
291209 // This is only applicable if user role is already created
@@ -309,32 +227,29 @@ func (r *PostgresUserReconciler) Reconcile(ctx context.Context, req ctrl.Request
309227 desiredGroup = database .Status .Roles .Owner
310228 }
311229
230+ // Ability user to be reassigned to another group role
312231 currentGroup := instance .Status .PostgresGroup
313232 if desiredGroup != "" && currentGroup != desiredGroup {
314233
315234 // Remove the old group membership if present
316235 if currentGroup != "" {
317- err = r .pg .RevokeRole (currentGroup , role )
318- if err != nil {
236+ if err := r .pg .RevokeRole (currentGroup , role ); err != nil {
319237 return r .requeue (ctx , instance , errors .NewInternalError (err ))
320238 }
321239 }
322240
323241 // Grant the new group role
324- err = r .pg .GrantRole (desiredGroup , role )
325- if err != nil {
242+ if err := r .pg .GrantRole (desiredGroup , role ); err != nil {
326243 return r .requeue (ctx , instance , errors .NewInternalError (err ))
327244 }
328245
329246 // Ensure objects created by the user are owned by the new group
330- err = r .pg .AlterDefaultLoginRole (role , desiredGroup )
331- if err != nil {
247+ if err := r .pg .AlterDefaultLoginRole (role , desiredGroup ); err != nil {
332248 return r .requeue (ctx , instance , errors .NewInternalError (err ))
333249 }
334250
335251 instance .Status .PostgresGroup = desiredGroup
336- err = r .Status ().Update (ctx , instance )
337- if err != nil {
252+ if err := r .Status ().Update (ctx , instance ); err != nil {
338253 return r .requeue (ctx , instance , err )
339254 }
340255 }
0 commit comments