Search in sources :

Example 1 with Lock

use of io.vertx.reactivex.core.shareddata.Lock in project api-framework by vinscom.

the class SecurityTools method startup.

@SuppressWarnings("unchecked")
@StartService
public void startup() {
    setRandom(new SecureRandom());
    if (!getVertx().isClustered()) {
        mGlobalUniqueString.complete("A" + mRandom.nextInt());
        return;
    }
    Map<String, Object> cryptCtx = new HashMap<>();
    Single<Lock> lock = getVertx().sharedData().rxGetLockWithTimeout("_in.erail.security", 5000);
    getVertx().sharedData().<String, byte[]>rxGetClusterWideMap("_in.erail.security").flatMap((m) -> {
        cryptCtx.put("map", m);
        return m.rxGet("key");
    }).map((k) -> {
        cryptCtx.put("key", k);
        return cryptCtx;
    }).flatMap((ctx) -> {
        if (ctx.get("key") == null) {
            return lock.map((l) -> {
                ctx.put("lock", l);
                return ctx;
            });
        }
        return Single.just(ctx);
    }).flatMap(ctx -> {
        if (ctx.get("lock") != null) {
            return ((AsyncMap<String, Object>) (ctx.get("map"))).rxGet("key").map((k) -> {
                ctx.put("key", k);
                return ctx;
            });
        }
        return Single.just(ctx);
    }).flatMap((ctx) -> {
        if (ctx.get("key") == null) {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128);
            byte[] key = keygen.generateKey().getEncoded();
            return ((AsyncMap<String, Object>) (ctx.get("map"))).rxPut("key", key).doOnComplete(() -> ctx.put("key", key)).toSingleDefault(ctx);
        }
        return Single.just(ctx);
    }).map(ctx -> (byte[]) ctx.get("key")).doFinally(() -> {
        if (cryptCtx.containsKey("lock")) {
            Lock l = (Lock) cryptCtx.get("lock");
            l.release();
        }
    }).subscribe((key) -> {
        mKeySpec.complete(new SecretKeySpec(key, "AES"));
        String unique = Base64.getEncoder().encodeToString(Arrays.copyOfRange(key, 0, 5));
        mGlobalUniqueString.complete(unique.replace("=", ""));
        getLog().info(() -> String.format("GlobalUniqueString:[%s]", unique));
    });
}
Also used : Arrays(java.util.Arrays) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncMap(io.vertx.reactivex.core.shareddata.AsyncMap) Single(io.reactivex.Single) Cipher(javax.crypto.Cipher) KeyGenerator(javax.crypto.KeyGenerator) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) ExecutionException(java.util.concurrent.ExecutionException) Vertx(io.vertx.reactivex.core.Vertx) Base64(java.util.Base64) BadPaddingException(javax.crypto.BadPaddingException) Logger(org.apache.logging.log4j.Logger) StartService(in.erail.glue.annotation.StartService) IvParameterSpec(javax.crypto.spec.IvParameterSpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Map(java.util.Map) InvalidKeyException(java.security.InvalidKeyException) Lock(io.vertx.reactivex.core.shareddata.Lock) HashMap(java.util.HashMap) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) Lock(io.vertx.reactivex.core.shareddata.Lock) StartService(in.erail.glue.annotation.StartService)

Aggregations

StartService (in.erail.glue.annotation.StartService)1 Single (io.reactivex.Single)1 Vertx (io.vertx.reactivex.core.Vertx)1 AsyncMap (io.vertx.reactivex.core.shareddata.AsyncMap)1 Lock (io.vertx.reactivex.core.shareddata.Lock)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SecureRandom (java.security.SecureRandom)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 BadPaddingException (javax.crypto.BadPaddingException)1 Cipher (javax.crypto.Cipher)1 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)1 KeyGenerator (javax.crypto.KeyGenerator)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1