Search in sources :

Example 61 with Principal

use of co.cask.cdap.proto.security.Principal in project cdap by caskdata.

the class LineageWriterDatasetFramework method getDataset.

@Nullable
@Override
public <T extends Dataset> T getDataset(final DatasetId datasetInstanceId, final Map<String, String> arguments, @Nullable final ClassLoader classLoader, final DatasetClassLoaderProvider classLoaderProvider, @Nullable final Iterable<? extends EntityId> owners, final AccessType accessType) throws DatasetManagementException, IOException {
    Principal principal = authenticationContext.getPrincipal();
    try {
        // For system, skip authorization and lineage (user program shouldn't allow to access system dataset CDAP-6649)
        // For non-system dataset, always perform authorization and lineage.
        AuthorizationEnforcer enforcer;
        DefaultDatasetRuntimeContext.DatasetAccessRecorder accessRecorder;
        if (!DatasetsUtil.isUserDataset(datasetInstanceId)) {
            enforcer = SYSTEM_NAMESPACE_ENFORCER;
            accessRecorder = SYSTEM_NAMESPACE_ACCESS_RECORDER;
        } else {
            enforcer = authorizationEnforcer;
            accessRecorder = new BasicDatasetAccessRecorder(datasetInstanceId, accessType, owners);
        }
        return DefaultDatasetRuntimeContext.execute(enforcer, accessRecorder, principal, datasetInstanceId, getConstructorDefaultAnnotation(accessType), new Callable<T>() {

            @Override
            public T call() throws Exception {
                return LineageWriterDatasetFramework.super.getDataset(datasetInstanceId, arguments, classLoader, classLoaderProvider, owners, accessType);
            }
        });
    } catch (IOException | DatasetManagementException | ServiceUnavailableException e) {
        throw e;
    } catch (Exception e) {
        throw new DatasetManagementException("Failed to create dataset instance: " + datasetInstanceId, e);
    }
}
Also used : AuthorizationEnforcer(co.cask.cdap.security.spi.authorization.AuthorizationEnforcer) IOException(java.io.IOException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DefaultDatasetRuntimeContext(co.cask.cdap.data2.dataset2.DefaultDatasetRuntimeContext) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) Principal(co.cask.cdap.proto.security.Principal) Nullable(javax.annotation.Nullable)

Example 62 with Principal

use of co.cask.cdap.proto.security.Principal in project cdap by caskdata.

the class DefaultSecureStoreService method putSecureData.

/**
 * Puts the user provided data in the secure store, if the user has admin access to the key.
 *
 * @throws BadRequestException If the request does not contain the value to be stored.
 * @throws UnauthorizedException If the user does not have write permissions on the namespace.
 * @throws NamespaceNotFoundException If the specified namespace does not exist.
 * @throws AlreadyExistsException If the key already exists in the namespace. Updating is not supported.
 * @throws IOException If there was a problem storing the key to underlying provider.
 */
@Override
public final synchronized void putSecureData(String namespace, String name, String value, String description, Map<String, String> properties) throws Exception {
    Principal principal = authenticationContext.getPrincipal();
    NamespaceId namespaceId = new NamespaceId(namespace);
    SecureKeyId secureKeyId = namespaceId.secureKey(name);
    authorizationEnforcer.enforce(secureKeyId, principal, Action.ADMIN);
    if (Strings.isNullOrEmpty(value)) {
        throw new BadRequestException("The data field should not be empty. This is the data that will be stored " + "securely.");
    }
    secureStoreManager.putSecureData(namespace, name, value, description, properties);
}
Also used : SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Principal(co.cask.cdap.proto.security.Principal)

Example 63 with Principal

use of co.cask.cdap.proto.security.Principal in project cdap by caskdata.

the class DefaultSecureStoreService method listSecureData.

/**
 * Lists all the secure keys in the given namespace that the user has access to.
 * Returns an empty list if the user does not have access to any of the keys in the namespace.
 *
 * @return A map of key names accessible by the user and their descriptions.
 * @throws NamespaceNotFoundException If the specified namespace does not exist.
 * @throws IOException If there was a problem reading from the store.
 */
@Override
public final Map<String, String> listSecureData(final String namespace) throws Exception {
    Principal principal = authenticationContext.getPrincipal();
    Map<String, String> metadatas = new HashMap<>(secureStore.listSecureData(namespace));
    metadatas.keySet().retainAll(AuthorizationUtil.isVisible(metadatas.keySet(), authorizationEnforcer, principal, new Function<String, EntityId>() {

        @Override
        public EntityId apply(String input) {
            return new SecureKeyId(namespace, input);
        }
    }, null));
    return metadatas;
}
Also used : Function(com.google.common.base.Function) SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) HashMap(java.util.HashMap) Principal(co.cask.cdap.proto.security.Principal)

