Search in sources :

Example 1 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class ArtifactHttpHandler method writeProperties.

@PUT
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/properties")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void writeProperties(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersion) throws Exception {
    NamespaceId namespace = NamespaceId.SYSTEM.getNamespace().equalsIgnoreCase(namespaceId) ? NamespaceId.SYSTEM : validateAndGetNamespace(namespaceId);
    Id.Artifact artifactId = validateAndGetArtifactId(namespace, artifactName, artifactVersion);
    Map<String, String> properties;
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8)) {
        properties = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Json Syntax Error while parsing properties from request. " + "Please check that the properties are a json map from string to string.", e);
    } catch (IOException e) {
        throw new BadRequestException("Unable to read properties from the request.", e);
    }
    try {
        artifactRepository.writeArtifactProperties(artifactId, properties);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (IOException e) {
        LOG.error("Exception writing properties for artifact {}.", artifactId, e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Error adding properties to artifact.");
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) IOException(java.io.IOException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 2 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class MetadataHttpHandler method addArtifactTags.

@POST
@Path("/namespaces/{namespace-id}/artifacts/{artifact-name}/versions/{artifact-version}/metadata/tags")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void addArtifactTags(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("artifact-name") String artifactName, @PathParam("artifact-version") String artifactVersionStr) throws BadRequestException, NotFoundException {
    ArtifactId artifactId = new ArtifactId(namespaceId, artifactName, artifactVersionStr);
    metadataAdmin.addTags(artifactId, readArray(request));
    responder.sendJson(HttpResponseStatus.OK, String.format("Added tags to artifact %s successfully.", artifactId));
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 3 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId 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(Action.ADMIN));
    cleanUpEntities.add(appArtifactId);
    ArtifactManager appArtifactManager = addAppArtifact(appArtifactId, ConfigTestApp.class);
    ArtifactId pluginArtifactId = AUTH_NAMESPACE.artifact(pluginArtifactName, pluginArtifactVersion);
    grantAndAssertSuccess(pluginArtifactId, ALICE, EnumSet.of(Action.ADMIN));
    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(co.cask.cdap.test.ArtifactManager) ArtifactId(co.cask.cdap.proto.id.ArtifactId) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 4 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class NamespacedEntityIdCodec method deserializeArtifactId.

private ArtifactId deserializeArtifactId(JsonObject id) {
    NamespaceId namespace = deserializeNamespace(id);
    String artifactName = id.get("artifact").getAsString();
    return new ArtifactId(namespace.getNamespace(), artifactName, id.get("version").getAsString());
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId)

Example 5 with ArtifactId

use of co.cask.cdap.proto.id.ArtifactId in project cdap by caskdata.

the class AuthorizableTest method testArtifact.

@Test
public void testArtifact() {
    Authorizable authorizable;
    ArtifactId artifactId = new ArtifactId("ns", "art", "1.0-SNAPSHOT");
    authorizable = Authorizable.fromEntityId(artifactId);
    // drop the version while asserting
    String artifactIdNoVer = artifactId.toString().replace(".1.0-SNAPSHOT", "");
    Assert.assertEquals(artifactIdNoVer, authorizable.toString());
    String widcardId = artifactIdNoVer.replace("est", "*es?t");
    Assert.assertEquals(widcardId, Authorizable.fromString(widcardId).toString());
    verifyInvalidString("artifact:art");
    verifyInvalidString("artifact:ns.art.1.0-SNAPSHOT");
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) Test(org.junit.Test)

Aggregations

ArtifactId (co.cask.cdap.proto.id.ArtifactId)105 Test (org.junit.Test)45 NamespaceId (co.cask.cdap.proto.id.NamespaceId)40 IOException (java.io.IOException)29 Path (javax.ws.rs.Path)29 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)24 ApplicationId (co.cask.cdap.proto.id.ApplicationId)22 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)15 PluginClass (co.cask.cdap.api.plugin.PluginClass)15 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)14 AppRequest (co.cask.cdap.proto.artifact.AppRequest)14 ProgramId (co.cask.cdap.proto.id.ProgramId)14 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)13 File (java.io.File)13 Id (co.cask.cdap.common.id.Id)11 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)11 Map (java.util.Map)10 BadRequestException (co.cask.cdap.common.BadRequestException)9 ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)8 ArtifactDetail (co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail)8