- private void setProperty(String propertyFilePath, String clientKey)
- throws PolicyEngineException {
- this.propertyFilePath = propertyFilePath;
- if (this.propertyFilePath == null) {
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error NO PropertyFile Path provided");
- } else {
- // Adding logic for remote Properties file.
- Properties prop = new Properties();
- if (propertyFilePath.startsWith("http")) {
- URL configURL;
- try {
- configURL = new URL(propertyFilePath);
- URLConnection connection = null;
- connection = configURL.openConnection();
- prop.load(connection.getInputStream());
- } catch (IOException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Maformed property URL "+ e.getMessage());
- }
- } else {
- Path file = Paths.get(propertyFilePath);
- if (Files.notExists(file)) {
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "File doesn't exist in the specified Path " + file.toString());
- }
- if (file.toString().endsWith(".properties")) {
- InputStream in;
- prop = new Properties();
- try {
- in = new FileInputStream(file.toFile());
- prop.load(in);
- } catch (IOException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Load the Properties file", e);
- }
- } else {
- LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + propertyFilePath);
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file");
- }
- }
- // UEB and DMAAP Settings
- String check_type = prop.getProperty("NOTIFICATION_TYPE");
- String serverList = prop.getProperty("NOTIFICATION_SERVERS");
- topic = prop.getProperty("NOTIFICATION_TOPIC");
- apiKey = prop.getProperty("UEB_API_KEY");
- apiSecret = prop.getProperty("UEB_API_SECRET");
-
- if(check_type==null) {
- notificationType.add(DEFAULT_NOTIFICATION);
- LOGGER.info("Properties file doesn't have the NOTIFICATION_TYPE parameter system will use defualt websockets");
- }else{
- check_type = check_type.trim();
- if(check_type.contains(",")) {
- typeDefault = new ArrayList<String>(Arrays.asList(prop.getProperty("NOTIFICATION_TYPE").split(",")));
- notificationType = typeDefault;
- } else {
- notificationType = new ArrayList<>();
- notificationType.add(check_type);
- }
- }
- if(serverList==null) {
- notificationType.clear();
- notificationType.add(DEFAULT_NOTIFICATION);
- LOGGER.info("Properties file doesn't have the NOTIFICATION_SERVERS parameter system will use defualt websockets");
- }else{
- serverList = serverList.trim();
- if(serverList.contains(",")) {
- notificationURLList = new ArrayList<String>(Arrays.asList(serverList.split(",")));
- } else {
- notificationURLList = new ArrayList<>();
- notificationURLList.add(serverList);
- }
- }
-
- if(topic!=null) {
- topic = topic.trim();
- } else {
- LOGGER.error("Properties file doesn't have the NOTIFICATION_TOPIC parameter.");
- }
-
- // Client ID Authorization Settings.
- String clientID = prop.getProperty("CLIENT_ID");
- if(clientKey==null){
- clientKey = prop.getProperty("CLIENT_KEY");
- try {
- clientKey = PolicyUtils.decode(clientKey);
- } catch (UnsupportedEncodingException|IllegalArgumentException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS+" Cannot Decode the given Password Proceeding with given Password!!");
- }
- }
- if(clientID ==null || clientKey == null || clientID.isEmpty() || clientKey.isEmpty()){
- LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS+" Cannot proceed without the CLIENT_KEY and CLIENT_ID values !!");
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_PERMISSIONS+ " Cannot proceed without the CLIENT_KEY and CLIENT_ID values !!");
- }else{
- userName = clientID.trim();
- pass = clientKey.trim();
- }
- setClientEncoding();
- environment = prop.getProperty("ENVIRONMENT", AAFEnvironment.DEVL.toString());
- if(environment.equalsIgnoreCase(AAFEnvironment.TEST.toString())){
- environment = AAFEnvironment.TEST.toString();
- }else if(environment.equalsIgnoreCase(AAFEnvironment.PROD.toString())){
- environment = AAFEnvironment.PROD.toString();
- }else{
- environment = AAFEnvironment.DEVL.toString();
- }
- // Initializing the values.
- pdps = new ArrayList<>();
- encoding = new ArrayList<>();
- // Check the Keys for PDP_URLs
- Collection<Object> unsorted = prop.keySet();
- @SuppressWarnings({ "rawtypes", "unchecked" })
- List<String> sorted = new ArrayList(unsorted);
- Collections.sort(sorted);
- for (String propKey : sorted) {
- if (propKey.startsWith("PDP_URL")) {
- String check_val = prop.getProperty(propKey);
- if (check_val == null) {
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Properties file doesn't have the PDP_URL parameter");
- }
- if (check_val.contains(";")) {
- pdpDefault = new ArrayList<String>(Arrays.asList(check_val.split("\\s*;\\s*")));
- int pdpCount = 0;
- while (pdpCount < pdpDefault.size()) {
- String pdpVal = pdpDefault.get(pdpCount);
- readPDPParam(pdpVal);
- pdpCount++;
- }
- } else {
- readPDPParam(check_val);
- }
- }
- }
- if (pdps == null || pdps.isEmpty()) {
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot Proceed without PDP_URLs");
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot Proceed without PDP_URLs");
- }
-
- // Get JUNIT property from properties file when running tests
- String junit = prop.getProperty("JUNIT");
- if(junit == null || junit.isEmpty()){
- LOGGER.info("No JUNIT property provided, this will not be executed as a test.");
- }else{
- if(junit.equalsIgnoreCase("test")){
- StdPolicyEngine.junit = true;
- } else {
- StdPolicyEngine.junit = false;
- }
- }
- }
- }
-
- /*
- * Read the PDP_URL parameter
- */
- private void readPDPParam(String pdpVal) throws PolicyEngineException{
- if(pdpVal.contains(",")){
- List<String> pdpValues = new ArrayList<String>(Arrays.asList(pdpVal.split("\\s*,\\s*")));
- if(pdpValues.size()==3){
- // 0 - PDPURL
- pdps.add(pdpValues.get(0));
- // 1:2 will be UserID:Password
- String userID = pdpValues.get(1);
- String pass = pdpValues.get(2);
- Base64.Encoder encoder = Base64.getEncoder();
- encoding.add(encoder.encodeToString((userID+":"+pass).getBytes(StandardCharsets.UTF_8)));
- }else{
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "No Credentials to send Request: " + pdpValues);
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "No enough Credentials to send Request. " + pdpValues);
- }
- }else{
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP value is improper/missing required values: " + pdpVal);
- throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP value is improper/missing required values.");
- }
- }
- /*
- * Allowing changes to the scheme and Handler.
- */
- public void notification(NotificationScheme scheme, NotificationHandler handler) {
- this.scheme = scheme;
- this.handler = handler;
- LOGGER.debug("Scheme is : " + scheme.toString());
- LOGGER.debug("Handler is : " + handler.getClass().getName());
-
- if (notificationType.get(0).equals("ueb")){
- if (this.uebThread) {
- uebClientThread.setAuto(scheme, handler);
- this.uebThread = registerUEBThread.isAlive();
- }
- } else if (notificationType.get(0).equals("dmaap")){
- if (this.dmaapThread) {
- dmaapClientThread.setAuto(scheme, handler);
- this.dmaapThread = registerDMAAPThread.isAlive();
- }
- } else {
- AutoClientEnd.setAuto(scheme, handler);
- }
-
- if(junit){
- return;
- }
-
- if(pdps!=null){
- if (notificationType.get(0).equals("ueb") && !this.uebThread){
- this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret);
- this.uebClientThread.setAuto(scheme, handler);
- this.registerUEBThread = new Thread(this.uebClientThread);
- this.registerUEBThread.start();
- this.uebThread = true;
- }else if (notificationType.get(0).equals("dmaap") && !this.dmaapThread){
- this.dmaapClientThread = new AutoClientDMAAP(notificationURLList,topic,userName,pass);
- this.dmaapClientThread.setAuto(scheme, handler);
- this.registerDMAAPThread = new Thread(this.dmaapClientThread);
- this.registerDMAAPThread.start();
- this.dmaapThread = true;
- }else{
- if(pdps.get(0)!=null){
- if(AutoClientEnd.getURL()==null){
- AutoClientEnd.start(pdps.get(0));
- }else {
- AutoClientEnd.stop();
- AutoClientEnd.start(pdps.get(0));
- }
- }
- }
- }
- }
-
- /*
- * Gets the Notification if one exists. Used only for Manual Polling
- * purposes.
- */
- public PDPNotification getNotification(){
- //Check if there is proper scheme..
- PDPNotification notification = null;
- if(this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS) || this.scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
- if (notificationType.get(0).equals("ueb")){
- ManualClientEndUEB.start(pdps.get(0), notificationURLList, UNIQUEID);
- notification = ManualClientEndUEB.result(scheme);
- }else if (notificationType.get(0).equals("dmaap")){
- ManualClientEndDMAAP.start(notificationURLList, topic, UNIQUEID, userName, pass);
- notification = ManualClientEndDMAAP.result(scheme);
- }else{
- ManualClientEnd.start(pdps.get(0));
- LOGGER.debug("manual notification requested.. : " + scheme.toString());
- notification = ManualClientEnd.result(scheme);
- }
- if (notification == null){
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "No Notification yet..");
- return null;
- } else {
- return notification;
- }
- }else {
- return null;
- }
- }
-
- /*
- * Setting the Scheme.
- */
- public void setScheme(NotificationScheme scheme) {
- this.scheme = scheme;
- if (notificationType.get(0).equals("ueb")){
- AutoClientUEB.setScheme(this.scheme);
- if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)){
- ManualClientEndUEB.createTopic(pdps.get(0), UNIQUEID, notificationURLList);
- }
- }else if (notificationType.get(0).equals("dmaap")){
- AutoClientDMAAP.setScheme(this.scheme);
- if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)){
- ManualClientEndDMAAP.createTopic(topic, UNIQUEID, notificationURLList, userName, pass);
- }
- }else{
- AutoClientEnd.setScheme(this.scheme);
- }
- }
-
- /*
- * Returns the Scheme
- */
- public NotificationScheme getScheme() {
- return this.scheme;
- }
-
- /*
- * Returns the NotificationHandler
- */
- public NotificationHandler getNotificationHandler() {
- return this.handler;
- }
-
- /*
- * Stop the Notification Service if its running.
- */
- public void stopNotification() {
- if (this.scheme != null && this.handler != null) {
- if (this.scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)
- || this.scheme
- .equals(NotificationScheme.AUTO_NOTIFICATIONS)) {
- LOGGER.info("Clear Notification called.. ");
- if (notificationType.get(0).equals("ueb")){
- this.uebClientThread.terminate();
- this.uebThread = false;
- }else if (notificationType.get(0).equals("dmaap")){
- this.dmaapClientThread.terminate();
- this.dmaapThread = false;
- }else{
- AutoClientEnd.stop();
- }
- }
- }
- }
-
- /*
- * Push a policy to the PDP API implementation
- */
- public String pushPolicy(String policyScope, String policyName, String policyType, String pdpGroup, UUID requestID) throws PolicyException {
- PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
- if(policyScope==null|| policyScope.trim().isEmpty()){
- String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
- LOGGER.error(message);
- throw new PolicyException(message);
- }
- if(policyName==null|| policyName.trim().isEmpty()){
- String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
- LOGGER.error(message);
- throw new PolicyException(message);
- }
- pushPolicyParameters.setPolicyName(policyScope+"."+policyName);
- pushPolicyParameters.setPolicyType(policyType);
- pushPolicyParameters.setPdpGroup(pdpGroup);
- pushPolicyParameters.setRequestID(requestID);
- return pushPolicyImpl(pushPolicyParameters).getResponseMessage();
- }
-
- public String createUpdateConfigPolicy(String policyName, String policyDescription, String onapName, String configName,
- Map<String, String> configAttributes, String configType, String body, String policyScope, UUID requestID,
- String riskLevel, String riskType, String guard, String ttlDate, boolean updateFlag) throws PolicyException {
- return createUpdateConfigPolicyImpl(policyName, policyDescription, onapName, configName,
- configAttributes, configType, body, policyScope, requestID,
- riskLevel, riskType, guard, ttlDate, updateFlag);
- }
-
- /*
- * Create Config Policy API Implementation
- */
- public String createUpdateConfigPolicyImpl(String policyName, String policyDescription, String onapName, String configName,
- Map<String, String> configAttributes, String configType, String body, String policyScope, UUID requestID,
- String riskLevel, String riskType, String guard, String ttlDate, boolean updateFlag) throws PolicyException {
- PolicyParameters policyParameters = new PolicyParameters();
- policyParameters.setPolicyClass(PolicyClass.Config);
- policyParameters.setPolicyConfigType(PolicyConfigType.Base);
- if(policyScope==null|| policyScope.trim().isEmpty()){
- String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
- LOGGER.error(message);
- throw new PolicyException(message);
- }
- if(policyName==null|| policyName.trim().isEmpty()){
- String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
- LOGGER.error(message);
- throw new PolicyException(message);
- }
- policyParameters.setPolicyName(policyScope+"."+policyName);
- policyParameters.setPolicyDescription(policyDescription);
- policyParameters.setOnapName(onapName);
- policyParameters.setConfigName(configName);
- Map<AttributeType, Map<String, String>> attributes = new HashMap<AttributeType, Map<String, String>>();
- attributes.put(AttributeType.MATCHING, configAttributes);
- policyParameters.setAttributes(attributes);
- policyParameters.setConfigBodyType(PolicyType.valueOf(configType));
- policyParameters.setConfigBody(body);
- policyParameters.setRequestID(requestID);
- policyParameters.setRiskLevel(riskLevel);
- policyParameters.setRiskType(riskType);
- policyParameters.setGuard(Boolean.parseBoolean(guard));
- try {
- policyParameters.setTtlDate(new SimpleDateFormat("dd-MM-yyyy").parse(ttlDate));
- } catch (ParseException e) {
- LOGGER.warn("Error Parsing date given " + ttlDate);
- policyParameters.setTtlDate(null);
- }
- return createUpdatePolicyImpl(policyParameters, updateFlag).getResponseMessage();
- }
-
- public String createUpdateConfigFirewallPolicy(String policyName, JsonObject firewallJson, String policyScope, UUID requestID,
- String riskLevel, String riskType, String guard, String ttlDate, boolean updateFlag) throws PolicyException {
- return createUpdateConfigFirewallPolicyImpl(policyName, firewallJson, policyScope, requestID,
- riskLevel, riskType, guard, ttlDate, updateFlag);
- }
-
- /*
- * Create Update Config Firewall Policy API implementation
- */
- public String createUpdateConfigFirewallPolicyImpl(String policyName, JsonObject firewallJson, String policyScope, UUID requestID,
- String riskLevel, String riskType, String guard, String ttlDate, boolean updateFlag) throws PolicyException {
- PolicyParameters policyParameters = new PolicyParameters();
- policyParameters.setPolicyClass(PolicyClass.Config);
- policyParameters.setPolicyConfigType(PolicyConfigType.Firewall);
- if(policyScope==null|| policyScope.trim().isEmpty()){
- String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
- LOGGER.error(message);
- throw new PolicyException(message);
- }
- if(policyName==null|| policyName.trim().isEmpty()){
- String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
- LOGGER.error(message);
- throw new PolicyException(message);
- }
- policyParameters.setPolicyName(policyScope+"."+policyName);
- policyParameters.setConfigBody(firewallJson.toString());
- policyParameters.setRequestID(requestID);
- policyParameters.setRiskLevel(riskLevel);
- policyParameters.setRiskType(riskType);
- policyParameters.setGuard(Boolean.parseBoolean(guard));
- try {
- policyParameters.setTtlDate(new SimpleDateFormat("dd-MM-yyyy").parse(ttlDate));
- } catch (NullPointerException | ParseException e) {
- LOGGER.warn("Error Parsing date given " + ttlDate);
- policyParameters.setTtlDate(null);
- }
- return createUpdatePolicyImpl(policyParameters, updateFlag).getResponseMessage();
- }
-
- public void setClientKey(String clientKey){
- if(clientKey!=null && !clientKey.isEmpty()){
- StdPolicyEngine.pass = clientKey;
- setClientEncoding();
- }
- }
- /*
- * Get the Environment.
- */
- public static String getEnvironment() {
- return environment;
- }
- /*
- * Rotate the PDP list upon WEBsocket Failures
- */
- public static void rotatePDPList() {
- Collections.rotate(pdps, -1);
- Collections.rotate(encoding, -1);
- }
- /*
- * Get the latest PDP
- */
- public static String getPDPURL() {
- return pdps.get(0);
- }
-}
\ No newline at end of file
+ private void setProperty(final String propertyFilePath, String clientKey) throws PolicyEngineException {
+ if (propertyFilePath == null) {
+ throw new PolicyEngineException(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "Error NO PropertyFile Path provided");
+ }
+ final Properties prop = getProperties(propertyFilePath);
+ setProperty(prop,clientKey);
+ }
+
+ private void setProperty(final Properties properties, String clientKey) throws PolicyEngineException {
+ if (properties == null) {
+ throw new PolicyEngineException(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "NO properties provided, the value is NULL");
+ }
+
+ // UEB and DMAAP Settings
+ final String notificationTypeValue = properties.getProperty(NOTIFICATION_TYPE_PROP_NAME);
+ final String serverList = properties.getProperty(NOTIFICATION_SERVERS_PROP_NAME);
+ topic = properties.getProperty(NOTIFICATION_TOPIC_PROP_NAME);
+ apiKey = properties.getProperty(UEB_API_KEY_PROP_NAME);
+ apiSecret = properties.getProperty(UEB_API_SECRET_PROP_NAME);
+
+ setNotificationType(notificationTypeValue);
+
+ if (serverList == null) {
+ notificationType.clear();
+ notificationType.add(DEFAULT_NOTIFICATION);
+ LOGGER.info(
+ "Properties file doesn't have the NOTIFICATION_SERVERS parameter system will use defualt websockets");
+ } else {
+ notificationURLList = getPropertyValueAsList(serverList.trim());
+ }
+
+ if (topic != null) {
+ topic = topic.trim();
+ } else {
+ LOGGER.error("Properties file doesn't have the NOTIFICATION_TOPIC parameter.");
+ }
+
+ // Client ID Authorization Settings.
+ final String clientID = properties.getProperty(CLIENT_ID_PROP_NAME);
+ if (clientKey == null) {
+ clientKey = getClientKeyFromProperties(properties);
+ }
+ if (clientID == null || clientKey == null || clientID.isEmpty() || clientKey.isEmpty()) {
+ LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS
+ + " Cannot proceed without the CLIENT_KEY and CLIENT_ID values !!");
+ throw new PolicyEngineException(XACMLErrorConstants.ERROR_PERMISSIONS
+ + " Cannot proceed without the CLIENT_KEY and CLIENT_ID values !!");
+ } else {
+ setClientId(clientID.trim());
+ setClientKey(clientKey.trim());
+ }
+ setEnvironment(properties);
+ // Initializing the values.
+ init();
+ readPdpProperites(properties);
+ // Get JUNIT property from properties file when running tests
+ checkJunit(properties);
+ }
+
+ private void readPdpProperites(final Properties prop) throws PolicyEngineException {
+ // Check the Keys for PDP_URLs
+ for (final String propertyKey : prop.stringPropertyNames()) {
+ if (propertyKey.startsWith(PDP_URL_PROP_NAME)) {
+ readPDPPropertyURL(prop, propertyKey);
+ }
+ }
+ if (pdps == null || pdps.isEmpty()) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot Proceed without PDP_URLs");
+ throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Cannot Proceed without PDP_URLs");
+ }
+ }
+
+ private void readPDPPropertyURL(Properties prop, String propertyKey) throws PolicyEngineException {
+ final String propertyValue = prop.getProperty(propertyKey);
+ if (propertyValue == null) {
+ throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE
+ + "Properties file doesn't have the PDP_URL parameter");
+ }
+ if (propertyValue.contains(SEMICOLLON)) {
+ final List<String> pdpDefault = Arrays.asList(propertyValue.split(REGEX));
+ for (final String pdpVal : pdpDefault) {
+ readPDPParam(pdpVal);
+ }
+ } else {
+ readPDPParam(propertyValue);
+ }
+ }
+
+ private void setNotificationType(final String propertyValue) {
+ if (propertyValue == null) {
+ notificationType.add(DEFAULT_NOTIFICATION);
+ LOGGER.info(
+ "Properties file doesn't have the NOTIFICATION_TYPE parameter system will use defualt websockets");
+ } else {
+ notificationType = getPropertyValueAsList(propertyValue.trim());
+ }
+ }
+
+ private String getClientKeyFromProperties(final Properties prop) {
+ final String clientKeyValue = prop.getProperty(CLIENT_KEY_PROP_NAME);
+ try {
+ return PolicyUtils.decode(clientKeyValue);
+ } catch (UnsupportedEncodingException | IllegalArgumentException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS
+ + " Cannot Decode the given Password Proceeding with given Password!!", e);
+ }
+ return clientKeyValue;
+ }
+
+ private Properties getProperties(final String propertyFilePath) throws PolicyEngineException {
+ // Adding logic for remote Properties file.
+ if (propertyFilePath.startsWith(HTTP)) {
+ return getRemoteProperties(propertyFilePath);
+ }
+ return getFileProperties(propertyFilePath);
+ }
+
+ private Properties getFileProperties(final String propertyFilePath) throws PolicyEngineException {
+ final Path file = Paths.get(propertyFilePath);
+ if (!file.toFile().exists()) {
+ throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE
+ + "File doesn't exist in the specified Path " + file.toString());
+ }
+ if (file.toString().endsWith(".properties")) {
+ try (BufferedReader bufferedReader = Files.newBufferedReader(file);) {
+ final Properties prop = new Properties();
+ prop.load(bufferedReader);
+ return prop;
+ } catch (final IOException exception) {
+ LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + exception);
+ throw new PolicyEngineException(
+ XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Cannot Load the Properties file", exception);
+ }
+ }
+ LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file " + propertyFilePath);
+ throw new PolicyEngineException(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Not a .properties file");
+ }
+
+ private Properties getRemoteProperties(final String propertyFilePath) throws PolicyEngineException {
+ try {
+ final URL configURL = new URL(propertyFilePath);
+ final URLConnection connection = configURL.openConnection();
+ final Properties prop = new Properties();
+ prop.load(connection.getInputStream());
+ return prop;
+ } catch (final IOException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
+ throw new PolicyEngineException(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "Maformed property URL " + e.getMessage());
+ }
+ }
+
+ private List<String> getPropertyValueAsList(final String propertyValue) {
+ if (propertyValue.contains(COMMA)) {
+ return Arrays.asList(propertyValue.split(COMMA));
+ }
+ final List<String> valuesList = new ArrayList<>();
+ valuesList.add(propertyValue);
+ return valuesList;
+ }
+
+ private static void checkJunit(final Properties prop) {
+ final String junitFlag = prop.getProperty(JUNIT_PROP_NAME);
+ if (junitFlag == null || junitFlag.isEmpty()) {
+ LOGGER.info("No JUNIT property provided, this will not be executed as a test.");
+ } else {
+ if ("test".equalsIgnoreCase(junitFlag)) {
+ StdPolicyEngine.junit = true;
+ } else {
+ StdPolicyEngine.junit = false;
+ }
+ }
+ }
+
+ private static void init() {
+ pdps = new ArrayList<>();
+ encoding = new ArrayList<>();
+ }
+
+ private static void setEnvironment(final Properties prop) {
+ environment = prop.getProperty(ENVIRONMENT_PROP_NAME, AAFEnvironment.DEVL.toString());
+ if (environment.equalsIgnoreCase(AAFEnvironment.TEST.toString())) {
+ environment = AAFEnvironment.TEST.toString();
+ } else if (environment.equalsIgnoreCase(AAFEnvironment.PROD.toString())) {
+ environment = AAFEnvironment.PROD.toString();
+ } else {
+ environment = AAFEnvironment.DEVL.toString();
+ }
+ }
+
+ private static void setClientId(final String clientID) {
+ userName = clientID;
+ }
+
+ /*
+ * Read the PDP_URL parameter
+ */
+ private void readPDPParam(final String pdpVal) throws PolicyEngineException {
+ if (pdpVal.contains(COMMA)) {
+ final List<String> pdpValues = Arrays.asList(pdpVal.split(PDP_VALUE_REGEX));
+ if (pdpValues.size() == 3) {
+ // 0 - PDPURL
+ pdps.add(pdpValues.get(0));
+ // 1:2 will be UserID:Password
+ final String userID = pdpValues.get(1);
+ final String userPas = pdpValues.get(2);
+ final Base64.Encoder encoder = Base64.getEncoder();
+ encoding.add(encoder.encodeToString((userID + ":" + userPas).getBytes(StandardCharsets.UTF_8)));
+ } else {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "No Credentials to send Request: " + pdpValues);
+ throw new PolicyEngineException(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "No enough Credentials to send Request. " + pdpValues);
+ }
+ } else {
+ LOGGER.error(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP value is improper/missing required values: " + pdpVal);
+ throw new PolicyEngineException(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "PDP value is improper/missing required values.");
+ }
+ }
+
+ /*
+ * Allowing changes to the scheme and Handler.
+ */
+ public void notification(final NotificationScheme scheme, final NotificationHandler handler) {
+ this.scheme = scheme;
+ this.handler = handler;
+ LOGGER.debug("Scheme is : " + scheme.toString());
+ LOGGER.debug("Handler is : " + handler.getClass().getName());
+
+ if (UEB.equals(notificationType.get(0))) {
+ if (this.uebThread) {
+ AutoClientUEB.setAuto(scheme, handler);
+ this.uebThread = registerUEBThread.isAlive();
+ }
+ } else if (notificationType.get(0).equals(DMAAP)) {
+ if (this.dmaapThread) {
+ AutoClientDMAAP.setAuto(scheme, handler);
+ this.dmaapThread = registerDMAAPThread.isAlive();
+ }
+ } else {
+ AutoClientEnd.setAuto(scheme, handler);
+ }
+
+ if (junit) {
+ return;
+ }
+ if (pdps == null) {
+ return;
+ }
+
+ if (UEB.equals(notificationType.get(0)) && !this.uebThread) {
+ this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret);
+ AutoClientUEB.setAuto(scheme, handler);
+ this.registerUEBThread = new Thread(this.uebClientThread);
+ this.registerUEBThread.start();
+ this.uebThread = true;
+ } else if (notificationType.get(0).equals(DMAAP) && !this.dmaapThread) {
+ this.dmaapClientThread = new AutoClientDMAAP(notificationURLList, topic, userName, pass);
+ AutoClientDMAAP.setAuto(scheme, handler);
+ this.registerDMAAPThread = new Thread(this.dmaapClientThread);
+ this.registerDMAAPThread.start();
+ this.dmaapThread = true;
+ } else {
+ if (pdps.get(0) != null) {
+ if (AutoClientEnd.getUrl() == null) {
+ AutoClientEnd.start(pdps.get(0));
+ } else {
+ AutoClientEnd.stop();
+ AutoClientEnd.start(pdps.get(0));
+ }
+ }
+ }
+ }
+
+ /*
+ * Gets the Notification if one exists. Used only for Manual Polling purposes.
+ */
+ public PDPNotification getNotification() {
+ // Check if there is proper scheme..
+ PDPNotification notification;
+ if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)
+ || this.scheme.equals(NotificationScheme.MANUAL_NOTIFICATIONS)) {
+ if (UEB.equals(notificationType.get(0))) {
+ ManualClientEndUEB.start(pdps.get(0), notificationURLList, UNIQUEID);
+ notification = ManualClientEndUEB.result(scheme);
+ } else if (notificationType.get(0).equals(DMAAP)) {
+ ManualClientEndDMAAP.start(notificationURLList, topic, UNIQUEID, userName, pass);
+ notification = ManualClientEndDMAAP.result(scheme);
+ } else {
+ ManualClientEnd.start(pdps.get(0));
+ LOGGER.debug("manual notification requested.. : " + scheme.toString());
+ notification = ManualClientEnd.result(scheme);
+ }
+ if (notification == null) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "No Notification yet..");
+ return null;
+ }
+ return notification;
+ }
+ return null;
+ }
+
+ /*
+ * Setting the Scheme.
+ */
+ public void setScheme(final NotificationScheme scheme) {
+ this.scheme = scheme;
+ if (UEB.equals(notificationType.get(0))) {
+ AutoClientUEB.setScheme(this.scheme);
+ if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
+ ManualClientEndUEB.createTopic(pdps.get(0), UNIQUEID, notificationURLList);
+ }
+ } else if (notificationType.get(0).equals(DMAAP)) {
+ AutoClientDMAAP.setScheme(this.scheme);
+ if (this.scheme.equals(NotificationScheme.MANUAL_ALL_NOTIFICATIONS)) {
+ ManualClientEndDMAAP.createTopic(topic, UNIQUEID, notificationURLList, userName, pass);
+ }
+ } else {
+ AutoClientEnd.setScheme(this.scheme);
+ }
+ }
+
+ /*
+ * Returns the Scheme
+ */
+ public NotificationScheme getScheme() {
+ return this.scheme;
+ }
+
+ /*
+ * Returns the NotificationHandler
+ */
+ public NotificationHandler getNotificationHandler() {
+ return this.handler;
+ }
+
+ /*
+ * Stop the Notification Service if its running.
+ */
+ public void stopNotification() {
+ if (this.scheme != null && this.handler != null
+ && (this.scheme.equals(NotificationScheme.AUTO_ALL_NOTIFICATIONS)
+ || this.scheme.equals(NotificationScheme.AUTO_NOTIFICATIONS))) {
+ LOGGER.info("Clear Notification called.. ");
+ if (UEB.equals(notificationType.get(0))) {
+ this.uebClientThread.terminate();
+ this.uebThread = false;
+ } else if (notificationType.get(0).equals(DMAAP)) {
+ this.dmaapClientThread.terminate();
+ this.dmaapThread = false;
+ } else {
+ AutoClientEnd.stop();
+ }
+ }
+ }
+
+ /*
+ * Push a policy to the PDP API implementation
+ */
+ public String pushPolicy(final String policyScope, final String policyName, final String policyType,
+ final String pdpGroup, final UUID requestID) throws PolicyException {
+ validateParameters(policyName, policyScope);
+
+ final PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();
+ pushPolicyParameters.setPolicyName(policyScope + "." + policyName);
+ pushPolicyParameters.setPolicyType(policyType);
+ pushPolicyParameters.setPdpGroup(pdpGroup);
+ pushPolicyParameters.setRequestID(requestID);
+ return pushPolicyImpl(pushPolicyParameters).getResponseMessage();
+ }
+
+ private boolean isNotValid(final String value) {
+ return value == null || value.trim().isEmpty();
+ }
+
+ public String createUpdateConfigPolicy(final String policyName, final String policyDescription,
+ final String onapName, final String configName, final Map<String, String> configAttributes,
+ final String configType, final String body, final String policyScope, final UUID requestID,
+ final String riskLevel, final String riskType, final String guard, final String ttlDate,
+ final boolean updateFlag) throws PolicyException {
+ return createUpdateConfigPolicyImpl(policyName, policyDescription, onapName, configName, configAttributes,
+ configType, body, policyScope, requestID, riskLevel, riskType, guard, ttlDate, updateFlag);
+ }
+
+ /*
+ * Create Config Policy API Implementation
+ */
+ private String createUpdateConfigPolicyImpl(final String policyName, final String policyDescription,
+ final String onapName, final String configName, final Map<String, String> configAttributes,
+ final String configType, final String body, final String policyScope, final UUID requestID,
+ final String riskLevel, final String riskType, final String guard, final String ttlDate,
+ final boolean updateFlag) throws PolicyException {
+
+ validateParameters(policyName, policyScope);
+
+ final PolicyParameters policyParameters = new PolicyParameters();
+ policyParameters.setPolicyClass(PolicyClass.Config);
+ policyParameters.setPolicyConfigType(PolicyConfigType.Base);
+ policyParameters.setPolicyName(policyScope + "." + policyName);
+ policyParameters.setPolicyDescription(policyDescription);
+ policyParameters.setOnapName(onapName);
+ policyParameters.setConfigName(configName);
+
+ final Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
+ attributes.put(AttributeType.MATCHING, configAttributes);
+
+ policyParameters.setAttributes(attributes);
+ policyParameters.setConfigBodyType(PolicyType.valueOf(configType));
+ policyParameters.setConfigBody(body);
+ policyParameters.setRequestID(requestID);
+ policyParameters.setRiskLevel(riskLevel);
+ policyParameters.setRiskType(riskType);
+ policyParameters.setGuard(Boolean.parseBoolean(guard));
+ policyParameters.setTtlDate(toDate(ttlDate));
+ return createUpdatePolicyImpl(policyParameters, updateFlag).getResponseMessage();
+ }
+
+ private Date toDate(final String dateString) {
+ try {
+ return new SimpleDateFormat(DATE_FORMAT).parse(dateString);
+ } catch (final ParseException e) {
+ LOGGER.warn("Error Parsing date given " + dateString);
+ }
+ return null;
+ }
+
+ public String createUpdateConfigFirewallPolicy(final String policyName, final JsonObject firewallJson,
+ final String policyScope, final UUID requestID, final String riskLevel, final String riskType,
+ final String guard, final String ttlDate, final boolean updateFlag) throws PolicyException {
+ return createUpdateConfigFirewallPolicyImpl(policyName, firewallJson, policyScope, requestID, riskLevel,
+ riskType, guard, ttlDate, updateFlag);
+ }
+
+ /*
+ * Create Update Config Firewall Policy API implementation
+ */
+ private String createUpdateConfigFirewallPolicyImpl(final String policyName, final JsonObject firewallJson,
+ final String policyScope, final UUID requestID, final String riskLevel, final String riskType,
+ final String guard, final String ttlDate, final boolean updateFlag) throws PolicyException {
+ validateParameters(policyName, policyScope);
+
+ final PolicyParameters policyParameters = new PolicyParameters();
+ policyParameters.setPolicyClass(PolicyClass.Config);
+ policyParameters.setPolicyConfigType(PolicyConfigType.Firewall);
+ policyParameters.setPolicyName(policyScope + "." + policyName);
+ policyParameters.setConfigBody(firewallJson.toString());
+ policyParameters.setRequestID(requestID);
+ policyParameters.setRiskLevel(riskLevel);
+ policyParameters.setRiskType(riskType);
+ policyParameters.setGuard(Boolean.parseBoolean(guard));
+ policyParameters.setTtlDate(toDate(ttlDate));
+ return createUpdatePolicyImpl(policyParameters, updateFlag).getResponseMessage();
+ }
+
+ private void validateParameters(final String policyName, final String policyScope) throws PolicyException {
+ if (isNotValid(policyScope)) {
+ final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
+ LOGGER.error(message);
+ throw new PolicyException(message);
+ }
+ if (isNotValid(policyName)) {
+ final String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
+ LOGGER.error(message);
+ throw new PolicyException(message);
+ }
+ }
+
+ public static void setClientKey(final String clientKey) {
+ if (clientKey != null && !clientKey.isEmpty()) {
+ StdPolicyEngine.pass = clientKey;
+ setClientEncoding();
+ }
+ }
+
+ /*
+ * Get the Environment.
+ */
+ public static String getEnvironment() {
+ return environment;
+ }
+
+ /*
+ * Rotate the PDP list upon WEBsocket Failures
+ */
+ public static void rotatePDPList() {
+ Collections.rotate(pdps, -1);
+ Collections.rotate(encoding, -1);
+ }
+
+ /*
+ * Get the latest PDP
+ */
+ public static String getPDPURL() {
+ return pdps.get(0);
+ }
+
+ // Added for test
+ String getTopic() {
+ return topic;
+ }
+
+ // Added for test
+ List<String> getNotificationType() {
+ return notificationType;
+ }
+
+ // Added for test
+ List<String> getNotificationURLList() {
+ return notificationURLList;
+ }
+
+}