Search in sources :

Example 31 with DatasetModuleId

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);
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) Transaction(org.apache.tephra.Transaction) KeyStructValueTableDefinition(io.cdap.cdap.explore.service.datasets.KeyStructValueTableDefinition) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 32 with DatasetModuleId

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
    }
}
Also used : LineageWriterDatasetFramework(io.cdap.cdap.data2.metadata.writer.LineageWriterDatasetFramework) DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) DatasetModule(io.cdap.cdap.api.dataset.module.DatasetModule) InstanceConflictException(io.cdap.cdap.api.dataset.InstanceConflictException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) Test(org.junit.Test)

Example 33 with DatasetModuleId

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));
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) DatasetTypeId(io.cdap.cdap.proto.id.DatasetTypeId) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) StandardPermission(io.cdap.cdap.proto.security.StandardPermission) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 34 with DatasetModuleId

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);
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) DatasetTypeId(io.cdap.cdap.proto.id.DatasetTypeId) StandardPermission(io.cdap.cdap.proto.security.StandardPermission) DatasetId(io.cdap.cdap.proto.id.DatasetId) Location(org.apache.twill.filesystem.Location) Test(org.junit.Test)

Example 35 with DatasetModuleId

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);
}
Also used : DatasetModuleId(io.cdap.cdap.proto.id.DatasetModuleId) Table(io.cdap.cdap.cli.util.table.Table) DatasetModuleMeta(io.cdap.cdap.proto.DatasetModuleMeta) RowMaker(io.cdap.cdap.cli.util.RowMaker)

Aggregations

DatasetModuleId (io.cdap.cdap.proto.id.DatasetModuleId)48 Test (org.junit.Test)20 DatasetModuleMeta (io.cdap.cdap.proto.DatasetModuleMeta)16 DatasetId (io.cdap.cdap.proto.id.DatasetId)14 DatasetTypeId (io.cdap.cdap.proto.id.DatasetTypeId)14 DatasetModule (io.cdap.cdap.api.dataset.module.DatasetModule)8 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)8 IOException (java.io.IOException)8 DatasetTypeTable (io.cdap.cdap.data2.datafabric.dataset.service.mds.DatasetTypeTable)6 DatasetTypeMeta (io.cdap.cdap.proto.DatasetTypeMeta)6 StandardPermission (io.cdap.cdap.proto.security.StandardPermission)6 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)6 HashSet (java.util.HashSet)6 Location (org.apache.twill.filesystem.Location)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)4 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)4 DatasetInstanceTable (io.cdap.cdap.data2.datafabric.dataset.service.mds.DatasetInstanceTable)4 TypeConflictException (io.cdap.cdap.data2.dataset2.TypeConflictException)4 Principal (io.cdap.cdap.proto.security.Principal)4