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);
}
}
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);
}
}
}
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;
}
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;
}
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);
}
}
Aggregations