-
Notifications
You must be signed in to change notification settings - Fork 215
/
DittoPublicKeyProvider.java
412 lines (343 loc) · 18.9 KB
/
DittoPublicKeyProvider.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.security.authentication.jwt;
import static org.eclipse.ditto.base.model.common.ConditionChecker.argumentNotNull;
import java.math.BigInteger;
import java.security.AlgorithmParameters;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.ECPoint;
import java.security.spec.ECPublicKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.KeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.Function;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.gateway.api.GatewayAuthenticationProviderUnavailableException;
import org.eclipse.ditto.gateway.api.GatewayJwtIssuerNotSupportedException;
import org.eclipse.ditto.gateway.service.security.cache.PublicKeyIdWithIssuer;
import org.eclipse.ditto.gateway.service.util.config.security.OAuthConfig;
import org.eclipse.ditto.internal.utils.cache.Cache;
import org.eclipse.ditto.internal.utils.cache.CaffeineCache;
import org.eclipse.ditto.internal.utils.cache.config.CacheConfig;
import org.eclipse.ditto.internal.utils.http.HttpClientFacade;
import org.eclipse.ditto.internal.utils.jwt.JjwtDeserializer;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonMissingFieldException;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.jwt.model.ImmutableJsonWebKey;
import org.eclipse.ditto.jwt.model.JsonWebKey;
import org.eclipse.ditto.policies.model.SubjectIssuer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import akka.http.javadsl.model.HttpRequest;
import akka.http.javadsl.model.HttpResponse;
import akka.stream.Materializer;
import akka.stream.SystemMaterializer;
import akka.stream.javadsl.Sink;
import akka.util.ByteString;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
/**
* Implementation of {@link PublicKeyProvider}. This provider requests keys at the {@link SubjectIssuer} and caches
* responses to reduce network io.
*/
public final class DittoPublicKeyProvider implements PublicKeyProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(DittoPublicKeyProvider.class);
private static final String OPENID_CONNECT_DISCOVERY_PATH = "/.well-known/openid-configuration";
private static final JsonFieldDefinition<String> JSON_JWKS_URI = JsonFieldDefinition.ofString("jwks_uri");
// Supported curve types described in the RFC https://www.rfc-editor.org/rfc/rfc7518#section-6.2.1.1
// Mapping of curve type to ECGenParameterSpec input
private static final Map<String, String> CURVE_TYPE_MAP = Map.of(
"P-256", "secp256r1",
"P-384", "secp384r1",
"P-521", "secp521r1"
);
private final JwtSubjectIssuersConfig jwtSubjectIssuersConfig;
private final HttpClientFacade httpClient;
private final Materializer materializer;
private final OAuthConfig oAuthConfig;
private final Cache<PublicKeyIdWithIssuer, PublicKeyWithParser> publicKeyCache;
private DittoPublicKeyProvider(final JwtSubjectIssuersConfig jwtSubjectIssuersConfig,
final HttpClientFacade httpClient,
final CacheConfig publicKeysConfig,
final String cacheName,
final OAuthConfig oAuthConfig) {
this.jwtSubjectIssuersConfig = argumentNotNull(jwtSubjectIssuersConfig);
this.httpClient = argumentNotNull(httpClient);
materializer = SystemMaterializer.get(httpClient::getActorSystem).materializer();
this.oAuthConfig = oAuthConfig;
argumentNotNull(publicKeysConfig, "config of the public keys cache");
argumentNotNull(cacheName);
final AsyncCacheLoader<PublicKeyIdWithIssuer, PublicKeyWithParser> loader = this::loadPublicKeyWithParser;
final Caffeine<PublicKeyIdWithIssuer, PublicKeyWithParser> caffeine = Caffeine.newBuilder()
.maximumSize(publicKeysConfig.getMaximumSize())
.expireAfterWrite(publicKeysConfig.getExpireAfterWrite())
.removalListener(new CacheRemovalListener());
publicKeyCache = CaffeineCache.of(caffeine, loader, cacheName);
}
DittoPublicKeyProvider(final JwtSubjectIssuersConfig jwtSubjectIssuersConfig,
final HttpClientFacade httpClient,
final OAuthConfig oAuthConfig,
final Function<AsyncCacheLoader<PublicKeyIdWithIssuer, PublicKeyWithParser>,
Cache<PublicKeyIdWithIssuer, PublicKeyWithParser>> publicKeyCacheFactory) {
this.jwtSubjectIssuersConfig = argumentNotNull(jwtSubjectIssuersConfig);
this.httpClient = argumentNotNull(httpClient);
materializer = SystemMaterializer.get(httpClient::getActorSystem).materializer();
this.oAuthConfig = oAuthConfig;
publicKeyCache = publicKeyCacheFactory.apply(this::loadPublicKeyWithParser);
}
/**
* Returns a new {@code PublicKeyProvider} for the given parameters.
*
* @param jwtSubjectIssuersConfig the configuration of supported JWT subject issuers
* @param httpClient the http client.
* @param publicKeysCacheConfig the config of the public keys cache.
* @param cacheName The name of the cache.
* @param oAuthConfig the OAuth configuration.
* @return the PublicKeyProvider.
* @throws NullPointerException if any argument is {@code null}.
*/
public static PublicKeyProvider of(final JwtSubjectIssuersConfig jwtSubjectIssuersConfig,
final HttpClientFacade httpClient,
final CacheConfig publicKeysCacheConfig,
final String cacheName,
final OAuthConfig oAuthConfig) {
return new DittoPublicKeyProvider(jwtSubjectIssuersConfig, httpClient, publicKeysCacheConfig, cacheName,
oAuthConfig);
}
@Override
public CompletableFuture<Optional<PublicKeyWithParser>> getPublicKeyWithParser(final String issuer,
final String keyId) {
argumentNotNull(issuer);
argumentNotNull(keyId);
return publicKeyCache.get(PublicKeyIdWithIssuer.of(keyId, issuer));
}
/* this method is used to asynchronously load the public key into the cache */
private CompletableFuture<PublicKeyWithParser> loadPublicKeyWithParser(
final PublicKeyIdWithIssuer publicKeyIdWithIssuer,
final Executor executor) {
final String issuer = publicKeyIdWithIssuer.getIssuer();
final String keyId = publicKeyIdWithIssuer.getKeyId();
LOGGER.debug("Loading public key with id <{}> from issuer <{}>.", keyId, issuer);
final Optional<JwtSubjectIssuerConfig> subjectIssuerConfigOpt = jwtSubjectIssuersConfig.getConfigItem(issuer);
if (subjectIssuerConfigOpt.isEmpty()) {
LOGGER.info("The JWT issuer <{}> is not included in Ditto's gateway configuration at " +
"'ditto.gateway.authentication.oauth.openid-connect-issuers', supported are: <{}>",
issuer, jwtSubjectIssuersConfig);
return CompletableFuture.failedFuture(GatewayJwtIssuerNotSupportedException.newBuilder(issuer).build());
}
final String discoveryEndpoint = getDiscoveryEndpoint(subjectIssuerConfigOpt.get().getIssuers()
.stream()
.filter(configuredIssuer -> configuredIssuer.contains(issuer))
.findAny()
.orElse(issuer));
final CompletionStage<HttpResponse> responseFuture = getPublicKeysFromDiscoveryEndpoint(discoveryEndpoint);
final CompletionStage<JsonArray> publicKeysFuture = responseFuture.thenCompose(this::mapResponseToJsonArray);
return publicKeysFuture.thenApply(publicKeysArray -> mapToPublicKey(publicKeysArray, keyId, discoveryEndpoint))
.thenApply(optionalKey -> optionalKey.map(this::mapToPublicKeyWithParser).orElse(null))
.toCompletableFuture();
}
private String getDiscoveryEndpoint(final String issuer) {
final String iss;
if (issuer.endsWith("/")) {
iss = issuer.substring(0, issuer.length() - 1);
} else {
iss = issuer;
}
return jwtSubjectIssuersConfig.getProtocolPrefix() + iss + OPENID_CONNECT_DISCOVERY_PATH;
}
private CompletableFuture<JsonArray> mapResponseToJsonArray(final HttpResponse response) {
final CompletionStage<JsonObject> body = mapResponseToJsonObject(response);
final JsonPointer keysPointer = JsonPointer.of("keys");
return body.toCompletableFuture()
.thenApply(jsonObject -> jsonObject.getValue(keysPointer).map(JsonValue::asArray)
.orElseThrow(() -> new JsonMissingFieldException(keysPointer)))
.exceptionally(t -> {
final String message =
MessageFormat.format("Failed to extract public keys from JSON response <{0}>", body);
LOGGER.warn(message, t);
throw PublicKeyProviderUnavailableException.newBuilder()
.cause(new IllegalStateException(message, t))
.build();
});
}
private CompletionStage<HttpResponse> getPublicKeysFromDiscoveryEndpoint(final String discoveryEndpoint) {
LOGGER.debug("Loading public keys from discovery endpoint <{}>.", discoveryEndpoint);
return httpClient.createSingleHttpRequest(HttpRequest.GET(discoveryEndpoint))
.thenCompose(this::mapResponseToJsonObject)
.thenApply(jsonObject -> jsonObject.getValueOrThrow(JSON_JWKS_URI))
.thenCompose(jwksUri -> httpClient.createSingleHttpRequest(HttpRequest.GET(jwksUri)))
.exceptionally(e -> {
throw DittoRuntimeException.asDittoRuntimeException(e,
cause -> handleUnexpectedException(cause, discoveryEndpoint));
});
}
private CompletionStage<JsonObject> mapResponseToJsonObject(final HttpResponse response) {
if (!response.status().isSuccess()) {
handleNonSuccessResponse(response);
}
return response.entity().getDataBytes().fold(ByteString.emptyByteString(), ByteString::concat)
.map(ByteString::utf8String)
.map(JsonFactory::readFrom)
.map(JsonValue::asObject)
.runWith(Sink.head(), materializer);
}
private void handleNonSuccessResponse(final HttpResponse response) {
getBodyAsString(response)
.thenAccept(stringBody -> LOGGER.info(
"Got non success response from public key provider with status code: <{}> and body: <{}>.",
response.status(), stringBody));
throw GatewayAuthenticationProviderUnavailableException.newBuilder()
.message("Got unexpected response from public key provider.")
.build();
}
private CompletionStage<String> getBodyAsString(final HttpResponse response) {
return response.entity().getDataBytes().fold(ByteString.emptyByteString(), ByteString::concat)
.map(ByteString::utf8String)
.runWith(Sink.head(), materializer);
}
private static PublicKeyProviderUnavailableException handleUnexpectedException(final Throwable e,
final String discoveryEndpoint) {
final String msg = MessageFormat.format("Got Exception from discovery endpoint <{0}>.", discoveryEndpoint);
LOGGER.warn(msg, e);
return PublicKeyProviderUnavailableException.newBuilder().cause(e).build();
}
private static Optional<PublicKey> mapToPublicKey(final JsonArray publicKeys, final String keyId,
final String discoveryEndpoint) {
LOGGER.debug("Trying to find JSON Web Key with id <{}> in json array <{}>.", keyId, publicKeys);
for (final JsonValue jsonValue : publicKeys) {
try {
final JsonObject jsonObject = jsonValue.asObject();
final JsonWebKey jsonWebKey = ImmutableJsonWebKey.fromJson(jsonObject);
if (jsonWebKey.getId().equals(keyId)) {
LOGGER.debug("Found matching Json Web Key for id <{}>: <{}>.", keyId, jsonWebKey);
return Optional.of(mapMatchingPublicKey(discoveryEndpoint, jsonWebKey));
}
} catch (final NoSuchAlgorithmException | InvalidKeySpecException | InvalidParameterSpecException |
JsonMissingFieldException e) {
final String msg =
MessageFormat.format("Got invalid JSON Web Key from JwkResource provider at discovery " +
"endpoint <{0}>", discoveryEndpoint);
throw getJwkInvalidExceptionException(msg, e);
}
}
LOGGER.debug("Did not find key with id <{}>.", keyId);
return Optional.empty();
}
private static PublicKey mapMatchingPublicKey(final String discoveryEndpoint, final JsonWebKey jsonWebKey)
throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException,
JsonMissingFieldException, JwkInvalidException {
final var type = jsonWebKey.getType();
final KeyFactory keyFactory = KeyFactory.getInstance(type);
final PublicKey result;
if ("EC".equals(type)) {
result = generateECPublicKey(keyFactory, jsonWebKey, discoveryEndpoint);
} else if ("RSA".equals(type)) {
result = generateRSAPublicKey(keyFactory, jsonWebKey);
} else {
final String msg = MessageFormat.format(
"Got invalid JSON Web Key from JwkResource provider at discovery endpoint <{0}>. " +
"The KeyType (kty): <{1}> is unknown.", discoveryEndpoint, type);
throw getJwkInvalidExceptionException(msg, null);
}
return result;
}
private static PublicKey generateECPublicKey(final KeyFactory keyFactory, final JsonWebKey jsonWebKey,
final String discoveryEndpoint)
throws InvalidKeySpecException, NoSuchAlgorithmException, InvalidParameterSpecException,
JsonMissingFieldException, JwkInvalidException {
final AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
final var curveType = jsonWebKey.getCurveType()
.orElseThrow(() -> new JsonMissingFieldException(JsonWebKey.JsonFields.KEY_CURVE.getPointer()));
final var stdName = CURVE_TYPE_MAP.get(curveType);
if (stdName == null) {
final String msg = MessageFormat.format(
"Got invalid JSON Web Key from JwkResource provider at discovery endpoint <{0}>. " +
"The curve parameter (crv): <{1}> is not supported.", discoveryEndpoint, curveType);
throw getJwkInvalidExceptionException(msg, null);
}
parameters.init(new ECGenParameterSpec(stdName));
final ECParameterSpec ecParameterSpec = parameters.getParameterSpec(ECParameterSpec.class);
final var xCoordinate = jsonWebKey.getXCoordinate()
.orElseThrow(() -> new JsonMissingFieldException(JsonWebKey.JsonFields.KEY_X_COORDINATE.getPointer()));
final var yCoordinate = jsonWebKey.getYCoordinate()
.orElseThrow(() -> new JsonMissingFieldException(JsonWebKey.JsonFields.KEY_Y_COORDINATE.getPointer()));
final var ecPoint = new ECPoint(xCoordinate, yCoordinate);
final var ecPublicKeySpec = new ECPublicKeySpec(ecPoint, ecParameterSpec);
return keyFactory.generatePublic(ecPublicKeySpec);
}
private static PublicKey generateRSAPublicKey(final KeyFactory keyFactory, final JsonWebKey jsonWebKey)
throws InvalidKeySpecException, JsonMissingFieldException {
final BigInteger modulus = jsonWebKey.getModulus()
.orElseThrow(() -> new JsonMissingFieldException(JsonWebKey.JsonFields.KEY_MODULUS.getPointer()));
final BigInteger exponent = jsonWebKey.getExponent()
.orElseThrow(() -> new JsonMissingFieldException(JsonWebKey.JsonFields.KEY_EXPONENT.getPointer()));
final KeySpec rsaPublicKeySpec = new RSAPublicKeySpec(modulus, exponent);
return keyFactory.generatePublic(rsaPublicKeySpec);
}
private static JwkInvalidException getJwkInvalidExceptionException(final String msg,
@Nullable Throwable e) {
final JwkInvalidException result;
if (null != e) {
LOGGER.warn(msg, e);
result = JwkInvalidException.newBuilder()
.message(msg)
.description(e.getMessage())
.cause(e)
.build();
} else {
LOGGER.warn(msg);
result = JwkInvalidException.newBuilder()
.message(msg)
.build();
}
return result;
}
private PublicKeyWithParser mapToPublicKeyWithParser(final PublicKey publicKey) {
final var jwtParserBuilder = Jwts.parserBuilder();
final JwtParser jwtParser = jwtParserBuilder.deserializeJsonWith(JjwtDeserializer.getInstance())
.setSigningKey(publicKey)
.setAllowedClockSkewSeconds(oAuthConfig.getAllowedClockSkew().getSeconds())
.build();
return new PublicKeyWithParser(publicKey, jwtParser);
}
private static final class CacheRemovalListener
implements RemovalListener<PublicKeyIdWithIssuer, PublicKeyWithParser> {
@Override
public void onRemoval(@Nullable final PublicKeyIdWithIssuer key, @Nullable final PublicKeyWithParser value,
@Nonnull final com.github.benmanes.caffeine.cache.RemovalCause cause) {
final String msgTemplate = "Removed PublicKey with ID <{}> from cache due to cause '{}'.";
LOGGER.debug(msgTemplate, key, cause);
}
}
}