X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fauth%2FAAIMicroServiceAuthCore.java;h=0eec7e1a0b45c25e9bc8c5e276281c81244d74c0;hb=13494d99a913817342da23ffc58029bdc4203814;hp=648d9d0a245cf837f0fb979c9587e31d84f873d6;hpb=716e7f240c2f4a71d48e7708aa27194db2dd7f21;p=aai%2Fbabel.git
diff --git a/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java b/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java
index 648d9d0..0eec7e1 100644
--- a/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java
+++ b/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java
@@ -1,9 +1,9 @@
/**
- * ============LICENSE_START=======================================================
+ * ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 European Software Marketing Ltd.
+ * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2017-2019 European Software Marketing Ltd.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.aai.auth;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -26,12 +27,14 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
@@ -43,13 +46,15 @@ public class AAIMicroServiceAuthCore {
private static LogHelper applicationLogger = LogHelper.INSTANCE;
- public static final String FILESEP =
- (System.getProperty("file.separator") == null) ? "/" : System.getProperty("file.separator");
- public static final String APPCONFIG_DIR = (System.getProperty("CONFIG_HOME") == null)
- ? System.getProperty("APP_HOME") + FILESEP + "appconfig" : System.getProperty("CONFIG_HOME");
-
- private static String appConfigAuthDir = APPCONFIG_DIR + FILESEP + "auth";
- private static String defaultAuthFileName = appConfigAuthDir + FILESEP + "auth_policy.json";
+ /**
+ * The default policy file is expected to be located in either one of
+ *
+ * $CONFIG_HOME/auth_policy.json
+ * $CONFIG_HOME/auth/auth_policy.json
+ *
+ * Note that if CONFIG_HOME
is not set then assume it has a value of $APP_HOME/appconfig
+ */
+ private static String defaultAuthFileName = "auth_policy.json";
private static boolean usersInitialized = false;
private static HashMap users;
@@ -58,11 +63,7 @@ public class AAIMicroServiceAuthCore {
private static String policyAuthFileName;
public enum HTTP_METHODS {
- GET,
- PUT,
- DELETE,
- HEAD,
- POST
+ GET, PUT, DELETE, HEAD, POST
}
// Don't instantiate
@@ -84,8 +85,9 @@ public class AAIMicroServiceAuthCore {
applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
throw new AAIAuthException(e.getMessage());
}
+
if (policyAuthFileName == null) {
- throw new AAIAuthException("Auth policy file could not be found" + System.getProperty("CONFIG_HOME") + APPCONFIG_DIR);
+ throw new AAIAuthException("Auth policy file could not be found");
}
AAIMicroServiceAuthCore.reloadUsers();
@@ -117,21 +119,46 @@ public class AAIMicroServiceAuthCore {
}
public static String getConfigFile(String authPolicyFile) throws IOException {
- File authFile = new File(authPolicyFile);
- if (authFile.exists()) {
- return authFile.getCanonicalPath();
- }
- authFile = new File(appConfigAuthDir + FILESEP + authPolicyFile);
- if (authFile.exists()) {
- return authFile.getCanonicalPath();
- }
- if (defaultAuthFileName != null) {
- authFile = new File(defaultAuthFileName);
- if (authFile.exists()) {
- return defaultAuthFileName;
+ return locateConfigFile(authPolicyFile).orElse(locateConfigFile(defaultAuthFileName).orElse(null));
+ }
+
+ /**
+ * Locate the auth policy file by its name or path.
+ *
+ * - First try to use the absolute path to the file (if provided), or instead locate the path relative to the
+ * current (or user) dir.
+ * - If this fails, try resolving the path relative to the configuration home location (either
+ *
$CONFIG_HOME
or $APP_HOME/appconfig
).
+ * - If this fails try resolving relative to the
auth
folder under configuration home.
+ *
+ * @param authPolicyFile
+ * filename or path
+ * @return the Optional canonical path to the located policy file
+ * @throws IOException
+ * if the construction of the canonical pathname requires filesystem queries which cause I/O error(s)
+ */
+ private static Optional locateConfigFile(String authPolicyFile) throws IOException {
+ if (authPolicyFile != null) {
+ List paths = new ArrayList<>();
+ paths.add(Paths.get("."));
+
+ String configHome = System.getProperty("CONFIG_HOME");
+ if (configHome == null) {
+ configHome = System.getProperty("APP_HOME") + "/appconfig";
+ }
+
+ paths.add(Paths.get(configHome));
+ paths.add(Paths.get(configHome).resolve("auth"));
+
+ for (Path path : paths) {
+ File authFile = path.resolve(authPolicyFile).toFile();
+ if (authFile.exists()) {
+ return Optional.of(authFile.getCanonicalPath());
+ }
}
}
- return null;
+
+ return Optional.empty();
}
public static synchronized void reloadUsers() throws AAIAuthException {
@@ -191,28 +218,18 @@ public class AAIMicroServiceAuthCore {
user = new AAIAuthUser();
}
applicationLogger.debug("Assigning " + roleName + " to user " + name);
- user.setUser(name);
user.addRole(roleName, r);
users.put(name, user);
}
}
public static class AAIAuthUser {
- private String username;
private HashMap roles;
public AAIAuthUser() {
this.roles = new HashMap<>();
}
- public String getUser() {
- return this.username;
- }
-
- public Map getRoles() {
- return this.roles;
- }
-
public void addRole(String roleName, AAIAuthRole r) {
this.roles.put(roleName, r);
}
@@ -226,10 +243,6 @@ public class AAIMicroServiceAuthCore {
}
return false;
}
-
- public void setUser(String myuser) {
- this.username = myuser;
- }
}
public static class AAIAuthRole {
@@ -244,14 +257,8 @@ public class AAIMicroServiceAuthCore {
this.allowedFunctions.add(func);
}
- public void delAllowedFunction(String delFunc) {
- if (this.allowedFunctions.contains(delFunc)) {
- this.allowedFunctions.remove(delFunc);
- }
- }
-
public boolean hasAllowedFunction(String afunc) {
- return this.allowedFunctions.contains(afunc) ? true : false;
+ return this.allowedFunctions.contains(afunc);
}
}