Last active
December 7, 2016 02:46
-
-
Save chenharryhua/ccfe7936b8bd8f62e37a0c90ccdcaeaf to your computer and use it in GitHub Desktop.
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
/** | |
* typesafe config is our default configuration library. the configuration can be output via the render methods. | |
* However, sometimes we don't want display the sensitive information to user, such as password. | |
* this gist is about how to remove sensitive information when output a configuration. | |
**/ | |
/** | |
* one solution is that we first render the config and then filter out sensitive information via regex: | |
*/ | |
config.root().render.replaceAll("(?m)^(\\s+\"password\".*)$", "") | |
/** | |
* alternatively, we can also filter out sensitive information and then render it: | |
*/ | |
val conf = config | |
.root() | |
.toConfig() | |
.entrySet | |
.filterNot(_.getKey.contains("password")) | |
.map{f => (f.getKey() -> f.getValue())} | |
.toMap | |
ConfigValueFactory.fromMap(conf) | |
/** | |
* I personally prefer to the second solution because the first one can't handle scenarios as: | |
* password { | |
* aws = "hello" | |
* github = "world" | |
* } | |
**/ | |
/** | |
* besides, my colleague Rahul come up with this code: | |
*/ | |
val secretKeys = config.entrySet().map(_.getKey).filter(_.toLowerCase.contains("password")) | |
secretKeys.foldLeft(config)((c, k) => c.withoutPath(k)).root().render |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment