1111import com .nimbusds .jose .jwk .JWKSet ;
1212import com .nimbusds .jose .util .JSONObjectUtils ;
1313import com .nimbusds .jwt .JWTClaimsSet ;
14- import com .nimbusds .jwt .SignedJWT ;
1514
1615import org .apache .http .HttpEntity ;
1716import org .apache .http .HttpResponse ;
3332import org .apache .logging .log4j .Logger ;
3433import org .elasticsearch .ElasticsearchSecurityException ;
3534import org .elasticsearch .SpecialPermission ;
36- import org .elasticsearch .action .support . PlainActionFuture ;
35+ import org .elasticsearch .action .ActionListener ;
3736import org .elasticsearch .common .Strings ;
37+ import org .elasticsearch .common .hash .MessageDigests ;
3838import org .elasticsearch .common .settings .SecureString ;
3939import org .elasticsearch .common .settings .SettingsException ;
4040import org .elasticsearch .common .ssl .SslConfiguration ;
5151import java .nio .file .Files ;
5252import java .nio .file .Path ;
5353import java .security .AccessController ;
54+ import java .security .MessageDigest ;
5455import java .security .PrivilegedAction ;
5556import java .security .PrivilegedActionException ;
5657import java .security .PrivilegedExceptionAction ;
@@ -185,16 +186,25 @@ public static URI parseHttpsUri(final String uriString) {
185186 return null ;
186187 }
187188
188- public static byte [] readUriContents (
189+ public static void readUriContents (
189190 final String jwkSetConfigKeyPkc ,
190191 final URI jwkSetPathPkcUri ,
191- final CloseableHttpAsyncClient httpClient
192- ) throws SettingsException {
193- try {
194- return JwtUtil .readBytes (httpClient , jwkSetPathPkcUri );
195- } catch (Exception e ) {
196- throw new SettingsException ("Can't get contents for setting [" + jwkSetConfigKeyPkc + "] value [" + jwkSetPathPkcUri + "]." , e );
197- }
192+ final CloseableHttpAsyncClient httpClient ,
193+ final ActionListener <byte []> listener
194+ ) {
195+ JwtUtil .readBytes (
196+ httpClient ,
197+ jwkSetPathPkcUri ,
198+ ActionListener .wrap (
199+ listener ::onResponse ,
200+ ex -> listener .onFailure (
201+ new SettingsException (
202+ "Can't get contents for setting [" + jwkSetConfigKeyPkc + "] value [" + jwkSetPathPkcUri + "]." ,
203+ ex
204+ )
205+ )
206+ )
207+ );
198208 }
199209
200210 public static byte [] readFileContents (final String jwkSetConfigKeyPkc , final String jwkSetPathPkc , final Environment environment )
@@ -211,7 +221,7 @@ public static byte[] readFileContents(final String jwkSetConfigKeyPkc, final Str
211221 }
212222
213223 public static String serializeJwkSet (final JWKSet jwkSet , final boolean publicKeysOnly ) {
214- if (( jwkSet == null ) || ( jwkSet . getKeys (). isEmpty ()) ) {
224+ if (jwkSet == null ) {
215225 return null ;
216226 }
217227 return JSONObjectUtils .toJSONString (jwkSet .toJSONObject (publicKeysOnly ));
@@ -262,13 +272,11 @@ public static CloseableHttpAsyncClient createHttpClient(final RealmConfig realmC
262272 }
263273
264274 /**
265- * Use the HTTP Client to get URL content bytes up to N max bytes .
275+ * Use the HTTP Client to get URL content bytes.
266276 * @param httpClient Configured HTTP/HTTPS client.
267277 * @param uri URI to download.
268- * @return Byte array of the URI contents up to N max bytes.
269278 */
270- public static byte [] readBytes (final CloseableHttpAsyncClient httpClient , final URI uri ) {
271- final PlainActionFuture <byte []> plainActionFuture = PlainActionFuture .newFuture ();
279+ public static void readBytes (final CloseableHttpAsyncClient httpClient , final URI uri , ActionListener <byte []> listener ) {
272280 AccessController .doPrivileged ((PrivilegedAction <Void >) () -> {
273281 httpClient .execute (new HttpGet (uri ), new FutureCallback <>() {
274282 @ Override
@@ -278,12 +286,12 @@ public void completed(final HttpResponse result) {
278286 if (statusCode == 200 ) {
279287 final HttpEntity entity = result .getEntity ();
280288 try (InputStream inputStream = entity .getContent ()) {
281- plainActionFuture .onResponse (inputStream .readAllBytes ());
289+ listener .onResponse (inputStream .readAllBytes ());
282290 } catch (Exception e ) {
283- plainActionFuture .onFailure (e );
291+ listener .onFailure (e );
284292 }
285293 } else {
286- plainActionFuture .onFailure (
294+ listener .onFailure (
287295 new ElasticsearchSecurityException (
288296 "Get [" + uri + "] failed, status [" + statusCode + "], reason [" + statusLine .getReasonPhrase () + "]."
289297 )
@@ -293,17 +301,16 @@ public void completed(final HttpResponse result) {
293301
294302 @ Override
295303 public void failed (Exception e ) {
296- plainActionFuture .onFailure (new ElasticsearchSecurityException ("Get [" + uri + "] failed." , e ));
304+ listener .onFailure (new ElasticsearchSecurityException ("Get [" + uri + "] failed." , e ));
297305 }
298306
299307 @ Override
300308 public void cancelled () {
301- plainActionFuture .onFailure (new ElasticsearchSecurityException ("Get [" + uri + "] was cancelled." ));
309+ listener .onFailure (new ElasticsearchSecurityException ("Get [" + uri + "] was cancelled." ));
302310 }
303311 });
304312 return null ;
305313 });
306- return plainActionFuture .actionGet ();
307314 }
308315
309316 public static Path resolvePath (final Environment environment , final String jwkSetPath ) {
@@ -335,14 +342,10 @@ public static SecureString join(final CharSequence delimiter, final CharSequence
335342 * JWSHeader: Header are not support.
336343 * JWTClaimsSet: Claims are supported. Claim keys are prefixed by "jwt_claim_".
337344 * Base64URL: Signature is not supported.
338- * @param jwt SignedJWT object.
339345 * @return Map of formatted and filtered values to be used as user metadata.
340- * @throws Exception Parse error.
341346 */
342- //
343347 // Values will be filtered by type using isAllowedTypeForClaim().
344- public static Map <String , Object > toUserMetadata (final SignedJWT jwt ) throws Exception {
345- final JWTClaimsSet claimsSet = jwt .getJWTClaimsSet ();
348+ public static Map <String , Object > toUserMetadata (JWTClaimsSet claimsSet ) {
346349 return claimsSet .getClaims ()
347350 .entrySet ()
348351 .stream ()
@@ -366,4 +369,10 @@ static boolean isAllowedTypeForClaim(final Object value) {
366369 || (value instanceof Collection
367370 && ((Collection <?>) value ).stream ().allMatch (e -> e instanceof String || e instanceof Boolean || e instanceof Number )));
368371 }
372+
373+ public static byte [] sha256 (final CharSequence charSequence ) {
374+ final MessageDigest messageDigest = MessageDigests .sha256 ();
375+ messageDigest .update (charSequence .toString ().getBytes (StandardCharsets .UTF_8 ));
376+ return messageDigest .digest ();
377+ }
369378}
0 commit comments