Example 64 with Principal

use of co.cask.cdap.proto.security.Principal in project cdap by caskdata.

the class DefaultAuthorizationEnforcerTest method testSystemUser.

@Test
public void testSystemUser() throws Exception {
    CConfiguration cConfCopy = CConfiguration.copy(CCONF);
    Principal systemUser = new Principal(UserGroupInformation.getCurrentUser().getShortUserName(), Principal.PrincipalType.USER);
    try (AuthorizerInstantiator authorizerInstantiator = new AuthorizerInstantiator(cConfCopy, AUTH_CONTEXT_FACTORY)) {
        Authorizer authorizer = authorizerInstantiator.get();
        DefaultAuthorizationEnforcer authorizationEnforcer = new DefaultAuthorizationEnforcer(cConfCopy, authorizerInstantiator);
        NamespaceId ns1 = new NamespaceId("ns1");
        authorizationEnforcer.enforce(NamespaceId.SYSTEM, systemUser, EnumSet.allOf(Action.class));
        Assert.assertEquals(ImmutableSet.of(NamespaceId.SYSTEM), authorizationEnforcer.isVisible(ImmutableSet.of(ns1, NamespaceId.SYSTEM), systemUser));
    }
}
Also used : Action(co.cask.cdap.proto.security.Action) Authorizer(co.cask.cdap.security.spi.authorization.Authorizer) NamespaceId(co.cask.cdap.proto.id.NamespaceId) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Principal(co.cask.cdap.proto.security.Principal) Test(org.junit.Test)

Example 65 with Principal

use of co.cask.cdap.proto.security.Principal in project cdap by caskdata.

the class AuthorizerTest method testAll.

@Test
public void testAll() throws Exception {
    Authorizer authorizer = get();
    verifyAuthFailure(namespace, user, Action.READ);
    authorizer.grant(Authorizable.fromEntityId(namespace), user, EnumSet.allOf(Action.class));
    authorizer.enforce(namespace, user, Action.READ);
    authorizer.enforce(namespace, user, Action.WRITE);
    authorizer.enforce(namespace, user, Action.ADMIN);
    authorizer.enforce(namespace, user, Action.EXECUTE);
    authorizer.revoke(Authorizable.fromEntityId(namespace), user, EnumSet.allOf(Action.class));
    verifyAuthFailure(namespace, user, Action.READ);
    Principal role = new Principal("admins", Principal.PrincipalType.ROLE);
    authorizer.grant(Authorizable.fromEntityId(namespace), user, Collections.singleton(Action.READ));
    authorizer.grant(Authorizable.fromEntityId(namespace), role, EnumSet.allOf(Action.class));
    authorizer.revoke(Authorizable.fromEntityId(namespace));
    verifyAuthFailure(namespace, user, Action.READ);
    verifyAuthFailure(namespace, role, Action.ADMIN);
    verifyAuthFailure(namespace, role, Action.READ);
    verifyAuthFailure(namespace, role, Action.WRITE);
    verifyAuthFailure(namespace, role, Action.EXECUTE);
}
Also used : Action(co.cask.cdap.proto.security.Action) Principal(co.cask.cdap.proto.security.Principal) Test(org.junit.Test)

Aggregations

Principal (co.cask.cdap.proto.security.Principal)76 EntityId (co.cask.cdap.proto.id.EntityId)22 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)16 Action (co.cask.cdap.proto.security.Action)13 NamespaceId (co.cask.cdap.proto.id.NamespaceId)12 IOException (java.io.IOException)12 Path (javax.ws.rs.Path)11 Test (org.junit.Test)9 Role (co.cask.cdap.proto.security.Role)8 POST (javax.ws.rs.POST)7 MethodArgument (co.cask.cdap.common.internal.remote.MethodArgument)6 DatasetModuleMeta (co.cask.cdap.proto.DatasetModuleMeta)5 KerberosPrincipalId (co.cask.cdap.proto.id.KerberosPrincipalId)5 Privilege (co.cask.cdap.proto.security.Privilege)5 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)4 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)4 SecureKeyId (co.cask.cdap.proto.id.SecureKeyId)4 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)3 DatasetModuleConflictException (co.cask.cdap.data2.datafabric.dataset.type.DatasetModuleConflictException)3 DatasetTypeMeta (co.cask.cdap.proto.DatasetTypeMeta)3