Fix blocker sonar issues in dmaap-kafka11aaf
[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.Properties;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import org.onap.aaf.cadi.CadiException;
31 import org.onap.aaf.cadi.PropAccess;
32 import org.onap.aaf.cadi.aaf.AAFPermission;
33 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
36 import org.onap.aaf.cadi.aaf.v2_0.AbsAAFLur;
37 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
38
39 public class Cadi3AAFProvider implements AuthorizationProvider {
40
41         private static PropAccess access;
42         private static AAFCon<?> aafcon;
43         private static final String CADI_PROPERTIES = "/opt/kafka/config/cadi.properties";
44         private static final String AAF_LOCATOR_ENV = "aaf_locate_url";
45         private static final String MR_NAMESPACE = "org.onap.dmaap.mr";
46
47         public static AAFAuthn<?> getAafAuthn() throws CadiException {
48                 if (aafAuthn == null) {
49                         throw new CadiException("Cadi is uninitialized in Cadi3AAFProvider.getAafAuthn()");
50                 }
51                 return aafAuthn;
52         }
53
54         private static AAFAuthn<?> aafAuthn;
55         private static AbsAAFLur<AAFPermission> aafLur;
56
57         private static final Logger logger = LoggerFactory.getLogger(Cadi3AAFProvider.class);
58
59         public Cadi3AAFProvider() {
60                 setup();
61         }
62
63         private synchronized void setup() {
64                 if (access == null) {
65
66                         Properties props = new Properties();
67                         FileInputStream fis = null;
68                         try {
69                                 if (System.getProperty("CADI_PROPERTIES") != null) {
70                                         fis = new FileInputStream(System.getProperty("CADI_PROPERTIES"));
71                                 } else {
72                                         fis = new FileInputStream(CADI_PROPERTIES);
73                                 }
74                                 try {
75                                         props.load(fis);
76                                         if (System.getenv(AAF_LOCATOR_ENV) != null)
77                                                 props.setProperty(AAF_LOCATOR_ENV, System.getenv(AAF_LOCATOR_ENV));
78                                         access = new PropAccess(props);
79                                 } finally {
80                                         fis.close();
81                                 }
82                         } catch (IOException e) {
83                                 logger.error("Unable to load " + CADI_PROPERTIES);
84                                 logger.error("Error", e);
85                         }
86                 }
87
88                 if (aafAuthn == null) {
89                         try {
90                                 aafcon = new AAFConHttp(access);
91                                 aafAuthn = aafcon.newAuthn();
92                                 aafLur = aafcon.newLur(aafAuthn);
93                         } catch (final Exception e) {
94                                 aafAuthn = null;
95                                 if (access != null)
96                                         access.log(e, "Failed to initialize AAF");
97                         }
98                 }
99
100         }
101
102         /**
103          * Checks if a user has a particular permission
104          * <p/>
105          * Returns true if the permission in found
106          */
107         public boolean hasPermission(String userId, String permission, String instance, String action) {
108                 boolean hasPermission = false;
109                 try {
110                         logger.info("^ Event at hasPermission to validate userid " + userId + " with " + permission + " " + instance
111                                         + " " + action);
112                         // AAF Style permissions are in the form
113                         // Resource Name, Resource Type, Action
114                         if (userId.equals("admin")) {
115                                 hasPermission = true;
116                                 return hasPermission;
117                         }
118                         AAFPermission perm = new AAFPermission(null, permission, instance, action);
119                         if (aafLur != null) {
120                                 hasPermission = aafLur.fish(new UnAuthPrincipal(userId), perm);
121                                 logger.trace("Permission: " + perm.getKey() + " for user :" + userId + " found: " + hasPermission);
122                         } else {
123                                 logger.error("AAF client not initialized. Not able to find permissions.");
124                         }
125                 } catch (Exception e) {
126                         logger.error("AAF client not initialized", e);
127                 }
128                 return hasPermission;
129         }
130
131         public String getId() {
132                 return "CADI_AAF_PROVIDER";
133         }
134
135         public String authenticate(String userId, String password) throws Exception {
136                 logger.info("^Event received  with   username " + userId);
137                 if (userId.equals("admin")) {
138                         logger.info("User Admin by passess AAF call ....");
139                         return null;
140                 }
141                 String aafResponse = aafAuthn.validate(userId, password);
142                 logger.info("aafResponse=" + aafResponse + " for " + userId);
143
144                 if (aafResponse != null) {
145                         logger.error("Authentication failed for user ." + userId);
146                 }
147                 return aafResponse;
148         }
149
150 }