Search in sources :

Example 1 with Fingerprint

use of org.gradle.security.internal.Fingerprint in project gradle by gradle.

the class CrossBuildCachingKeyService method findByLongId.

@Override
public void findByLongId(long keyId, PublicKeyResultBuilder builder) {
    longIdGuard.guardByKey(keyId, () -> {
        CacheEntry<List<Fingerprint>> fingerprints = longIdToFingerprint.getIfPresent(keyId);
        if (fingerprints == null || hasExpired(fingerprints)) {
            buildOperationExecutor.run(new RunnableBuildOperation() {

                @Override
                public void run(BuildOperationContext context) {
                    long currentTime = timeProvider.getCurrentTime();
                    AtomicBoolean missing = new AtomicBoolean(true);
                    delegate.findByLongId(keyId, new PublicKeyResultBuilder() {

                        @Override
                        public void keyRing(PGPPublicKeyRing keyring) {
                            missing.set(false);
                            builder.keyRing(keyring);
                            Iterator<PGPPublicKey> pkIt = keyring.getPublicKeys();
                            while (pkIt.hasNext()) {
                                PGPPublicKey publicKey = pkIt.next();
                                Fingerprint fingerprint = Fingerprint.of(publicKey);
                                publicKeyRings.put(fingerprint, new CacheEntry<>(currentTime, keyring));
                                updateLongKeyIndex(fingerprint, keyId);
                            }
                        }

                        @Override
                        public void publicKey(PGPPublicKey publicKey) {
                            missing.set(false);
                            if (publicKey.getKeyID() == keyId) {
                                builder.publicKey(publicKey);
                            }
                        }
                    });
                    if (missing.get()) {
                        longIdToFingerprint.put(keyId, new CacheEntry<>(currentTime, null));
                    }
                }

                @Override
                public BuildOperationDescriptor.Builder description() {
                    return BuildOperationDescriptor.displayName("Fetching public key").progressDisplayName("Downloading public key " + toLongIdHexString(keyId));
                }
            });
        } else {
            if (fingerprints.value != null) {
                for (Fingerprint fingerprint : fingerprints.value) {
                    findByFingerprint(fingerprint.getBytes(), new PublicKeyResultBuilder() {

                        @Override
                        public void keyRing(PGPPublicKeyRing keyring) {
                            builder.keyRing(keyring);
                        }

                        @Override
                        public void publicKey(PGPPublicKey publicKey) {
                            if (publicKey.getKeyID() == keyId) {
                                builder.publicKey(publicKey);
                            }
                        }
                    });
                }
            }
        }
        return null;
    });
}
Also used : BuildOperationContext(org.gradle.internal.operations.BuildOperationContext) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Fingerprint(org.gradle.security.internal.Fingerprint) PublicKeyResultBuilder(org.gradle.security.internal.PublicKeyResultBuilder) LockOptionsBuilder(org.gradle.cache.internal.filelock.LockOptionsBuilder) CacheBuilder(org.gradle.cache.CacheBuilder) PublicKeyResultBuilder(org.gradle.security.internal.PublicKeyResultBuilder) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) RunnableBuildOperation(org.gradle.internal.operations.RunnableBuildOperation)

Example 2 with Fingerprint

use of org.gradle.security.internal.Fingerprint in project gradle by gradle.

the class CrossBuildCachingKeyService method findByFingerprint.

@Override
public void findByFingerprint(byte[] bytes, PublicKeyResultBuilder builder) {
    Fingerprint fingerprint = Fingerprint.wrap(bytes);
    fingerPrintguard.guardByKey(fingerprint, () -> {
        CacheEntry<PGPPublicKeyRing> cacheEntry = publicKeyRings.getIfPresent(fingerprint);
        if (cacheEntry == null || hasExpired(cacheEntry)) {
            LookupPublicKeyResultBuilder keyResultBuilder = new LookupPublicKeyResultBuilder();
            delegate.findByFingerprint(bytes, keyResultBuilder);
            cacheEntry = keyResultBuilder.entry;
        }
        if (cacheEntry != null) {
            builder.keyRing(cacheEntry.value);
            Iterator<PGPPublicKey> pkIt = cacheEntry.value.getPublicKeys();
            while (pkIt.hasNext()) {
                PGPPublicKey publicKey = pkIt.next();
                if (Arrays.equals(publicKey.getFingerprint(), bytes)) {
                    builder.publicKey(publicKey);
                }
            }
        }
        return null;
    });
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Fingerprint(org.gradle.security.internal.Fingerprint) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey)

Aggregations

PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)2 PGPPublicKeyRing (org.bouncycastle.openpgp.PGPPublicKeyRing)2 Fingerprint (org.gradle.security.internal.Fingerprint)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CacheBuilder (org.gradle.cache.CacheBuilder)1 LockOptionsBuilder (org.gradle.cache.internal.filelock.LockOptionsBuilder)1 BuildOperationContext (org.gradle.internal.operations.BuildOperationContext)1 RunnableBuildOperation (org.gradle.internal.operations.RunnableBuildOperation)1 PublicKeyResultBuilder (org.gradle.security.internal.PublicKeyResultBuilder)1