Skip to content

Commit

Permalink
Fix checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
vagaerg committed Dec 21, 2023
1 parent 701c0e0 commit 9dd9265
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
37 changes: 25 additions & 12 deletions plugin/trino-opa/src/test/java/io/trino/plugin/opa/TestHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,34 @@ public static Stream<Arguments> allErrorCasesArgumentProvider()
Stream.of(Arguments.of(Named.of("No access response", NO_ACCESS_RESPONSE), AccessDeniedException.class, "Access Denied")));
}

public static SystemSecurityContext systemSecurityContextFromIdentity(Identity identity) {
public static SystemSecurityContext systemSecurityContextFromIdentity(Identity identity)
{
return new SystemSecurityContext(identity, new QueryIdGenerator().createNextQueryId(), Instant.now());
}

public abstract static class MethodWrapper {
public abstract static class MethodWrapper
{
public abstract boolean isAccessAllowed(OpaAccessControl opaAccessControl);
}

public static class ThrowingMethodWrapper extends MethodWrapper {
public static class ThrowingMethodWrapper
extends MethodWrapper
{
private final Consumer<OpaAccessControl> callable;

public ThrowingMethodWrapper(Consumer<OpaAccessControl> callable) {
public ThrowingMethodWrapper(Consumer<OpaAccessControl> callable)
{
this.callable = callable;
}

@Override
public boolean isAccessAllowed(OpaAccessControl opaAccessControl) {
public boolean isAccessAllowed(OpaAccessControl opaAccessControl)
{
try {
this.callable.accept(opaAccessControl);
return true;
} catch (AccessDeniedException e) {
}
catch (AccessDeniedException e) {
if (!e.getMessage().contains("Access Denied")) {
throw new AssertionError("Expected AccessDenied exception to contain 'Access Denied' in the message");
}
Expand All @@ -134,15 +141,19 @@ public boolean isAccessAllowed(OpaAccessControl opaAccessControl) {
}
}

public static class ReturningMethodWrapper extends MethodWrapper {
public static class ReturningMethodWrapper
extends MethodWrapper
{
private final Function<OpaAccessControl, Boolean> callable;

public ReturningMethodWrapper(Function<OpaAccessControl, Boolean> callable) {
public ReturningMethodWrapper(Function<OpaAccessControl, Boolean> callable)
{
this.callable = callable;
}

@Override
public boolean isAccessAllowed(OpaAccessControl opaAccessControl) {
public boolean isAccessAllowed(OpaAccessControl opaAccessControl)
{
return this.callable.apply(opaAccessControl);
}
}
Expand Down Expand Up @@ -194,13 +205,15 @@ public Map<String, String> buildConfig()
convertPropertyToString(attribute.getGetter().invoke(config)).ifPresent(
propertyValue -> opaConfigBuilder.put(attribute.getInjectionPoint().getProperty(), propertyValue));
}
} catch (InvocationTargetException|IllegalAccessException e) {
}
catch (InvocationTargetException | IllegalAccessException e) {
throw new AssertionError("Failed to build config map", e);
}
return opaConfigBuilder.buildOrThrow();
}

private static Optional<String> convertPropertyToString(Object value) {
private static Optional<String> convertPropertyToString(Object value)
{
if (value instanceof Optional<?> optionalValue) {
return optionalValue.map(Object::toString);
}
Expand All @@ -209,7 +222,7 @@ private static Optional<String> convertPropertyToString(Object value) {
}

static final class TestingSystemAccessControlContext
implements SystemAccessControlFactory.SystemAccessControlContext
implements SystemAccessControlFactory.SystemAccessControlContext
{
private final String trinoVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class TestOpaAccessControl
@Test
public void testResponseHasExtraFields()
{
InstrumentedHttpClient mockClient = createMockHttpClient(OPA_SERVER_URI, buildValidatingRequestHandler(requestingIdentity, 200,"""
InstrumentedHttpClient mockClient = createMockHttpClient(OPA_SERVER_URI, buildValidatingRequestHandler(requestingIdentity, 200, """
{
"result": true,
"decision_id": "foo",
Expand Down Expand Up @@ -1173,7 +1173,11 @@ private void testGetRowFilters(String responseContent, List<OpaViewExpression> e
@Test
public void testGetRowFiltersDoesNothingIfNotConfigured()
{
InstrumentedHttpClient httpClient = createMockHttpClient(OPA_SERVER_ROW_FILTERING_URI, request -> {throw new AssertionError("Should not have been called");});
InstrumentedHttpClient httpClient = createMockHttpClient(
OPA_SERVER_ROW_FILTERING_URI,
request -> {
throw new AssertionError("Should not have been called");
});
OpaAccessControl authorizer = createOpaAuthorizer(OPA_CONFIG_WITH_ONLY_ALLOW, httpClient);
CatalogSchemaTableName tableName = new CatalogSchemaTableName("some_catalog", "some_schema", "some_table");

Expand Down Expand Up @@ -1271,7 +1275,11 @@ private void testGetColumnMask(String responseContent, Optional<OpaViewExpressio
@Test
public void testGetColumnMaskDoesNothingIfNotConfigured()
{
InstrumentedHttpClient httpClient = createMockHttpClient(OPA_SERVER_COLUMN_MASK_URI, request -> {throw new AssertionError("Should not have been called");});
InstrumentedHttpClient httpClient = createMockHttpClient(
OPA_SERVER_COLUMN_MASK_URI,
request -> {
throw new AssertionError("Should not have been called");
});
OpaAccessControl authorizer = createOpaAuthorizer(OPA_CONFIG_WITH_ONLY_ALLOW, httpClient);
CatalogSchemaTableName tableName = new CatalogSchemaTableName("some_catalog", "some_schema", "some_table");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public class TestOpaAccessControlDataFilteringSystem

private DistributedQueryRunnerHelper runner;


@AfterEach
public void teardown()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void testFilterColumns()
requestingIdentity,
parsedRequest -> {
String tableName = parsedRequest.at("/input/action/filterResources/0/table/tableName").asText();
String responseContents = switch(tableName) {
String responseContents = switch (tableName) {
case "table_one" -> "{\"result\": [0, 1]}";
case "table_two" -> "{\"result\": [1]}";
default -> "{\"result\": []}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,9 @@ public void testRowFilteringResponseWithExtraFields()
public void testRowFilteringResponseIllegalResponseThrows()
{
testIllegalResponseDecodingThrows("""
{
"result": ["foo"]
}""", rowFilteringResponseCodec);
{
"result": ["foo"]
}""", rowFilteringResponseCodec);
}

@Test
Expand Down Expand Up @@ -280,9 +280,9 @@ public void testColumnMaskingResponseWithExtraFields()
public void testColumnMaskingResponseIllegalResponseThrows()
{
testIllegalResponseDecodingThrows("""
{
"result": {"foo": "bar"}
}""", columnMaskingResponseCodec);
{
"result": {"foo": "bar"}
}""", columnMaskingResponseCodec);
}

private <T> void testIllegalResponseDecodingThrows(String rawResponse, JsonCodec<T> codec)
Expand Down

0 comments on commit 9dd9265

Please sign in to comment.