This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
/
ServiceModule.java
285 lines (238 loc) · 11.2 KB
/
ServiceModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package keywhiz;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.codahale.metrics.jdbi.InstrumentedTimingCollector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import io.dropwizard.Configuration;
import io.dropwizard.auth.basic.BasicCredentials;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.db.ManagedDataSource;
import io.dropwizard.java8.auth.Authenticator;
import io.dropwizard.java8.jdbi.DBIFactory;
import io.dropwizard.java8.jdbi.OptionalContainerFactory;
import io.dropwizard.java8.jdbi.args.LocalDateTimeArgumentFactory;
import io.dropwizard.java8.jdbi.args.LocalDateTimeMapper;
import io.dropwizard.java8.jdbi.args.OptionalArgumentFactory;
import io.dropwizard.jdbi.ImmutableListContainerFactory;
import io.dropwizard.jdbi.ImmutableSetContainerFactory;
import io.dropwizard.jdbi.NamePrependingStatementRewriter;
import io.dropwizard.jdbi.args.JodaDateTimeArgumentFactory;
import io.dropwizard.jdbi.args.JodaDateTimeMapper;
import io.dropwizard.jdbi.logging.LogbackLog;
import io.dropwizard.setup.Environment;
import java.sql.SQLException;
import java.time.Clock;
import keywhiz.auth.BouncyCastle;
import keywhiz.auth.User;
import keywhiz.auth.cookie.CookieConfig;
import keywhiz.auth.cookie.CookieModule;
import keywhiz.auth.cookie.SessionCookie;
import keywhiz.auth.xsrf.Xsrf;
import keywhiz.generators.SecretGeneratorBindingModule;
import keywhiz.generators.TemplatedSecretGenerator;
import keywhiz.jdbi.SanerNamingStrategy;
import keywhiz.service.config.Readonly;
import keywhiz.service.crypto.ContentCryptographer;
import keywhiz.service.crypto.CryptoModule;
import keywhiz.service.crypto.SecretTransformer;
import keywhiz.service.daos.AclDAO;
import keywhiz.service.daos.ClientDAO;
import keywhiz.service.daos.GroupDAO;
import keywhiz.service.daos.MapArgumentFactory;
import keywhiz.service.daos.SecretContentJooqDao;
import keywhiz.service.daos.SecretController;
import keywhiz.service.daos.SecretDAO;
import keywhiz.service.daos.SecretSeriesDAO;
import keywhiz.service.daos.SecretSeriesJooqDao;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.skife.jdbi.v2.ColonPrefixNamedParamStatementRewriter;
import org.skife.jdbi.v2.DBI;
import org.slf4j.LoggerFactory;
import static com.google.common.base.Preconditions.checkNotNull;
public class ServiceModule extends AbstractModule {
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ServiceModule.class);
private static final Logger DBI_LOGGER = (Logger) LoggerFactory.getLogger(DBI.class);
private final Environment environment;
private final KeywhizConfig config;
public ServiceModule(KeywhizConfig config, Environment environment) {
this.config = checkNotNull(config);
this.environment = checkNotNull(environment);
}
@Override protected void configure() {
// Initialize the BouncyCastle security provider for BKS and cryptography support.
BouncyCastle.require();
bind(Clock.class).toInstance(Clock.systemUTC());
install(new CookieModule(config.getCookieKey()));
install(new CryptoModule(config.getDerivationProviderClass(), config.getContentKeyStore()));
bind(CookieConfig.class).annotatedWith(SessionCookie.class)
.toInstance(config.getSessionCookieConfig());
bind(CookieConfig.class).annotatedWith(Xsrf.class)
.toInstance(config.getXsrfCookieConfig());
// TODO(justin): Consider https://github.com/HubSpot/dropwizard-guice.
bind(Environment.class).toInstance(environment);
bind(Configuration.class).toInstance(config);
bind(KeywhizConfig.class).toInstance(config);
install(new SecretGeneratorBindingModule() {
@Override protected void configure() {
bindSecretGenerator("templated", TemplatedSecretGenerator.class);
}
});
}
// ManagedDataSource
@Provides @Singleton ManagedDataSource dataSource(Environment environment,
KeywhizConfig config) {
DataSourceFactory dataSourceFactory = config.getDataSourceFactory();
ManagedDataSource dataSource = dataSourceFactory.build(environment.metrics(), "postgres-writable");
environment.lifecycle().manage(dataSource);
return dataSource;
}
@Provides @Singleton @Readonly ManagedDataSource readonlyDataSource(Environment environment,
KeywhizConfig config) {
DataSourceFactory dataSourceFactory = config.getReadonlyDataSourceFactory();
ManagedDataSource dataSource = dataSourceFactory.build(environment.metrics(), "postgres-readonly");
environment.lifecycle().manage(dataSource);
return dataSource;
}
// jOOQ
@Provides @Singleton DSLContext jooqContext(ManagedDataSource dataSource) throws SQLException {
return DSL.using(dataSource.getConnection());
}
@Provides @Singleton @Readonly DSLContext readonlyJooqContext(@Readonly ManagedDataSource dataSource)
throws SQLException {
return DSL.using(dataSource.getConnection());
}
// DBI
@Provides ObjectMapper configuredObjectMapper(Environment environment) {
return environment.getObjectMapper();
}
@Provides @Singleton DBIFactory dbiFactory() {
return new DBIFactory();
}
/**
* Super lame copy of functionality from {@link DBIFactory}. Want both DBI instances to perform
* similarly, however do NOT want a health check of the writable DBI. Failure should allow host
* to remain in rotation of healthy servers since readonly DBI sufficient for critical tasks.
*/
@Provides @Singleton DBI dbi(Environment environment, KeywhizConfig config,
MapArgumentFactory mapArgumentFactory, ManagedDataSource dataSource) throws ClassNotFoundException {
logger.debug("Creating DBI");
DataSourceFactory dataSourceFactory = config.getDataSourceFactory();
final DBI dbi = new DBI(dataSource);
dbi.setSQLLog(new LogbackLog(DBI_LOGGER, Level.TRACE));
dbi.setTimingCollector(new InstrumentedTimingCollector(environment.metrics(),
new SanerNamingStrategy()));
if (dataSourceFactory.isAutoCommentsEnabled()) {
dbi.setStatementRewriter(new NamePrependingStatementRewriter(new ColonPrefixNamedParamStatementRewriter()));
}
dbi.registerArgumentFactory(mapArgumentFactory);
dbi.registerArgumentFactory(
new io.dropwizard.jdbi.args.OptionalArgumentFactory(dataSourceFactory.getDriverClass()));
dbi.registerContainerFactory(new ImmutableListContainerFactory());
dbi.registerContainerFactory(new ImmutableSetContainerFactory());
dbi.registerContainerFactory(new io.dropwizard.jdbi.OptionalContainerFactory());
dbi.registerArgumentFactory(new JodaDateTimeArgumentFactory());
dbi.registerMapper(new JodaDateTimeMapper());
dbi.registerArgumentFactory(new OptionalArgumentFactory(dataSourceFactory.getDriverClass()));
dbi.registerContainerFactory(new OptionalContainerFactory());
dbi.registerArgumentFactory(new LocalDateTimeArgumentFactory());
dbi.registerMapper(new LocalDateTimeMapper());
return dbi;
}
@Provides @Singleton
@Readonly DBI readonlyDbi(KeywhizConfig config, MapArgumentFactory mapArgumentFactory,
@Readonly ManagedDataSource dataSource)
throws ClassNotFoundException {
logger.debug("Creating read-only DBI");
final DBI dbi = new DBI(dataSource);
dbi.registerArgumentFactory(new OptionalArgumentFactory(config.getReadonlyDataSourceFactory().getDriverClass()));
dbi.registerContainerFactory(new OptionalContainerFactory());
dbi.registerArgumentFactory(new LocalDateTimeArgumentFactory());
dbi.registerMapper(new LocalDateTimeMapper());
dbi.registerArgumentFactory(mapArgumentFactory);
return dbi;
}
// DAOs using DBI
@Provides @Singleton @Readonly SecretController readonlySecretController(
SecretTransformer transformer, ContentCryptographer cryptographer,
@Readonly SecretDAO secretDAO) {
return new SecretController(transformer, cryptographer, secretDAO);
}
@Provides @Singleton SecretController secretController(SecretTransformer transformer,
ContentCryptographer cryptographer, SecretDAO secretDAO) {
return new SecretController(transformer, cryptographer, secretDAO);
}
@Provides @Singleton @Readonly SecretDAO readonlySecretDAO(@Readonly DBI dbi) {
return dbi.onDemand(SecretDAO.class);
}
@Provides @Singleton SecretDAO secretDAO(DBI dbi) {
return dbi.onDemand(SecretDAO.class);
}
@Provides @Singleton @Readonly SecretSeriesDAO readonlySecretSeriesDAO(@Readonly DBI dbi) {
return dbi.onDemand(SecretSeriesDAO.class);
}
@Provides @Singleton SecretSeriesDAO secretSeriesDAO(DBI dbi) {
return dbi.onDemand(SecretSeriesDAO.class);
}
// DAOs using jOOQ
@Provides @Singleton AclDAO aclDAO(DSLContext jooqContext, ClientDAO clientDAO,
GroupDAO groupDAO, SecretContentJooqDao secretContentJooqDao,
SecretSeriesJooqDao secretSeriesJooqDao) {
return new AclDAO(jooqContext, clientDAO, groupDAO, secretContentJooqDao, secretSeriesJooqDao);
}
@Provides @Singleton
@Readonly AclDAO readonlyAclDAO(@Readonly DSLContext jooqContext, @Readonly ClientDAO clientDAO,
@Readonly GroupDAO groupDAO, @Readonly SecretContentJooqDao secretContentJooqDao,
@Readonly SecretSeriesJooqDao secretSeriesJooqDao) {
return new AclDAO(jooqContext, clientDAO, groupDAO, secretContentJooqDao, secretSeriesJooqDao);
}
@Provides @Singleton ClientDAO clientDAO(DSLContext jooqContext) {
return new ClientDAO(jooqContext);
}
@Provides @Singleton @Readonly ClientDAO readonlyClientDAO(@Readonly DSLContext jooqContext) {
return new ClientDAO(jooqContext);
}
@Provides @Singleton GroupDAO groupDAO(DSLContext jooqContext) {
return new GroupDAO(jooqContext);
}
@Provides @Singleton @Readonly GroupDAO readonlyGroupDAO(@Readonly DSLContext jooqContext) {
return new GroupDAO(jooqContext);
}
@Provides @Singleton SecretContentJooqDao secretContentJooqDao(DSLContext jooqContext) {
return new SecretContentJooqDao(jooqContext);
}
@Provides @Singleton
@Readonly SecretContentJooqDao readonlySecretContentJooqDao(@Readonly DSLContext jooqContext) {
return new SecretContentJooqDao(jooqContext);
}
@Provides @Singleton SecretSeriesJooqDao secretSeriesJooqDao(DSLContext jooqContext) {
return new SecretSeriesJooqDao(jooqContext);
}
@Provides @Singleton
@Readonly SecretSeriesJooqDao readonlySecretSeriesJooqDao(@Readonly DSLContext jooqContext) {
return new SecretSeriesJooqDao(jooqContext);
}
@Provides @Singleton
@Readonly Authenticator<BasicCredentials, User> authenticator(KeywhizConfig config,
@Readonly DSLContext jooqContext) {
return config.getUserAuthenticatorFactory().build(jooqContext);
}
}