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.");
}
}
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));
}
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();
}
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());
}
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");
}
Aggregations