|
| 1 | +/* |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License") |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.netflix.spinnaker.tomcat.x509; |
| 18 | + |
| 19 | +import com.google.common.cache.CacheBuilder; |
| 20 | +import com.google.common.cache.CacheLoader; |
| 21 | +import com.google.common.cache.LoadingCache; |
| 22 | +import com.google.common.collect.ImmutableSet; |
| 23 | + |
| 24 | +import javax.security.auth.x500.X500Principal; |
| 25 | +import java.io.File; |
| 26 | +import java.math.BigInteger; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.security.cert.X509Certificate; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.concurrent.ExecutionException; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +public class ReloadingFileBlacklist implements Blacklist { |
| 37 | + private static class Entry { |
| 38 | + private static final String DELIMITER = ":::"; |
| 39 | + private final X500Principal issuer; |
| 40 | + private final BigInteger serial; |
| 41 | + |
| 42 | + private static Entry fromString(String blacklistEntry) { |
| 43 | + int idx = blacklistEntry.indexOf(DELIMITER); |
| 44 | + if (idx == -1) { |
| 45 | + throw new IllegalArgumentException("Missing delimiter " + DELIMITER + " in " + blacklistEntry); |
| 46 | + } |
| 47 | + X500Principal principal = new X500Principal(blacklistEntry.substring(0, idx)); |
| 48 | + BigInteger serial = new BigInteger(blacklistEntry.substring(idx + DELIMITER.length())); |
| 49 | + return new Entry(principal, serial); |
| 50 | + } |
| 51 | + |
| 52 | + private Entry(X500Principal issuer, BigInteger serial) { |
| 53 | + this.issuer = Objects.requireNonNull(issuer); |
| 54 | + this.serial = Objects.requireNonNull(serial); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public boolean equals(Object o) { |
| 59 | + if (this == o) return true; |
| 60 | + if (o == null || getClass() != o.getClass()) return false; |
| 61 | + |
| 62 | + Entry entry = (Entry) o; |
| 63 | + |
| 64 | + if (!issuer.equals(entry.issuer)) return false; |
| 65 | + return serial.equals(entry.serial); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int hashCode() { |
| 70 | + int result = issuer.hashCode(); |
| 71 | + result = 31 * result + serial.hashCode(); |
| 72 | + return result; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static final int DEFAULT_RELOAD_INTERVAL_SECONDS = 5; |
| 77 | + private final String blacklistFile; |
| 78 | + private final LoadingCache<String, Set<Entry>> blacklist; |
| 79 | + |
| 80 | + public ReloadingFileBlacklist(String blacklistFile, long reloadInterval, TimeUnit unit) { |
| 81 | + this.blacklistFile = blacklistFile; |
| 82 | + this.blacklist = CacheBuilder |
| 83 | + .newBuilder() |
| 84 | + .expireAfterAccess(reloadInterval, unit) |
| 85 | + .build(new CacheLoader<String, Set<Entry>>() { |
| 86 | + @Override |
| 87 | + public Set<Entry> load(String key) throws Exception { |
| 88 | + File f = new File(key); |
| 89 | + if (!f.exists()) { |
| 90 | + return Collections.emptySet(); |
| 91 | + } |
| 92 | + return ImmutableSet.copyOf(Files.lines(f.toPath()) |
| 93 | + .map(String::trim) |
| 94 | + .filter(line -> !(line.isEmpty() || line.startsWith("#"))) |
| 95 | + .map(Entry::fromString) |
| 96 | + .collect(Collectors.toSet())); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + public ReloadingFileBlacklist(String blacklistFile) { |
| 102 | + this(blacklistFile, DEFAULT_RELOAD_INTERVAL_SECONDS, TimeUnit.SECONDS); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean isBlacklisted(X509Certificate cert) { |
| 107 | + try { |
| 108 | + return blacklist.get(blacklistFile).contains(new Entry(cert.getIssuerX500Principal(), cert.getSerialNumber())); |
| 109 | + } catch (ExecutionException ee) { |
| 110 | + Throwable cause = ee.getCause(); |
| 111 | + if (cause != null) { |
| 112 | + if (cause instanceof RuntimeException) { |
| 113 | + throw (RuntimeException) cause; |
| 114 | + } |
| 115 | + throw new RuntimeException(cause); |
| 116 | + } |
| 117 | + throw new RuntimeException(ee); |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | +} |
0 commit comments