Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions jdbc/impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
<version>1.21.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mssqlserver</artifactId>
<version>1.21.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
Expand All @@ -101,6 +107,24 @@
<version>42.7.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.6.5.jre11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.5.11</version>
<scope>test</scope>
</dependency>
<!-- JUnit + AssertJ for DDL generator tests (no OSGi). -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.daanse.sql.jdbc.api.MetadataScopeException;
import org.eclipse.daanse.sql.jdbc.api.meta.StructureInfo;
import org.eclipse.daanse.sql.jdbc.api.schema.TableDefinition;
import org.eclipse.daanse.sql.jdbc.metadata.H2MetadataProvider;
import org.eclipse.daanse.sql.jdbc.record.schema.TableDefinitionRecord;
import org.eclipse.daanse.sql.jdbc.record.schema.TableMetaDataRecord;
import org.eclipse.daanse.sql.model.schema.SchemaReference;
Expand Down Expand Up @@ -122,6 +123,16 @@ void theUserScopeAskedForIsReadInIt() throws SQLException {
assertThat(structure.tables()).extracting(t -> t.table().name()).containsExactly("ORDERS");
}

@Test
void aDatabaseThatReadsOneWayOnlyReportsTheScopeAskedFor() throws SQLException {
StructureInfo structure = new DatabaseServiceImpl()
.createMetaInfo(connection, new H2MetadataProvider(MetadataScope.USER), true).structureInfo();

assertThat(structure.requestedScope()).isEqualTo(MetadataScope.USER);
assertThat(structure.scope()).isEqualTo(MetadataScope.USER);
assertThat(structure.tables()).extracting(t -> t.table().name()).contains("ORDERS", "SECRET");
}

