Search in sources :

Example 21 with NamespaceNotFoundException

use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class RemoteApplicationDetailFetcher method list.

/**
 * Get details of all applications in the given namespace
 */
public List<ApplicationDetail> list(String namespace) throws IOException, NamespaceNotFoundException, UnauthorizedException {
    String url = String.format("namespaces/%s/apps", namespace);
    HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.GET, url);
    HttpResponse httpResponse;
    try {
        httpResponse = execute(requestBuilder.build());
    } catch (NotFoundException e) {
        throw new NamespaceNotFoundException(new NamespaceId(namespace));
    }
    ObjectResponse<List<ApplicationDetail>> objectResponse = ObjectResponse.fromJsonBody(httpResponse, APPLICATION_DETAIL_LIST_TYPE, GSON);
    return objectResponse.getResponseObject();
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) HttpResponse(io.cdap.common.http.HttpResponse) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) List(java.util.List) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Example 22 with NamespaceNotFoundException

use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class SecureStoreClient method listKeys.

/**
 * List all the secure keys in the namespace
 * @param namespaceId {@link NamespaceId} namespace id
 * @return list of key names and descriptions
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NamespaceNotFoundException if the given namespace is not found
 */
public List<SecureStoreMetadata> listKeys(NamespaceId namespaceId) throws IOException, UnauthenticatedException, NamespaceNotFoundException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespaceId, SECURE_KEYS);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    return ObjectResponse.fromJsonBody(response, new TypeToken<List<SecureStoreMetadata>>() {
    }).getResponseObject();
}
Also used : SecureStoreMetadata(io.cdap.cdap.api.security.store.SecureStoreMetadata) TypeToken(com.google.common.reflect.TypeToken) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Example 23 with NamespaceNotFoundException

use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class DefaultNamespaceAdmin method updateProperties.

@Override
public synchronized void updateProperties(NamespaceId namespaceId, NamespaceMeta namespaceMeta) throws Exception {
    if (!exists(namespaceId)) {
        throw new NamespaceNotFoundException(namespaceId);
    }
    accessEnforcer.enforce(namespaceId, authenticationContext.getPrincipal(), StandardPermission.UPDATE);
    NamespaceMeta existingMeta = nsStore.get(namespaceId);
    // Already ensured that namespace exists, so namespace meta should not be null
    Preconditions.checkNotNull(existingMeta);
    NamespaceMeta.Builder builder = new NamespaceMeta.Builder(existingMeta);
    if (namespaceMeta.getDescription() != null) {
        builder.setDescription(namespaceMeta.getDescription());
    }
    NamespaceConfig config = namespaceMeta.getConfig();
    if (config != null && !Strings.isNullOrEmpty(config.getSchedulerQueueName())) {
        builder.setSchedulerQueueName(config.getSchedulerQueueName());
    }
    if (config != null && config.getKeytabURI() != null) {
        String keytabURI = config.getKeytabURI();
        if (keytabURI.isEmpty()) {
            throw new BadRequestException("Cannot update keytab URI with an empty URI.");
        }
        String existingKeytabURI = existingMeta.getConfig().getKeytabURIWithoutVersion();
        if (existingKeytabURI == null) {
            throw new BadRequestException("Cannot update keytab URI since there is no existing principal or keytab URI.");
        }
        if (keytabURI.equals(existingKeytabURI)) {
            // The given keytab URI is the same as the existing one, but the content of the keytab file might be changed.
            // Increment the keytab URI version so that the cache will reload content in the updated keytab file.
            builder.incrementKeytabURIVersion();
        } else {
            builder.setKeytabURIWithoutVersion(keytabURI);
            // clear keytab URI version
            builder.setKeytabURIVersion(0);
        }
    }
    if (config != null) {
        builder.setExploreAsPrincipal(config.isExploreAsPrincipal());
    }
    Set<String> difference = existingMeta.getConfig().getDifference(config);
    if (!difference.isEmpty()) {
        throw new BadRequestException(String.format("Mappings %s for namespace %s cannot be updated once the namespace " + "is created.", difference, namespaceId));
    }
    NamespaceMeta updatedMeta = builder.build();
    nsStore.update(updatedMeta);
    // refresh the cache with new meta
    namespaceMetaCache.refresh(namespaceId);
    LOG.info("Namespace {} updated with meta {}", namespaceId, updatedMeta);
}
Also used : NamespaceConfig(io.cdap.cdap.proto.NamespaceConfig) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) CacheBuilder(com.google.common.cache.CacheBuilder) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Example 24 with NamespaceNotFoundException

use of io.cdap.cdap.common.NamespaceNotFoundException in project cdap by caskdata.

the class DefaultNamespaceAdmin method get.

/**
 * Gets details of a namespace
 *
 * @param namespaceId the {@link Id.Namespace} of the requested namespace
 * @return the {@link NamespaceMeta} of the requested namespace
 * @throws NamespaceNotFoundException if the requested namespace is not found
 * @throws UnauthorizedException if the namespace is not authorized to the logged-user
 */
@Override
public NamespaceMeta get(NamespaceId namespaceId) throws Exception {
    Principal principal = authenticationContext.getPrincipal();
    UnauthorizedException lastUnauthorizedException = null;
    // See: CDAP-7387
    if (masterShortUserName == null || !masterShortUserName.equals(principal.getName())) {
        try {
            accessEnforcer.enforce(namespaceId, principal, StandardPermission.GET);
        } catch (UnauthorizedException e) {
            lastUnauthorizedException = e;
        }
    }
    NamespaceMeta namespaceMeta = null;
    try {
        namespaceMeta = namespaceMetaCache.get(namespaceId);
    } catch (Exception e) {
        if (lastUnauthorizedException == null) {
            Throwable cause = e.getCause();
            if (cause instanceof NamespaceNotFoundException || cause instanceof IOException || cause instanceof UnauthorizedException) {
                throw (Exception) cause;
            }
            throw e;
        }
    }
    // If the requesting user is same as namespace owner, we do not care about if the user is authorized or not
    if (namespaceMeta != null && principal.getName().equals(namespaceMeta.getConfig().getPrincipal())) {
        return namespaceMeta;
    }
    if (lastUnauthorizedException != null) {
        throw lastUnauthorizedException;
    }
    return namespaceMeta;
}
Also used : NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) Principal(io.cdap.cdap.proto.security.Principal) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) NamespaceCannotBeDeletedException(io.cdap.cdap.common.NamespaceCannotBeDeletedException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NamespaceCannotBeCreatedException(io.cdap.cdap.common.NamespaceCannotBeCreatedException) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException)

Aggregations

NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)24 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)12 IOException (java.io.IOException)10 NotFoundException (io.cdap.cdap.common.NotFoundException)8 HttpResponse (io.cdap.common.http.HttpResponse)6 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)5 Path (javax.ws.rs.Path)5 BadRequestException (io.cdap.cdap.common.BadRequestException)4 NamespaceCannotBeDeletedException (io.cdap.cdap.common.NamespaceCannotBeDeletedException)4 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)4 GET (javax.ws.rs.GET)4 Test (org.junit.Test)4 NamespaceAlreadyExistsException (io.cdap.cdap.common.NamespaceAlreadyExistsException)3 URL (java.net.URL)3 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)2 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)2 SecureStoreMetadata (io.cdap.cdap.api.security.store.SecureStoreMetadata)2 NamespaceCannotBeCreatedException (io.cdap.cdap.common.NamespaceCannotBeCreatedException)2 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)2 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)2