- 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());
+ }