Skip to content

Commit

Permalink
Fix create schema in glue catalog when username contains uppercase char
Browse files Browse the repository at this point in the history
TrinoPrincipal sets the owner name in lowercase.
  • Loading branch information
krvikash authored and ebyhr committed Apr 6, 2023
1 parent 0f91cfa commit bb4294b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,15 @@ public void testSchemaAuthorizationForRole()
assertUpdate(admin, "DROP ROLE authorized_users IN hive");
}

@Override
public void testCreateSchemaWithNonLowercaseOwnerName()
{
// Override because HivePrincipal's username is case-sensitive unlike TrinoPrincipal
assertThatThrownBy(super::testCreateSchemaWithNonLowercaseOwnerName)
.hasMessageContaining("Access Denied: Cannot create schema")
.hasStackTraceContaining("CREATE SCHEMA");
}

@Test
public void testCreateSchemaWithAuthorizationForUser()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,13 @@ public void testShowCreateTable()
" format = 'ORC'\n" +
")");
}

@Override
public void testCreateSchemaWithNonLowercaseOwnerName()
{
// Override because HivePrincipal's username is case-sensitive unlike TrinoPrincipal
assertThatThrownBy(super::testCreateSchemaWithNonLowercaseOwnerName)
.hasMessageContaining("Access Denied: Cannot create schema")
.hasStackTraceContaining("CREATE SCHEMA");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public Optional<TrinoPrincipal> getNamespacePrincipal(ConnectorSession session,
public void createNamespace(ConnectorSession session, String namespace, Map<String, Object> properties, TrinoPrincipal owner)
{
checkArgument(owner.getType() == PrincipalType.USER, "Owner type must be USER");
checkArgument(owner.getName().equals(session.getUser()), "Explicit schema owner is not supported");
checkArgument(owner.getName().equals(session.getUser().toLowerCase(ENGLISH)), "Explicit schema owner is not supported");

try {
stats.getCreateDatabase().call(() ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void testCreateSchema()
.hasMessage("Creating schema in Kudu connector not allowed if schema emulation is disabled.");
}

@Override
public void testCreateSchemaWithNonLowercaseOwnerName()
{
assertThatThrownBy(super::testCreateSchemaWithNonLowercaseOwnerName)
.hasMessage("Creating schema in Kudu connector not allowed if schema emulation is disabled.");
}

@Test
@Override
public void testRenameTableAcrossSchemas()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public void testCreateSchema()
.hasMessage("Creating schema in Kudu connector not allowed if schema emulation is disabled.");
}

@Override
public void testCreateSchemaWithNonLowercaseOwnerName()
{
assertThatThrownBy(super::testCreateSchemaWithNonLowercaseOwnerName)
.hasMessage("Creating schema in Kudu connector not allowed if schema emulation is disabled.");
}

@Override
public void testCreateSchemaWithLongName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package io.trino.testing;

import com.google.common.collect.ImmutableList;
import io.trino.Session;
import io.trino.spi.security.Identity;
import io.trino.testing.sql.TestTable;
import io.trino.tpch.TpchTable;
import org.testng.SkipException;
Expand Down Expand Up @@ -315,6 +317,22 @@ public void testCreateSchema()
assertUpdate("DROP SCHEMA " + schemaName);
}

@Test
public void testCreateSchemaWithNonLowercaseOwnerName()
{
skipTestUnless(hasBehavior(SUPPORTS_CREATE_SCHEMA));

Session newSession = Session.builder(getSession())
.setIdentity(Identity.ofUser("ADMIN"))
.build();
String schemaName = "test_schema_create_uppercase_owner_name_" + randomNameSuffix();
assertUpdate(newSession, createSchemaSql(schemaName));
assertThat(query(newSession, "SHOW SCHEMAS"))
.skippingTypesCheck()
.containsAll(format("VALUES '%s'", schemaName));
assertUpdate(newSession, "DROP SCHEMA " + schemaName);
}

@Test
public void testRenameSchema()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.trino.server.BasicQueryInfo;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.MaterializedViewFreshness;
import io.trino.spi.security.Identity;
import io.trino.sql.planner.OptimizerConfig.JoinDistributionType;
import io.trino.sql.planner.Plan;
import io.trino.sql.planner.assertions.PlanMatchPattern;
Expand Down Expand Up @@ -2881,6 +2882,22 @@ public void testCreateTable()
assertFalse(getQueryRunner().tableExists(getSession(), tableNameLike));
}

@Test
public void testCreateSchemaWithNonLowercaseOwnerName()
{
skipTestUnless(hasBehavior(SUPPORTS_CREATE_SCHEMA));

Session newSession = Session.builder(getSession())
.setIdentity(Identity.ofUser("ADMIN"))
.build();
String schemaName = "test_schema_create_uppercase_owner_name_" + randomNameSuffix();
assertUpdate(newSession, createSchemaSql(schemaName));
assertThat(query(newSession, "SHOW SCHEMAS"))
.skippingTypesCheck()
.containsAll(format("VALUES '%s'", schemaName));
assertUpdate(newSession, "DROP SCHEMA " + schemaName);
}

@Test
public void testCreateSchemaWithLongName()
{
Expand Down

0 comments on commit bb4294b

Please sign in to comment.