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 = "/etc/kafka/data/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;
53 private static boolean enableCadi = false;
54 private static final Logger logger = LoggerFactory.getLogger(Cadi3AAFProvider.class);
58 if (System.getenv("enableCadi") != null && System.getenv("enableCadi").equals("true")) {
61 Configuration config = Configuration.getConfiguration();
64 logger.error("CRITICAL ERROR|Check java.security.auth.login.config VM argument|");
66 // read the section for KafkaServer
67 AppConfigurationEntry[] entries = config.getAppConfigurationEntry("KafkaServer");
68 if (entries == null) {
70 "CRITICAL ERROR|Check config contents passed in java.security.auth.login.config VM argument|");
71 kafkaUsername = "kafkaUsername";
75 for (int i = 0; i < entries.length; i++) {
76 AppConfigurationEntry entry = entries[i];
77 Map<String, ?> optionsMap = entry.getOptions();
78 kafkaUsername = (String) optionsMap.get("username");
79 apiKey = (String) optionsMap.get("password");
83 } catch (Exception e) {
84 logger.error("CRITICAL ERROR: JAAS configuration incorrectly set: " + e.getMessage());
88 public static String getKafkaUsername() {
92 public static boolean isCadiEnabled() {
97 public static AAFAuthn<?> getAafAuthn() throws CadiException {
98 if (aafAuthn == null) {
99 throw new CadiException("Cadi is uninitialized in Cadi3AAFProvider.getAafAuthn()");
104 public Cadi3AAFProvider() {
108 private synchronized void setup() {
109 if (access == null) {
111 Properties props = new Properties();
112 FileInputStream fis = null;
114 if (System.getProperty("CADI_PROPERTIES") != null) {
115 fis = new FileInputStream(System.getProperty("CADI_PROPERTIES"));
117 fis = new FileInputStream(CADI_PROPERTIES);
121 if (System.getenv(AAF_LOCATOR_ENV) != null)
122 props.setProperty(AAF_LOCATOR_ENV, System.getenv(AAF_LOCATOR_ENV));
123 access = new PropAccess(props);
127 } catch (IOException e) {
128 logger.error("Unable to load " + CADI_PROPERTIES);
129 logger.error("Error", e);
133 if (aafAuthn == null) {
135 aafcon = new AAFConHttp(access);
136 aafAuthn = aafcon.newAuthn();
137 aafLur = aafcon.newLur(aafAuthn);
138 } catch (final Exception e) {
141 access.log(e, "Failed to initialize AAF");
148 * Checks if a user has a particular permission
150 * Returns true if the permission in found
152 public boolean hasPermission(String userId, String permission, String instance, String action) {
153 boolean hasPermission = false;
155 logger.info("^ Event at hasPermission to validate userid " + userId + " with " + permission + " " + instance
157 // AAF Style permissions are in the form
158 // Resource Name, Resource Type, Action
159 if (userId.equals("admin")) {
160 hasPermission = true;
161 return hasPermission;
163 AAFPermission perm = new AAFPermission(null, permission, instance, action);
164 if (aafLur != null) {
165 hasPermission = aafLur.fish(new UnAuthPrincipal(userId), perm);
166 logger.trace("Permission: " + perm.getKey() + " for user :" + userId + " found: " + hasPermission);
168 logger.error("AAF client not initialized. Not able to find permissions.");
170 } catch (Exception e) {
171 logger.error("AAF client not initialized", e);
173 return hasPermission;
176 public String getId() {
177 return "CADI_AAF_PROVIDER";
180 public String authenticate(String userId, String password) throws Exception {
182 logger.info("^Event received with username " + userId);
184 boolean enableCadi = System.getenv("enableCadi") == null ? true : false;
188 if (userId.equals(kafkaUsername)) {
189 if (password.equals(apiKey)) {
190 logger.info("by passes the authentication for the admin " + kafkaUsername);
193 String errorMessage = "Authentication failed for user " + kafkaUsername;
194 logger.error(errorMessage);
200 String aafResponse = aafAuthn.validate(userId, password);
201 logger.info("aafResponse=" + aafResponse + " for " + userId);
203 if (aafResponse != null) {
204 logger.error("Authentication failed for user ." + userId);