Skip to content

Commit 2020658

Browse files
committed
test the VALID UNTIL flag
1 parent 7e11c98 commit 2020658

3 files changed

Lines changed: 90 additions & 11 deletions

File tree

src/main/java/it/aboutbits/postgresql/crd/role/RoleReconciler.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public UpdateControl<Role> reconcile(
4545
var name = resource.getMetadata().getName();
4646
var namespace = resource.getMetadata().getNamespace();
4747

48-
log.info("Reconciling Role [resource={}/{}, status.phase={}]",
48+
log.info(
49+
"Reconciling Role [resource={}/{}, status.phase={}]",
4950
namespace,
5051
name,
5152
status.getPhase()
@@ -102,7 +103,8 @@ public UpdateControl<Role> reconcile(
102103

103104
// Create the role if it doesn't exist yet
104105
if (!RoleUtil.roleExists(tx, spec)) {
105-
log.info("Creating Role [resource={}/{}]",
106+
log.info(
107+
"Creating Role [resource={}/{}]",
106108
namespace,
107109
name
108110
);
@@ -133,7 +135,8 @@ public UpdateControl<Role> reconcile(
133135
}
134136

135137
if (roleLoginMatches && passwordMatches && flagsMatch) {
136-
log.info("Role up-to-date [resource={}/{}]",
138+
log.info(
139+
"Role up-to-date [resource={}/{}]",
137140
namespace,
138141
name
139142
);
@@ -143,7 +146,8 @@ public UpdateControl<Role> reconcile(
143146

144147
var changePassword = loginExpected && !passwordMatches;
145148

146-
log.info("Updating Role flags [resource={}/{}]",
149+
log.info(
150+
"Updating Role flags [resource={}/{}]",
147151
namespace,
148152
name
149153
);
@@ -155,7 +159,8 @@ public UpdateControl<Role> reconcile(
155159
password
156160
);
157161

158-
log.info("Updating Role membership [resource={}/{}]",
162+
log.info(
163+
"Updating Role membership [resource={}/{}]",
159164
namespace,
160165
name
161166
);

src/main/java/it/aboutbits/postgresql/crd/role/RoleUtil.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static it.aboutbits.postgresql.core.infrastructure.persistence.Tables.PG_AUTHID;
2020
import static it.aboutbits.postgresql.core.infrastructure.persistence.Tables.PG_AUTH_MEMBERS;
21+
import static org.jooq.impl.DSL.field;
2122
import static org.jooq.impl.DSL.keyword;
2223
import static org.jooq.impl.DSL.multiset;
2324
import static org.jooq.impl.DSL.name;
@@ -127,7 +128,7 @@ public static RoleSpec.Flags fetchCurrentFlags(
127128
PG_AUTHID.ROLREPLICATION.as("replication"),
128129
PG_AUTHID.ROLBYPASSRLS.as("bypassrls"),
129130
PG_AUTHID.ROLCONNLIMIT.as("connectionLimit"),
130-
PG_AUTHID.ROLVALIDUNTIL.as("validUntil"),
131+
field("nullif({0}, 'infinity')", PG_AUTHID.ROLVALIDUNTIL.getDataType(), PG_AUTHID.ROLVALIDUNTIL).as("validUntil"),
131132
multiset(
132133
select(parent.ROLNAME)
133134
.from(PG_AUTH_MEMBERS)
@@ -194,7 +195,7 @@ private static Query buildCreateRole(
194195
}
195196
if (flags.getValidUntil() != null) {
196197
options.add(keyword(RoleFlags.VALID_UNTIL.flag()));
197-
options.add(val(flags.getValidUntil()));
198+
options.add(val(flags.getValidUntil().toString()));
198199
}
199200
if (!flags.getInRole().isEmpty()) {
200201
options.add(keyword(RoleFlags.IN_ROLE.flag()));
@@ -281,9 +282,11 @@ private static Query buildAlterRole(
281282
options.add(keyword(RoleFlags.CONNECTION_LIMIT.flag()));
282283
options.add(val(flags.getConnectionLimit()));
283284

285+
options.add(keyword(RoleFlags.VALID_UNTIL.flag()));
284286
if (flags.getValidUntil() != null) {
285-
options.add(keyword(RoleFlags.VALID_UNTIL.flag()));
286-
options.add(val(flags.getValidUntil()));
287+
options.add(val(flags.getValidUntil().toString()));
288+
} else {
289+
options.add(val("infinity"));
287290
}
288291

289292
return query(
@@ -386,7 +389,7 @@ private static Query buildCommentOnRole(
386389
))
387390
.from(PG_AUTHID)
388391
.where(PG_AUTHID.ROLNAME.eq(roleName))
389-
.fetchOneInto(String.class);
392+
.fetchSingleInto(String.class);
390393
}
391394

392395
private static @Nullable String normalizeComment(@Nullable String comment) {

src/test/java/it/aboutbits/postgresql/crd/role/RoleReconcilerTest.java

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,77 @@ void connectionLimit_updatesCorrectly() throws SQLException {
425425
).isEqualTo(-1);
426426
}
427427

428+
@Test
429+
@DisplayName("When the VALID UNTIL is changed, it should be updated in the database")
430+
void validUntil_updatesCorrectly() throws SQLException {
431+
// given
432+
var clusterConnection = given.one()
433+
.clusterConnection()
434+
.withName("test-role-valid-until")
435+
.returnFirst();
436+
437+
var dsl = postgreSQLContextFactory.getDSLContext(clusterConnection);
438+
439+
var roleName = "test-role-valid-until";
440+
441+
var role = buildRole(
442+
roleName,
443+
clusterConnection.getMetadata().getName(),
444+
/*login*/ false
445+
);
446+
447+
var expiry = OffsetDateTime.now(ZoneOffset.UTC)
448+
.plusDays(1)
449+
.truncatedTo(ChronoUnit.SECONDS);
450+
451+
// 1. Set a valid until date
452+
role.getSpec().getFlags().setValidUntil(expiry);
453+
454+
// when
455+
var reconciled = applyRole(role);
456+
var initialGeneration = reconciled.getStatus().getObservedGeneration();
457+
458+
var currentFlags = RoleUtil.fetchCurrentFlags(dsl, role.getSpec());
459+
460+
// then
461+
assertThat(
462+
currentFlags.getValidUntil()
463+
).isEqualTo(expiry);
464+
465+
// 2. Change valid until date
466+
var newExpiry = expiry.plusDays(1);
467+
role.getSpec().getFlags().setValidUntil(newExpiry);
468+
469+
// when
470+
applyRole(
471+
role,
472+
r -> r.getStatus().getObservedGeneration() == initialGeneration + 1
473+
);
474+
475+
currentFlags = RoleUtil.fetchCurrentFlags(dsl, role.getSpec());
476+
477+
// then
478+
assertThat(
479+
currentFlags.getValidUntil()
480+
).isEqualTo(newExpiry);
481+
482+
// 3. Reset valid until to null (infinity)
483+
role.getSpec().getFlags().setValidUntil(null);
484+
485+
// when
486+
applyRole(
487+
role,
488+
r -> r.getStatus().getObservedGeneration() == initialGeneration + 2
489+
);
490+
491+
currentFlags = RoleUtil.fetchCurrentFlags(dsl, role.getSpec());
492+
493+
// then
494+
assertThat(
495+
currentFlags.getValidUntil()
496+
).isNull();
497+
}
498+
428499
private <T> T getRoleFlagValue(
429500
DSLContext dsl,
430501
String roleName,
@@ -433,7 +504,7 @@ private <T> T getRoleFlagValue(
433504
return dsl.select(field)
434505
.from(PG_AUTHID)
435506
.where(PG_AUTHID.ROLNAME.eq(roleName))
436-
.fetchSingleInto(field.getType());
507+
.fetchSingle(field);
437508
}
438509

439510
private static Stream<Arguments> provideBooleanFlags() {

0 commit comments

Comments
 (0)