Skip to content

Commit e8ca7fe

Browse files
authored
fix(db): use text constraints, not limited size data types (#757)
* fix(db): use text constraints, not limited size data types * test(rule): add test for maximum rule name length
1 parent 4960a47 commit e8ca7fe

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/main/resources/db/migration/V4.0.0__cryostat.sql

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
maxAge bigint not null,
2020
maxSize bigint not null,
2121
metadata jsonb,
22-
name varchar(255),
22+
name text check (char_length(name) < 64),
2323
remoteId bigint not null,
2424
startTime bigint not null,
2525
state smallint check (state between 0 and 4),
@@ -40,48 +40,48 @@
4040
create table DiscoveryNode (
4141
id bigint not null,
4242
labels jsonb,
43-
name varchar(255) not null,
44-
nodeType varchar(255) not null,
43+
name text not null check (char_length(name) < 255),
44+
nodeType text not null check (char_length(nodeType) < 255),
4545
parentNode bigint,
4646
primary key (id)
4747
);
4848

4949
create table DiscoveryPlugin (
5050
id uuid not null,
5151
builtin boolean not null,
52-
callback varchar(255) unique,
52+
callback text unique,
5353
credential_id bigint unique,
5454
realm_id bigint not null unique,
5555
primary key (id)
5656
);
5757

5858
create table MatchExpression (
5959
id bigint not null,
60-
script varchar(255) not null,
60+
script text not null check (char_length(script) < 1024),
6161
primary key (id)
6262
);
6363

6464
create table Rule (
6565
id bigint not null,
6666
archivalPeriodSeconds integer not null,
67-
description varchar(255),
67+
description text check (char_length(description) < 1024),
6868
enabled boolean not null,
69-
eventSpecifier varchar(255) not null,
69+
eventSpecifier text not null check (char_length(eventSpecifier) < 255),
7070
initialDelaySeconds integer not null,
7171
maxAgeSeconds integer not null,
7272
maxSizeBytes integer not null,
73-
name varchar(255) unique,
73+
name text unique check (char_length(name) < 255),
7474
preservedArchives integer not null,
7575
matchExpression bigint unique,
7676
primary key (id)
7777
);
7878

7979
create table Target (
8080
id bigint not null,
81-
alias varchar(255),
81+
alias text check (char_length(alias) < 255),
8282
annotations jsonb,
8383
connectUrl bytea unique,
84-
jvmId varchar(255),
84+
jvmId text check (char_length(jvmId) < 255),
8585
labels jsonb,
8686
discoveryNode bigint unique,
8787
primary key (id)

src/test/java/io/cryostat/rules/RulesTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.hamcrest.CoreMatchers.is;
2020
import static org.hamcrest.Matchers.*;
2121

22+
import java.util.Arrays;
23+
2224
import io.cryostat.AbstractTransactionalTestBase;
2325

2426
import io.quarkus.test.common.http.TestHTTPEndpoint;
@@ -30,6 +32,8 @@
3032
import jakarta.transaction.Transactional;
3133
import org.hamcrest.Matchers;
3234
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.params.ParameterizedTest;
36+
import org.junit.jupiter.params.provider.ValueSource;
3337
import org.mockito.Mockito;
3438

3539
@QuarkusTest
@@ -244,6 +248,20 @@ public void testCreateThrowsWhenBodyNull() {
244248
.statusCode(400);
245249
}
246250

251+
@ParameterizedTest
252+
@ValueSource(ints = {1, 16, 64, 128, 256})
253+
public void testCreateThrowsWhenNameTooLong(int len) {
254+
char[] c = new char[len];
255+
Arrays.fill(c, 'a');
256+
rule.put("name", new String(c));
257+
final int limit = 255;
258+
given().body(rule.toString())
259+
.contentType(ContentType.JSON)
260+
.post()
261+
.then()
262+
.statusCode(len <= limit ? 201 : 400);
263+
}
264+
247265
@Test
248266
public void testCreateThrowsWhenMandatoryFieldsUnspecified() {
249267
var badRule = new JsonObject();

0 commit comments

Comments
 (0)