use of io.cdap.cdap.proto.id.DatasetModuleId in project cdap by caskdata.
the class ExploreDisabledTest method testDeployRecordScannable.
@Test
public void testDeployRecordScannable() throws Exception {
// Try to deploy a dataset that is not record scannable, when explore is enabled.
// This should be processed with no exception being thrown
DatasetModuleId module1 = new DatasetModuleId(namespaceId.getNamespace(), "module1");
DatasetId instance1 = namespaceId.dataset("table1");
datasetFramework.addModule(module1, new KeyStructValueTableDefinition.KeyStructValueTableModule());
// Performing admin operations to create dataset instance
datasetFramework.addInstance("keyStructValueTable", instance1, DatasetProperties.EMPTY);
Transaction tx1 = transactionManager.startShort(100);
// Accessing dataset instance to perform data operations
KeyStructValueTableDefinition.KeyStructValueTable table = datasetFramework.getDataset(instance1, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
table.startTx(tx1);
KeyStructValueTableDefinition.KeyValue.Value value1 = new KeyStructValueTableDefinition.KeyValue.Value("first", Lists.newArrayList(1, 2, 3, 4, 5));
KeyStructValueTableDefinition.KeyValue.Value value2 = new KeyStructValueTableDefinition.KeyValue.Value("two", Lists.newArrayList(10, 11, 12, 13, 14));
table.put("1", value1);
table.put("2", value2);
Assert.assertEquals(value1, table.get("1"));
Assert.assertTrue(table.commitTx());
transactionManager.canCommit(tx1.getTransactionId(), table.getTxChanges());
transactionManager.commit(tx1.getTransactionId(), tx1.getWritePointer());
table.postTxCommit();
Transaction tx2 = transactionManager.startShort(100);
table.startTx(tx2);
Assert.assertEquals(value1, table.get("1"));
datasetFramework.deleteInstance(instance1);
datasetFramework.deleteModule(module1);
}
use of io.cdap.cdap.proto.id.DatasetModuleId in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testNamespaceModuleIsolation.
@Test
public void testNamespaceModuleIsolation() throws Exception {
DatasetFramework framework = getFramework();
// create 2 namespaces
NamespaceId namespace1 = new NamespaceId("ns1");
NamespaceId namespace2 = new NamespaceId("ns2");
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace1).build());
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace2).build());
namespacePathLocator.get(namespace1).mkdirs();
namespacePathLocator.get(namespace2).mkdirs();
// add modules in each namespace, with one module that shares the same name
DatasetModuleId simpleModuleNs1 = namespace1.datasetModule(SimpleKVTable.class.getName());
DatasetModuleId simpleModuleNs2 = namespace2.datasetModule(SimpleKVTable.class.getName());
DatasetModuleId doubleModuleNs2 = namespace2.datasetModule(DoubleWrappedKVTable.class.getName());
DatasetModule module1 = new SingleTypeModule(SimpleKVTable.class);
DatasetModule module2 = new SingleTypeModule(DoubleWrappedKVTable.class);
framework.addModule(simpleModuleNs1, module1);
framework.addModule(simpleModuleNs2, module1);
framework.addModule(doubleModuleNs2, module2);
// check that we can add instances of datasets in those modules
framework.addInstance(SimpleKVTable.class.getName(), namespace1.dataset("kv1"), DatasetProperties.EMPTY);
framework.addInstance(SimpleKVTable.class.getName(), namespace2.dataset("kv1"), DatasetProperties.EMPTY);
// check that only namespace2 can add an instance of this type, since the module should only be in namespace2
framework.addInstance(DoubleWrappedKVTable.class.getName(), namespace2.dataset("kv2"), DatasetProperties.EMPTY);
try {
framework.addInstance(DoubleWrappedKVTable.class.getName(), namespace2.dataset("kv2"), DatasetProperties.EMPTY);
Assert.fail();
} catch (Exception e) {
// expected
}
// check that deleting all modules from namespace2 does not affect namespace1
framework.deleteAllInstances(namespace2);
framework.deleteAllModules(namespace2);
// should still be able to add an instance in namespace1
framework.addInstance(SimpleKVTable.class.getName(), namespace1.dataset("kv3"), DatasetProperties.EMPTY);
// but not in namespace2
try {
framework.addInstance(SimpleKVTable.class.getName(), namespace2.dataset("kv3"), DatasetProperties.EMPTY);
Assert.fail();
} catch (Exception e) {
// expected
}
// add back modules to namespace2
framework.addModule(simpleModuleNs2, module1);
framework.addModule(doubleModuleNs2, module2);
// check that deleting a single module from namespace1 does not affect namespace2
framework.deleteAllInstances(namespace1);
framework.deleteModule(simpleModuleNs1);
// should still be able to add an instance in namespace2
framework.addInstance(DoubleWrappedKVTable.class.getName(), namespace2.dataset("kv1"), DatasetProperties.EMPTY);
// but not in namespace1
try {
framework.addInstance(SimpleKVTable.class.getName(), namespace1.dataset("kv1"), DatasetProperties.EMPTY);
Assert.fail();
} catch (Exception e) {
// expected
}
}
use of io.cdap.cdap.proto.id.DatasetModuleId in project cdap by caskdata.
the class DatasetServiceAuthorizationTest method testNotFound.
@Test
public void testNotFound() throws Exception {
final DatasetId nonExistingInstance = NamespaceId.DEFAULT.dataset("notfound");
final DatasetModuleId nonExistingModule = NamespaceId.DEFAULT.datasetModule("notfound");
final DatasetTypeId nonExistingType = NamespaceId.DEFAULT.datasetType("notfound");
try {
// user will not be able to get the info about the instance since he does not have any privilege on the instance
dsFramework.getDatasetSpec(nonExistingInstance);
Assert.fail();
} catch (UnauthorizedException e) {
// Expected
}
try {
// user will not be able to check the existence on the instance since he does not have any privilege on the
// instance
dsFramework.hasInstance(nonExistingInstance);
Assert.fail();
} catch (UnauthorizedException e) {
// expected
}
SecurityRequestContext.setUserId(ALICE.getName());
// user need to have access to the dataset to do any operations, even though the dataset does not exist
grantAndAssertSuccess(nonExistingInstance, ALICE, EnumSet.allOf(StandardPermission.class));
grantAndAssertSuccess(nonExistingModule, ALICE, EnumSet.allOf(StandardPermission.class));
// after grant user should be able to check the dataset info
Assert.assertNull(dsFramework.getDatasetSpec(nonExistingInstance));
Assert.assertFalse(dsFramework.hasInstance(nonExistingInstance));
assertNotFound(() -> dsFramework.updateInstance(nonExistingInstance, DatasetProperties.EMPTY), String.format("Expected %s to not exist", nonExistingInstance));
assertNotFound(() -> dsFramework.deleteInstance(nonExistingInstance), String.format("Expected %s to not exist", nonExistingInstance));
assertNotFound(() -> dsFramework.truncateInstance(nonExistingInstance), String.format("Expected %s to not exist", nonExistingInstance));
assertAuthorizationFailure(() -> dsFramework.addInstance(nonExistingType.getType(), nonExistingInstance, DatasetProperties.EMPTY), "Alice needs to have READ/ADMIN on the dataset type to create the dataset");
assertNotFound(() -> dsFramework.deleteModule(nonExistingModule), String.format("Expected %s to not exist", nonExistingModule));
grantAndAssertSuccess(nonExistingType, ALICE, EnumSet.allOf(StandardPermission.class));
Assert.assertNull(String.format("Expected %s to not exist", nonExistingType), dsFramework.getTypeInfo(nonExistingType));
}
use of io.cdap.cdap.proto.id.DatasetModuleId in project cdap by caskdata.
the class DatasetServiceAuthorizationTest method testDatasetTypes.
@Test
public void testDatasetTypes() throws Exception {
final DatasetModuleId module1 = NamespaceId.DEFAULT.datasetModule("module1");
final DatasetModuleId module2 = NamespaceId.DEFAULT.datasetModule("module2");
final DatasetTypeId type1 = NamespaceId.DEFAULT.datasetType("datasetType1");
DatasetTypeId type1x = NamespaceId.DEFAULT.datasetType("datasetType1x");
final DatasetTypeId type2 = NamespaceId.DEFAULT.datasetType("datasetType2");
DatasetId datasetId = NamespaceId.DEFAULT.dataset("succeed");
SecurityRequestContext.setUserId(ALICE.getName());
final Location moduleJar = createModuleJar(TestModule1x.class);
assertAuthorizationFailure(() -> dsFramework.addModule(module1, new TestModule1x(), moduleJar), String.format("Expected module add operation to fail for %s because she does not have %s on %s", ALICE, StandardPermission.UPDATE, module1));
// grant alice ADMIN on module1
grantAndAssertSuccess(module1, ALICE, EnumSet.of(StandardPermission.UPDATE, StandardPermission.DELETE, StandardPermission.CREATE));
// grant all privileges needed to create a dataset
grantAndAssertSuccess(type1, ALICE, EnumSet.of(StandardPermission.UPDATE, StandardPermission.GET, StandardPermission.DELETE));
grantAndAssertSuccess(type1x, ALICE, EnumSet.of(StandardPermission.UPDATE, StandardPermission.GET, StandardPermission.DELETE));
grantAndAssertSuccess(datasetId, ALICE, EnumSet.allOf(StandardPermission.class));
dsFramework.addModule(module1, new TestModule1x(), moduleJar);
// all operations on module1 should succeed as alice
Assert.assertNotNull(dsFramework.getTypeInfo(type1));
Assert.assertNotNull(dsFramework.getTypeInfo(type1x));
// should be able to use the type from the module to add an instance as well
dsFramework.addInstance(type1x.getType(), datasetId, DatasetProperties.EMPTY);
// but should fail as Bob
SecurityRequestContext.setUserId(BOB.getName());
assertAuthorizationFailure(() -> dsFramework.addInstance(type1.getType(), NamespaceId.DEFAULT.dataset("fail"), DatasetProperties.EMPTY), String.format("Creating an instance of a type from %s should fail as %s does not have any privileges on it.", module1, BOB));
// granting CREATE on the module, BOB should now be able to create the dataset type
grantAndAssertSuccess(module2, BOB, EnumSet.of(StandardPermission.CREATE, StandardPermission.DELETE));
grantAndAssertSuccess(type2, BOB, EnumSet.of(StandardPermission.UPDATE, StandardPermission.GET));
// adding a module should now succeed as bob though, because bob has admin privilege on the module
dsFramework.addModule(module2, new TestModule2(), createModuleJar(TestModule2.class));
// get operation on module2 should succeed as Bob
Assert.assertNotNull(dsFramework.getTypeInfo(type2));
// but should fail as Alice since Alice does not have ADMIN on module2 or type2
SecurityRequestContext.setUserId(ALICE.getName());
assertAuthorizationFailure(() -> dsFramework.addInstance(type2.getType(), NamespaceId.DEFAULT.dataset("fail"), DatasetProperties.EMPTY), String.format("Creating an instance of a type from %s should fail as %s does not have any privileges on it.", module2, ALICE));
assertAuthorizationFailure(() -> dsFramework.deleteModule(module2), String.format("Deleting module %s should fail as %s does not have any privileges on it.", module2, ALICE));
SecurityRequestContext.setUserId(BOB.getName());
assertAuthorizationFailure(() -> dsFramework.deleteModule(module1), String.format("Deleting module %s should fail as %s does not have any privileges on it.", module1, BOB));
assertAuthorizationFailure(() -> dsFramework.deleteAllModules(NamespaceId.DEFAULT), String.format("Deleting all modules in %s should fail as %s does not have ADMIN privileges on it.", NamespaceId.DEFAULT, BOB));
// delete all instances so modules can be deleted
SecurityRequestContext.setUserId(ALICE.getName());
dsFramework.deleteAllInstances(NamespaceId.DEFAULT);
SecurityRequestContext.setUserId(BOB.getName());
// After granting admin on the modules, deleting all modules should succeed
grantAndAssertSuccess(module1, BOB, EnumSet.of(StandardPermission.UPDATE, StandardPermission.DELETE));
dsFramework.deleteAllModules(NamespaceId.DEFAULT);
}
use of io.cdap.cdap.proto.id.DatasetModuleId in project cdap by caskdata.
the class DescribeDatasetModuleCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
DatasetModuleId moduleId = cliConfig.getCurrentNamespace().datasetModule(arguments.get(ArgumentName.DATASET_MODULE.toString()));
DatasetModuleMeta datasetModuleMeta = datasetModuleClient.get(moduleId);
Table table = Table.builder().setHeader("name", "className", "jarLocationPath", "types", "usesModules", "usedByModules").setRows(ImmutableList.of(datasetModuleMeta), new RowMaker<DatasetModuleMeta>() {
@Override
public List<?> makeRow(DatasetModuleMeta object) {
return Lists.newArrayList(object.getName(), object.getClassName(), object.getJarLocationPath(), Joiner.on(", ").join(object.getTypes()), Joiner.on(", ").join(object.getUsesModules()), Joiner.on(", ").join(object.getUsedByModules()));
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Aggregations