use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class IntegrationTestBaseTest method testDeployApplicationInNamespace.
@Test
public void testDeployApplicationInNamespace() throws Exception {
NamespaceId namespace = new NamespaceId("Test1");
NamespaceMeta namespaceMeta = new NamespaceMeta.Builder().setName(namespace).build();
getNamespaceClient().create(namespaceMeta);
ClientConfig clientConfig = new ClientConfig.Builder(getClientConfig()).build();
deployApplication(namespace, TestApplication.class);
// Check the default namespaces applications to see whether the application wasnt made in the default namespace
ClientConfig defaultClientConfig = new ClientConfig.Builder(getClientConfig()).build();
Assert.assertEquals(0, new ApplicationClient(defaultClientConfig).list(NamespaceId.DEFAULT).size());
ApplicationClient applicationClient = new ApplicationClient(clientConfig);
Assert.assertEquals(TestApplication.NAME, applicationClient.list(namespace).get(0).getName());
applicationClient.delete(namespace.app(TestApplication.NAME));
Assert.assertEquals(0, new ApplicationClient(clientConfig).list(namespace).size());
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class AuditMessageTest method testAccessMessage.
@Test
public void testAccessMessage() throws Exception {
String flowAccessJson = "{\"version\":1,\"time\":2000,\"entityId\":{\"namespace\":\"ns1\",\"stream\":\"stream1\"," + "\"entity\":\"STREAM\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"WRITE\"," + "\"accessor\":{\"namespace\":\"ns1\",\"application\":\"app1\",\"version\":\"v1\",\"type\":\"Flow\"," + "\"program\":\"flow1\",\"run\":\"run1\",\"entity\":\"PROGRAM_RUN\"}}}";
AuditMessage flowAccess = new AuditMessage(2000L, new NamespaceId("ns1").stream("stream1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.WRITE, new NamespaceId("ns1").app("app1", "v1").flow("flow1").run("run1")));
Assert.assertEquals(jsonToMap(flowAccessJson), jsonToMap(GSON.toJson(flowAccess)));
Assert.assertEquals(flowAccess, GSON.fromJson(flowAccessJson, AuditMessage.class));
String exploreAccessJson = "{\"version\":1,\"time\":2500,\"entityId\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\",\"entity\":\"DATASET\"}," + "\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"UNKNOWN\"," + "\"accessor\":{\"service\":\"explore\",\"entity\":\"SYSTEM_SERVICE\"}}}";
AuditMessage exploreAccess = new AuditMessage(2500L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.UNKNOWN, new SystemServiceId("explore")));
Assert.assertEquals(jsonToMap(exploreAccessJson), jsonToMap(GSON.toJson(exploreAccess)));
Assert.assertEquals(exploreAccess, GSON.fromJson(exploreAccessJson, AuditMessage.class));
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class AuditMessageTest method testMetadataChange.
@Test
public void testMetadataChange() throws Exception {
String metadataJson = "{\"version\":1,\"time\":3000,\"entityId\":{\"namespace\":\"ns1\",\"application\":\"app1\",\"version\":\"v1\"," + "\"entity\":\"APPLICATION\"},\"user\":\"user1\",\"type\":\"METADATA_CHANGE\",\"payload\":{" + "\"previous\":{\"USER\":{\"properties\":{\"uk\":\"uv\",\"uk1\":\"uv2\"},\"tags\":[\"ut1\",\"ut2\"]}," + "\"SYSTEM\":{\"properties\":{\"sk\":\"sv\"},\"tags\":[]}}," + "\"additions\":{\"SYSTEM\":{\"properties\":{\"sk\":\"sv\"},\"tags\":[\"t1\",\"t2\"]}}," + "\"deletions\":{\"USER\":{\"properties\":{\"uk\":\"uv\"},\"tags\":[\"ut1\"]}}}}";
Map<String, String> userProperties = new HashMap<>();
userProperties.put("uk", "uv");
userProperties.put("uk1", "uv2");
Map<String, String> systemProperties = new HashMap<>();
systemProperties.put("sk", "sv");
Set<String> userTags = new LinkedHashSet<>();
userTags.add("ut1");
userTags.add("ut2");
Map<MetadataScope, Metadata> previous = new LinkedHashMap<>();
previous.put(MetadataScope.USER, new Metadata(Collections.unmodifiableMap(userProperties), Collections.unmodifiableSet(userTags)));
previous.put(MetadataScope.SYSTEM, new Metadata(Collections.unmodifiableMap(systemProperties), Collections.unmodifiableSet(new LinkedHashSet<String>())));
Map<String, String> sysPropertiesAdded = new HashMap<>();
sysPropertiesAdded.put("sk", "sv");
Set<String> systemTagsAdded = new LinkedHashSet<>();
systemTagsAdded.add("t1");
systemTagsAdded.add("t2");
Map<MetadataScope, Metadata> additions = new HashMap<>();
additions.put(MetadataScope.SYSTEM, new Metadata(Collections.unmodifiableMap(sysPropertiesAdded), Collections.unmodifiableSet(systemTagsAdded)));
Map<String, String> userPropertiesDeleted = new HashMap<>();
userPropertiesDeleted.put("uk", "uv");
Set<String> userTagsDeleted = new LinkedHashSet<>();
userTagsDeleted.add("ut1");
Map<MetadataScope, Metadata> deletions = new HashMap<>();
deletions.put(MetadataScope.USER, new Metadata(Collections.unmodifiableMap(userPropertiesDeleted), Collections.unmodifiableSet(userTagsDeleted)));
AuditMessage metadataChange = new AuditMessage(3000L, new NamespaceId("ns1").app("app1", "v1"), "user1", AuditType.METADATA_CHANGE, new MetadataPayload(previous, additions, deletions));
Assert.assertEquals(jsonToMap(metadataJson), jsonToMap(GSON.toJson(metadataChange)));
Assert.assertEquals(metadataChange, GSON.fromJson(metadataJson, AuditMessage.class));
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class AuthorizerTest method testRBAC.
@Test
public void testRBAC() throws Exception {
Authorizer authorizer = get();
Role admins = new Role("admins");
Role engineers = new Role("engineers");
// create a role
authorizer.createRole(admins);
// add another role
authorizer.createRole(engineers);
// listing role should show the added role
Set<Role> roles = authorizer.listAllRoles();
Set<Role> expectedRoles = new HashSet<>();
expectedRoles.add(admins);
expectedRoles.add(engineers);
Assert.assertEquals(expectedRoles, roles);
// creating a role which already exists should throw an exception
try {
authorizer.createRole(admins);
Assert.fail(String.format("Created a role %s which already exists. Should have failed.", admins.getName()));
} catch (RoleAlreadyExistsException expected) {
// expected
}
// drop an existing role
authorizer.dropRole(admins);
// the list should not have the dropped role
roles = authorizer.listAllRoles();
Assert.assertEquals(Collections.singleton(engineers), roles);
// dropping a non-existing role should throw exception
try {
authorizer.dropRole(admins);
Assert.fail(String.format("Dropped a role %s which does not exists. Should have failed.", admins.getName()));
} catch (RoleNotFoundException expected) {
// expected
}
// add an user to an existing role
Principal spiderman = new Principal("spiderman", Principal.PrincipalType.USER);
authorizer.addRoleToPrincipal(engineers, spiderman);
// add an user to an non-existing role should throw an exception
try {
authorizer.addRoleToPrincipal(admins, spiderman);
Assert.fail(String.format("Added role %s to principal %s. Should have failed.", admins, spiderman));
} catch (RoleNotFoundException expected) {
// expectedRoles
}
// check listing roles for spiderman have engineers role
Assert.assertEquals(Collections.singleton(engineers), authorizer.listRoles(spiderman));
// authorization checks with roles
NamespaceId ns1 = new NamespaceId("ns1");
// check that spiderman who has engineers roles cannot read from ns1
verifyAuthFailure(ns1, spiderman, Action.READ);
// give a permission to engineers role
authorizer.grant(ns1, engineers, Collections.singleton(Action.READ));
// check that a spiderman who has engineers role has access
authorizer.enforce(ns1, spiderman, Action.READ);
// list privileges for spiderman should have read action on ns1
Assert.assertEquals(Collections.singleton(new Privilege(ns1, Action.READ)), authorizer.listPrivileges(spiderman));
// revoke action from the role
authorizer.revoke(ns1, engineers, Collections.singleton(Action.READ));
// now the privileges for spiderman should be empty
Assert.assertEquals(Collections.EMPTY_SET, authorizer.listPrivileges(spiderman));
// check that the user of this role is not authorized to do the revoked operation
verifyAuthFailure(ns1, spiderman, Action.READ);
// remove an user from a existing role
authorizer.removeRoleFromPrincipal(engineers, spiderman);
// check listing roles for spiderman should be empty
Assert.assertEquals(Collections.EMPTY_SET, authorizer.listRoles(spiderman));
// remove an user from a non-existing role should throw exception
try {
authorizer.removeRoleFromPrincipal(admins, spiderman);
Assert.fail(String.format("Removed non-existing role %s from principal %s. Should have failed.", admins, spiderman));
} catch (RoleNotFoundException expected) {
// expectedRoles
}
}
use of co.cask.cdap.proto.id.NamespaceId in project cdap by caskdata.
the class FlowQueuePendingCorrector method main.
public static void main(String[] args) throws Exception {
CommandLine cmd = parseArgs(args);
FlowQueuePendingCorrector corrector = createCorrector();
corrector.startAndWait();
try {
String namespace = cmd.getOptionValue("namespace");
String app = cmd.getOptionValue("app");
String flow = cmd.getOptionValue("flow");
if (!cmd.hasOption("namespace")) {
corrector.run();
} else if (!cmd.hasOption("app")) {
corrector.run(new NamespaceId(cmd.getOptionValue("namespace")));
} else if (!cmd.hasOption("flow")) {
Preconditions.checkArgument(cmd.hasOption("namespace"));
corrector.run(new ApplicationId(cmd.getOptionValue("namespace"), cmd.getOptionValue("app")));
} else if (!cmd.hasOption("producer-flowlet") && !cmd.hasOption("consumer-flowlet")) {
corrector.run(new FlowId(cmd.getOptionValue("namespace"), cmd.getOptionValue("app"), cmd.getOptionValue("flow")));
} else {
Preconditions.checkArgument(cmd.hasOption("producer-flowlet"), "Missing producer-flowlet option");
Preconditions.checkArgument(cmd.hasOption("consumer-flowlet"), "Missing consumer-flowlet option");
String producerFlowlet = cmd.getOptionValue("producer-flowlet");
String consumerFlowlet = cmd.getOptionValue("consumer-flowlet");
String queue = cmd.getOptionValue("queue", "queue");
corrector.run(new FlowId(namespace, app, flow), producerFlowlet, consumerFlowlet, queue);
}
} finally {
corrector.stopAndWait();
}
}
Aggregations