2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.utils;
22 import java.security.Principal;
23 import java.util.Properties;
25 import org.apache.log4j.Logger;
26 import org.onap.aaf.cadi.Access.Level;
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.PropAccess;
29 import org.onap.aaf.cadi.aaf.AAFPermission;
30 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
31 import org.onap.aaf.cadi.aaf.v2_0.AAFCon;
32 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
33 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
34 import org.onap.aaf.cadi.config.Config;
35 import org.onap.aaf.cadi.locator.PropertyLocator;
36 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
41 * AAF Client: Generic AAF Client implementation to connect to AAF Resources to validate permissions and authorization.
44 public class AAFPolicyClientImpl implements AAFPolicyClient{
45 private static Logger logger = Logger.getLogger(AAFPolicyClientImpl.class.getName());
47 private static final String ENVIRONMENT = "ENVIRONMENT";
49 // Warning Please don't Change these Values. Confirm with AAF team.
50 private static final String DEVL_AAF_URL = "";
51 private static final String TEST_AAF_URL = "";
52 private static final String PROD_AAF_URL = "";
53 private static final String DEFAULT_AFT_LATITUDE = "32.780140";
54 private static final String DEFAULT_AFT_LONGITUDE = "-96.800451";
55 private static final String TEST_AFT_ENVIRONMENT = "AFTUAT";
56 private static final String PROD_AFT_ENVIRONMENT = "AFTPRD";
57 private static final String DEFAULT_AAF_USER_EXPIRES = Integer.toString(5*60000); // 5 minutes for found items to live in cache
58 private static final String DEFAULT_AAF_HIGH_COUNT = Integer.toString(400); // Maximum number of items in Cache
60 private static AAFPolicyClientImpl instance = null;
62 private static Properties props = new Properties();
63 private static AAFCon<?> aafCon = null;
64 private static AAFLurPerm aafLurPerm = null;
65 private static AAFAuthn<?> aafAuthn = null;
66 private static PropAccess access = null;
68 private AAFPolicyClientImpl(Properties properties) throws AAFPolicyException{
73 * Gets the instance of the AAFClient instance. Needs Proper properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
75 * @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
76 * @return AAFClient instance.
77 * @throws AAFPolicyException Exceptions.
79 public static synchronized AAFPolicyClientImpl getInstance(Properties properties) throws AAFPolicyException{
80 if(instance == null) {
81 logger.info("Creating AAFClient Instance ");
82 instance = new AAFPolicyClientImpl(properties);
87 // To set Property values && Connections.
88 private static void setup(Properties properties) throws AAFPolicyException {
89 if(properties!=null && !properties.isEmpty()){
90 props = System.getProperties();
91 props.setProperty("AFT_LATITUDE", properties.getProperty("AFT_LATITUDE", DEFAULT_AFT_LATITUDE));
92 props.setProperty("AFT_LONGITUDE", properties.getProperty("AFT_LONGITUDE", DEFAULT_AFT_LONGITUDE));
93 String aftEnv = TEST_AFT_ENVIRONMENT;
94 props.setProperty("aaf_id",properties.getProperty("aaf_id", "aafID"));
95 props.setProperty("aaf_password", properties.getProperty("aaf_password", "aafPass"));
96 if(properties.containsKey(Config.AAF_URL)){
97 // if given a value in properties file.
98 props.setProperty(Config.AAF_URL, properties.getProperty(Config.AAF_URL));
100 // Set Default values.
101 if(properties.getProperty(ENVIRONMENT, "DEVL").equalsIgnoreCase(AAFEnvironment.TEST.toString())){
102 props.setProperty(Config.AAF_URL, TEST_AAF_URL);
103 }else if(properties.getProperty(ENVIRONMENT, "DEVL").equalsIgnoreCase(AAFEnvironment.PROD.toString())){
104 props.setProperty(Config.AAF_URL, PROD_AAF_URL);
105 aftEnv = PROD_AFT_ENVIRONMENT;
107 props.setProperty(Config.AAF_URL, DEVL_AAF_URL);
110 props.setProperty("AFT_ENVIRONMENT", properties.getProperty("AFT_ENVIRONMENT", aftEnv));
111 props.setProperty(Config.AAF_USER_EXPIRES, properties.getProperty(Config.AAF_USER_EXPIRES, DEFAULT_AAF_USER_EXPIRES));
112 props.setProperty(Config.AAF_HIGH_COUNT, properties.getProperty(Config.AAF_HIGH_COUNT, DEFAULT_AAF_HIGH_COUNT));
114 logger.error("Required Property value is missing : " + ENVIRONMENT);
115 throw new AAFPolicyException("Required Property value is missing : " + ENVIRONMENT);
117 access = new PolicyAccess(props, Level.valueOf(properties.getProperty("AAF_LOG_LEVEL", Level.ERROR.toString())));
122 * Updates the Properties file in case if required.
124 * @param properties Properties with CLIENT_ID, CLIENT_KEY and ENVIRONMENT
125 * @throws AAFPolicyException exceptions if any.
128 public void updateProperties(Properties properties) throws AAFPolicyException{
133 * Checks the Authentication and Permissions for the given values.
135 * @param mechID MechID or ATT ID must be registered under the Name space.
136 * @param pass Password pertaining to the MechID or ATTID.
137 * @param type Permissions Type.
138 * @param instance Permissions Instance.
139 * @param action Permissions Action.
143 public boolean checkAuthPerm(String mechID, String pass, String type, String instance, String action){
144 return checkAuth(mechID, pass) && checkPerm(mechID, pass, type, instance, action);
148 * Checks the Authentication of the UserName and Password Given.
150 * @param userName UserName or MechID
151 * @param pass Password.
152 * @return True or False.
155 public boolean checkAuth(String userName, String pass){
160 if(aafAuthn.validate(userName, pass)==null){
165 } catch (Exception e) {
166 logger.error(e.getMessage() + e);
173 * Checks Permissions for the given UserName, Password and Type, Instance Action.
175 * @param userName UserName or MechID
176 * @param pass Password.
177 * @param type Permissions Type.
178 * @param instance Permissions Instance.
179 * @param action Permissions Action.
180 * @return True or False.
183 public boolean checkPerm(String userName, String pass, String type, String instance, String action){
185 Boolean result= false;
187 if(aafCon!=null && aafLurPerm !=null){
189 aafCon.basicAuth(userName, pass);
190 AAFPermission perm = new AAFPermission(type, instance, action);
191 final Principal p = new UnAuthPrincipal(userName);
192 result = aafLurPerm.fish(p, perm);
193 } catch (CadiException e) {
194 logger.error(e.getMessage() + e);
195 aafLurPerm.destroy();
199 }while(i<2 && !result); // Try once more to check if this can be passed. AAF has some issues.
203 private static boolean setUpAAF(){
205 aafCon = new AAFConHttp(access,new PropertyLocator("https://aaf-onap-beijing-test.osaaf.org:8100"));
206 aafLurPerm = aafCon.newLur();
207 aafAuthn = aafCon.newAuthn(aafLurPerm);
209 } catch (Exception e) {
210 logger.error("Error while setting up AAF Connection " + e.getMessage() + e);