Re-format source code
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / utils / AAFPolicyClientImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineUtils
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.utils;
23
24 import java.security.Principal;
25 import java.util.Properties;
26
27 import org.apache.log4j.Logger;
28 import org.onap.aaf.cadi.Access.Level;
29 import org.onap.aaf.cadi.CadiException;
30 import org.onap.aaf.cadi.PropAccess;
31 import org.onap.aaf.cadi.aaf.AAFPermission;
32 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
33 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
34 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
35 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
36 import org.onap.aaf.cadi.locator.PropertyLocator;
37 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
38
39 /**
40  * AAF Client: Generic AAF Client implementation to connect to AAF Resources to
41  * validate permissions and authorization.
42  *
43  */
44 public class AAFPolicyClientImpl implements AAFPolicyClient {
45     private static Logger logger = Logger.getLogger(AAFPolicyClientImpl.class.getName());
46
47     private static final String ENVIRONMENT = "ENVIRONMENT";
48     private static AAFPolicyClientImpl instance = null;
49     private static Properties cadiprops = new Properties();
50     private static AAFCon<?> aafCon = null;
51     private static AAFLurPerm aafLurPerm = null;
52     private static AAFAuthn<?> aafAuthn = null;
53     private static PropAccess access = null;
54
55     private AAFPolicyClientImpl(Properties properties) throws AAFPolicyException {
56         setup(properties);
57     }
58
59     /**
60      * Gets the instance of the AAFClient instance. Needs Proper properties with
61      * CLIENT_ID, CLIENT_KEY and ENVIRONMENT
62      *
63      * @param properties
64      *        Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
65      * @return AAFClient instance.
66      * @throws AAFPolicyException
67      *         Exceptions.
68      */
69     public static synchronized AAFPolicyClientImpl getInstance(Properties properties) throws AAFPolicyException {
70         if (instance == null) {
71             logger.info("Creating AAFClient Instance ");
72             instance = new AAFPolicyClientImpl(properties);
73         }
74         return instance;
75     }
76
77     // To set Property values && Connections.
78     private static void setup(Properties properties) throws AAFPolicyException {
79         if (properties != null && !properties.isEmpty()) {
80             cadiprops = properties;
81             access = new PolicyAccess(cadiprops,
82                     Level.valueOf(cadiprops.getProperty("cadi_loglevel", Level.DEBUG.toString())));
83         } else {
84             logger.error("Required Property value is missing : " + ENVIRONMENT);
85             throw new AAFPolicyException("Required Property value is missing : " + ENVIRONMENT);
86         }
87         setUpAaf();
88     }
89
90     /**
91      * Updates the Properties file in case if required.
92      *
93      * @param properties
94      *        Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
95      * @throws AAFPolicyException
96      *         exceptions if any.
97      */
98     @Override
99     public void updateProperties(Properties properties) throws AAFPolicyException {
100         setup(properties);
101     }
102
103     /**
104      * Checks the Authentication and Permissions for the given values.
105      *
106      * @param userName
107      *        Username must be registered under the Name space.
108      * @param pass
109      *        Password pertaining to the Username.
110      * @param type
111      *        Permissions Type.
112      * @param instance
113      *        Permissions Instance.
114      * @param action
115      *        Permissions Action.
116      * @return
117      */
118     @Override
119     public boolean checkAuthPerm(String userName, String pass, String type, String instance, String action) {
120         return checkAuth(userName, pass) && checkPerm(userName, pass, type, instance, action);
121     }
122
123     /**
124      * Checks the Authentication of the UserName and Password Given.
125      *
126      * @param userName
127      *        UserName
128      * @param pass
129      *        Password.
130      * @return True or False.
131      */
132     @Override
133     public boolean checkAuth(String userName, String pass) {
134         if (aafAuthn == null) {
135             return false;
136         }
137         try {
138             int index = 0;
139             do {
140                 String aafAuthResponse = aafAuthn.validate(userName, pass);
141                 if (aafAuthResponse == null) {
142                     return true;
143                 } else {
144                     logger.warn("User, " + userName + ", failed to authenticate with AAF. \n" + "AAF Response is "
145                             + aafAuthResponse);
146                 }
147                 index++;
148             }
149             while (index < 2);
150         } catch (Exception e) {
151             logger.error(e.getMessage() + e);
152         }
153
154         return false;
155     }
156
157     /**
158      * Checks Permissions for the given UserName, Password and Type, Instance
159      * Action.
160      *
161      * @param userName
162      *        UserName
163      * @param pass
164      *        Password.
165      * @param type
166      *        Permissions Type.
167      * @param instance
168      *        Permissions Instance.
169      * @param action
170      *        Permissions Action.
171      * @return True or False.
172      */
173     @Override
174     public boolean checkPerm(String userName, String pass, String type, String instance, String action) {
175         int index = 0;
176         Boolean result = false;
177         do {
178             if (aafCon != null && aafLurPerm != null) {
179                 try {
180                     aafCon.basicAuth(userName, pass);
181                     AAFPermission perm =
182                             new AAFPermission(cadiprops.getProperty("policy.aaf.namespace"), type, instance, action);
183                     final Principal p = new UnAuthPrincipal(userName);
184                     result = aafLurPerm.fish(p, perm);
185                 } catch (CadiException e) {
186                     logger.error(e.getMessage() + e);
187                     aafLurPerm.destroy();
188                 }
189             }
190             index++;
191         }
192         while (index < 2 && !result); // Try once more to check if this can be passed. AAF has some issues.
193         return result;
194     }
195
196     private static boolean setUpAaf() {
197         try {
198             aafCon = new AAFConHttp(access,
199                     new PropertyLocator("https://" + cadiprops.getProperty("aaf_fqdn") + ":8100"));
200             aafLurPerm = aafCon.newLur();
201             aafAuthn = aafCon.newAuthn(aafLurPerm);
202             return true;
203         } catch (Exception e) {
204             logger.error("Error while setting up AAF Connection " + e.getMessage() + e);
205             return false;
206         }
207     }
208 }