Search in sources :

Example 86 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalRevokePermissionsOnTopic.

protected void internalRevokePermissionsOnTopic(String role) {
    // This operation should be reading from zookeeper and it should be allowed without having admin privileges
    validateAdminAccessOnProperty(namespaceName.getProperty());
    validatePoliciesReadOnlyAccess();
    String topicUri = topicName.toString();
    Stat nodeStat = new Stat();
    Policies policies;
    try {
        byte[] content = globalZk().getData(path(POLICIES, namespaceName.toString()), null, nodeStat);
        policies = jsonMapper().readValue(content, Policies.class);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to revoke permissions on topic {}: Namespace does not exist", clientAppId(), topicUri);
        throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to revoke permissions for topic {}", clientAppId(), topicUri, e);
        throw new RestException(e);
    }
    if (!policies.auth_policies.destination_auth.containsKey(topicUri) || !policies.auth_policies.destination_auth.get(topicUri).containsKey(role)) {
        log.warn("[{}] Failed to revoke permission from role {} on topic: Not set at topic level", clientAppId(), role, topicUri);
        throw new RestException(Status.PRECONDITION_FAILED, "Permissions are not set at the topic level");
    }
    policies.auth_policies.destination_auth.get(topicUri).remove(role);
    try {
        // Write the new policies to zookeeper
        String namespacePath = path(POLICIES, namespaceName.toString());
        globalZk().setData(namespacePath, jsonMapper().writeValueAsBytes(policies), nodeStat.getVersion());
        // invalidate the local cache to force update
        policiesCache().invalidate(namespacePath);
        globalZkCache().invalidate(namespacePath);
        log.info("[{}] Successfully revoke access for role {} - topic {}", clientAppId(), role, topicUri);
    } catch (Exception e) {
        log.error("[{}] Failed to revoke permissions for topic {}", clientAppId(), topicUri, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) AuthPolicies(org.apache.pulsar.common.policies.data.AuthPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 87 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalDeleteTopic.

protected void internalDeleteTopic(boolean authoritative) {
    validateAdminOperationOnTopic(authoritative);
    Topic topic = getTopicReference(topicName);
    // v2 topics have a global name so check if the topic is replicated.
    if (topic.isReplicated()) {
        // Delete is disallowed on global topic
        log.error("[{}] Delete topic is forbidden on global namespace {}", clientAppId(), topicName);
        throw new RestException(Status.FORBIDDEN, "Delete forbidden on global namespace");
    }
    try {
        topic.delete().get();
        log.info("[{}] Successfully removed topic {}", clientAppId(), topicName);
    } catch (Exception e) {
        Throwable t = e.getCause();
        log.error("[{}] Failed to get delete topic {}", clientAppId(), topicName, t);
        if (t instanceof TopicBusyException) {
            throw new RestException(Status.PRECONDITION_FAILED, "Topic has active producers/subscriptions");
        } else {
            throw new RestException(t);
        }
    }
}
Also used : TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) RestException(org.apache.pulsar.broker.web.RestException) Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 88 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalGetPartitionedStats.

protected PartitionedTopicStats internalGetPartitionedStats(boolean authoritative) {
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions == 0) {
        throw new RestException(Status.NOT_FOUND, "Partitioned Topic not found");
    }
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    PartitionedTopicStats stats = new PartitionedTopicStats(partitionMetadata);
    try {
        for (int i = 0; i < partitionMetadata.partitions; i++) {
            PersistentTopicStats partitionStats = pulsar().getAdminClient().persistentTopics().getStats(topicName.getPartition(i).toString());
            stats.add(partitionStats);
            stats.partitions.put(topicName.getPartition(i).toString(), partitionStats);
        }
    } catch (Exception e) {
        throw new RestException(e);
    }
    return stats;
}
Also used : PartitionedTopicStats(org.apache.pulsar.common.policies.data.PartitionedTopicStats) RestException(org.apache.pulsar.broker.web.RestException) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 89 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class AdminResource method getPartitionedTopicMetadata.

protected PartitionedTopicMetadata getPartitionedTopicMetadata(TopicName topicName, boolean authoritative) {
    validateClusterOwnership(topicName.getCluster());
    // validates global-namespace contains local/peer cluster: if peer/local cluster present then lookup can
    // serve/redirect request else fail partitioned-metadata-request so, client fails while creating
    // producer/consumer
    validateGlobalNamespaceOwnership(topicName.getNamespaceObject());
    try {
        checkConnect(topicName);
    } catch (WebApplicationException e) {
        validateAdminAccessOnProperty(topicName.getProperty());
    } catch (Exception e) {
        // unknown error marked as internal server error
        log.warn("Unexpected error while authorizing lookup. topic={}, role={}. Error: {}", topicName, clientAppId(), e.getMessage(), e);
        throw new RestException(e);
    }
    String path = path(PARTITIONED_TOPIC_PATH_ZNODE, namespaceName.toString(), domain(), topicName.getEncodedLocalName());
    PartitionedTopicMetadata partitionMetadata = fetchPartitionedTopicMetadata(pulsar(), path);
    if (log.isDebugEnabled()) {
        log.debug("[{}] Total number of partitions for topic {} is {}", clientAppId(), topicName, partitionMetadata.partitions);
    }
    return partitionMetadata;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RestException(org.apache.pulsar.broker.web.RestException) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 90 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class AdminResource method getNamespacePolicies.

protected Policies getNamespacePolicies(NamespaceName namespaceName) {
    try {
        Policies policies = policiesCache().get(AdminResource.path(POLICIES, namespaceName.toString())).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace does not exist"));
        // fetch bundles from LocalZK-policies
        NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
        BundlesData bundleData = NamespaceBundleFactory.getBundlesData(bundles);
        policies.bundles = bundleData != null ? bundleData : policies.bundles;
        return policies;
    } catch (RestException re) {
        throw re;
    } catch (Exception e) {
        log.error("[{}] Failed to get namespace policies {}", clientAppId(), namespaceName, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) RestException(org.apache.pulsar.broker.web.RestException) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

RestException (org.apache.pulsar.broker.web.RestException)112 KeeperException (org.apache.zookeeper.KeeperException)81 WebApplicationException (javax.ws.rs.WebApplicationException)55 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)55 ExecutionException (java.util.concurrent.ExecutionException)53 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)51 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)49 IOException (java.io.IOException)36 Policies (org.apache.pulsar.common.policies.data.Policies)33 ApiResponses (io.swagger.annotations.ApiResponses)30 Path (javax.ws.rs.Path)29 ApiOperation (io.swagger.annotations.ApiOperation)27 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)25 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)24 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)24 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)24 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)24 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)24 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)24 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)22