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.commonauth.kafka.base.authorization;
23 import java.io.FileInputStream;
24 import java.io.IOException;
26 import java.util.Properties;
28 import javax.security.auth.login.AppConfigurationEntry;
29 import javax.security.auth.login.Configuration;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import org.onap.aaf.cadi.CadiException;
35 import org.onap.aaf.cadi.PropAccess;
36 import org.onap.aaf.cadi.aaf.AAFPermission;
37 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
38 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
39 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
40 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLur;
41 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
43 public class Cadi3AAFProvider implements AuthorizationProvider {
45 private static PropAccess access;
46 private static AAFCon<?> aafcon;
47 private static final String CADI_PROPERTIES = "/opt/kafka/config/cadi.properties";
48 private static final String AAF_LOCATOR_ENV = "aaf_locate_url";
49 private static String apiKey = null;
50 private static String kafkaUsername = null;
51 private static AAFAuthn<?> aafAuthn;
52 private static AbsAAFLur<AAFPermission> aafLur;
54 private static final Logger logger = LoggerFactory.getLogger(Cadi3AAFProvider.class);
58 Configuration config = Configuration.getConfiguration();
61 logger.error("CRITICAL ERROR|Check java.security.auth.login.config VM argument|");
63 // read the section for KafkaServer
64 AppConfigurationEntry[] entries = config.getAppConfigurationEntry("KafkaServer");
65 if (entries == null) {
67 "CRITICAL ERROR|Check config contents passed in java.security.auth.login.config VM argument|");
68 kafkaUsername = "kafkaUsername";
72 for (int i = 0; i < entries.length; i++) {
73 AppConfigurationEntry entry = entries[i];
74 Map<String, ?> optionsMap = entry.getOptions();
75 kafkaUsername = (String) optionsMap.get("username");
76 apiKey = (String) optionsMap.get("password");
80 } catch (Exception e) {
81 logger.error("CRITICAL ERROR: JAAS configuration incorrectly set: " + e.getMessage());
85 public static String getKafkaUsername() {
89 public static AAFAuthn<?> getAafAuthn() throws CadiException {
90 if (aafAuthn == null) {
91 throw new CadiException("Cadi is uninitialized in Cadi3AAFProvider.getAafAuthn()");
96 public Cadi3AAFProvider() {
100 private synchronized void setup() {
101 if (access == null) {
103 Properties props = new Properties();
104 FileInputStream fis = null;
106 if (System.getProperty("CADI_PROPERTIES") != null) {
107 fis = new FileInputStream(System.getProperty("CADI_PROPERTIES"));
109 fis = new FileInputStream(CADI_PROPERTIES);
113 if (System.getenv(AAF_LOCATOR_ENV) != null)
114 props.setProperty(AAF_LOCATOR_ENV, System.getenv(AAF_LOCATOR_ENV));
115 access = new PropAccess(props);
119 } catch (IOException e) {
120 logger.error("Unable to load " + CADI_PROPERTIES);
121 logger.error("Error", e);
125 if (aafAuthn == null) {
127 aafcon = new AAFConHttp(access);
128 aafAuthn = aafcon.newAuthn();
129 aafLur = aafcon.newLur(aafAuthn);
130 } catch (final Exception e) {
133 access.log(e, "Failed to initialize AAF");
140 * Checks if a user has a particular permission
142 * Returns true if the permission in found
144 public boolean hasPermission(String userId, String permission, String instance, String action) {
145 boolean hasPermission = false;
147 logger.info("^ Event at hasPermission to validate userid " + userId + " with " + permission + " " + instance
149 // AAF Style permissions are in the form
150 // Resource Name, Resource Type, Action
151 if (userId.equals("admin")) {
152 hasPermission = true;
153 return hasPermission;
155 AAFPermission perm = new AAFPermission(null, permission, instance, action);
156 if (aafLur != null) {
157 hasPermission = aafLur.fish(new UnAuthPrincipal(userId), perm);
158 logger.trace("Permission: " + perm.getKey() + " for user :" + userId + " found: " + hasPermission);
160 logger.error("AAF client not initialized. Not able to find permissions.");
162 } catch (Exception e) {
163 logger.error("AAF client not initialized", e);
165 return hasPermission;
168 public String getId() {
169 return "CADI_AAF_PROVIDER";
172 public String authenticate(String userId, String password) throws Exception {
174 logger.info("^Event received with username " + userId);
175 if (userId.equals(kafkaUsername)) {
176 if (password.equals(apiKey)) {
177 logger.info("by passes the authentication for the admin " + kafkaUsername);
180 String errorMessage = "Authentication failed for user " + kafkaUsername;
181 logger.error(errorMessage);
187 String aafResponse = aafAuthn.validate(userId, password);
188 logger.info("aafResponse=" + aafResponse + " for " + userId);
190 if (aafResponse != null) {
191 logger.error("Authentication failed for user ." + userId);