|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.entitlement.initialization; |
| 11 | + |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.entitlement.runtime.policy.PathLookup; |
| 14 | +import org.elasticsearch.entitlement.runtime.policy.Policy; |
| 15 | +import org.elasticsearch.entitlement.runtime.policy.Scope; |
| 16 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.CreateClassLoaderEntitlement; |
| 17 | +import org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement; |
| 18 | +import org.elasticsearch.test.ESTestCase; |
| 19 | +import org.junit.BeforeClass; |
| 20 | + |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import static org.hamcrest.Matchers.both; |
| 26 | +import static org.hamcrest.Matchers.endsWith; |
| 27 | +import static org.hamcrest.Matchers.startsWith; |
| 28 | + |
| 29 | +public class EntitlementInitializationTests extends ESTestCase { |
| 30 | + |
| 31 | + private static PathLookup TEST_PATH_LOOKUP; |
| 32 | + |
| 33 | + private static Path TEST_CONFIG_DIR; |
| 34 | + |
| 35 | + private static Path TEST_PLUGINS_DIR; |
| 36 | + private static Path TEST_MODULES_DIR; |
| 37 | + private static Path TEST_LIBS_DIR; |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void beforeClass() { |
| 41 | + try { |
| 42 | + Path testBaseDir = createTempDir().toAbsolutePath(); |
| 43 | + TEST_CONFIG_DIR = testBaseDir.resolve("config"); |
| 44 | + TEST_PLUGINS_DIR = testBaseDir.resolve("plugins"); |
| 45 | + TEST_MODULES_DIR = testBaseDir.resolve("modules"); |
| 46 | + TEST_LIBS_DIR = testBaseDir.resolve("libs"); |
| 47 | + |
| 48 | + TEST_PATH_LOOKUP = new PathLookup( |
| 49 | + testBaseDir.resolve("user/home"), |
| 50 | + TEST_CONFIG_DIR, |
| 51 | + new Path[] { testBaseDir.resolve("data1"), testBaseDir.resolve("data2") }, |
| 52 | + new Path[] { testBaseDir.resolve("shared1"), testBaseDir.resolve("shared2") }, |
| 53 | + testBaseDir.resolve("temp"), |
| 54 | + Settings.EMPTY::getValues |
| 55 | + ); |
| 56 | + } catch (Exception e) { |
| 57 | + throw new IllegalStateException(e); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public void testValidationPass() { |
| 62 | + var policy = new Policy( |
| 63 | + "plugin", |
| 64 | + List.of( |
| 65 | + new Scope( |
| 66 | + "module1", |
| 67 | + List.of( |
| 68 | + new FilesEntitlement(List.of(FilesEntitlement.FileData.ofPath(TEST_CONFIG_DIR, FilesEntitlement.Mode.READ))), |
| 69 | + new CreateClassLoaderEntitlement() |
| 70 | + ) |
| 71 | + ) |
| 72 | + ) |
| 73 | + ); |
| 74 | + EntitlementInitialization.validateFilesEntitlements( |
| 75 | + Map.of("plugin", policy), |
| 76 | + TEST_PATH_LOOKUP, |
| 77 | + TEST_CONFIG_DIR, |
| 78 | + TEST_PLUGINS_DIR, |
| 79 | + TEST_MODULES_DIR, |
| 80 | + TEST_LIBS_DIR |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public void testValidationFailForRead() { |
| 85 | + var policy = new Policy( |
| 86 | + "plugin", |
| 87 | + List.of( |
| 88 | + new Scope( |
| 89 | + "module2", |
| 90 | + List.of( |
| 91 | + new FilesEntitlement(List.of(FilesEntitlement.FileData.ofPath(TEST_PLUGINS_DIR, FilesEntitlement.Mode.READ))), |
| 92 | + new CreateClassLoaderEntitlement() |
| 93 | + ) |
| 94 | + ) |
| 95 | + ) |
| 96 | + ); |
| 97 | + |
| 98 | + var ex = expectThrows( |
| 99 | + IllegalArgumentException.class, |
| 100 | + () -> EntitlementInitialization.validateFilesEntitlements( |
| 101 | + Map.of("plugin", policy), |
| 102 | + TEST_PATH_LOOKUP, |
| 103 | + TEST_CONFIG_DIR, |
| 104 | + TEST_PLUGINS_DIR, |
| 105 | + TEST_MODULES_DIR, |
| 106 | + TEST_LIBS_DIR |
| 107 | + ) |
| 108 | + ); |
| 109 | + assertThat( |
| 110 | + ex.getMessage(), |
| 111 | + both(startsWith("policy for [plugin] cannot contain a file entitlement for")).and(endsWith("is forbidden for mode [READ].")) |
| 112 | + ); |
| 113 | + |
| 114 | + // check fails for mode READ_WRITE too |
| 115 | + var policy2 = new Policy( |
| 116 | + "plugin", |
| 117 | + List.of( |
| 118 | + new Scope( |
| 119 | + "module1", |
| 120 | + List.of( |
| 121 | + new FilesEntitlement(List.of(FilesEntitlement.FileData.ofPath(TEST_LIBS_DIR, FilesEntitlement.Mode.READ_WRITE))), |
| 122 | + new CreateClassLoaderEntitlement() |
| 123 | + ) |
| 124 | + ) |
| 125 | + ) |
| 126 | + ); |
| 127 | + |
| 128 | + ex = expectThrows( |
| 129 | + IllegalArgumentException.class, |
| 130 | + () -> EntitlementInitialization.validateFilesEntitlements( |
| 131 | + Map.of("plugin2", policy2), |
| 132 | + TEST_PATH_LOOKUP, |
| 133 | + TEST_CONFIG_DIR, |
| 134 | + TEST_PLUGINS_DIR, |
| 135 | + TEST_MODULES_DIR, |
| 136 | + TEST_LIBS_DIR |
| 137 | + ) |
| 138 | + ); |
| 139 | + assertThat( |
| 140 | + ex.getMessage(), |
| 141 | + both(startsWith("policy for [plugin2] cannot contain a file entitlement")).and(endsWith("is forbidden for mode [READ].")) |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + public void testValidationFailForWrite() { |
| 146 | + var policy = new Policy( |
| 147 | + "plugin", |
| 148 | + List.of( |
| 149 | + new Scope( |
| 150 | + "module1", |
| 151 | + List.of( |
| 152 | + new FilesEntitlement(List.of(FilesEntitlement.FileData.ofPath(TEST_CONFIG_DIR, FilesEntitlement.Mode.READ_WRITE))), |
| 153 | + new CreateClassLoaderEntitlement() |
| 154 | + ) |
| 155 | + ) |
| 156 | + ) |
| 157 | + ); |
| 158 | + |
| 159 | + var ex = expectThrows( |
| 160 | + IllegalArgumentException.class, |
| 161 | + () -> EntitlementInitialization.validateFilesEntitlements( |
| 162 | + Map.of("plugin", policy), |
| 163 | + TEST_PATH_LOOKUP, |
| 164 | + TEST_CONFIG_DIR, |
| 165 | + TEST_PLUGINS_DIR, |
| 166 | + TEST_MODULES_DIR, |
| 167 | + TEST_LIBS_DIR |
| 168 | + ) |
| 169 | + ); |
| 170 | + assertThat( |
| 171 | + ex.getMessage(), |
| 172 | + both(startsWith("policy for [plugin] cannot contain a file entitlement")).and(endsWith("is forbidden for mode [READ_WRITE].")) |
| 173 | + ); |
| 174 | + } |
| 175 | +} |
0 commit comments