Search in sources :

Example 1 with ArtifactManager

use of io.cdap.cdap.test.ArtifactManager in project cdap by caskdata.

the class AuthorizationTest method testArtifacts.

@Test
public void testArtifacts() throws Exception {
    String appArtifactName = "app-artifact";
    String appArtifactVersion = "1.1.1";
    try {
        ArtifactId defaultNsArtifact = NamespaceId.DEFAULT.artifact(appArtifactName, appArtifactVersion);
        addAppArtifact(defaultNsArtifact, ConfigTestApp.class);
        Assert.fail("Should not be able to add an app artifact to the default namespace because alice does not have " + "admin privileges on the artifact.");
    } catch (UnauthorizedException expected) {
    // expected
    }
    String pluginArtifactName = "plugin-artifact";
    String pluginArtifactVersion = "1.2.3";
    try {
        ArtifactId defaultNsArtifact = NamespaceId.DEFAULT.artifact(pluginArtifactName, pluginArtifactVersion);
        addAppArtifact(defaultNsArtifact, ToStringPlugin.class);
        Assert.fail("Should not be able to add a plugin artifact to the default namespace because alice does not have " + "admin privileges on the artifact.");
    } catch (UnauthorizedException expected) {
    // expected
    }
    // create a new namespace
    createAuthNamespace();
    ArtifactId appArtifactId = AUTH_NAMESPACE.artifact(appArtifactName, appArtifactVersion);
    grantAndAssertSuccess(appArtifactId, ALICE, EnumSet.of(StandardPermission.CREATE, StandardPermission.UPDATE, StandardPermission.DELETE));
    cleanUpEntities.add(appArtifactId);
    ArtifactManager appArtifactManager = addAppArtifact(appArtifactId, ConfigTestApp.class);
    ArtifactId pluginArtifactId = AUTH_NAMESPACE.artifact(pluginArtifactName, pluginArtifactVersion);
    grantAndAssertSuccess(pluginArtifactId, ALICE, EnumSet.of(StandardPermission.CREATE, StandardPermission.DELETE));
    cleanUpEntities.add(pluginArtifactId);
    ArtifactManager pluginArtifactManager = addPluginArtifact(pluginArtifactId, appArtifactId, ToStringPlugin.class);
    // Bob should not be able to delete or write properties to artifacts since he does not have ADMIN permission on
    // the artifacts
    SecurityRequestContext.setUserId(BOB.getName());
    try {
        appArtifactManager.writeProperties(ImmutableMap.of("authorized", "no"));
        Assert.fail("Writing properties to artifact should have failed because Bob does not have admin privileges on " + "the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        appArtifactManager.delete();
        Assert.fail("Deleting artifact should have failed because Bob does not have admin privileges on the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        pluginArtifactManager.writeProperties(ImmutableMap.of("authorized", "no"));
        Assert.fail("Writing properties to artifact should have failed because Bob does not have admin privileges on " + "the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        pluginArtifactManager.removeProperties();
        Assert.fail("Removing properties to artifact should have failed because Bob does not have admin privileges on " + "the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        pluginArtifactManager.delete();
        Assert.fail("Deleting artifact should have failed because Bob does not have admin privileges on the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    // alice should be permitted to update properties/delete artifact
    SecurityRequestContext.setUserId(ALICE.getName());
    appArtifactManager.writeProperties(ImmutableMap.of("authorized", "yes"));
    appArtifactManager.removeProperties();
    appArtifactManager.delete();
    pluginArtifactManager.delete();
}
Also used : ArtifactManager(io.cdap.cdap.test.ArtifactManager) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 2 with ArtifactManager

use of io.cdap.cdap.test.ArtifactManager in project cdap by cdapio.

the class AuthorizationTest method testArtifacts.

@Test
public void testArtifacts() throws Exception {
    String appArtifactName = "app-artifact";
    String appArtifactVersion = "1.1.1";
    try {
        ArtifactId defaultNsArtifact = NamespaceId.DEFAULT.artifact(appArtifactName, appArtifactVersion);
        addAppArtifact(defaultNsArtifact, ConfigTestApp.class);
        Assert.fail("Should not be able to add an app artifact to the default namespace because alice does not have " + "admin privileges on the artifact.");
    } catch (UnauthorizedException expected) {
    // expected
    }
    String pluginArtifactName = "plugin-artifact";
    String pluginArtifactVersion = "1.2.3";
    try {
        ArtifactId defaultNsArtifact = NamespaceId.DEFAULT.artifact(pluginArtifactName, pluginArtifactVersion);
        addAppArtifact(defaultNsArtifact, ToStringPlugin.class);
        Assert.fail("Should not be able to add a plugin artifact to the default namespace because alice does not have " + "admin privileges on the artifact.");
    } catch (UnauthorizedException expected) {
    // expected
    }
    // create a new namespace
    createAuthNamespace();
    ArtifactId appArtifactId = AUTH_NAMESPACE.artifact(appArtifactName, appArtifactVersion);
    grantAndAssertSuccess(appArtifactId, ALICE, EnumSet.of(StandardPermission.CREATE, StandardPermission.UPDATE, StandardPermission.DELETE));
    cleanUpEntities.add(appArtifactId);
    ArtifactManager appArtifactManager = addAppArtifact(appArtifactId, ConfigTestApp.class);
    ArtifactId pluginArtifactId = AUTH_NAMESPACE.artifact(pluginArtifactName, pluginArtifactVersion);
    grantAndAssertSuccess(pluginArtifactId, ALICE, EnumSet.of(StandardPermission.CREATE, StandardPermission.DELETE));
    cleanUpEntities.add(pluginArtifactId);
    ArtifactManager pluginArtifactManager = addPluginArtifact(pluginArtifactId, appArtifactId, ToStringPlugin.class);
    // Bob should not be able to delete or write properties to artifacts since he does not have ADMIN permission on
    // the artifacts
    SecurityRequestContext.setUserId(BOB.getName());
    try {
        appArtifactManager.writeProperties(ImmutableMap.of("authorized", "no"));
        Assert.fail("Writing properties to artifact should have failed because Bob does not have admin privileges on " + "the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        appArtifactManager.delete();
        Assert.fail("Deleting artifact should have failed because Bob does not have admin privileges on the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        pluginArtifactManager.writeProperties(ImmutableMap.of("authorized", "no"));
        Assert.fail("Writing properties to artifact should have failed because Bob does not have admin privileges on " + "the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        pluginArtifactManager.removeProperties();
        Assert.fail("Removing properties to artifact should have failed because Bob does not have admin privileges on " + "the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    try {
        pluginArtifactManager.delete();
        Assert.fail("Deleting artifact should have failed because Bob does not have admin privileges on the artifact");
    } catch (UnauthorizedException expected) {
    // expected
    }
    // alice should be permitted to update properties/delete artifact
    SecurityRequestContext.setUserId(ALICE.getName());
    appArtifactManager.writeProperties(ImmutableMap.of("authorized", "yes"));
    appArtifactManager.removeProperties();
    appArtifactManager.delete();
    pluginArtifactManager.delete();
}
Also used : ArtifactManager(io.cdap.cdap.test.ArtifactManager) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Aggregations

ArtifactId (io.cdap.cdap.proto.id.ArtifactId)2 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)2 ArtifactManager (io.cdap.cdap.test.ArtifactManager)2 Test (org.junit.Test)2