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
2 changes: 1 addition & 1 deletion amazon-redshift-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>database-plugins-parent</artifactId>
<groupId>io.cdap.plugin</groupId>
<version>1.11.16</version>
<version>1.11.17-SNAPSHOT</version>
</parent>

<name>Amazon Redshift plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected SchemaReader getSchemaReader(String sessionID) {

@Override
protected String getTableName(String database, String schema, String table) {
return String.format("\"%s\".\"%s\"", schema, table);
return quoteIdentifier(schema, '"') + "." + quoteIdentifier(table, '"');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public void getTableNameTest() {
CONNECTOR.getTableName("db", "schema", "table"));
}

/**
* Unit test for getTableName() with identifiers containing double quotes
*/
@Test
public void getTableNameEscapesQuotesTest() {
Assert.assertEquals("\"my\"\"schema\".\"t\"\"; DELETE FROM x; --\"",
CONNECTOR.getTableName("db", "my\"schema", "t\"; DELETE FROM x; --"));
}

/**
* Unit tests for getTableQuery()
*/
Expand Down
2 changes: 1 addition & 1 deletion aurora-mysql-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>database-plugins-parent</artifactId>
<groupId>io.cdap.plugin</groupId>
<version>1.11.16</version>
<version>1.11.17-SNAPSHOT</version>
</parent>

<name>Aurora DB MySQL plugin</name>
Expand Down
2 changes: 1 addition & 1 deletion aurora-postgresql-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>database-plugins-parent</artifactId>
<groupId>io.cdap.plugin</groupId>
<version>1.11.16</version>
<version>1.11.17-SNAPSHOT</version>
</parent>

<name>Aurora DB PostgreSQL plugin</name>
Expand Down
2 changes: 1 addition & 1 deletion cloudsql-mysql-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>database-plugins-parent</artifactId>
<groupId>io.cdap.plugin</groupId>
<version>1.11.16</version>
<version>1.11.17-SNAPSHOT</version>
</parent>

<name>CloudSQL MySQL plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected SchemaReader getSchemaReader(String sessionID) {

@Override
protected String getTableName(String database, String schema, String table) {
return String.format("`%s`.`%s`", database, table);
return quoteIdentifier(database, '`') + "." + quoteIdentifier(table, '`');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2026 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.plugin.cloudsql.mysql;

import org.junit.Assert;
import org.junit.Test;

/**
* Unit tests for {@link CloudSQLMySQLConnector}
*/
public class CloudSQLMySQLConnectorUnitTest {

private static final CloudSQLMySQLConnector CONNECTOR = new CloudSQLMySQLConnector(
new CloudSQLMySQLConnectorConfig("user", "password", "cloudsql-mysql", "", "public",
"project:region:instance", "db", null));

/**
* Unit test for getTableName()
*/
@Test
public void getTableNameTest() {
Assert.assertEquals("`db`.`table`",
CONNECTOR.getTableName("db", null, "table"));
}

/**
* Unit test for getTableName() with identifiers containing backticks
*/
@Test
public void getTableNameEscapesBackticksTest() {
Assert.assertEquals("`my``db`.`t``; DELETE FROM x; --`",
CONNECTOR.getTableName("my`db", null, "t`; DELETE FROM x; --"));
}
}
2 changes: 1 addition & 1 deletion cloudsql-postgresql-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>database-plugins-parent</artifactId>
<groupId>io.cdap.plugin</groupId>
<version>1.11.16</version>
<version>1.11.17-SNAPSHOT</version>
</parent>

<name>CloudSQL PostgreSQL plugin</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected SchemaReader getSchemaReader(String sessionID) {

@Override
protected String getTableName(String database, String schema, String table) {
return String.format("\"%s\".\"%s\"", schema, table);
return quoteIdentifier(schema, '"') + "." + quoteIdentifier(table, '"');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright © 2026 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.plugin.cloudsql.postgres;

import org.junit.Assert;
import org.junit.Test;

/**
* Unit tests for {@link CloudSQLPostgreSQLConnector}
*/
public class CloudSQLPostgreSQLConnectorUnitTest {

private static final CloudSQLPostgreSQLConnector CONNECTOR = new CloudSQLPostgreSQLConnector(
new CloudSQLPostgreSQLConnectorConfig("user", "password", "cloudsql-postgresql", "", "public",
"project:region:instance", "db", null));

/**
* Unit test for getTableName()
*/
@Test
public void getTableNameTest() {
Assert.assertEquals("\"schema\".\"table\"",
CONNECTOR.getTableName("db", "schema", "table"));
}

/**
* Unit test for getTableName() with identifiers containing double quotes, using the payload from the
* reported sample path SQL injection
*/
@Test
public void getTableNameEscapesQuotesTest() {
String table = "pg_class\" LIMIT 0; UPDATE audit_vault.write_marker SET value='INJECTED_BY_SAMPLE_PATH' " +
"WHERE id=1; SELECT * FROM \"pg_catalog\".\"pg_class";
Assert.assertEquals("\"pg_catalog\".\"pg_class\"\" LIMIT 0; UPDATE audit_vault.write_marker SET " +
"value='INJECTED_BY_SAMPLE_PATH' WHERE id=1; SELECT * FROM \"\"pg_catalog\"\".\"\"pg_class\"",
CONNECTOR.getTableName("db", "pg_catalog", table));
Assert.assertEquals("\"my\"\"schema\".\"table\"",
CONNECTOR.getTableName("db", "my\"schema", "table"));
}
}
7 changes: 6 additions & 1 deletion database-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<parent>
<artifactId>database-plugins-parent</artifactId>
<groupId>io.cdap.plugin</groupId>
<version>1.11.16</version>
<version>1.11.17-SNAPSHOT</version>
</parent>

<name>Database Commons</name>
Expand Down Expand Up @@ -64,6 +64,11 @@
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ConnectionConfigAccessor {
private static final String CONNECTION_ARGUMENTS = "io.cdap.plugin.db.connection.arguments";
private static final String INIT_QUERIES = "io.cdap.plugin.db.init.queries";
public static final String AUTO_COMMIT_ENABLED = "io.cdap.plugin.db.output.autocommit.enabled";
public static final String READ_ONLY = "io.cdap.plugin.db.readonly";
public static final String FETCH_SIZE = "io.cdap.plugin.db.fetch.size";
public static final String OPERATION_NAME = "io.cdap.plugin.db.operation.name";
public static final String RELATION_TABLE_KEY = "io.cdap.plugin.db.relation.table.key";
Expand Down Expand Up @@ -101,6 +102,14 @@ public boolean isAutoCommitEnabled() {
return configuration.getBoolean(AUTO_COMMIT_ENABLED, false);
}

public void setReadOnly(boolean readOnly) {
configuration.setBoolean(READ_ONLY, readOnly);
}

public boolean isReadOnly() {
return configuration.getBoolean(READ_ONLY, false);
}

public void setFetchSize(Integer fetchSize) {
configuration.setInt(FETCH_SIZE, fetchSize);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright © 2026 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.plugin.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
* A {@link NoOpCommitConnection} for read-only operations whose {@link #commit()} is a no-op and whose {@link #close()}
* rolls back any active transaction before closing, so Hadoop's {@code DBRecordReader#close()} (which calls
* {@code connection.commit(); connection.close();} without a {@code finally} block) or an implicit driver
* commit-on-close can never commit changes or skip closing the connection when rollback fails. Callers must disable
* auto-commit ({@code setAutoCommit(false)}) on the delegate connection before executing statements.
*/
public class RollbackOnCloseConnection extends NoOpCommitConnection {

private static final Logger LOG = LoggerFactory.getLogger(RollbackOnCloseConnection.class);

public RollbackOnCloseConnection(Connection delegate) {
super(delegate);
}

@Override
public void close() throws SQLException {
try {
if (!getDelegate().isClosed() && !getDelegate().getAutoCommit()) {
getDelegate().rollback();
}
} catch (SQLException e) {
LOG.warn("Failed to roll back read-only connection ({}) before close.",
getDelegate().getClass().getName(), e);
} finally {
super.close();
}
}
}
Loading
Loading