* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017 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.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
+
package org.onap.aai.auth;
import com.fasterxml.jackson.core.JsonProcessingException;
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;
import org.onap.aai.babel.logging.ApplicationMsgs;
-import org.onap.aai.cl.api.Logger;
-import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aai.babel.logging.LogHelper;
-/**
- * Authentication and authorization by user and role.
- *
- */
+/** Authentication and authorization by user and role. */
public class AAIMicroServiceAuthCore {
- private static Logger applicationLogger = LoggerFactory.getInstance().getLogger(AAIMicroServiceAuthCore.class);
-
- 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("AJSC_HOME") + FILESEP + "appconfig" : System.getProperty("CONFIG_HOME");
+ private static LogHelper applicationLogger = LogHelper.INSTANCE;
- 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
+ * <ul>
+ * <li><code>$CONFIG_HOME/auth_policy.json</code></li>
+ * <li><code>$CONFIG_HOME/auth/auth_policy.json</code></li>
+ * <p>
+ * Note that if <code>CONFIG_HOME</code> is not set then assume it has a value of <code>$APP_HOME/appconfig</code>
+ */
+ private static String defaultAuthFileName = "auth_policy.json";
private static boolean usersInitialized = false;
private static HashMap<String, AAIAuthUser> users;
private static String policyAuthFileName;
public enum HTTP_METHODS {
- GET,
- PUT,
- DELETE,
- HEAD,
- POST
+ GET, PUT, DELETE, HEAD, POST
}
// Don't instantiate
}
public static synchronized void init(String authPolicyFile) throws AAIAuthException {
-
try {
policyAuthFileName = AAIMicroServiceAuthCore.getConfigFile(authPolicyFile);
} catch (IOException e) {
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");
}
}
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.
+ * <ul>
+ * <li>First try to use the absolute path to the file (if provided), or instead locate the path relative to the
+ * current (or user) dir.</li>
+ * <li>If this fails, try resolving the path relative to the configuration home location (either
+ * <code>$CONFIG_HOME</code> or <code>$APP_HOME/appconfig</code>).</li>
+ * <li>If this fails try resolving relative to the <code>auth</code> folder under configuration home.</li>
+ *
+ * @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<String> locateConfigFile(String authPolicyFile) throws IOException {
+ if (authPolicyFile != null) {
+ List<Path> 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 {
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<String, AAIAuthRole> roles;
public AAIAuthUser() {
this.roles = new HashMap<>();
}
- public String getUser() {
- return this.username;
- }
-
- public Map<String, AAIAuthRole> getRoles() {
- return this.roles;
- }
-
public void addRole(String roleName, AAIAuthRole r) {
this.roles.put(roleName, r);
}
}
return false;
}
-
- public void setUser(String myuser) {
- this.username = myuser;
- }
-
}
public static class AAIAuthRole {
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);
}
}