Search in sources :

Example 11 with Principal

use of io.cdap.cdap.proto.security.Principal in project cdap by caskdata.

the class DatasetInstanceService method executeAdmin.

/**
 * Executes an admin operation on a dataset.
 *
 * @param datasetId the datasetId to execute the admin operation on
 * @param method the type of admin operation to execute
 * @return the {@link DatasetAdminOpResponse} from the HTTP handler
 * @throws NamespaceNotFoundException if the requested namespace was not found
 * @throws IOException if there was a problem in checking if the namespace exists over HTTP
 * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
 *  have -
 *  <ol>
 *    <li>{@link StandardPermission#DELETE} privileges on the dataset for "truncate" </li>
 *    <li>{@link StandardPermission#UPDATE} privileges on the dataset for "upgrade" </li>
 *    <li>read privileges on the dataset for "exists"</li>
 *  <ol>
 */
DatasetAdminOpResponse executeAdmin(DatasetId datasetId, String method) throws Exception {
    ensureNamespaceExists(datasetId.getParent());
    Object result = null;
    // NOTE: one cannot directly call create and drop, instead this should be called thru
    // POST/DELETE @ /data/datasets/{datasetId-id}. Because we must create/drop metadata for these at same time
    Principal principal = authenticationContext.getPrincipal();
    switch(method) {
        case "exists":
            // ensure the user has some privilege on the dataset datasetId if it is not system dataset
            if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
                accessEnforcer.enforce(datasetId, principal, StandardPermission.GET);
            }
            result = opExecutorClient.exists(datasetId);
            break;
        case "truncate":
            if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
                accessEnforcer.enforce(datasetId, principal, StandardPermission.DELETE);
            }
            if (instanceManager.get(datasetId) == null) {
                throw new DatasetNotFoundException(datasetId);
            }
            opExecutorClient.truncate(datasetId);
            publishAudit(datasetId, AuditType.TRUNCATE);
            break;
        case "upgrade":
            if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
                accessEnforcer.enforce(datasetId, principal, StandardPermission.UPDATE);
            }
            if (instanceManager.get(datasetId) == null) {
                throw new DatasetNotFoundException(datasetId);
            }
            opExecutorClient.upgrade(datasetId);
            publishAudit(datasetId, AuditType.UPDATE);
            break;
        default:
            throw new HandlerException(HttpResponseStatus.NOT_FOUND, "Invalid admin operation: " + method);
    }
    return new DatasetAdminOpResponse(result, null);
}
Also used : HandlerException(io.cdap.cdap.common.HandlerException) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) DatasetAdminOpResponse(io.cdap.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpResponse) Principal(io.cdap.cdap.proto.security.Principal)

Example 12 with Principal

use of io.cdap.cdap.proto.security.Principal in project cdap by caskdata.

the class DatasetInstanceService method drop.

/**
 * Drops the specified dataset.
 *
 * @param datasetId the {@link DatasetId} to drop
 * @throws NamespaceNotFoundException if the namespace was not found
 * @throws DatasetNotFoundException if the dataset datasetId was not found
 * @throws IOException if there was a problem in checking if the namespace exists over HTTP
 * @throws UnauthorizedException if perimeter security and authorization are enabled, and the current user does not
 *  have {@link StandardPermission#DELETE} privileges on the dataset
 */
void drop(DatasetId datasetId) throws Exception {
    Principal requestingUser = authenticationContext.getPrincipal();
    if (!DatasetsUtil.isSystemDatasetInUserNamespace(datasetId)) {
        accessEnforcer.enforce(datasetId, requestingUser, StandardPermission.DELETE);
    }
    ensureNamespaceExists(datasetId.getParent());
    DatasetSpecification spec = instanceManager.get(datasetId);
    if (spec == null) {
        throw new DatasetNotFoundException(datasetId);
    }
    dropDataset(datasetId, spec);
}
Also used : DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetNotFoundException(io.cdap.cdap.common.DatasetNotFoundException) Principal(io.cdap.cdap.proto.security.Principal)

Example 13 with Principal

use of io.cdap.cdap.proto.security.Principal in project cdap by caskdata.

the class DefaultPreviewRequestQueueTest method testPreviewRequestQueue.