/**
* Reads {@code VISIBLE} and {@code HIDDEN} in {@code DATABASE}, {@code VISIBLE} in {@code USER};
* counts what it reads.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.daanse.sql.jdbc.impl.scope;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import java.util.List;

import org.eclipse.daanse.sql.jdbc.api.MetadataProvider;
import org.eclipse.daanse.sql.jdbc.api.MetadataScope;
import org.eclipse.daanse.sql.jdbc.api.MetadataScopeException;
import org.eclipse.daanse.sql.jdbc.api.meta.MetaInfo;
import org.eclipse.daanse.sql.jdbc.api.meta.StructureInfo;
import org.eclipse.daanse.sql.jdbc.metadata.MariaDbMetadataProvider;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

/**
* The scopes on MariaDB, in a container. A schema is a database. {@code root} creates the objects
* and reads in {@link MetadataScope#DATABASE}; {@code clerk} may not read the grant tables of the
* schema {@code mysql} and reads in {@link MetadataScope#USER} what its default role, active in each
* of its sessions, lets it see. {@code information_schema} shows no grant to a role; the grants of
* the role are read from {@code SHOW GRANTS FOR} it. A reader that holds the rights the provider
* names, {@code SELECT, SHOW VIEW, TRIGGER, EXECUTE ON *.*}, and no other, reads in
* {@link MetadataScope#DATABASE} what {@code root} reads; one that may read the grant tables alone
* may not.
*/
@EnabledIfSystemProperty(named = "integration.docker", matches = "true")
class MariaDbMetadataScopeTest extends MetadataScopeContract {

private static final String PASSWORD = "scope";

@SuppressWarnings("resource")
private final GenericContainer<?> container = new GenericContainer<>("mariadb:11.4")
.withEnv("MARIADB_ROOT_PASSWORD", PASSWORD).withExposedPorts(3306)
.waitingFor(Wait.forLogMessage(".*ready for connections.*\\n", 2)
.withStartupTimeout(Duration.ofMinutes(3)));

@Override
protected void start() {
container.start();
}

@Override
protected void stop() {
container.stop();
}

private String url() {
return "jdbc:mariadb://" + container.getHost() + ":" + container.getMappedPort(3306) + "/";
}

@Override
protected Connection admin() throws SQLException {
return DriverManager.getConnection(url(), "root", PASSWORD);
}

@Override
protected Connection clerk() throws SQLException {
return DriverManager.getConnection(url(), "clerk", "clerk");
}

/** A new connection of {@code user}, whose password is its name. */
private Connection connect(String user) throws SQLException {
return DriverManager.getConnection(url(), user, user);
}

@Override
protected List<String> fixture() {
return List.of("CREATE DATABASE shop", "CREATE DATABASE stock", "CREATE DATABASE hidden",
"CREATE TABLE shop.orders (id INT PRIMARY KEY, qty INT)",
"CREATE TABLE stock.product (id INT PRIMARY KEY)",
"CREATE TABLE stock.secret (id INT PRIMARY KEY)",
"CREATE TABLE hidden.t (id INT PRIMARY KEY)",
"CREATE FUNCTION stock.f() RETURNS INT DETERMINISTIC RETURN 1",
"CREATE ROLE r_reader", "CREATE ROLE r_other",
"GRANT SELECT ON shop.orders TO r_reader", "GRANT SELECT ON stock.product TO r_reader",
"GRANT SELECT ON stock.secret TO r_other",
"CREATE USER clerk IDENTIFIED BY 'clerk'", "GRANT r_reader TO clerk",
"SET DEFAULT ROLE r_reader FOR clerk", "GRANT UPDATE (qty) ON shop.orders TO clerk",
"GRANT EXECUTE ON FUNCTION stock.f TO clerk",
"CREATE USER other IDENTIFIED BY 'other'", "GRANT r_other TO other",
"SET DEFAULT ROLE r_other FOR other");
}

@Override
protected List<String> grantTheHiddenTable() {
return List.of("GRANT SELECT ON hidden.t TO clerk");
}

@Override
protected List<String> revokeTheHiddenTable() {
return List.of("REVOKE SELECT ON hidden.t FROM clerk");
}

@Override
protected MetadataProvider provider(MetadataScope scope) {
return new MariaDbMetadataProvider(scope);
}

@Override
protected List<String> catalogOfTheAdministrator() {
return List.of("MYSQL.");
}

/**
* A role granted to the role of the user is active too, and so are its grants; the user may not
* run {@code SHOW GRANTS FOR} it, so they are read from {@code SHOW GRANTS FOR} the role of the
* user.
*/
@Test
void theGrantsOfARoleGrantedToTheActiveRoleAreRead() throws SQLException {
run(List.of("CREATE TABLE shop.lines (id INT PRIMARY KEY)", "CREATE ROLE r_base",
"GRANT SELECT ON shop.lines TO r_base", "GRANT r_base TO r_reader"));
try (Connection clerk = clerk()) {
StructureInfo structure = snapshot(clerk, MetadataScope.USER);

assertThat(tables(structure)).contains("shop.lines");
assertThat(tableGrants(structure)).contains("r_base select shop.lines", "r_reader select shop.orders");
assertThat(memberships(structure)).contains("clerk > r_reader", "r_reader > r_base");
} finally {
run(List.of("DROP ROLE r_base", "DROP TABLE shop.lines"));
}
}

/**
* The keys of a table the user may only read: {@code TABLE_CONSTRAINTS} shows them only to a
* holder of more than {@code SELECT}, {@code KEY_COLUMN_USAGE} to every holder of a right.
*/
@Test
void theKeysOfATableTheUserMayOnlyReadAreRead() throws SQLException {
run(List.of("CREATE TABLE stock.part (id INT, code INT, CONSTRAINT pk_part PRIMARY KEY (id),"
+ " CONSTRAINT uq_part_code UNIQUE (code))", "GRANT SELECT ON stock.part TO r_reader"));
try (Connection clerk = clerk()) {
StructureInfo structure = snapshot(clerk, MetadataScope.USER);

assertThat(structure.primaryKeys()).filteredOn(pk -> pk.table().name().equals("part"))
.flatExtracting(pk -> pk.columns()).extracting(column -> column.name()).containsExactly("id");
assertThat(structure.uniqueConstraints()).filteredOn(uc -> uc.table().name().equals("part"))
.extracting(uc -> uc.name()).containsExactly("uq_part_code");
} finally {
run(List.of("DROP TABLE stock.part"));
}
}

/** The grants of a role, with the columns a privilege holds for, read from {@code SHOW GRANTS FOR} it. */
@Test
void theGrantsOfTheActiveRoleAreReadFromShowGrants() throws SQLException {
run(List.of("GRANT INSERT (id, qty) ON shop.orders TO r_reader"));
try (Connection clerk = clerk()) {
StructureInfo structure = snapshot(clerk, MetadataScope.USER);

assertThat(columnGrants(structure)).contains("r_reader insert shop.orders.id",
"r_reader insert shop.orders.qty", "clerk update shop.orders.qty");
assertThat(tableGrants(structure)).noneMatch(grant -> grant.contains(" insert "));
} finally {
run(List.of("REVOKE INSERT (id, qty) ON shop.orders FROM r_reader"));
}
}

/**
* A schema none of the readers below holds a right on, with an object of each kind the provider
* reads, and grants on them to {@code other}.
*/
private static List<String> vault() {
return List.of("CREATE DATABASE vault",
"CREATE TABLE vault.parent (id INT PRIMARY KEY, code INT, qty INT,"
+ " CONSTRAINT uq_parent_code UNIQUE (code), CONSTRAINT ck_parent_qty CHECK (qty > 0))",
"CREATE TABLE vault.child (id INT PRIMARY KEY, parent_id INT,"
+ " CONSTRAINT fk_child_parent FOREIGN KEY (parent_id) REFERENCES vault.parent (id))",
"CREATE INDEX idx_child_parent ON vault.child (parent_id)",
"CREATE TABLE vault.part (id INT PRIMARY KEY) PARTITION BY HASH (id) PARTITIONS 2",
"CREATE SEQUENCE vault.seq START WITH 10 INCREMENT BY 5",
"CREATE FUNCTION vault.f_next(p INT) RETURNS INT DETERMINISTIC RETURN p + 1",
"CREATE VIEW vault.v_parent AS SELECT id, qty FROM vault.parent WHERE qty > 1",
"CREATE VIEW vault.v_next AS SELECT id, vault.f_next(qty) AS next_qty FROM vault.parent",
"CREATE TRIGGER vault.trg_child_inserted AFTER INSERT ON vault.child FOR EACH ROW"
+ " SET @last_child = NEW.id",
"CREATE PROCEDURE vault.p_double(IN a INT, OUT b INT) SET b = a * 2",
"GRANT SELECT ON vault.parent TO other", "GRANT UPDATE (parent_id) ON vault.child TO other",
"GRANT EXECUTE ON PROCEDURE vault.p_double TO other");
}

private static List<String> dropTheVault() {
return List.of("REVOKE SELECT ON vault.parent FROM other", "REVOKE UPDATE (parent_id) ON vault.child FROM other",
"REVOKE EXECUTE ON PROCEDURE vault.p_double FROM other", "DROP DATABASE vault");
}

/** What the administrator reads of the vault, each body among it. */
private static final List<String> VAULT = List.of("table child", "table parent", "table part", "view v_next", "view v_parent",
"column v_next.id", "column v_next.next_qty", "column v_parent.id", "column v_parent.qty",
"sequence seq", "primary key child [id]", "primary key parent [id]", "unique parent.uq_parent_code [code]",
"check parent.ck_parent_qty", "foreign key child.parent_id -> parent.id",
"index child.idx_child_parent parent_id", "query of v_next body", "query of v_parent body",
"trigger child.trg_child_inserted body", "function f_next [, p] body", "procedure p_double [a, b] body",
"grant select on parent to other", "grant update on child.parent_id to other",
"grant execute on p_double to other");

/**
* {@code SELECT} on the grant tables alone shows a reader the grants but not the schemas it holds
* no other right on: asked for {@link MetadataScope#DATABASE}, it reads in
* {@link MetadataScope#USER}, and strictly not at all.
*/
@Test
void theGrantTablesAloneAreNotTheWholeDatabase() throws SQLException {
run(List.of("CREATE USER grant_reader IDENTIFIED BY 'grant_reader'",
"GRANT SELECT ON mysql.user TO grant_reader", "GRANT SELECT ON mysql.roles_mapping TO grant_reader",
"GRANT SELECT ON mysql.tables_priv TO grant_reader", "GRANT SELECT ON mysql.columns_priv TO grant_reader",
"GRANT SELECT ON mysql.procs_priv TO grant_reader"));
MetadataProvider provider = provider(MetadataScope.DATABASE);
try (Connection reader = connect("grant_reader")) {
StructureInfo structure = snapshot(reader, MetadataScope.DATABASE);

assertThat(structure.requestedScope()).isEqualTo(MetadataScope.DATABASE);
assertThat(structure.scope()).isEqualTo(MetadataScope.USER);
assertThat(tables(structure)).isEmpty();
assertThatThrownBy(() -> metaInfo(reader, MetadataScope.DATABASE, true))
.isInstanceOfSatisfying(MetadataScopeException.class, e -> {
assertThat(e.available()).isEqualTo(MetadataScope.USER);
assertThat(e.requirement()).isEqualTo(provider.scopeRequirement())
.isEqualTo("SELECT, SHOW VIEW, TRIGGER, EXECUTE ON *.*");
});
} finally {
run(List.of("DROP USER grant_reader"));
}
}

/**
* A reader granted the rights the provider names, as a {@code GRANT}, and no other, reads in
* {@link MetadataScope#DATABASE}, strictly, what {@code root} reads: the whole vault, each body
* and query, every grant, and everything else. MariaDB shows the columns of a view that calls a
* function, {@code v_next}, only to a reader that may execute the function.
*/
@Test
void aReaderHoldingTheRequirementReadsWhatTheAdministratorReads() throws SQLException {
run(vault());
run(List.of("CREATE USER catalog_reader IDENTIFIED BY 'catalog_reader'",
"GRANT " + provider(MetadataScope.DATABASE).scopeRequirement() + " TO catalog_reader"));
try (Connection admin = admin(); Connection reader = connect("catalog_reader")) {
MetaInfo expected = metaInfo(admin, MetadataScope.DATABASE, true);
MetaInfo actual = metaInfo(reader, MetadataScope.DATABASE, true);

assertThat(actual.structureInfo().scope()).isEqualTo(MetadataScope.DATABASE);
assertThat(inventory(expected, "vault")).containsAll(VAULT);
assertThat(inventory(actual, "vault")).isEqualTo(inventory(expected, "vault"));
assertSameMetadata(expected, actual);
} finally {
run(List.of("DROP USER catalog_reader"));
run(dropTheVault());
}
}

/**
* The rights count where a role active in the session holds them, also through a role granted
* to that role: not where the role is granted but not active.
*/
@Test
void theRequirementHeldThroughAnActiveRole() throws SQLException {
run(List.of("CREATE ROLE catalog_role", "CREATE ROLE catalog_outer",
"GRANT " + provider(MetadataScope.DATABASE).scopeRequirement() + " TO catalog_role",
"GRANT catalog_role TO catalog_outer", "CREATE USER role_reader IDENTIFIED BY 'role_reader'",
"GRANT catalog_outer TO role_reader"));
try {
try (Connection reader = connect("role_reader")) {
assertThat(provider(MetadataScope.DATABASE).effectiveScope(reader)).isEqualTo(MetadataScope.USER);
}
run(List.of("SET DEFAULT ROLE catalog_outer FOR role_reader"));
try (Connection reader = connect("role_reader")) {
assertThat(provider(MetadataScope.DATABASE).effectiveScope(reader)).isEqualTo(MetadataScope.DATABASE);
}
} finally {
run(List.of("DROP USER role_reader", "DROP ROLE catalog_outer", "DROP ROLE catalog_role"));
}
}
}
Loading
Loading