Skip to content

Commit b47f942

Browse files
authored
Finished up secure config files with ansible. Finally removed the private repo. (#54)
1 parent 22c5d8b commit b47f942

File tree

13 files changed

+106
-49
lines changed

13 files changed

+106
-49
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
---
22
ansible_ssh_user: ec2-user
3+
env: prod
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
$ANSIBLE_VAULT;1.1;AES256
2-
39666666393138366664303864626130323134656239316636666138363532386264353232373761
3-
3135623930323134353731366432346433633237613633310a303234373566373763363663643666
4-
33306631316662373261643164633535396534373234323833323266393963386638663663613065
5-
3165626231343232390a373735393362313663623265613232333738653539343231633839306331
6-
33643935363134366631343133373662663665323464653730356264323537333264346434623233
7-
31316663393438656461333431656631613262353562333964653230313331653839346236343364
8-
39666339633564666438646365643166326562353338376265326234666633653238393237623636
9-
39313433363732326234353863363730626263356536613732313561653062383965653363356665
10-
37383262343134326163336235656266633333653332343461626330333639366635636638636664
11-
37313837316361313464353465393832636332323332666464303762643636613938326536353838
12-
31313062323730663034633239336236383632353535623432333932356539373565376361303638
13-
33303534343838303134323839633863313237303337646532343064666330333966643635356337
14-
65623265366330623935633230376238353837353662303039666335383533306139
2+
62383035313961363234303436316238633235343139323264356462393132303962383033623136
3+
3463383832376634343961373932646132666663643732650a333763393432633635303735393163
4+
34623936316439646666303663656462376234646561626635353465396332623933346132386664
5+
3365613034663366660a666133333263326230373235623635633732333661656636383938663863
6+
31643339653664663766303063353062356230313239663030626233323434346631663137623465
7+
65353935346530333734656364656362376234623935636633363638353063653534353031306430
8+
31393233353561616634346231343265663132306366303035313466653036653232306433343564
9+
35323736316462323664666434643938623636373131623635353365376336346538353538616266
10+
33666662343638663464323661366339346364633232333335643464393066363832333830303132
11+
38626130613939373161393266343837343161336130613162333036376562353261313538646666
12+
33306630323664363131303032633131316466333366363465306464626566386336656136663939
13+
36613834653638323463373534376365346131613838366130663630626431643530386434373133
14+
35663763656364363237366666323231386130653365386263623463656232343239373362386530
15+
32393832666333383337336261393332373934343262613066353931393839306639376134623334
16+
61616262353362393931656536346663346164333064313532383035666438626163643733333137
17+
31383263626666316130336536323761646232326532633739623431316134383266623435636134
18+
38346333303664326237636236646161303761326464633635643763336134636566396166356263
19+
64636639323338326366376335636131373532663936623763346565383631366166396332386535
20+
376464376465633439326131623036343763

ansible/inventories/production/group_vars/stubbornjava/webserver_vars.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
url: "{{_vault['db']['url']}}"
44
user: "{{_vault['db']['user']}}"
55
password: "{{_vault['db']['password']}}"
6+
7+
github:
8+
client_id: "{{_vault['github']['client_id']}}"
9+
client_secret: "{{_vault['github']['client_secret']}}"

ansible/roles/apps/app_base/templates/secure.conf.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ db {
33
user="{{db['user']}}"
44
password="{{db['password']}}"
55
}
6+
7+
github {
8+
clientId="{{github['client_id']}}"
9+
clientSecret="{{github['client_secret']}}"
10+
}

ansible/stubbornjava.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
- {
99
role: apps/app_base,
1010
app_name: stubbornjava,
11-
app_command: java8 -Denv=prod -Xmx640m -cp 'stubbornjava-all.jar' com.stubbornjava.webapp.StubbornJavaWebApp,
11+
app_command: "java8 -Denv={{env}} -server -Xmx640m -cp 'stubbornjava-all.jar' com.stubbornjava.webapp.StubbornJavaWebApp",
1212
become: true
1313
}

settings.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ include ':stubbornjava-common'
66
include ':stubbornjava-examples'
77
include ':stubbornjava-webapp'
88

9-
def rootProjectDescriptor = settings.rootProject
10-
settings.createProjectDescriptor(rootProjectDescriptor, 'stubbornjava-private', file('../stubbornjava'))
119
// {{end:settings}}

stubbornjava-common/src/main/java/com/stubbornjava/common/Configs.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
package com.stubbornjava.common;
22

3-
import java.util.List;
3+
import java.io.File;
44
import java.util.Map;
55

66
import org.jooq.lambda.Seq;
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10-
import com.google.common.base.Preconditions;
11-
import com.google.common.collect.Lists;
1210
import com.typesafe.config.Config;
1311
import com.typesafe.config.ConfigFactory;
1412
import com.typesafe.config.ConfigRenderOptions;
1513

1614
// {{start:config}}
1715
public class Configs {
18-
private static final Logger logger = LoggerFactory.getLogger(Configs.class);
16+
private static final Logger log = LoggerFactory.getLogger(Configs.class);
1917
/*
2018
* I am letting the typesafe configs bleed out on purpose here.
2119
* We could abstract out and delegate but its not worth it.
2220
* I am gambling on the fact that I will not switch out the config library.
2321
*/
2422
private static final Config system = ConfigFactory.systemProperties();
25-
private static final Config properties = new Builder().envAwareApp().build();
23+
private static final Config properties = new Builder().withSecureConf().envAwareApp().build();
2624

2725
public static Config system() {
2826
return system;
@@ -38,40 +36,52 @@ public static Map<String, Object> asMap(Config config) {
3836
}
3937

4038
public static class Builder {
41-
private final List<String> configs;
39+
private Config conf;
4240

4341
public Builder() {
44-
this.configs = Lists.newLinkedList();
42+
log.info("Loading configs first row is highest priority, second row is fallback and so on");
4543
}
4644

4745
public Builder withResource(String resource) {
48-
configs.add(resource);
46+
conf = returnOrFallback(ConfigFactory.parseResources(resource));
47+
log.info("Loaded config file from resource ({})", resource);
48+
return this;
49+
}
50+
51+
public Builder withOptionalFile(String path) {
52+
File secureConfFile = new File(path);
53+
if (secureConfFile.exists()) {
54+
log.info("Loaded config file from path ({})", path);
55+
conf = returnOrFallback(ConfigFactory.parseFile(secureConfFile));
56+
} else {
57+
log.info("Attempted to load file from path ({}) but it was not found", path);
58+
}
4959
return this;
5060
}
5161

5262
public Builder envAwareApp() {
5363
String env = system.hasPath("env") ? system.getString("env") : "local";
5464
String envFile = "application." + env + ".conf";
55-
configs.add(envFile);
56-
configs.add("application.conf");
57-
return this;
65+
return withResource(envFile).withResource("application.conf");
5866
}
5967

60-
public Config build() {
61-
logger.info("Loading configs first row is highest priority, second row is fallback and so on");
62-
configs.forEach(logger::info);
63-
Preconditions.checkArgument(configs.size() > 0, "No config resources specified!");
64-
Config appConfig = ConfigFactory.parseResources(configs.remove(0));
65-
for (String resource : configs) {
66-
appConfig = appConfig.withFallback(ConfigFactory.parseResources(resource));
67-
}
68+
public Builder withSecureConf() {
69+
return withOptionalFile("./secure.conf");
70+
}
6871

72+
public Config build() {
6973
// Resolve substitutions.
70-
appConfig = appConfig.resolve();
74+
conf = conf.resolve();
75+
log.debug("Logging properties. Make sure sensitive data such as passwords or secrets are not logged!");
76+
log.debug(conf.root().render(ConfigRenderOptions.concise().setFormatted(true)));
77+
return conf;
78+
}
7179

72-
logger.debug("Logging properties. Make sure sensitive data such as passwords or secrets are not logged!");
73-
logger.debug(appConfig.root().render(ConfigRenderOptions.concise().setFormatted(true)));
74-
return appConfig;
80+
private Config returnOrFallback(Config config) {
81+
if (this.conf == null) {
82+
return config;
83+
}
84+
return this.conf.withFallback(config);
7585
}
7686
}
7787

stubbornjava-common/src/main/java/com/stubbornjava/common/Env.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ public String getName() {
1919
}
2020

2121
// {{start:logger}}
22-
private static final Logger logger = LoggerFactory.getLogger(Env.class);
22+
private static final Logger log = LoggerFactory.getLogger(Env.class);
2323
private static final Env currentEnv;
2424
static {
2525
String env = "local";
2626
if (Configs.system().hasPath("env")) {
2727
env = Configs.system().getString("env");
2828
}
2929
currentEnv = Env.valueOf(env.toUpperCase());
30-
logger.debug("Current Env: {}", currentEnv.getName());
30+
log.info("Current Env: {}", currentEnv.getName());
3131
}
3232

3333
public static Env get() {
3434
return currentEnv;
3535
}
3636

3737
public static void main(String[] args) {
38-
Env env = currentEnv.get();
38+
Env env = Env.get();
39+
log.debug(env.toString());
3940
}
4041
// {{end:logger}}
4142
}

stubbornjava-common/src/main/java/com/stubbornjava/common/undertow/SimpleServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public void start() {
3030
Undertow undertow = undertowBuilder.build();
3131
undertow.start();
3232
/*
33-
* Undertow logs this on its own but we generally set 3rd party
34-
* default logger levels to warn so we log it here. If it wasn't using the
33+
* Undertow logs this on debug but we generally set 3rd party
34+
* default logger levels to info so we log it here. If it wasn't using the
3535
* io.undertow context we could turn on just that logger but no big deal.
3636
*/
3737
undertow.getListenerInfo()
3838
.stream()
39-
.forEach(listenerInfo -> logger.debug(listenerInfo.toString()));
39+
.forEach(listenerInfo -> logger.info(listenerInfo.toString()));
4040
}
4141

4242
public static SimpleServer simpleServer(HttpHandler handler) {

stubbornjava-webapp/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ dependencies {
33
// Project reference
44
compile project(':stubbornjava-undertow')
55
compile project(':stubbornjava-common')
6-
compile project(':stubbornjava-private')
76

87
compile libs.lombok
98

0 commit comments

Comments
 (0)