Skip to content

Commit

Permalink
Make entitlement IT tests reflective (#121355) (#121417)
Browse files Browse the repository at this point in the history
This commit adds an EntitlementTest annotation that can be used on
classes containing test actions for entitlements. The annotation mirrors
the parameters of CheckAction. Only file check actions are currently
converted, the rest can be moved and annotated as followups. Note that
the check action name is simply the method name, no fancy name
manipulation is done.
  • Loading branch information
rjernst authored Jan 31, 2025
1 parent 1df7f45 commit 8233a99
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EntitlementTest {
enum ExpectedAccess {
PLUGINS,
ES_MODULES_ONLY,
ALWAYS_DENIED
}

ExpectedAccess expectedAccess();

int fromJavaVersion() default -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.nio.file.attribute.UserPrincipal;
import java.util.Scanner;

import static org.elasticsearch.entitlement.qa.test.EntitlementTest.ExpectedAccess.PLUGINS;

@SuppressForbidden(reason = "Explicitly checking APIs that are forbidden")
class FileCheckActions {

Expand All @@ -43,38 +45,47 @@ private static Path readWriteFile() {
return testRootDir.resolve("read_write_file");
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createScannerFile() throws FileNotFoundException {
new Scanner(readFile().toFile());
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createScannerFileWithCharset() throws IOException {
new Scanner(readFile().toFile(), StandardCharsets.UTF_8);
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createScannerFileWithCharsetName() throws FileNotFoundException {
new Scanner(readFile().toFile(), "UTF-8");
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createFileOutputStreamString() throws IOException {
new FileOutputStream(readWriteFile().toString()).close();
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createFileOutputStreamStringWithAppend() throws IOException {
new FileOutputStream(readWriteFile().toString(), false).close();
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createFileOutputStreamFile() throws IOException {
new FileOutputStream(readWriteFile().toFile()).close();
}

@EntitlementTest(expectedAccess = PLUGINS)
static void createFileOutputStreamFileWithAppend() throws IOException {
new FileOutputStream(readWriteFile().toFile(), false).close();
}

@EntitlementTest(expectedAccess = PLUGINS)
static void filesProbeContentType() throws IOException {
Files.probeContentType(readFile());
}

@EntitlementTest(expectedAccess = PLUGINS)
static void filesSetOwner() throws IOException {
UserPrincipal owner = EntitledActions.getFileOwner(readWriteFile());
Files.setOwner(readWriteFile(), owner); // set to existing owner, just trying to execute the method
Expand Down
Loading

0 comments on commit 8233a99

Please sign in to comment.