1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 *******************************************************************************/
21 package org.onap.dmaap.kafkaAuthorize;
23 import java.util.EnumSet;
26 import org.apache.kafka.common.acl.AclOperation;
27 import org.apache.kafka.common.security.auth.KafkaPrincipal;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
32 import org.onap.dmaap.commonauth.kafka.base.authorization.Cadi3AAFProvider;
34 import kafka.network.RequestChannel.Session;
35 import kafka.security.auth.Acl;
36 import kafka.security.auth.Authorizer;
37 import kafka.security.auth.Operation;
38 import kafka.security.auth.Resource;
39 import scala.collection.immutable.Set;
42 * A trivial Kafka Authorizer for use with SSL and AAF
43 * Authentication/Authorization.
46 public class KafkaCustomAuthorizer implements Authorizer {
48 private String[] adminPermission = new String[3];
49 public static final EnumSet<AclOperation> TOPIC_DESCRIBE_OPERATIONS = EnumSet.of(AclOperation.DESCRIBE_CONFIGS);
50 public static final EnumSet<AclOperation> TOPIC_READ_WRITE_DESCRIBE_OPERATIONS = EnumSet.of(AclOperation.WRITE,
51 AclOperation.READ, AclOperation.DESCRIBE_CONFIGS);
52 public static final EnumSet<AclOperation> TOPIC_ADMIN_OPERATIONS = EnumSet.of(AclOperation.ALTER,
53 AclOperation.ALTER_CONFIGS, AclOperation.CREATE);
55 private static final Logger logger = LoggerFactory.getLogger(KafkaCustomAuthorizer.class);
58 public void configure(final Map<String, ?> arg0) {
59 // TODO Auto-generate method stub
63 public void addAcls(final Set<Acl> arg0, final Resource arg1) {
64 // TODO Auto-generated method stub
68 private String[] getTopicPermission(String topicName, AclOperation aclOperation) {
70 String namspace = topicName.substring(0, topicName.lastIndexOf("."));
71 String[] permission = new String[3];
72 if (TOPIC_READ_WRITE_DESCRIBE_OPERATIONS.contains(aclOperation)) {
73 permission[0] = namspace + ".topic";
74 String instancePart = (System.getenv("pubSubInstPart") != null) ? System.getenv("pubSubInstPart")
76 permission[1] = instancePart + topicName;
78 if (aclOperation.equals(AclOperation.WRITE)) {
79 permission[2] = "pub";
80 } else if (aclOperation.equals(AclOperation.READ)) {
81 permission[2] = "sub";
83 } else if (TOPIC_DESCRIBE_OPERATIONS.contains(aclOperation)) {
84 permission[2] = "view";
87 } else if (aclOperation.equals(AclOperation.DELETE)) {
88 permission = new String(System.getProperty("msgRtr.topicfactory.aaf") + namspace + "|destroy").split("\\|");
90 } else if (TOPIC_ADMIN_OPERATIONS.contains(aclOperation)) {
91 permission = new String(System.getProperty("msgRtr.topicfactory.aaf") + namspace + "|create").split("\\|");
97 private String[] getAdminPermission() {
99 if (adminPermission[0] == null) {
100 adminPermission[0] = System.getProperty("namespace") + ".kafka.access";
101 adminPermission[1] = "*";
102 adminPermission[2] = "*";
105 return adminPermission;
108 private String[] getPermission(AclOperation aclOperation, String resource, String topicName) {
109 String[] permission = new String[3];
110 switch (aclOperation) {
116 if (resource.equals("Topic")) {
117 permission = getTopicPermission(topicName, aclOperation);
118 } else if (resource.equals("Cluster")) {
119 permission = getAdminPermission();
122 case DESCRIBE_CONFIGS:
125 if (resource.equals("Topic")) {
126 permission = getTopicPermission(topicName, aclOperation);
129 case IDEMPOTENT_WRITE:
130 if (resource.equals("Cluster")) {
131 permission = getAdminPermission();
144 public boolean authorize(final Session arg0, final Operation arg1, final Resource arg2) {
145 if (arg0.principal() == null) {
149 String fullName = arg0.principal().getName();
150 fullName = fullName != null ? fullName.trim() : fullName;
151 String topicName = null;
152 String[] permission = new String[3];
154 String resource = arg2.resourceType().name();
156 if (resource.equals("Topic")) {
157 topicName = arg2.name();
160 if (fullName != null && fullName.equals(Cadi3AAFProvider.getKafkaUsername())) {
164 if ((!Cadi3AAFProvider.isCadiEnabled())||(null != topicName && !topicName.startsWith("org.onap"))) {
168 permission = getPermission(arg1.toJava(), resource, topicName);
170 if (permission[0] == null) {
176 if (null != topicName) {
177 boolean hasResp = AuthorizationProviderFactory.getProviderFactory().getProvider()
178 .hasPermission(fullName, permission[0], permission[1], permission[2]);
180 logger.info("Successful Authorization for " + fullName + " on " + topicName + " for "
181 + permission[0] + "|" + permission[1] + "|" + permission[2]);
184 logger.info(fullName + " is not allowed in " + permission[0] + "|" + permission[1] + "|"
189 } catch (final Exception e) {
198 public void close() {
199 // TODO Auto-generated method stub
204 public scala.collection.immutable.Map<Resource, Set<Acl>> getAcls() {
205 // TODO Auto-generated method stub
210 public scala.collection.immutable.Map<Resource, Set<Acl>> getAcls(final KafkaPrincipal arg0) {
211 // TODO Auto-generated method stub
216 public boolean removeAcls(final Resource arg0) {
217 // TODO Auto-generated method stub
222 public boolean removeAcls(final Set<Acl> arg0, final Resource arg1) {
223 // TODO Auto-generated method stub
227 public Set<Acl> getAcls(Resource arg0) {
228 // TODO Auto-generated method stub