confluent based image
[dmaap/kafka11aaf.git] / src / main / java / org / onap / dmaap / commonauth / kafka / base / authorization / Cadi3AAFProvider.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11 *  
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=========================================================
18  *  
19  *  
20  *******************************************************************************/
21 package org.onap.dmaap.commonauth.kafka.base.authorization;
22
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import javax.security.auth.login.AppConfigurationEntry;
29 import javax.security.auth.login.Configuration;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
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;
42
43 public class Cadi3AAFProvider implements AuthorizationProvider {
44
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);
55
56         static {
57
58                 if (System.getenv("enableCadi") != null && System.getenv("enableCadi").equals("true")) {
59                         enableCadi = true;
60                 }
61                 Configuration config = Configuration.getConfiguration();
62                 try {
63                         if (config == null) {
64                                 logger.error("CRITICAL ERROR|Check java.security.auth.login.config VM argument|");
65                         } else {
66                                 // read the section for KafkaServer
67                                 AppConfigurationEntry[] entries = config.getAppConfigurationEntry("KafkaServer");
68                                 if (entries == null) {
69                                         logger.error(
70                                                         "CRITICAL ERROR|Check config contents passed in java.security.auth.login.config VM argument|");
71                                         kafkaUsername = "kafkaUsername";
72                                         apiKey = "apiKey";
73
74                                 } else {
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");
80                                         }
81                                 }
82                         }
83                 } catch (Exception e) {
84                         logger.error("CRITICAL ERROR: JAAS configuration incorrectly set: " + e.getMessage());
85                 }
86         }
87
88         public static String getKafkaUsername() {
89                 return kafkaUsername;
90         }
91
92         public static boolean isCadiEnabled() {
93
94                 return enableCadi;
95         }
96
97         public static AAFAuthn<?> getAafAuthn() throws CadiException {
98                 if (aafAuthn == null) {
99                         throw new CadiException("Cadi is uninitialized in Cadi3AAFProvider.getAafAuthn()");
100                 }
101                 return aafAuthn;
102         }
103
104         public Cadi3AAFProvider() {
105                 setup();
106         }
107
108         private synchronized void setup() {
109                 if (access == null) {
110
111                         Properties props = new Properties();
112                         FileInputStream fis = null;
113                         try {
114                                 if (System.getProperty("CADI_PROPERTIES") != null) {
115                                         fis = new FileInputStream(System.getProperty("CADI_PROPERTIES"));
116                                 } else {
117                                         fis = new FileInputStream(CADI_PROPERTIES);
118                                 }
119                                 try {
120                                         props.load(fis);
121                                         if (System.getenv(AAF_LOCATOR_ENV) != null)
122                                                 props.setProperty(AAF_LOCATOR_ENV, System.getenv(AAF_LOCATOR_ENV));
123                                         access = new PropAccess(props);
124                                 } finally {
125                                         fis.close();
126                                 }
127                         } catch (IOException e) {
128                                 logger.error("Unable to load " + CADI_PROPERTIES);
129                                 logger.error("Error", e);
130                         }
131                 }
132
133                 if (aafAuthn == null) {
134                         try {
135                                 aafcon = new AAFConHttp(access);
136                                 aafAuthn = aafcon.newAuthn();
137                                 aafLur = aafcon.newLur(aafAuthn);
138                         } catch (final Exception e) {
139                                 aafAuthn = null;
140                                 if (access != null)
141                                         access.log(e, "Failed to initialize AAF");
142                         }
143                 }
144
145         }
146
147         /**
148          * Checks if a user has a particular permission
149          * <p/>
150          * Returns true if the permission in found
151          */
152         public boolean hasPermission(String userId, String permission, String instance, String action) {
153                 boolean hasPermission = false;
154                 try {
155                         logger.info("^ Event at hasPermission to validate userid " + userId + " with " + permission + " " + instance
156                                         + " " + action);
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;
162                         }
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);
167                         } else {
168                                 logger.error("AAF client not initialized. Not able to find permissions.");
169                         }
170                 } catch (Exception e) {
171                         logger.error("AAF client not initialized", e);
172                 }
173                 return hasPermission;
174         }
175
176         public String getId() {
177                 return "CADI_AAF_PROVIDER";
178         }
179
180         public String authenticate(String userId, String password) throws Exception {
181
182                 logger.info("^Event received  with   username " + userId);
183
184                 boolean enableCadi = System.getenv("enableCadi") == null ? true : false;
185                 if (!enableCadi) {
186                         return null;
187                 } else {
188                         if (userId.equals(kafkaUsername)) {
189                                 if (password.equals(apiKey)) {
190                                         logger.info("by passes the authentication for the admin " + kafkaUsername);
191                                         return null;
192                                 } else {
193                                         String errorMessage = "Authentication failed for user " + kafkaUsername;
194                                         logger.error(errorMessage);
195                                         return errorMessage;
196                                 }
197
198                         }
199
200                         String aafResponse = aafAuthn.validate(userId, password);
201                         logger.info("aafResponse=" + aafResponse + " for " + userId);
202
203                         if (aafResponse != null) {
204                                 logger.error("Authentication failed for user ." + userId);
205                         }
206                         return aafResponse;
207                 }
208
209         }
210
211 }