Search in sources :

Example 96 with NamespaceId

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());
}
Also used : ApplicationClient(co.cask.cdap.client.ApplicationClient) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ClientConfig(co.cask.cdap.client.config.ClientConfig) Test(org.junit.Test)

Example 97 with NamespaceId

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));
}
Also used : SystemServiceId(co.cask.cdap.proto.id.SystemServiceId) AccessPayload(co.cask.cdap.proto.audit.payload.access.AccessPayload) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 98 with NamespaceId

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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Metadata(co.cask.cdap.proto.metadata.Metadata) LinkedHashMap(java.util.LinkedHashMap) NamespaceId(co.cask.cdap.proto.id.NamespaceId) MetadataScope(co.cask.cdap.proto.metadata.MetadataScope) MetadataPayload(co.cask.cdap.proto.audit.payload.metadata.MetadataPayload) Test(org.junit.Test)

Example 99 with NamespaceId

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
    }
}
Also used : Role(co.cask.cdap.proto.security.Role) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Privilege(co.cask.cdap.proto.security.Privilege) Principal(co.cask.cdap.proto.security.Principal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 100 with NamespaceId

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();
    }
}
Also used : FlowId(co.cask.cdap.proto.id.FlowId) CommandLine(org.apache.commons.cli.CommandLine) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Aggregations

NamespaceId (co.cask.cdap.proto.id.NamespaceId)234 Test (org.junit.Test)99 Path (javax.ws.rs.Path)47 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)43 ApplicationId (co.cask.cdap.proto.id.ApplicationId)35 IOException (java.io.IOException)34 StreamId (co.cask.cdap.proto.id.StreamId)30 DatasetId (co.cask.cdap.proto.id.DatasetId)27 TableId (co.cask.cdap.data2.util.TableId)26 Id (co.cask.cdap.proto.Id)24 ProgramId (co.cask.cdap.proto.id.ProgramId)24 NotFoundException (co.cask.cdap.common.NotFoundException)22 ArtifactId (co.cask.cdap.proto.id.ArtifactId)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 TopicId (co.cask.cdap.proto.id.TopicId)19 GET (javax.ws.rs.GET)18 Location (org.apache.twill.filesystem.Location)18 ArrayList (java.util.ArrayList)15 TopicMetadata (co.cask.cdap.messaging.TopicMetadata)13 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)12