Skip to content

Commit e3c5e97

Browse files
prdoyleelasticsearchmachine
andauthored
Entitled plugin for testing (#120840)
* Entitled plugin * [CI] Auto commit changes from spotless * SuppressForbidden in entitled plugin * Respond to PR comments * Reinstate entitled plugin * Make System_clearProperty package-private --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 9bc9ba7 commit e3c5e97

File tree

11 files changed

+137
-3
lines changed

11 files changed

+137
-3
lines changed

libs/entitlement/qa/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ apply plugin: 'elasticsearch.internal-test-artifact'
1313

1414
dependencies {
1515
javaRestTestImplementation project(':libs:entitlement:qa:entitlement-test-plugin')
16+
javaRestTestImplementation project(':libs:entitlement:qa:entitled-plugin')
1617
clusterModules project(':libs:entitlement:qa:entitlement-test-plugin')
18+
clusterModules project(':libs:entitlement:qa:entitled-plugin')
1719
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
import org.elasticsearch.gradle.internal.precommit.CheckForbiddenApisTask
11+
12+
apply plugin: 'elasticsearch.base-internal-es-plugin'
13+
apply plugin: 'elasticsearch.build'
14+
15+
esplugin {
16+
name = 'entitled'
17+
description = 'A utility plugin that provides access to functionality denied to the main test plugin'
18+
classname = 'org.elasticsearch.entitlement.qa.entitled.EntitledPlugin'
19+
}
20+
21+
dependencies {
22+
compileOnly project(':server')
23+
compileOnly project(':libs:logging')
24+
compileOnly project(':libs:entitlement')
25+
}
26+
27+
tasks.named("javadoc").configure {
28+
enabled = false
29+
}
30+
31+
tasks.withType(CheckForbiddenApisTask).configureEach {
32+
replaceSignatureFiles 'jdk-signatures'
33+
}
34+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
module org.elasticsearch.entitlement.qa.entitled {
11+
requires org.elasticsearch.server;
12+
requires org.elasticsearch.entitlement;
13+
requires org.elasticsearch.base; // SuppressForbidden
14+
requires org.elasticsearch.logging;
15+
16+
exports org.elasticsearch.entitlement.qa.entitled; // Must be unqualified so non-modular IT tests can call us
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.qa.entitled;
11+
12+
import org.elasticsearch.core.SuppressForbidden;
13+
14+
public final class EntitledActions {
15+
private EntitledActions() {}
16+
17+
@SuppressForbidden(reason = "Exposes forbidden APIs for testing purposes")
18+
static void System_clearProperty(String key) {
19+
System.clearProperty(key);
20+
}
21+
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.qa.entitled;
11+
12+
import org.elasticsearch.entitlement.runtime.api.NotEntitledException;
13+
import org.elasticsearch.logging.LogManager;
14+
import org.elasticsearch.logging.Logger;
15+
import org.elasticsearch.plugins.ExtensiblePlugin;
16+
import org.elasticsearch.plugins.Plugin;
17+
18+
import static org.elasticsearch.entitlement.qa.entitled.EntitledActions.System_clearProperty;
19+
20+
public class EntitledPlugin extends Plugin implements ExtensiblePlugin {
21+
22+
/**
23+
* Runs some actions that should be allowed or denied for this plugin,
24+
* to ensure the entitlement system is handling them correctly.
25+
*/
26+
public static void selfTest() {
27+
selfTestEntitled();
28+
selfTestNotEntitled();
29+
}
30+
31+
private static final String SELF_TEST_PROPERTY = "org.elasticsearch.entitlement.qa.selfTest";
32+
33+
private static void selfTestEntitled() {
34+
logger.debug("selfTestEntitled");
35+
System_clearProperty(SELF_TEST_PROPERTY);
36+
}
37+
38+
private static void selfTestNotEntitled() {
39+
logger.debug("selfTestNotEntitled");
40+
try {
41+
System.setIn(System.in);
42+
} catch (NotEntitledException e) {
43+
// All is well
44+
return;
45+
}
46+
throw new AssertionError("Expected self-test not to be entitled");
47+
}
48+
49+
private static final Logger logger = LogManager.getLogger(EntitledPlugin.class);
50+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
org.elasticsearch.entitlement.qa.entitled:
2+
- write_system_properties:
3+
properties:
4+
- org.elasticsearch.entitlement.qa.selfTest

libs/entitlement/qa/entitlement-test-plugin/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ esplugin {
1717
name = 'entitlement-test-plugin'
1818
description = 'A test plugin that invokes methods checked by entitlements'
1919
classname = 'org.elasticsearch.entitlement.qa.test.EntitlementTestPlugin'
20+
extendedPlugins = ['entitled']
2021
}
2122

2223
dependencies {
23-
implementation project(':server')
24-
implementation project(':libs:logging')
24+
compileOnly project(':server')
25+
compileOnly project(':libs:logging')
26+
compileOnly project(":libs:entitlement:qa:entitled-plugin")
2527
}
2628

2729
tasks.named("javadoc").configure {

libs/entitlement/qa/entitlement-test-plugin/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
requires org.elasticsearch.server;
1212
requires org.elasticsearch.base;
1313
requires org.elasticsearch.logging;
14+
requires org.elasticsearch.entitlement.qa.entitled;
1415

1516
// Modules we'll attempt to use in order to exercise entitlements
1617
requires java.logging;

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/RestEntitlementsCheckAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public class RestEntitlementsCheckAction extends BaseRestHandler {
7474
record CheckAction(CheckedRunnable<Exception> action, boolean isAlwaysDeniedToPlugins, Integer fromJavaVersion) {
7575
/**
7676
* These cannot be granted to plugins, so our test plugins cannot test the "allowed" case.
77-
* Used both for always-denied entitlements and those granted only to the server itself.
7877
*/
7978
static CheckAction deniedToPlugins(CheckedRunnable<Exception> action) {
8079
return new CheckAction(action, true, null);

libs/entitlement/qa/entitlement-test-plugin/src/main/java/org/elasticsearch/entitlement/qa/test/WritePropertiesCheckActions.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.entitlement.qa.test;
1111

1212
import org.elasticsearch.core.SuppressForbidden;
13+
import org.elasticsearch.entitlement.qa.entitled.EntitledPlugin;
1314

1415
import java.util.Locale;
1516
import java.util.TimeZone;
@@ -29,6 +30,7 @@ static void setSystemProperty() {
2930
}
3031

3132
static void clearSystemProperty() {
33+
EntitledPlugin.selfTest(); // TODO: find a better home
3234
System.clearProperty("es.entitlements.checkClearSystemProperty");
3335
}
3436

0 commit comments

Comments
 (0)