Skip to content

Commit

Permalink
fix(rules): disallow configurations on archiver rules
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Aug 18, 2021
1 parent 2280c87 commit 8872916
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/io/cryostat/rules/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public void validate() throws IllegalArgumentException, MatchExpressionValidatio
requireNonNegative(this.archivalPeriodSeconds, Attribute.ARCHIVAL_PERIOD_SECONDS);
requireNonNegative(this.preservedArchives, Attribute.PRESERVED_ARCHIVES);
validateMatchExpression(this);

if (isArchiver()) {
requireNonPositive(this.archivalPeriodSeconds, Attribute.ARCHIVAL_PERIOD_SECONDS);
requireNonPositive(this.preservedArchives, Attribute.PRESERVED_ARCHIVES);
requireNonPositive(this.maxSizeBytes, Attribute.MAX_SIZE_BYTES);
requireNonPositive(this.maxAgeSeconds, Attribute.MAX_AGE_SECONDS);
}
}

private static String validateMatchExpression(Rule rule)
Expand All @@ -152,6 +159,14 @@ private static int requireNonNegative(int i, Attribute attr) {
return i;
}

private static int requireNonPositive(int i, Attribute attr) {
if (i > 0) {
throw new IllegalArgumentException(
String.format("\"%s\" cannot be positive, was \"%d\"", attr, i));
}
return i;
}

private static String validateEventSpecifier(String eventSpecifier)
throws IllegalArgumentException {
if (eventSpecifier.equals(ARCHIVE_EVENT)) {
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/io/cryostat/rules/RuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,72 @@ void shouldAcceptEventSpecifierArchiveSpecialCase() throws Exception {
.build();
MatcherAssert.assertThat(rule.getEventSpecifier(), Matchers.equalTo("archive"));
}

@Test
void shouldThrowOnArchiverWithArchivalPeriod() throws Exception {
IllegalArgumentException ex =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
builder.name(NAME)
.matchExpression(MATCH_EXPRESSION)
.eventSpecifier("archive")
.archivalPeriodSeconds(5)
.build();
});
MatcherAssert.assertThat(
ex.getMessage(),
Matchers.containsString("\"archivalPeriodSeconds\" cannot be positive, was \"5\""));
}

@Test
void shouldThrowOnArchiverWithPreservedArchives() throws Exception {
IllegalArgumentException ex =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
builder.name(NAME)
.matchExpression(MATCH_EXPRESSION)
.eventSpecifier("archive")
.preservedArchives(5)
.build();
});
MatcherAssert.assertThat(
ex.getMessage(),
Matchers.containsString("\"preservedArchives\" cannot be positive, was \"5\""));
}

@Test
void shouldThrowOnArchiverWithMaxSizeBytes() throws Exception {
IllegalArgumentException ex =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
builder.name(NAME)
.matchExpression(MATCH_EXPRESSION)
.eventSpecifier("archive")
.maxSizeBytes(5)
.build();
});
MatcherAssert.assertThat(
ex.getMessage(),
Matchers.containsString("\"maxSizeBytes\" cannot be positive, was \"5\""));
}

@Test
void shouldThrowOnArchiverWithMaxAgeSeconds() throws Exception {
IllegalArgumentException ex =
Assertions.assertThrows(
IllegalArgumentException.class,
() -> {
builder.name(NAME)
.matchExpression(MATCH_EXPRESSION)
.eventSpecifier("archive")
.maxAgeSeconds(5)
.build();
});
MatcherAssert.assertThat(
ex.getMessage(),
Matchers.containsString("\"maxAgeSeconds\" cannot be positive, was \"5\""));
}
}

0 comments on commit 8872916

Please sign in to comment.