use of io.cdap.cdap.security.spi.authorization.AccessController in project cdap by caskdata.
the class DefaultAccessEnforcerTest method testInternalAuthEnforce.
@Test
public void testInternalAuthEnforce() throws IOException, AccessException {
Principal userWithInternalCred = new Principal("system", Principal.PrincipalType.USER, null, new Credential("credential", Credential.CredentialType.INTERNAL));
CConfiguration cConfCopy = CConfiguration.copy(CCONF);
cConfCopy.setBoolean(Constants.Security.INTERNAL_AUTH_ENABLED, true);
ControllerWrapper controllerWrapper = createControllerWrapper(cConfCopy, SCONF, new NoOpAccessController());
AccessController accessController = controllerWrapper.accessController;
DefaultAccessEnforcer accessEnforcer = controllerWrapper.defaultAccessEnforcer;
// Make sure that the actual access controller does not have access.
assertAuthorizationFailure(accessController, NS, userWithInternalCred, StandardPermission.GET);
assertAuthorizationFailure(accessController, NS, userWithInternalCred, StandardPermission.UPDATE);
// The no-op access enforcer allows all requests through, so this should succeed if it is using the right
// access controller.
accessEnforcer.enforce(NS, userWithInternalCred, StandardPermission.GET);
accessEnforcer.enforce(NS, userWithInternalCred, StandardPermission.UPDATE);
// Verify the metrics context was called with correct metrics
verify(controllerWrapper.mockMetricsContext, times(2)).increment(Constants.Metrics.Authorization.INTERNAL_CHECK_SUCCESS_COUNT, 1);
}
use of io.cdap.cdap.security.spi.authorization.AccessController in project cdap by caskdata.
the class DefaultAccessEnforcerTest method testAuthEnforce.
@Test
public void testAuthEnforce() throws IOException, AccessException {
ControllerWrapper controllerWrapper = createControllerWrapper(CCONF, SCONF, null);
AccessController accessController = controllerWrapper.accessController;
DefaultAccessEnforcer authEnforcementService = controllerWrapper.defaultAccessEnforcer;
// update privileges for alice. Currently alice has not been granted any privileges.
assertAuthorizationFailure(authEnforcementService, NS, ALICE, StandardPermission.UPDATE);
// grant some test privileges
DatasetId ds = NS.dataset("ds");
accessController.grant(Authorizable.fromEntityId(NS), ALICE, ImmutableSet.of(StandardPermission.GET, StandardPermission.UPDATE));
accessController.grant(Authorizable.fromEntityId(ds), BOB, ImmutableSet.of(StandardPermission.UPDATE));
accessController.grant(Authorizable.fromEntityId(NS, EntityType.DATASET), ALICE, ImmutableSet.of(StandardPermission.LIST));
// auth enforcement for alice should succeed on ns for actions read, write and list datasets
authEnforcementService.enforce(NS, ALICE, ImmutableSet.of(StandardPermission.GET, StandardPermission.UPDATE));
authEnforcementService.enforceOnParent(EntityType.DATASET, NS, ALICE, StandardPermission.LIST);
assertAuthorizationFailure(authEnforcementService, NS, ALICE, EnumSet.allOf(StandardPermission.class));
// alice do not have CREATE, READ or WRITE on the dataset, so authorization should fail
assertAuthorizationFailure(authEnforcementService, ds, ALICE, StandardPermission.GET);
assertAuthorizationFailure(authEnforcementService, ds, ALICE, StandardPermission.UPDATE);
assertAuthorizationFailure(authEnforcementService, EntityType.DATASET, NS, ALICE, StandardPermission.CREATE);
// Alice doesn't have Delete right on NS, hence should fail.
assertAuthorizationFailure(authEnforcementService, NS, ALICE, StandardPermission.DELETE);
// bob enforcement should succeed since we grant him admin privilege
authEnforcementService.enforce(ds, BOB, StandardPermission.UPDATE);
// revoke all of alice's privileges
accessController.revoke(Authorizable.fromEntityId(NS), ALICE, ImmutableSet.of(StandardPermission.GET));
assertAuthorizationFailure(authEnforcementService, NS, ALICE, StandardPermission.GET);
accessController.revoke(Authorizable.fromEntityId(NS));
assertAuthorizationFailure(authEnforcementService, NS, ALICE, StandardPermission.GET);
assertAuthorizationFailure(authEnforcementService, NS, ALICE, StandardPermission.UPDATE);
authEnforcementService.enforce(ds, BOB, StandardPermission.UPDATE);
// Verify the metrics context was called with correct metrics
verify(controllerWrapper.mockMetricsContext, times(4)).increment(Constants.Metrics.Authorization.EXTENSION_CHECK_SUCCESS_COUNT, 1);
verify(controllerWrapper.mockMetricsContext, times(9)).increment(Constants.Metrics.Authorization.EXTENSION_CHECK_FAILURE_COUNT, 1);
verify(controllerWrapper.mockMetricsContext, times(13)).gauge(eq(Constants.Metrics.Authorization.EXTENSION_CHECK_MILLIS), any(Long.class));
}
use of io.cdap.cdap.security.spi.authorization.AccessController in project cdap by caskdata.
the class AccessControllerInstantiatorTest method assertDisabled.
private void assertDisabled(CConfiguration cConf, FeatureDisabledException.Feature feature) throws IOException {
try (AccessControllerInstantiator instantiator = new AccessControllerInstantiator(cConf, AUTH_CONTEXT_FACTORY)) {
AccessController accessController = instantiator.get();
Assert.assertTrue(String.format("When %s is disabled, a %s must be returned, but got %s.", feature.name().toLowerCase(), NoOpAccessController.class.getSimpleName(), accessController.getClass().getName()), accessController instanceof NoOpAccessController);
}
}
use of io.cdap.cdap.security.spi.authorization.AccessController in project cdap by caskdata.
the class AccessControllerInstantiatorTest method testAccessControllerExtension.
@Test
public void testAccessControllerExtension() throws Exception {
Location externalAuthJar = createValidAuthExtensionJar();
CConfiguration cConfCopy = CConfiguration.copy(CCONF);
cConfCopy.set(Constants.Security.Authorization.EXTENSION_JAR_PATH, externalAuthJar.toString());
// Create a temporary file.
final File tempFile = TEMP_FOLDER.newFile("conf-file.xml");
cConfCopy.set(Constants.Security.Authorization.EXTENSION_EXTRA_CLASSPATH, tempFile.getParent());
try (AccessControllerInstantiator instantiator = new AccessControllerInstantiator(cConfCopy, AUTH_CONTEXT_FACTORY)) {
// should be able to load the ExternalAccessController class via the AccessControllerInstantiatorService
AccessController externalAccessController1 = instantiator.get();
externalAccessController1.listAllRoles();
externalAccessController1.listGrants(new Principal("test", Principal.PrincipalType.USER));
ClassLoader accessControllerClassLoader = externalAccessController1.getClass().getClassLoader();
// should be able to load the ExternalAccessController class via the AccessControllerClassLoader
accessControllerClassLoader.loadClass(ValidExternalAccessController.class.getName());
Assert.assertNotNull(accessControllerClassLoader.getResource("conf-file.xml"));
}
}
use of io.cdap.cdap.security.spi.authorization.AccessController in project cdap by caskdata.
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));
}
Aggregations