-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added catalog and schema level access checks in USE statement #24182
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than using try...catch, I would recommend using TestNG's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have changed the code accordingly |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* 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 com.facebook.presto.execution; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.connector.MockConnectorFactory; | ||
import com.facebook.presto.metadata.Catalog; | ||
import com.facebook.presto.metadata.CatalogManager; | ||
import com.facebook.presto.metadata.MetadataManager; | ||
import com.facebook.presto.spi.ConnectorId; | ||
import com.facebook.presto.spi.connector.Connector; | ||
import com.facebook.presto.spi.security.AccessControl; | ||
import com.facebook.presto.spi.security.AccessDeniedException; | ||
import com.facebook.presto.spi.security.AllowAllAccessControl; | ||
import com.facebook.presto.spi.security.DenyAllAccessControl; | ||
import com.facebook.presto.spi.security.Identity; | ||
import com.facebook.presto.sql.analyzer.SemanticException; | ||
import com.facebook.presto.sql.tree.Identifier; | ||
import com.facebook.presto.sql.tree.Use; | ||
import com.facebook.presto.testing.TestingConnectorContext; | ||
import com.facebook.presto.transaction.TransactionManager; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
import static com.facebook.airlift.concurrent.Threads.daemonThreadsNamed; | ||
import static com.facebook.presto.SessionTestUtils.TEST_SESSION; | ||
import static com.facebook.presto.execution.TaskTestUtils.createQueryStateMachine; | ||
import static com.facebook.presto.metadata.MetadataManager.createTestMetadataManager; | ||
import static com.facebook.presto.spi.ConnectorId.createInformationSchemaConnectorId; | ||
import static com.facebook.presto.spi.ConnectorId.createSystemTablesConnectorId; | ||
import static com.facebook.presto.testing.TestingSession.testSessionBuilder; | ||
import static com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager; | ||
import static java.util.Collections.emptyList; | ||
import static java.util.concurrent.Executors.newCachedThreadPool; | ||
|
||
@Test(singleThreaded = true) | ||
public class TestUseTask | ||
{ | ||
private final ExecutorService executor = newCachedThreadPool(daemonThreadsNamed("test-%s")); | ||
private CatalogManager catalogManager; | ||
private TransactionManager transactionManager; | ||
private MetadataManager metadata = createTestMetadataManager(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are already creating this in |
||
MockConnectorFactory.Builder builder = MockConnectorFactory.builder(); | ||
MockConnectorFactory mockConnectorFactory = builder.withListSchemaNames(connectorSession -> ImmutableList.of("test_schema")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you create these in |
||
.build(); | ||
|
||
@BeforeClass | ||
public void setUp() | ||
{ | ||
catalogManager = new CatalogManager(); | ||
transactionManager = createTestTransactionManager(catalogManager); | ||
metadata = createTestMetadataManager(transactionManager); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can these be created during There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added it in |
||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
BryanCutler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public void tearDown() | ||
{ | ||
executor.shutdownNow(); | ||
} | ||
|
||
@Test | ||
public void testUse() | ||
{ | ||
Use use = new Use(Optional.of(identifier("test_catalog")), identifier("test_schema")); | ||
String sqlString = "USE test_catalog.test_schema"; | ||
executeUse(use, sqlString, TEST_SESSION); | ||
} | ||
|
||
@Test( | ||
expectedExceptions = SemanticException.class, | ||
expectedExceptionsMessageRegExp = "Catalog must be specified when session catalog is not set") | ||
public void testUseNoCatalog() | ||
{ | ||
Use use = new Use(Optional.empty(), identifier("test_schema")); | ||
String sqlString = "USE test_schema"; | ||
Session session = testSessionBuilder() | ||
.setCatalog(null) | ||
.setSchema(null) | ||
.build(); | ||
executeUse(use, sqlString, session); | ||
} | ||
|
||
@Test( | ||
expectedExceptions = SemanticException.class, | ||
expectedExceptionsMessageRegExp = "Catalog does not exist: invalid_catalog") | ||
public void testUseInvalidCatalog() | ||
{ | ||
Use use = new Use(Optional.of(identifier("invalid_catalog")), identifier("test_schema")); | ||
String sqlString = "USE invalid_catalog.test_schema"; | ||
executeUse(use, sqlString, TEST_SESSION); | ||
} | ||
|
||
@Test( | ||
expectedExceptions = SemanticException.class, | ||
expectedExceptionsMessageRegExp = "Schema does not exist: test_catalog.invalid_schema") | ||
public void testUseInvalidSchema() | ||
{ | ||
Use use = new Use(Optional.of(identifier("test_catalog")), identifier("invalid_schema")); | ||
String sqlString = "USE test_catalog.invalid_schema"; | ||
Session session = testSessionBuilder() | ||
.setSchema("invalid_schema") | ||
.build(); | ||
executeUse(use, sqlString, session); | ||
} | ||
|
||
@Test( | ||
expectedExceptions = AccessDeniedException.class, | ||
expectedExceptionsMessageRegExp = "Access Denied: Cannot access catalog test_catalog") | ||
public void testUseAccessDenied() | ||
{ | ||
Use use = new Use(Optional.of(identifier("test_catalog")), identifier("test_schema")); | ||
String sqlString = "USE test_catalog.test_schema"; | ||
Session session = testSessionBuilder() | ||
.setIdentity(new Identity("user", Optional.empty())) | ||
.build(); | ||
AccessControl accessControl = new DenyAllAccessControl(); | ||
executeUse(use, sqlString, session, accessControl); | ||
} | ||
|
||
private void executeUse(Use use, String sqlString, Session session) | ||
{ | ||
executeUse(use, sqlString, session, new AllowAllAccessControl()); | ||
} | ||
|
||
private void executeUse(Use use, String sqlString, Session session, AccessControl accessControl) | ||
{ | ||
catalogManager = new CatalogManager(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we have already created these in the beforeClass method, do we need to create them again here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we gave that only in beforeClass we need to give that in execute () otherwise any changes made by one test (e.g., catalog registrations, state updates) can interfere with subsequent tests. |
||
transactionManager = createTestTransactionManager(catalogManager); | ||
metadata = createTestMetadataManager(transactionManager); | ||
|
||
Connector testConnector = mockConnectorFactory.create("test", ImmutableMap.of(), new TestingConnectorContext()); | ||
String catalogName = "test_catalog"; | ||
ConnectorId connectorId = new ConnectorId(catalogName); | ||
catalogManager.registerCatalog(new Catalog( | ||
catalogName, | ||
connectorId, | ||
testConnector, | ||
createInformationSchemaConnectorId(connectorId), | ||
testConnector, | ||
createSystemTablesConnectorId(connectorId), | ||
testConnector)); | ||
|
||
QueryStateMachine stateMachine = createQueryStateMachine(sqlString, session, false, transactionManager, executor, metadata); | ||
UseTask useTask = new UseTask(); | ||
useTask.execute(use, transactionManager, metadata, accessControl, stateMachine, emptyList()); | ||
} | ||
|
||
private Identifier identifier(String name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add an empty line before this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added new line |
||
{ | ||
return new Identifier(name); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be moved to
checkAndSetCatalog
before callingstateMachine.setSetCatalog(catalog);
?