use of io.cdap.cdap.proto.security.GrantedPermission in project cdap by cdapio.
the class AuthorizationTest method testNamespaces.
@Test
public void testNamespaces() throws Exception {
NamespaceAdmin namespaceAdmin = getNamespaceAdmin();
AccessController accessController = getAccessController();
try {
namespaceAdmin.create(AUTH_NAMESPACE_META);
Assert.fail("Namespace create should have failed because alice is not authorized on " + AUTH_NAMESPACE);
} catch (UnauthorizedException expected) {
// expected
}
createAuthNamespace();
Assert.assertTrue(namespaceAdmin.list().contains(AUTH_NAMESPACE_META));
namespaceAdmin.get(AUTH_NAMESPACE);
// revoke privileges
revokeAndAssertSuccess(AUTH_NAMESPACE);
try {
Assert.assertTrue(namespaceAdmin.list().isEmpty());
namespaceAdmin.exists(AUTH_NAMESPACE);
Assert.fail("Namespace existence check should fail since the privilege of alice has been revoked");
} catch (UnauthorizedException expected) {
// expected
}
// grant privileges again
grantAndAssertSuccess(AUTH_NAMESPACE, ALICE, ImmutableSet.of(StandardPermission.GET, StandardPermission.UPDATE));
namespaceAdmin.exists(AUTH_NAMESPACE);
Assert.assertEquals(ImmutableSet.of(new GrantedPermission(AUTH_NAMESPACE, StandardPermission.GET), new GrantedPermission(AUTH_NAMESPACE, StandardPermission.UPDATE)), accessController.listGrants(ALICE));
NamespaceMeta updated = new NamespaceMeta.Builder(AUTH_NAMESPACE_META).setDescription("new desc").build();
namespaceAdmin.updateProperties(AUTH_NAMESPACE, updated);
Assert.assertEquals(updated, namespaceAdmin.get(AUTH_NAMESPACE));
}
use of io.cdap.cdap.proto.security.GrantedPermission in project cdap by cdapio.
the class AuthorizationTest method createAuthNamespace.
private void createAuthNamespace() throws Exception {
AccessController accessController = getAccessController();
grantAndAssertSuccess(AUTH_NAMESPACE, ALICE, ImmutableSet.of(StandardPermission.GET, StandardPermission.CREATE));
getNamespaceAdmin().create(AUTH_NAMESPACE_META);
Assert.assertEquals(ImmutableSet.of(new GrantedPermission(AUTH_NAMESPACE, StandardPermission.GET), new GrantedPermission(AUTH_NAMESPACE, StandardPermission.CREATE)), accessController.listGrants(ALICE));
}
use of io.cdap.cdap.proto.security.GrantedPermission in project cdap by cdapio.
the class AuthorizationTest method grantAndAssertSuccess.
private void grantAndAssertSuccess(Authorizable authorizable, Principal principal, Set<? extends Permission> permissions) throws Exception {
AccessController accessController = getAccessController();
Set<GrantedPermission> existingPrivileges = accessController.listGrants(principal);
accessController.grant(authorizable, principal, permissions);
ImmutableSet.Builder<GrantedPermission> expectedPrivilegesAfterGrant = ImmutableSet.builder();
for (Permission permission : permissions) {
expectedPrivilegesAfterGrant.add(new GrantedPermission(authorizable, permission));
}
Assert.assertEquals(Sets.union(existingPrivileges, expectedPrivilegesAfterGrant.build()), accessController.listGrants(principal));
}
use of io.cdap.cdap.proto.security.GrantedPermission in project cdap by cdapio.
the class AuthorizationTest method assertNoAccess.
private void assertNoAccess(Principal principal, final EntityId entityId) throws Exception {
AccessController accessController = getAccessController();
Predicate<GrantedPermission> entityFilter = new Predicate<GrantedPermission>() {
@Override
public boolean apply(GrantedPermission input) {
return Authorizable.fromEntityId(entityId).equals(input.getAuthorizable());
}
};
Assert.assertTrue(Sets.filter(accessController.listGrants(principal), entityFilter).isEmpty());
}
use of io.cdap.cdap.proto.security.GrantedPermission in project cdap by cdapio.
the class SystemArtifactsAuthorizationTest method testAuthorizationForSystemArtifacts.
@Test
public void testAuthorizationForSystemArtifacts() throws Exception {
artifactRepository.addSystemArtifacts();
// alice should not be able to refresh system artifacts because she does not have admin privileges on namespace
// system
SecurityRequestContext.setUserId(ALICE.getName());
try {
artifactRepository.addSystemArtifacts();
Assert.fail("Adding system artifacts should have failed because alice does not have admin privileges on " + "the namespace system.");
} catch (UnauthorizedException expected) {
// expected
}
// grant alice admin privileges on the CDAP system namespace
Authorizable authorizable = Authorizable.fromEntityId(NamespaceId.SYSTEM, EntityType.ARTIFACT);
accessController.grant(authorizable, ALICE, Collections.singleton(StandardPermission.CREATE));
Assert.assertEquals(Collections.singleton(new GrantedPermission(authorizable, StandardPermission.CREATE)), accessController.listGrants(ALICE));
// refreshing system artifacts should succeed now
artifactRepository.addSystemArtifacts();
SecurityRequestContext.setUserId("bob");
// deleting a system artifact should fail because bob does not have admin privileges on the artifact
try {
artifactRepository.deleteArtifact(Id.Artifact.fromEntityId(SYSTEM_ARTIFACT));
Assert.fail("Deleting a system artifact should have failed because alice does not have admin privileges on " + "the artifact.");
} catch (UnauthorizedException expected) {
// expected
}
// grant alice admin privileges on test namespace
SecurityRequestContext.setUserId(ALICE.getName());
NamespaceId namespaceId = new NamespaceId("test");
accessController.grant(Authorizable.fromEntityId(namespaceId), ALICE, EnumSet.allOf(StandardPermission.class));
accessController.grant(Authorizable.fromEntityId(namespaceId, EntityType.ARTIFACT), ALICE, EnumSet.of(StandardPermission.LIST));
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespaceId.getNamespace()).build());
// test that system artifacts are available to everyone
List<ArtifactSummary> artifacts = artifactRepository.getArtifactSummaries(namespaceId, true);
Assert.assertEquals(1, artifacts.size());
ArtifactSummary artifactSummary = artifacts.get(0);
Assert.assertEquals(SYSTEM_ARTIFACT.getArtifact(), artifactSummary.getName());
Assert.assertEquals(SYSTEM_ARTIFACT.getVersion(), artifactSummary.getVersion());
Assert.assertEquals(SYSTEM_ARTIFACT.getNamespace(), artifactSummary.getScope().name().toLowerCase());
// test the getArtifact API
ArtifactDetail artifactDetail = artifactRepository.getArtifact(Id.Artifact.fromEntityId(SYSTEM_ARTIFACT));
io.cdap.cdap.api.artifact.ArtifactId artifactId = artifactDetail.getDescriptor().getArtifactId();
Assert.assertEquals(SYSTEM_ARTIFACT.getArtifact(), artifactId.getName());
Assert.assertEquals(SYSTEM_ARTIFACT.getVersion(), artifactId.getVersion().getVersion());
Assert.assertEquals(SYSTEM_ARTIFACT.getNamespace(), artifactId.getScope().name().toLowerCase());
namespaceAdmin.delete(namespaceId);
// enforce on the system artifact should fail in unit test, since we do not have auto-grant now
try {
accessController.enforce(SYSTEM_ARTIFACT, ALICE, EnumSet.allOf(StandardPermission.class));
Assert.fail();
} catch (UnauthorizedException e) {
// expected
}
try {
artifactRepository.deleteArtifact(Id.Artifact.fromEntityId(SYSTEM_ARTIFACT));
Assert.fail();
} catch (UnauthorizedException e) {
// expected
}
// deleting system artifact should succeed if alice has DELETE on the artifact
accessController.grant(Authorizable.fromEntityId(SYSTEM_ARTIFACT), ALICE, EnumSet.of(StandardPermission.DELETE));
artifactRepository.deleteArtifact(Id.Artifact.fromEntityId(SYSTEM_ARTIFACT));
// clean up privilege
accessController.revoke(Authorizable.fromEntityId(SYSTEM_ARTIFACT));
accessController.revoke(Authorizable.fromEntityId(NamespaceId.SYSTEM, EntityType.ARTIFACT));
accessController.revoke(Authorizable.fromEntityId(namespaceId, EntityType.ARTIFACT));
accessController.revoke(Authorizable.fromEntityId(namespaceId));
}
Aggregations