|
9 | 9 |
|
10 | 10 | package org.elasticsearch.entitlement.runtime.policy; |
11 | 11 |
|
| 12 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.Entitlement; |
| 13 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement; |
| 14 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.InboundNetworkEntitlement; |
12 | 15 | import org.elasticsearch.entitlement.runtime.policy.entitlements.LoadNativeLibrariesEntitlement; |
| 16 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.ManageThreadsEntitlement; |
| 17 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.OutboundNetworkEntitlement; |
13 | 18 | import org.elasticsearch.entitlement.runtime.policy.entitlements.SetHttpsConnectionPropertiesEntitlement; |
| 19 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.WriteAllSystemPropertiesEntitlement; |
| 20 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.WriteSystemPropertiesEntitlement; |
14 | 21 | import org.elasticsearch.test.ESTestCase; |
15 | 22 |
|
16 | 23 | import java.nio.charset.StandardCharsets; |
| 24 | +import java.nio.file.Path; |
17 | 25 | import java.util.Base64; |
18 | 26 | import java.util.List; |
19 | 27 | import java.util.Set; |
20 | 28 |
|
| 29 | +import static org.elasticsearch.entitlement.runtime.policy.PolicyUtils.mergeEntitlement; |
| 30 | +import static org.elasticsearch.entitlement.runtime.policy.PolicyUtils.mergeEntitlements; |
| 31 | +import static org.elasticsearch.test.LambdaMatchers.transformedMatch; |
| 32 | +import static org.hamcrest.Matchers.both; |
| 33 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
21 | 34 | import static org.hamcrest.Matchers.equalTo; |
22 | 35 | import static org.hamcrest.Matchers.nullValue; |
23 | 36 |
|
@@ -159,4 +172,120 @@ public void testNoOverriddenPolicyWithParsingError() { |
159 | 172 |
|
160 | 173 | assertThat(policy, nullValue()); |
161 | 174 | } |
| 175 | + |
| 176 | + public void testMergeScopes() { |
| 177 | + var originalPolicy = List.of( |
| 178 | + new Scope("module1", List.of(new LoadNativeLibrariesEntitlement())), |
| 179 | + new Scope("module2", List.of(new ManageThreadsEntitlement())), |
| 180 | + new Scope("module3", List.of(new InboundNetworkEntitlement())) |
| 181 | + ); |
| 182 | + |
| 183 | + var patchPolicy = List.of( |
| 184 | + new Scope("module2", List.of(new ManageThreadsEntitlement())), |
| 185 | + new Scope("module3", List.of(new OutboundNetworkEntitlement())), |
| 186 | + new Scope("module4", List.of(new WriteAllSystemPropertiesEntitlement())) |
| 187 | + ); |
| 188 | + |
| 189 | + var resultPolicy = PolicyUtils.mergeScopes(originalPolicy, patchPolicy); |
| 190 | + assertThat( |
| 191 | + resultPolicy, |
| 192 | + containsInAnyOrder( |
| 193 | + equalTo(new Scope("module1", List.of(new LoadNativeLibrariesEntitlement()))), |
| 194 | + equalTo(new Scope("module2", List.of(new ManageThreadsEntitlement()))), |
| 195 | + both(transformedMatch(Scope::moduleName, equalTo("module3"))).and( |
| 196 | + transformedMatch( |
| 197 | + Scope::entitlements, |
| 198 | + containsInAnyOrder(new InboundNetworkEntitlement(), new OutboundNetworkEntitlement()) |
| 199 | + ) |
| 200 | + ), |
| 201 | + equalTo(new Scope("module4", List.of(new WriteAllSystemPropertiesEntitlement()))) |
| 202 | + ) |
| 203 | + ); |
| 204 | + } |
| 205 | + |
| 206 | + public void testMergeSameFlagEntitlement() { |
| 207 | + var e1 = new InboundNetworkEntitlement(); |
| 208 | + var e2 = new InboundNetworkEntitlement(); |
| 209 | + |
| 210 | + assertThat(mergeEntitlement(e1, e2), equalTo(new InboundNetworkEntitlement())); |
| 211 | + } |
| 212 | + |
| 213 | + public void testMergeFilesEntitlement() { |
| 214 | + var e1 = new FilesEntitlement( |
| 215 | + List.of( |
| 216 | + FilesEntitlement.FileData.ofPath(Path.of("/a/b"), FilesEntitlement.Mode.READ), |
| 217 | + FilesEntitlement.FileData.ofPath(Path.of("/a/c"), FilesEntitlement.Mode.READ_WRITE), |
| 218 | + FilesEntitlement.FileData.ofRelativePath(Path.of("c/d"), FilesEntitlement.BaseDir.CONFIG, FilesEntitlement.Mode.READ) |
| 219 | + ) |
| 220 | + ); |
| 221 | + var e2 = new FilesEntitlement( |
| 222 | + List.of( |
| 223 | + FilesEntitlement.FileData.ofPath(Path.of("/a/b"), FilesEntitlement.Mode.READ), // identical |
| 224 | + FilesEntitlement.FileData.ofPath(Path.of("/a/c"), FilesEntitlement.Mode.READ), // different mode |
| 225 | + FilesEntitlement.FileData.ofPath(Path.of("/c/d"), FilesEntitlement.Mode.READ) // different type |
| 226 | + ) |
| 227 | + ); |
| 228 | + |
| 229 | + var merged = mergeEntitlement(e1, e2); |
| 230 | + assertThat( |
| 231 | + merged, |
| 232 | + transformedMatch( |
| 233 | + x -> ((FilesEntitlement) x).filesData(), |
| 234 | + containsInAnyOrder( |
| 235 | + FilesEntitlement.FileData.ofPath(Path.of("/a/b"), FilesEntitlement.Mode.READ), |
| 236 | + FilesEntitlement.FileData.ofPath(Path.of("/a/c"), FilesEntitlement.Mode.READ), |
| 237 | + FilesEntitlement.FileData.ofPath(Path.of("/a/c"), FilesEntitlement.Mode.READ_WRITE), |
| 238 | + FilesEntitlement.FileData.ofRelativePath(Path.of("c/d"), FilesEntitlement.BaseDir.CONFIG, FilesEntitlement.Mode.READ), |
| 239 | + FilesEntitlement.FileData.ofPath(Path.of("/c/d"), FilesEntitlement.Mode.READ) |
| 240 | + ) |
| 241 | + ) |
| 242 | + ); |
| 243 | + } |
| 244 | + |
| 245 | + public void testMergeWritePropertyEntitlement() { |
| 246 | + var e1 = new WriteSystemPropertiesEntitlement(List.of("a", "b", "c")); |
| 247 | + var e2 = new WriteSystemPropertiesEntitlement(List.of("b", "c", "d")); |
| 248 | + |
| 249 | + var merged = mergeEntitlement(e1, e2); |
| 250 | + assertThat( |
| 251 | + merged, |
| 252 | + transformedMatch(x -> ((WriteSystemPropertiesEntitlement) x).properties(), containsInAnyOrder("a", "b", "c", "d")) |
| 253 | + ); |
| 254 | + } |
| 255 | + |
| 256 | + public void testMergeEntitlements() { |
| 257 | + List<Entitlement> a = List.of( |
| 258 | + new InboundNetworkEntitlement(), |
| 259 | + new OutboundNetworkEntitlement(), |
| 260 | + new FilesEntitlement( |
| 261 | + List.of( |
| 262 | + FilesEntitlement.FileData.ofPath(Path.of("/a/b"), FilesEntitlement.Mode.READ), |
| 263 | + FilesEntitlement.FileData.ofPath(Path.of("/a/c"), FilesEntitlement.Mode.READ_WRITE) |
| 264 | + ) |
| 265 | + ) |
| 266 | + ); |
| 267 | + List<Entitlement> b = List.of( |
| 268 | + new InboundNetworkEntitlement(), |
| 269 | + new LoadNativeLibrariesEntitlement(), |
| 270 | + new FilesEntitlement(List.of()), |
| 271 | + new WriteSystemPropertiesEntitlement(List.of("a")) |
| 272 | + ); |
| 273 | + |
| 274 | + var merged = mergeEntitlements(a, b); |
| 275 | + assertThat( |
| 276 | + merged, |
| 277 | + containsInAnyOrder( |
| 278 | + new InboundNetworkEntitlement(), |
| 279 | + new OutboundNetworkEntitlement(), |
| 280 | + new LoadNativeLibrariesEntitlement(), |
| 281 | + new FilesEntitlement( |
| 282 | + List.of( |
| 283 | + FilesEntitlement.FileData.ofPath(Path.of("/a/b"), FilesEntitlement.Mode.READ), |
| 284 | + FilesEntitlement.FileData.ofPath(Path.of("/a/c"), FilesEntitlement.Mode.READ_WRITE) |
| 285 | + ) |
| 286 | + ), |
| 287 | + new WriteSystemPropertiesEntitlement(List.of("a")) |
| 288 | + ) |
| 289 | + ); |
| 290 | + } |
162 | 291 | } |
0 commit comments