authorization check for more Kafka operations
[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 = "/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;
53
54         private static final Logger logger = LoggerFactory.getLogger(Cadi3AAFProvider.class);
55
56         static {
57
58                 Configuration config = Configuration.getConfiguration();
59                 try {
60                         if (config == null) {
61                                 logger.error("CRITICAL ERROR|Check java.security.auth.login.config VM argument|");
62                         } else {
63                                 // read the section for KafkaServer
64                                 AppConfigurationEntry[] entries = config.getAppConfigurationEntry("KafkaServer");
65                                 if (entries == null) {
66                                         logger.error(
67                                                         "CRITICAL ERROR|Check config contents passed in java.security.auth.login.config VM argument|");
68                                         kafkaUsername = "kafkaUsername";
69                                         apiKey = "apiKey";
70
71                                 } else {
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");
77                                         }
78                                 }
79                         }
80                 } catch (Exception e) {
81                         logger.error("CRITICAL ERROR: JAAS configuration incorrectly set: " + e.getMessage());
82                 }
83         }
84
85         public static String getKafkaUsername() {
86                 return kafkaUsername;
87         }
88
89         public static AAFAuthn<?> getAafAuthn() throws CadiException {
90                 if (aafAuthn == null) {
91                         throw new CadiException("Cadi is uninitialized in Cadi3AAFProvider.getAafAuthn()");
92                 }
93                 return aafAuthn;
94         }
95
96         public Cadi3AAFProvider() {
97                 setup();
98         }
99
100         private synchronized void setup() {
101                 if (access == null) {
102
103                         Properties props = new Properties();
104                         FileInputStream fis = null;
105                         try {
106                                 if (System.getProperty("CADI_PROPERTIES") != null) {
107                                         fis = new FileInputStream(System.getProperty("CADI_PROPERTIES"));
108                                 } else {
109                                         fis = new FileInputStream(CADI_PROPERTIES);
110                                 }
111                                 try {
112                                         props.load(fis);
113                                         if (System.getenv(AAF_LOCATOR_ENV) != null)
114                                                 props.setProperty(AAF_LOCATOR_ENV, System.getenv(AAF_LOCATOR_ENV));
115                                         access = new PropAccess(props);
116                                 } finally {
117                                         fis.close();
118                                 }
119                         } catch (IOException e) {
120                                 logger.error("Unable to load " + CADI_PROPERTIES);
121                                 logger.error("Error", e);
122                         }
123                 }
124
125                 if (aafAuthn == null) {
126                         try {
127                                 aafcon = new AAFConHttp(access);
128                                 aafAuthn = aafcon.newAuthn();
129                                 aafLur = aafcon.newLur(aafAuthn);
130                         } catch (final Exception e) {
131                                 aafAuthn = null;
132                                 if (access != null)
133                                         access.log(e, "Failed to initialize AAF");
134                         }
135                 }
136
137         }
138
139         /**
140          * Checks if a user has a particular permission
141          * <p/>
142          * Returns true if the permission in found
143          */
144         public boolean hasPermission(String userId, String permission, String instance, String action) {
145                 boolean hasPermission = false;
146                 try {
147                         logger.info("^ Event at hasPermission to validate userid " + userId + " with " + permission + " " + instance
148                                         + " " + action);
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;
154                         }
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);
159                         } else {
160                                 logger.error("AAF client not initialized. Not able to find permissions.");
161                         }
162                 } catch (Exception e) {
163                         logger.error("AAF client not initialized", e);
164                 }
165                 return hasPermission;
166         }
167
168         public String getId() {
169                 return "CADI_AAF_PROVIDER";
170         }
171
172         public String authenticate(String userId, String password) throws Exception {
173
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);
178                                 return null;
179                         } else {
180                                 String errorMessage = "Authentication failed for user " + kafkaUsername;
181                                 logger.error(errorMessage);
182                                 return errorMessage;
183                         }
184
185                 }
186
187                 String aafResponse = aafAuthn.validate(userId, password);
188                 logger.info("aafResponse=" + aafResponse + " for " + userId);
189
190                 if (aafResponse != null) {
191                         logger.error("Authentication failed for user ." + userId);
192                 }
193                 return aafResponse;
194         }
195
196 }