@Test
public void testPreviewRequestQueue() {
    PreviewConfig previewConfig = new PreviewConfig("WordCount", ProgramType.WORKFLOW, null, null);
    AppRequest<?> testRequest = new AppRequest<>(new ArtifactSummary("test", "1.0"), null, previewConfig);
    byte[] pollerInfo = Bytes.toBytes("runner-1");
    Optional<PreviewRequest> requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertFalse(requestOptional.isPresent());
    ApplicationId app1 = new ApplicationId("default", RunIds.generate().getId());
    PreviewRequest request = new PreviewRequest(app1, testRequest, null);
    previewRequestQueue.add(request);
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    ProgramId programId1 = new ProgramId(app1, ProgramType.WORKFLOW, "WordCount");
    Assert.assertEquals(programId1, request.getProgram());
    Principal principal = new Principal("userFoo", Principal.PrincipalType.USER, new Credential("userFooCredential", Credential.CredentialType.EXTERNAL));
    PreviewRequest requestWithPrinciple = new PreviewRequest(app1, testRequest, principal);
    previewRequestQueue.add(requestWithPrinciple);
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    Assert.assertTrue(request.getPrincipal().equals(principal));
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertFalse(requestOptional.isPresent());
    ApplicationId app2 = new ApplicationId("default", RunIds.generate().getId());
    request = new PreviewRequest(app2, testRequest, null);
    previewRequestQueue.add(request);
    Assert.assertEquals(0, previewRequestQueue.positionOf(app2));
    ApplicationId app3 = new ApplicationId("default", RunIds.generate().getId());
    request = new PreviewRequest(app3, testRequest, null);
    previewRequestQueue.add(request);
    Assert.assertEquals(1, previewRequestQueue.positionOf(app3));
    ApplicationId app4 = new ApplicationId("default", RunIds.generate().getId());
    request = new PreviewRequest(app4, testRequest, null);
    boolean exceptionThrown = false;
    try {
        previewRequestQueue.add(request);
    } catch (IllegalStateException e) {
        exceptionThrown = true;
    }
    Assert.assertTrue(exceptionThrown);
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    ProgramId programId2 = new ProgramId(app2, ProgramType.WORKFLOW, "WordCount");
    Assert.assertEquals(programId2, request.getProgram());
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertTrue(requestOptional.isPresent());
    request = requestOptional.get();
    ProgramId programId3 = new ProgramId(app3, ProgramType.WORKFLOW, "WordCount");
    Assert.assertEquals(programId3, request.getProgram());
    requestOptional = previewRequestQueue.poll(pollerInfo);
    Assert.assertFalse(requestOptional.isPresent());
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) ProgramId(io.cdap.cdap.proto.id.ProgramId) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) PreviewRequest(io.cdap.cdap.app.preview.PreviewRequest) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) PreviewConfig(io.cdap.cdap.proto.artifact.preview.PreviewConfig) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Example 14 with Principal

use of io.cdap.cdap.proto.security.Principal in project cdap by caskdata.

the class ListRolesCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String principalType = arguments.getOptional(ArgumentName.PRINCIPAL_TYPE.toString());
    String principalName = arguments.getOptional(ArgumentName.PRINCIPAL_NAME.toString());
    Set<Role> roles;
    if (!(Strings.isNullOrEmpty(principalType) && Strings.isNullOrEmpty(principalName))) {
        roles = client.listRoles(new Principal(principalName, Principal.PrincipalType.valueOf(principalType.toUpperCase())));
    } else {
        roles = client.listAllRoles();
    }
    Table table = Table.builder().setHeader("Role").setRows(Lists.newArrayList(roles), new RowMaker<Role>() {

        @Override
        public List<?> makeRow(Role role) {
            return Lists.newArrayList(role.getName());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : Role(io.cdap.cdap.proto.security.Role) Table(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker) Principal(io.cdap.cdap.proto.security.Principal)

Example 15 with Principal

use of io.cdap.cdap.proto.security.Principal in project cdap by caskdata.

the class DefaultInternalAuthenticatorTest method testProperHeadersSet.

@Test
public void testProperHeadersSet() {
    Map<String, String> stringMap = new HashMap<>();
    // Set authentication context principal.
    String expectedName = "somebody";
    String expectedCredValue = "credential";
    Credential.CredentialType expectedCredType = Credential.CredentialType.EXTERNAL;
    Credential credential = new Credential(expectedCredValue, expectedCredType);
    Principal expectedPrincipal = new Principal(expectedName, Principal.PrincipalType.USER, credential);
    DefaultInternalAuthenticator defaultInternalAuthenticator = new DefaultInternalAuthenticator(new TestAuthenticationContext(expectedPrincipal));
    defaultInternalAuthenticator.applyInternalAuthenticationHeaders(stringMap::put);
    // Verify return values
    Assert.assertEquals(expectedName, stringMap.get(Constants.Security.Headers.USER_ID));
    Assert.assertEquals(String.format("%s %s", expectedCredType.getQualifiedName(), expectedCredValue), stringMap.get(Constants.Security.Headers.RUNTIME_TOKEN));
}
Also used : Credential(io.cdap.cdap.proto.security.Credential) HashMap(java.util.HashMap) Principal(io.cdap.cdap.proto.security.Principal) Test(org.junit.Test)

Aggregations

Principal (io.cdap.cdap.proto.security.Principal)172 Test (org.junit.Test)70 Credential (io.cdap.cdap.proto.security.Credential)58 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)56 UserIdentity (io.cdap.cdap.security.auth.UserIdentity)26 EntityId (io.cdap.cdap.proto.id.EntityId)24 IOException (java.io.IOException)24 StandardPermission (io.cdap.cdap.proto.security.StandardPermission)18 Role (io.cdap.cdap.proto.security.Role)16 Path (javax.ws.rs.Path)16 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)14 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)14 AccessController (io.cdap.cdap.security.spi.authorization.AccessController)12 NoOpAccessController (io.cdap.cdap.security.spi.authorization.NoOpAccessController)12 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)10 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)10 KerberosPrincipalId (io.cdap.cdap.proto.id.KerberosPrincipalId)10 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)8 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)8 DatasetNotFoundException (io.cdap.cdap.common.DatasetNotFoundException)8