-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #449 from RADAR-base/update-auth
Update auth configuration
- Loading branch information
Showing
7 changed files
with
139 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/org/radarbase/appserver/config/TokenVerifierPublicKeyConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.radarbase.appserver.config; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Created by dverbeec on 14/06/2017. | ||
*/ | ||
@SuppressWarnings("PMD.DataflowAnomalyAnalysis") | ||
public class TokenVerifierPublicKeyConfig { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(TokenVerifierPublicKeyConfig.class); | ||
|
||
public static final String LOCATION_ENV = "RADAR_IS_CONFIG_LOCATION"; | ||
|
||
private static final String CONFIG_FILE_NAME = "radar-is.yml"; | ||
|
||
private transient List<URI> publicKeyEndpoints = new LinkedList<>(); | ||
|
||
private transient String resourceName; | ||
|
||
/** | ||
* Read the configuration from file. This method will first check if the environment variable | ||
* <code>RADAR_IS_CONFIG_LOCATION</code> is set. If not set, it will look for a file called | ||
* <code>radar_is.yml</code> on the classpath. The configuration will be kept in a static field, | ||
* so subsequent calls to this method will return the same object. | ||
* | ||
* @return The initialized configuration object based on the contents of the configuration file | ||
* @throws RuntimException If there is any problem loading the configuration | ||
*/ | ||
public static TokenVerifierPublicKeyConfig readFromFileOrClasspath() { | ||
String customLocation = System.getenv(LOCATION_ENV); | ||
URL configFile; | ||
if (customLocation != null) { | ||
log.info(LOCATION_ENV + " environment variable set, loading config from {}", | ||
customLocation); | ||
try { | ||
configFile = new File(customLocation).toURI().toURL(); | ||
} catch (MalformedURLException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} else { | ||
// if config location not defined, look for it on the classpath | ||
log.info(LOCATION_ENV | ||
+ " environment variable not set, looking for it on the classpath"); | ||
configFile = Thread.currentThread().getContextClassLoader() | ||
.getResource(CONFIG_FILE_NAME); | ||
|
||
if (configFile == null) { | ||
throw new RuntimeException( | ||
"Cannot find " + CONFIG_FILE_NAME + " file in classpath. "); | ||
} | ||
} | ||
log.info("Config file found at {}", configFile.getPath()); | ||
|
||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
try (InputStream stream = configFile.openStream()) { | ||
return mapper.readValue(stream, TokenVerifierPublicKeyConfig.class); | ||
} catch (IOException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
public List<URI> getPublicKeyEndpoints() { | ||
return publicKeyEndpoints; | ||
} | ||
|
||
public String getResourceName() { | ||
return resourceName; | ||
} | ||
|
||
} |