|
1 | 1 | package com.datadog.appsec.api.security; |
2 | 2 |
|
3 | 3 | import com.datadog.appsec.gateway.AppSecRequestContext; |
4 | | -import javax.annotation.Nonnull; |
| 4 | +import datadog.trace.util.AgentTaskScheduler; |
5 | 5 |
|
6 | | -public interface ApiSecuritySampler { |
7 | | - /** |
8 | | - * Prepare a request context for later sampling decision. This method should be called at request |
9 | | - * end, and is thread-safe. If a request can potentially be sampled, this method will return true. |
10 | | - * If this method returns true, the caller MUST call {@link #releaseOne()} once the context is not |
11 | | - * needed anymore. |
12 | | - */ |
13 | | - boolean preSampleRequest(final @Nonnull AppSecRequestContext ctx); |
| 6 | +import java.util.Random; |
| 7 | +import java.util.concurrent.Executor; |
| 8 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 9 | +import java.util.concurrent.atomic.AtomicInteger; |
| 10 | +import java.util.concurrent.atomic.AtomicLong; |
| 11 | +import java.util.concurrent.atomic.AtomicReference; |
14 | 12 |
|
15 | | - /** Get the final sampling decision. This method is NOT required to be thread-safe. */ |
16 | | - boolean sampleRequest(AppSecRequestContext ctx); |
| 13 | +/** |
| 14 | + * Internal map for API Security sampling. |
| 15 | + * See "[RFC-1021] API Security Sampling Algorithm for thread-based concurrency". |
| 16 | + */ |
| 17 | +final public class ApiSecuritySampler { |
17 | 18 |
|
18 | | - /** Release one permit for the sampler. This must be called after processing a span. */ |
19 | | - void releaseOne(); |
| 19 | + private static final int DEFAULT_MAX_ITEM_COUNT = 4096; |
| 20 | + private static final int DEFAULT_INTERVAL_SECONDS = 30; |
20 | 21 |
|
21 | | - final class NoOp implements ApiSecuritySampler { |
22 | | - @Override |
23 | | - public boolean preSampleRequest(@Nonnull AppSecRequestContext ctx) { |
| 22 | + private final MonotonicClock clock; |
| 23 | + private final Executor executor; |
| 24 | + private final int intervalSeconds; |
| 25 | + private final AtomicReference<Table> table; |
| 26 | + private final AtomicBoolean rebuild = new AtomicBoolean(false); |
| 27 | + private final long zero; |
| 28 | + private final long maxItemCount; |
| 29 | + |
| 30 | + public ApiSecuritySampler() { |
| 31 | + this(DEFAULT_MAX_ITEM_COUNT, DEFAULT_INTERVAL_SECONDS, new Random().nextLong(), new DefaultMonotonicClock(), AgentTaskScheduler.INSTANCE); |
| 32 | + } |
| 33 | + |
| 34 | + public ApiSecuritySampler(final int maxItemCount, final int intervalSeconds, final long zero, final MonotonicClock clock, Executor executor) { |
| 35 | + table = new AtomicReference<>(new Table(maxItemCount)); |
| 36 | + this.maxItemCount = maxItemCount; |
| 37 | + this.intervalSeconds = intervalSeconds; |
| 38 | + this.zero = zero; |
| 39 | + this.clock = clock != null ? clock : new DefaultMonotonicClock(); |
| 40 | + this.executor = executor != null ? executor : AgentTaskScheduler.INSTANCE; |
| 41 | + } |
| 42 | + |
| 43 | + public boolean sample(AppSecRequestContext ctx) { |
| 44 | + final String route = ctx.getRoute(); |
| 45 | + if (route == null) { |
24 | 46 | return false; |
25 | 47 | } |
26 | | - |
27 | | - @Override |
28 | | - public boolean sampleRequest(AppSecRequestContext ctx) { |
| 48 | + final String method = ctx.getMethod(); |
| 49 | + if (method == null) { |
29 | 50 | return false; |
30 | 51 | } |
| 52 | + final int statusCode = ctx.getResponseStatus(); |
| 53 | + if (statusCode <= 0) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + final long hash = computeApiHash(route, method, statusCode); |
| 57 | + return sample(hash); |
| 58 | + } |
31 | 59 |
|
| 60 | + public boolean sample(long key) { |
| 61 | + if (key == 0L) { |
| 62 | + key = zero; |
| 63 | + } |
| 64 | + final int now = clock.now(); |
| 65 | + final Table table = this.table.get(); |
| 66 | + Table.FindSlotResult findSlotResult; |
| 67 | + while (true) { |
| 68 | + findSlotResult = table.findSlot(key); |
| 69 | + if (!findSlotResult.exists) { |
| 70 | + final int newCount = table.count.incrementAndGet(); |
| 71 | + if (newCount > maxItemCount && rebuild.compareAndSet(false, true)) { |
| 72 | + runRebuild(); |
| 73 | + } |
| 74 | + if (newCount > maxItemCount * 2) { |
| 75 | + table.count.decrementAndGet(); |
| 76 | + return false; |
| 77 | + } |
| 78 | + if (!findSlotResult.entry.key.compareAndSet(0, key)) { |
| 79 | + if (findSlotResult.entry.key.get() == key) { |
| 80 | + // Another thread just added this entry |
| 81 | + return false; |
| 82 | + } |
| 83 | + // This entry was just claimed for another key, try another slot. |
| 84 | + table.count.decrementAndGet(); |
| 85 | + continue; |
| 86 | + } |
| 87 | + final long newEntryData = buildDataEntry(now, now); |
| 88 | + if (findSlotResult.entry.data.compareAndSet(0, newEntryData)) { |
| 89 | + return true; |
| 90 | + } else { |
| 91 | + return false; |
| 92 | + } |
| 93 | + } |
| 94 | + break; |
| 95 | + } |
| 96 | + long curData = findSlotResult.entry.data.get(); |
| 97 | + final int stime = getStime(curData); |
| 98 | + final int deadline = now - intervalSeconds; |
| 99 | + if (stime <= deadline) { |
| 100 | + final long newData = buildDataEntry(now, now); |
| 101 | + while (!findSlotResult.entry.data.compareAndSet(curData, newData)) { |
| 102 | + curData = findSlotResult.entry.data.get(); |
| 103 | + if (getStime(curData) == getAtime(curData)) { |
| 104 | + // Another thread just issued a keep decision |
| 105 | + return false; |
| 106 | + } |
| 107 | + if (getStime(curData) > now) { |
| 108 | + // Another thread is in our fugure, but did not issue a keep decision. |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | + final long newData = buildDataEntry(getStime(curData), now); |
| 115 | + while (getAtime(curData) < now) { |
| 116 | + if (!findSlotResult.entry.data.compareAndSet(curData, newData)) { |
| 117 | + curData = findSlotResult.entry.data.get(); |
| 118 | + } |
| 119 | + } |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + private void runRebuild() { |
| 124 | + // TODO |
| 125 | + } |
| 126 | + |
| 127 | + private static class Table { |
| 128 | + private final Entry[] table; |
| 129 | + private final AtomicInteger count = new AtomicInteger(0); |
| 130 | + private final int maxItemCount; |
| 131 | + |
| 132 | + public Table(int maxItemCount) { |
| 133 | + this.maxItemCount = maxItemCount; |
| 134 | + final int size = 2 * maxItemCount + 1; |
| 135 | + table = new Entry[size]; |
| 136 | + for (int i = 0; i < size; i++) { |
| 137 | + table[i] = new Entry(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public FindSlotResult findSlot(final long key) { |
| 142 | + final int startIndex = (int) (key % (2L * maxItemCount)); |
| 143 | + int index = startIndex; |
| 144 | + do { |
| 145 | + final Entry slot = table[index]; |
| 146 | + final long slotKey = slot.key.get(); |
| 147 | + if (slotKey == key) { |
| 148 | + return new FindSlotResult(slot, true); |
| 149 | + } else if (slotKey == 0L) { |
| 150 | + return new FindSlotResult(slot, false); |
| 151 | + } |
| 152 | + index++; |
| 153 | + if (index >= table.length) { |
| 154 | + index = 0; |
| 155 | + } |
| 156 | + } while (index != startIndex); |
| 157 | + return new FindSlotResult(table[(int)(maxItemCount * 2)], false); |
| 158 | + } |
| 159 | + |
| 160 | + static class FindSlotResult { |
| 161 | + public final Entry entry; |
| 162 | + public final boolean exists; |
| 163 | + |
| 164 | + public FindSlotResult(final Entry entry, final boolean exists) { |
| 165 | + this.entry = entry; |
| 166 | + this.exists = exists; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + static class Entry { |
| 171 | + private final AtomicLong key = new AtomicLong(0L); |
| 172 | + private final AtomicLong data = new AtomicLong(0L); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + interface MonotonicClock { |
| 177 | + int now(); |
| 178 | + } |
| 179 | + |
| 180 | + static class DefaultMonotonicClock implements MonotonicClock { |
32 | 181 | @Override |
33 | | - public void releaseOne() {} |
| 182 | + public int now() { |
| 183 | + return (int) (System.nanoTime() / 1_000_000); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + long buildDataEntry(final int stime, final int atime) { |
| 188 | + long result = stime; |
| 189 | + result <<= 32; |
| 190 | + result |= atime & 0xFFFFFFFFL; |
| 191 | + return result; |
| 192 | + } |
| 193 | + |
| 194 | + int getStime(final long data) { |
| 195 | + return (int) (data >> 32); |
| 196 | + } |
| 197 | + |
| 198 | + int getAtime(final long data) { |
| 199 | + return (int) (data & 0xFFFFFFFFL); |
| 200 | + } |
| 201 | + |
| 202 | + private long computeApiHash(final String route, final String method, final int statusCode) { |
| 203 | + long result = 17; |
| 204 | + result = 31 * result + route.hashCode(); |
| 205 | + result = 31 * result + method.hashCode(); |
| 206 | + result = 31 * result + statusCode; |
| 207 | + return result; |
34 | 208 | } |
35 | 209 | } |
0 commit comments