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;
25 import org.apache.kafka.common.security.auth.KafkaPrincipal;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import org.onap.aaf.cadi.PropAccess;
30 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
31 import kafka.network.RequestChannel.Session;
32 import kafka.security.auth.Acl;
33 import kafka.security.auth.Authorizer;
34 import kafka.security.auth.Operation;
35 import kafka.security.auth.Resource;
36 import scala.collection.immutable.Set;
39 * A trivial Kafka Authorizer for use with SSL and AAF
40 * Authentication/Authorization.
43 public class KafkaCustomAuthorizer implements Authorizer {
44 private PropAccess access;
45 private static final Logger logger = LoggerFactory.getLogger(KafkaCustomAuthorizer.class);
47 // I'm assuming this is called BEFORE any usage...
49 public void configure(final Map<String, ?> arg0) {
50 // TODO Auto-generate method stub
54 public void addAcls(final Set<Acl> arg0, final Resource arg1) {
55 // TODO Auto-generated method stub
60 public boolean authorize(final Session arg0, final Operation arg1, final Resource arg2) {
61 if (arg0.principal() == null) {
65 String fullName = arg0.principal().getName();
66 fullName = fullName != null ? fullName.trim() : fullName;
67 String topicName = null;
68 String namspace = null;
73 String kafkaactivity = arg1.name();
75 if (kafkaactivity.equals("Read")) {
77 } else if (kafkaactivity.equals("Write")) {
79 } else if (kafkaactivity.equals("Create")) {
85 if (arg2.resourceType().name().equals("Topic")) {
86 topicName = arg2.name();
93 if (null != topicName && topicName.indexOf(".") > 0) {
95 if (action.equals("create")) {
96 String instancePart = (System.getenv("msgRtr.topicfactory.aaf") != null)
97 ? System.getenv("msgRtr.topicfactory.aaf")
98 : "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:";
99 String[] instandType = (instancePart + namspace + "|create").split("|");
100 ins = instandType[0];
101 type = instandType[1];
102 } else if (action.equals("pub") || action.equals("sub")) {
103 namspace = topicName.substring(0, topicName.lastIndexOf("."));
104 String instancePart = (System.getenv("pubSubInstPart") != null) ? System.getenv("pubSubInstPart")
106 ins = namspace + instancePart;
107 type = ":topic." + topicName;
109 logger.info("^Event Received for topic " + topicName + " , User " + fullName + " , action = " + action);
112 if (null != fullName && fullName.equals("admin")) {
116 if (null != topicName) {
117 boolean hasResp = AuthorizationProviderFactory.getProviderFactory().getProvider()
118 .hasPermission(fullName, ins, type, action);
120 logger.info("Successful Authorization for " + fullName + " on " + topicName + " for " + ins + "|"
121 + type + "|" + action);
124 logger.info(fullName + " is not allowed in " + ins + "|" + type + "|" + action);
128 } catch (final Exception e) {
135 public void close() {
136 // TODO Auto-generated method stub
141 public scala.collection.immutable.Map<Resource, Set<Acl>> getAcls() {
142 // TODO Auto-generated method stub
147 public scala.collection.immutable.Map<Resource, Set<Acl>> getAcls(final KafkaPrincipal arg0) {
148 // TODO Auto-generated method stub
153 public boolean removeAcls(final Resource arg0) {
154 // TODO Auto-generated method stub
159 public boolean removeAcls(final Set<Acl> arg0, final Resource arg1) {
160 // TODO Auto-generated method stub
164 public Set<Acl> getAcls(Resource arg0) {
165 // TODO Auto-generated method stub