-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Entitlements] Allow policy overrides via system properties #124489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
4636e67
7a8d4e1
c92ef83
aa3f266
0b635b0
c1738b7
1c2e374
c46d606
5b75e1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.entitlement.qa; | ||
|
|
||
| import com.carrotsearch.randomizedtesting.annotations.Name; | ||
| import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; | ||
|
|
||
| import org.elasticsearch.core.Strings; | ||
| import org.junit.ClassRule; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Path; | ||
| import java.util.Base64; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static org.elasticsearch.entitlement.qa.EntitlementsTestRule.ENTITLEMENT_QA_TEST_MODULE_NAME; | ||
| import static org.elasticsearch.entitlement.qa.EntitlementsTestRule.ENTITLEMENT_TEST_PLUGIN_NAME; | ||
|
|
||
| public class EntitlementsAllowedViaOverrideIT extends AbstractEntitlementsIT { | ||
|
|
||
| private static void addPolicyOverrideSystemProperties(BiConsumer<String, Function<Path, String>> adder) { | ||
| adder.accept("es.entitlements.policy." + ENTITLEMENT_TEST_PLUGIN_NAME, tempDir -> { | ||
| String policyOverride = Strings.format(""" | ||
| policy: | ||
| %s: | ||
| - load_native_libraries | ||
| - files: | ||
| - path: %s | ||
| mode: read | ||
| """, ENTITLEMENT_QA_TEST_MODULE_NAME, tempDir.resolve("read_dir")); | ||
| return new String(Base64.getEncoder().encode(policyOverride.getBytes(StandardCharsets.UTF_8))); | ||
| }); | ||
| } | ||
|
|
||
| @ClassRule | ||
| public static EntitlementsTestRule testRule = new EntitlementsTestRule( | ||
| true, | ||
| null, | ||
| EntitlementsAllowedViaOverrideIT::addPolicyOverrideSystemProperties | ||
| ); | ||
|
|
||
| public EntitlementsAllowedViaOverrideIT(@Name("actionName") String actionName) { | ||
| super(actionName, true); | ||
| } | ||
|
|
||
| @ParametersFactory | ||
| public static Iterable<Object[]> data() { | ||
| return Stream.of("runtime_load_library", "fileList").map(action -> new Object[] { action }).toList(); | ||
| } | ||
|
|
||
| @Override | ||
| protected String getTestRestCluster() { | ||
| return testRule.cluster.getHttpAddresses(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,10 +33,12 @@ | |
| import java.lang.reflect.Modifier; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Collectors; | ||
|
|
@@ -97,6 +99,58 @@ public PolicyParser(InputStream inputStream, String policyName, boolean isExtern | |
| this.externalEntitlements = externalEntitlements; | ||
| } | ||
|
|
||
| public VersionedPolicy parseVersionedPolicy() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My suggestion would be to not have the version parsing as part of the I see two different possibilities to accomplish this:
I think this may also remove the need for a VersionedPolicy record as well. |
||
| Set<String> versions = Set.of(); | ||
| Policy policy = emptyPolicy(); | ||
| try { | ||
| if (policyParser.nextToken() != XContentParser.Token.START_OBJECT) { | ||
| throw newPolicyParserException("expected object <versioned policy>"); | ||
| } | ||
|
|
||
| while (policyParser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| if (policyParser.currentToken() == XContentParser.Token.FIELD_NAME) { | ||
| if (policyParser.currentName().equals("versions")) { | ||
| versions = parseVersions(); | ||
| } else if (policyParser.currentName().equals("policy")) { | ||
| policy = parsePolicy(); | ||
| } else { | ||
| throw newPolicyParserException("expected either <version> or <policy> field"); | ||
| } | ||
| } else { | ||
| throw newPolicyParserException("expected either <version> or <policy> field"); | ||
| } | ||
| } | ||
|
|
||
| return new VersionedPolicy(policy, versions); | ||
| } catch (IOException ioe) { | ||
| throw new UncheckedIOException(ioe); | ||
| } | ||
| } | ||
|
|
||
| private Policy emptyPolicy() { | ||
| return new Policy(policyName, List.of()); | ||
| } | ||
|
|
||
| private Set<String> parseVersions() throws IOException { | ||
| try { | ||
| if (policyParser.nextToken() != XContentParser.Token.START_ARRAY) { | ||
| throw newPolicyParserException("expected array of <versions>"); | ||
| } | ||
| Set<String> versions = new HashSet<>(); | ||
| while (policyParser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
| if (policyParser.currentToken() == XContentParser.Token.VALUE_STRING) { | ||
| String version = policyParser.text(); | ||
| versions.add(version); | ||
| } else { | ||
| throw newPolicyParserException("expected <version>"); | ||
| } | ||
| } | ||
| return versions; | ||
| } catch (IOException ioe) { | ||
| throw new UncheckedIOException(ioe); | ||
| } | ||
| } | ||
|
|
||
| public Policy parsePolicy() { | ||
| try { | ||
| if (policyParser.nextToken() != XContentParser.Token.START_OBJECT) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.entitlement.runtime.policy; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * A Policy and associated versions to which the policy applies | ||
| */ | ||
| public record VersionedPolicy(Policy policy, Set<String> versions) {} |
Uh oh!
There was an error while loading. Please reload this page.