|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog.collectors; |
| 18 | + |
| 19 | +import com.github.benmanes.caffeine.cache.Cache; |
| 20 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 21 | +import com.github.benmanes.caffeine.cache.Expiry; |
| 22 | +import com.google.common.annotations.VisibleForTesting; |
| 23 | +import com.google.common.eventbus.EventBus; |
| 24 | +import com.google.common.eventbus.Subscribe; |
| 25 | +import com.google.common.util.concurrent.AbstractIdleService; |
| 26 | +import jakarta.inject.Inject; |
| 27 | +import jakarta.inject.Singleton; |
| 28 | +import org.graylog.collectors.events.CollectorCaConfigUpdated; |
| 29 | +import org.graylog.security.pki.CertificateEntry; |
| 30 | +import org.graylog.security.pki.CertificateService; |
| 31 | +import org.graylog.security.pki.PemUtils; |
| 32 | +import org.graylog2.security.encryption.EncryptedValueService; |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import java.security.PrivateKey; |
| 37 | +import java.security.cert.X509Certificate; |
| 38 | +import java.time.Clock; |
| 39 | +import java.time.Duration; |
| 40 | +import java.time.Instant; |
| 41 | +import java.util.Optional; |
| 42 | +import java.util.function.Supplier; |
| 43 | + |
| 44 | +import static org.graylog2.shared.utilities.StringUtils.requireNonBlank; |
| 45 | + |
| 46 | +/** |
| 47 | + * Provides a CA cache that caches {@link CertificateEntry} instances based on their expiration date. |
| 48 | + */ |
| 49 | +@Singleton |
| 50 | +public class CollectorCaCache extends AbstractIdleService { |
| 51 | + private static final Logger LOG = LoggerFactory.getLogger(CollectorCaCache.class); |
| 52 | + |
| 53 | + private static final String SERVER_KEY = "_static_:server"; |
| 54 | + private static final String SIGNING_KEY = "_static_:signing"; |
| 55 | + private static final String CA_KEY = "_static_:ca"; |
| 56 | + |
| 57 | + private final CollectorCaService caService; |
| 58 | + private final CertificateService certificateService; |
| 59 | + private final EncryptedValueService encryptedValueService; |
| 60 | + private final EventBus eventBus; |
| 61 | + private final Cache<String, CacheEntry> cache; |
| 62 | + |
| 63 | + public record CacheEntry(PrivateKey privateKey, X509Certificate cert, String fingerprint) { |
| 64 | + } |
| 65 | + |
| 66 | + @Inject |
| 67 | + public CollectorCaCache(CollectorCaService caService, |
| 68 | + CertificateService certificateService, |
| 69 | + EncryptedValueService encryptedValueService, |
| 70 | + EventBus eventBus, |
| 71 | + Clock clock) { |
| 72 | + this.caService = caService; |
| 73 | + this.certificateService = certificateService; |
| 74 | + this.encryptedValueService = encryptedValueService; |
| 75 | + this.eventBus = eventBus; |
| 76 | + this.cache = Caffeine.newBuilder() |
| 77 | + .expireAfter(Expiry.<String, CacheEntry>creating((key, value) -> |
| 78 | + Duration.between(Instant.now(clock), value.cert().getNotAfter().toInstant()))) |
| 79 | + .initialCapacity(3) |
| 80 | + .build(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get entry by certificate Subject Key Identifier. |
| 85 | + * |
| 86 | + * @param ski the cert Subject Key Identifier value |
| 87 | + * @return the cache entry or an empty optional |
| 88 | + */ |
| 89 | + public Optional<CacheEntry> getBySubjectKeyIdentifier(String ski) { |
| 90 | + requireNonBlank(ski, "Subject Key Identifier can't be blank"); |
| 91 | + |
| 92 | + return Optional.ofNullable(cache.get(ski, key -> getCacheEntry( |
| 93 | + () -> certificateService.findBySubjectKeyIdentifier(ski).orElse(null) |
| 94 | + ).orElse(null))); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the server entry. |
| 99 | + * |
| 100 | + * @return the server entry |
| 101 | + */ |
| 102 | + public CacheEntry getServer() { |
| 103 | + return cache.get(SERVER_KEY, key -> getCacheEntry(caService::getOtlpServerCert).orElseThrow(() -> new IllegalStateException("Server certificate not found"))); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the signing entry. |
| 108 | + * |
| 109 | + * @return the signing entry |
| 110 | + */ |
| 111 | + public CacheEntry getSigning() { |
| 112 | + return cache.get(SIGNING_KEY, key -> getCacheEntry(caService::getSigningCert).orElseThrow(() -> new IllegalStateException("Signing certificate not found"))); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get the CA entry. |
| 117 | + * |
| 118 | + * @return the CA entry |
| 119 | + */ |
| 120 | + public CacheEntry getCa() { |
| 121 | + return cache.get(CA_KEY, key -> getCacheEntry(caService::getCaCert).orElseThrow(() -> new IllegalStateException("CA certificate not found"))); |
| 122 | + } |
| 123 | + |
| 124 | + private Optional<CacheEntry> getCacheEntry(Supplier<CertificateEntry> certSupplier) { |
| 125 | + try { |
| 126 | + final var certEntry = certSupplier.get(); |
| 127 | + if (certEntry == null) { |
| 128 | + return Optional.empty(); |
| 129 | + } |
| 130 | + final var cert = PemUtils.parseCertificate(certEntry.certificate()); |
| 131 | + final var privateKey = PemUtils.parsePrivateKey(encryptedValueService.decrypt(certEntry.privateKey())); |
| 132 | + LOG.debug("Loaded cert <{}>", certEntry.fingerprint()); |
| 133 | + return Optional.of(new CacheEntry(privateKey, cert, certEntry.fingerprint())); |
| 134 | + } catch (Exception e) { |
| 135 | + LOG.error("Couldn't load certificate", e); |
| 136 | + throw new RuntimeException(e); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected void startUp() throws Exception { |
| 142 | + eventBus.register(this); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + protected void shutDown() throws Exception { |
| 147 | + eventBus.unregister(this); |
| 148 | + } |
| 149 | + |
| 150 | + @Subscribe |
| 151 | + @VisibleForTesting |
| 152 | + void handleCollectorsConfigEvent(CollectorCaConfigUpdated ignored) { |
| 153 | + cache.invalidateAll(); |
| 154 | + } |
| 155 | +} |
0 commit comments