@@ -258,6 +258,34 @@ func (r *PostgresUserReconciler) Reconcile(ctx context.Context, req ctrl.Request
258258 } else {
259259 role = instance .Status .PostgresRole
260260 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" )
261289 }
262290
263291 err = r .addFinalizer (ctx , reqLogger , instance )
0 commit comments