mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(verifiedpermissions): run Cedar from the floci-sidecar-cedar image (#3799)
* feat(verifiedpermissions): run Cedar from the floci-sidecar-cedar image The Cedar sidecar now comes from floci-io/floci-sidecars as floci/floci-sidecar-cedar, pinned to an exact version because a cached :latest is never re-pulled. CedarSidecarManager reads the contract JSON that the image's /health returns: a sidecar speaking another contract major is refused immediately with a message naming the image and the FLOCI_SERVICES_VERIFIEDPERMISSIONS_CEDAR_IMAGE and _CEDAR_URL settings to change, instead of polling for thirty seconds or failing later on a request. A pre-configured URL is probed the same way. A sidecar whose health body is not JSON predates the contract and is accepted with a one-time warning. * test(verifiedpermissions): exercise the published Cedar image VerifiedPermissionsIntegrationTest no longer boots the in-tree sidecar in process: CedarSidecarManager starts the pinned image through Docker, the same path production takes, under a vp-test resource namespace so the manager's remove-before-start cannot kill a developer's live sidecar. It is skipped without Docker, and locally when the image is not present; in CI a missing image fails. VerifiedPermissionsServiceTest keeps its reject-before-persist coverage against a fake sidecar answering 400, and CedarSidecarClientTest pins the request shapes and the 200/400/other mapping Floci owns. * chore(build): drop the in-tree Cedar sidecar sidecars/cedar now lives in floci-io/floci-sidecars as cedar/ with its history. The build-helper test source root and the test-scoped cedar-java dependency existed only to boot that server inside the test JVM; the tests now run against the published image, so both go, along with the .dockerignore allowlist entry that let the sidecar Dockerfile see its sources from the repository root context. * ci: stop publishing the Cedar sidecar from this repository The release, nightly and reusable image workflows no longer build a Cedar image or record it in the provenance manifest; the sidecar is released from floci-io/floci-sidecars on its own cadence. The compatibility workflow no longer builds and starts the sidecar by hand: Floci pulls the pinned image itself over the mounted socket onto compat-net, exactly as it already does for floci-duck. The unit-test shards prefetch the pinned image, reading the tag from application.yml so the two cannot drift. The floci/floci:*-cedar tags stop at this change; whether the next release cut retags the sidecar image under the old name for a deprecation window is a release decision, not a workflow default. * docs(verifiedpermissions): document the external Cedar sidecar image The Verified Permissions page names floci/floci-sidecar-cedar, why it is pinned to an exact version, how to pre-pull it, where its API and the sidecar contract are documented, and what a contract mismatch looks like. The README's Docker integration table gains the Verified Permissions row it never had. AGENTS.md records the sidecar rule: own repository, no tag suffix on the Floci image, no :latest default, the sidecar releases first, and how a sidecar is tested from here.
This commit is contained in:
1 parent
f004aeaad5
commit
892927ceb5
21 files changed
+480
-766
No files matched your search
+168
@@ -0,0 +1,168 @@
|
||||
package io.github.hectorvent.floci.services.verifiedpermissions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.verifiedpermissions.model.Policy;
|
||||
import io.github.hectorvent.floci.services.verifiedpermissions.model.PolicyTemplate;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The wire contract Floci owns towards the Cedar sidecar: request shapes for each endpoint and
|
||||
* how the sidecar's 200 / 400 / other answers become AWS errors. Cedar semantics live in the
|
||||
* sidecar's own repository.
|
||||
*/
|
||||
class CedarSidecarClientTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final AtomicReference<String> lastPath = new AtomicReference<>();
|
||||
private final AtomicReference<JsonNode> lastBody = new AtomicReference<>();
|
||||
private HttpServer server;
|
||||
private CedarSidecarClient client;
|
||||
|
||||
@BeforeEach
|
||||
void startServer() throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.start();
|
||||
CedarSidecarManager manager = mock(CedarSidecarManager.class);
|
||||
when(manager.ensureReady()).thenReturn("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
client = new CedarSidecarClient(manager, mapper);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopServer() {
|
||||
server.stop(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateSchemaPostsTheSchema() {
|
||||
answer("/v1/schema/validate", 200, "{\"valid\":true}");
|
||||
|
||||
client.validateSchema("{\"Demo\":{}}");
|
||||
|
||||
assertThat(lastPath.get(), equalTo("/v1/schema/validate"));
|
||||
assertThat(lastBody.get().get("schema").asText(), equalTo("{\"Demo\":{}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validateEntityTypePostsTheType() {
|
||||
answer("/v1/entity-type/validate", 200, "{\"valid\":true}");
|
||||
|
||||
client.validateEntityType("Demo::User");
|
||||
|
||||
assertThat(lastBody.get().get("entityType").asText(), equalTo("Demo::User"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsePolicyReturnsEffectAndAst() {
|
||||
answer("/v1/policy/parse", 200, "{\"effect\":\"Forbid\",\"ast\":{\"effect\":\"forbid\"}}");
|
||||
|
||||
CedarSidecarClient.ParsedPolicy parsed = client.parsePolicy("forbid(principal, action, resource);", true);
|
||||
|
||||
assertThat(lastBody.get().get("statement").asText(), equalTo("forbid(principal, action, resource);"));
|
||||
assertThat(lastBody.get().get("template").asBoolean(), is(true));
|
||||
assertThat(parsed.effect(), equalTo("Forbid"));
|
||||
assertThat(parsed.ast().get("effect").asText(), equalTo("forbid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatePolicyPostsSchemaStatementAndTemplateFlag() {
|
||||
answer("/v1/policy/validate", 200, "{\"valid\":true}");
|
||||
|
||||
client.validatePolicy("{\"Demo\":{}}", "permit(principal, action, resource);", false);
|
||||
|
||||
assertThat(lastBody.get().get("schema").asText(), equalTo("{\"Demo\":{}}"));
|
||||
assertThat(lastBody.get().get("statement").asText(), equalTo("permit(principal, action, resource);"));
|
||||
assertThat(lastBody.get().get("template").asBoolean(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizePostsRequestPoliciesAndTemplatesAndReadsTheDecision() throws Exception {
|
||||
answer("/v1/authorize", 200,
|
||||
"{\"decision\":\"DENY\",\"determiningPolicyIds\":[\"deny\"],\"errors\":[\"e1\"]}");
|
||||
JsonNode request = mapper.readTree("{\"principal\":{\"entityType\":\"Demo::User\",\"entityId\":\"a\"}}");
|
||||
Policy policy = new Policy("store", "deny", null, "STATIC", "forbid(principal, action, resource);",
|
||||
null, null, null, null, "Forbid", null, null);
|
||||
PolicyTemplate template = new PolicyTemplate("store", "t1", null,
|
||||
"permit(principal == ?principal, action, resource == ?resource);", null, null, null);
|
||||
|
||||
CedarSidecarClient.EvaluationResult result = client.authorize(request, List.of(policy), Map.of("t1", template));
|
||||
|
||||
assertThat(lastBody.get().get("request").get("principal").get("entityId").asText(), equalTo("a"));
|
||||
assertThat(lastBody.get().get("policies").get(0).get("policyId").asText(), equalTo("deny"));
|
||||
assertThat(lastBody.get().get("templates").get("t1").get("policyTemplateId").asText(), equalTo("t1"));
|
||||
assertThat(result.decision(), equalTo("DENY"));
|
||||
assertThat(result.determiningPolicyIds(), contains("deny"));
|
||||
assertThat(result.errors(), contains("e1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sidecarBadRequestBecomesValidationExceptionWithTheSidecarMessage() {
|
||||
answer("/v1/policy/parse", 400, "{\"error\":\"unexpected token\"}");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> client.parsePolicy("permit(", false));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("ValidationException"));
|
||||
assertThat(error.getMessage(), equalTo("unexpected token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherSidecarFailuresBecomeInternalServerException() {
|
||||
answer("/v1/authorize", 500, "{\"error\":\"boom\"}");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> client.authorize(mapper.createObjectNode(), List.of(), Map.of()));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("InternalServerException"));
|
||||
assertThat(error.getHttpStatus(), equalTo(500));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unreachableSidecarBecomesInternalServerException() {
|
||||
CedarSidecarManager down = mock(CedarSidecarManager.class);
|
||||
when(down.ensureReady()).thenThrow(new IllegalStateException("speaks sidecar contract 2"));
|
||||
CedarSidecarClient offline = new CedarSidecarClient(down, mapper);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> offline.validateSchema("{}"));
|
||||
|
||||
assertThat(error.getErrorCode(), equalTo("InternalServerException"));
|
||||
assertThat(error.getMessage(), equalTo("Failed to call Cedar sidecar for schema validation: speaks sidecar contract 2"));
|
||||
}
|
||||
|
||||
private void answer(String path, int status, String body) {
|
||||
server.createContext(path, exchange -> {
|
||||
lastPath.set(exchange.getRequestURI().getPath());
|
||||
lastBody.set(mapper.readTree(exchange.getRequestBody()));
|
||||
respond(exchange, status, body);
|
||||
});
|
||||
}
|
||||
|
||||
private static void respond(HttpExchange exchange, int status, String body) throws IOException {
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
-21
@@ -1,16 +1,27 @@
|
||||
package io.github.hectorvent.floci.services.verifiedpermissions;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
@@ -18,38 +29,35 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
class CedarSidecarManagerTest {
|
||||
|
||||
private final ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
private final ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
private final EmulatorConfig.VerifiedPermissionsServiceConfig verifiedPermissions =
|
||||
mock(EmulatorConfig.VerifiedPermissionsServiceConfig.class);
|
||||
private final EmulatorConfig config = config(verifiedPermissions);
|
||||
private HttpServer fakeSidecar;
|
||||
|
||||
@AfterEach
|
||||
void stopFakeSidecar() {
|
||||
if (fakeSidecar != null) {
|
||||
fakeSidecar.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void configuredUrlSkipsContainerManagement() {
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.VerifiedPermissionsServiceConfig verifiedPermissions =
|
||||
mock(EmulatorConfig.VerifiedPermissionsServiceConfig.class);
|
||||
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.verifiedpermissions()).thenReturn(verifiedPermissions);
|
||||
when(verifiedPermissions.cedarUrl()).thenReturn(Optional.of("http://cedar.internal:8180/"));
|
||||
|
||||
CedarSidecarManager manager = new CedarSidecarManager(containerBuilder, lifecycleManager, config);
|
||||
CedarSidecarManager manager = manager();
|
||||
|
||||
assertEquals("http://cedar.internal:8180", manager.ensureReady());
|
||||
verifyNoInteractions(containerBuilder, lifecycleManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void staleManagedEndpointIsDiscarded() throws Exception {
|
||||
ContainerBuilder containerBuilder = mock(ContainerBuilder.class);
|
||||
ContainerLifecycleManager lifecycleManager = mock(ContainerLifecycleManager.class);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.VerifiedPermissionsServiceConfig verifiedPermissions =
|
||||
mock(EmulatorConfig.VerifiedPermissionsServiceConfig.class);
|
||||
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.verifiedpermissions()).thenReturn(verifiedPermissions);
|
||||
when(verifiedPermissions.cedarUrl()).thenReturn(Optional.empty());
|
||||
|
||||
CedarSidecarManager manager = new CedarSidecarManager(containerBuilder, lifecycleManager, config);
|
||||
CedarSidecarManager manager = manager();
|
||||
setField(manager, "resolvedUrl", "http://127.0.0.1:1");
|
||||
setField(manager, "containerId", "stale-cedar");
|
||||
|
||||
@@ -59,6 +67,84 @@ class CedarSidecarManagerTest {
|
||||
assertNull(getField(manager, "containerId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void contractOneSidecarIsAvailable() throws Exception {
|
||||
String url = startFakeSidecar(200, "{\"status\":\"ok\",\"name\":\"cedar\",\"version\":\"1.0.0\",\"contract\":\"1\"}");
|
||||
when(verifiedPermissions.cedarUrl()).thenReturn(Optional.of(url));
|
||||
|
||||
CedarSidecarManager manager = manager();
|
||||
|
||||
assertTrue(manager.isAvailable());
|
||||
assertEquals(url, manager.ensureReady());
|
||||
verifyNoInteractions(containerBuilder, lifecycleManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
void otherContractMajorFailsFastNamingTheFix() throws Exception {
|
||||
String url = startFakeSidecar(200, "{\"status\":\"ok\",\"name\":\"cedar\",\"version\":\"2.3.0\",\"contract\":\"2\"}");
|
||||
when(verifiedPermissions.cedarUrl()).thenReturn(Optional.of(url));
|
||||
when(verifiedPermissions.cedarImage()).thenReturn("floci/floci-sidecar-cedar:1.1.0");
|
||||
|
||||
CedarSidecarManager manager = manager();
|
||||
|
||||
IllegalStateException available = assertThrows(IllegalStateException.class, manager::isAvailable);
|
||||
IllegalStateException ready = assertThrows(IllegalStateException.class, manager::ensureReady);
|
||||
assertThat(available.getMessage(), containsString("speaks sidecar contract 2"));
|
||||
assertThat(available.getMessage(), containsString("requires contract 1"));
|
||||
assertThat(available.getMessage(), containsString("cedar 2.3.0"));
|
||||
assertThat(ready.getMessage(), containsString(CedarSidecarManager.IMAGE_ENV));
|
||||
assertThat(ready.getMessage(), containsString("floci/floci-sidecar-cedar:1.1.0"));
|
||||
assertThat(ready.getMessage(), containsString(CedarSidecarManager.URL_ENV));
|
||||
assertNull(getField(manager, "resolvedUrl"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void preContractSidecarWithTextHealthIsAccepted() throws Exception {
|
||||
String url = startFakeSidecar(200, "ok");
|
||||
when(verifiedPermissions.cedarUrl()).thenReturn(Optional.of(url));
|
||||
|
||||
CedarSidecarManager manager = manager();
|
||||
|
||||
assertTrue(manager.isAvailable());
|
||||
assertEquals(url, manager.ensureReady());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unhealthySidecarIsNotReadyRatherThanWrong() throws Exception {
|
||||
String url = startFakeSidecar(503, "{\"status\":\"starting\",\"contract\":\"2\"}");
|
||||
when(verifiedPermissions.cedarUrl()).thenReturn(Optional.of(url));
|
||||
|
||||
CedarSidecarManager manager = manager();
|
||||
|
||||
assertFalse(manager.isAvailable());
|
||||
assertEquals(url, manager.ensureReady());
|
||||
}
|
||||
|
||||
private CedarSidecarManager manager() {
|
||||
return new CedarSidecarManager(containerBuilder, lifecycleManager, config, new ObjectMapper());
|
||||
}
|
||||
|
||||
private static EmulatorConfig config(EmulatorConfig.VerifiedPermissionsServiceConfig verifiedPermissions) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
when(config.services()).thenReturn(services);
|
||||
when(services.verifiedpermissions()).thenReturn(verifiedPermissions);
|
||||
return config;
|
||||
}
|
||||
|
||||
private String startFakeSidecar(int status, String healthBody) throws IOException {
|
||||
fakeSidecar = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
fakeSidecar.createContext("/health", exchange -> {
|
||||
byte[] bytes = healthBody.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(status, bytes.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(bytes);
|
||||
}
|
||||
});
|
||||
fakeSidecar.start();
|
||||
return "http://127.0.0.1:" + fakeSidecar.getAddress().getPort();
|
||||
}
|
||||
|
||||
private static void setField(Object target, String name, Object value) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
@@ -70,5 +156,4 @@ class CedarSidecarManagerTest {
|
||||
field.setAccessible(true);
|
||||
return field.get(target);
|
||||
}
|
||||
|
||||
}
|
||||
+49
-11
@@ -2,9 +2,10 @@ package io.github.hectorvent.floci.services.verifiedpermissions;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.cedar.CedarSidecarServer;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.eclipse.microprofile.config.ConfigProvider;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -16,21 +17,58 @@ import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasEntry;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
|
||||
/**
|
||||
* Runs against the real Cedar sidecar image ({@code floci.services.verifiedpermissions.cedar-image}),
|
||||
* started by {@link CedarSidecarManager} exactly as in production. Needs Docker; skipped without
|
||||
* it. Locally it is also skipped when the pinned image is not present, so a developer without
|
||||
* registry access is not stuck; in CI a missing image is a failure, since a vanished tag must
|
||||
* not pass silently.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(VerifiedPermissionsIntegrationTest.CedarSidecarProfile.class)
|
||||
class VerifiedPermissionsIntegrationTest {
|
||||
private static HttpServer cedarServer;
|
||||
private static final String CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String AUTH = "AWS4-HMAC-SHA256 Credential=111122223333/20260101/us-east-1/verifiedpermissions/aws4_request";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() throws Exception {
|
||||
cedarServer = CedarSidecarServer.start(18180);
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
/**
|
||||
* Namespaces the sidecar container as {@code floci-vp-test-cedar}: the manager removes any
|
||||
* container of its name before starting, and the test config uses the same empty namespace
|
||||
* as a developer's running Floci, whose live sidecar would otherwise be killed.
|
||||
*/
|
||||
public static class CedarSidecarProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.docker.resource-namespace", "vp-test");
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopCedarSidecar() {
|
||||
cedarServer.stop(0);
|
||||
@BeforeAll
|
||||
static void requireDockerAndTheSidecarImage() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
Assumptions.assumeTrue(isDockerAvailable(),
|
||||
"Docker daemon must be available for Verified Permissions integration tests");
|
||||
String image = ConfigProvider.getConfig().getValue("floci.services.verifiedpermissions.cedar-image", String.class);
|
||||
Assumptions.assumeTrue(imageUsable(image), "Cedar sidecar image " + image + " is not present locally");
|
||||
}
|
||||
|
||||
private static boolean isDockerAvailable() {
|
||||
return run("docker", "version", "--format", "{{.Server.Version}}");
|
||||
}
|
||||
|
||||
private static boolean imageUsable(String image) {
|
||||
if ("true".equals(System.getenv("CI"))) {
|
||||
return true;
|
||||
}
|
||||
return run("docker", "image", "inspect", image);
|
||||
}
|
||||
|
||||
private static boolean run(String... command) {
|
||||
try {
|
||||
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
|
||||
return process.waitFor() == 0;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+24
-7
@@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.verifiedpermissions;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.cedar.CedarSidecarServer;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -20,6 +19,10 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -30,18 +33,32 @@ class VerifiedPermissionsServiceTest {
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
private static HttpServer cedarServer;
|
||||
/**
|
||||
* A stand-in for the Cedar sidecar that rejects every schema. These tests cover the service's
|
||||
* ordering (reject before persist), not Cedar itself; Cedar semantics are covered by
|
||||
* {@code VerifiedPermissionsIntegrationTest} against the real image.
|
||||
*/
|
||||
private static HttpServer fakeCedar;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private VerifiedPermissionsService service;
|
||||
|
||||
@BeforeAll
|
||||
static void startCedarSidecar() throws Exception {
|
||||
cedarServer = CedarSidecarServer.start(18181);
|
||||
static void startFakeCedar() throws Exception {
|
||||
fakeCedar = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
fakeCedar.createContext("/v1/schema/validate", exchange -> {
|
||||
byte[] bytes = "{\"error\":\"entityTypes must be an object\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json");
|
||||
exchange.sendResponseHeaders(400, bytes.length);
|
||||
try (OutputStream out = exchange.getResponseBody()) {
|
||||
out.write(bytes);
|
||||
}
|
||||
});
|
||||
fakeCedar.start();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void stopCedarSidecar() {
|
||||
cedarServer.stop(0);
|
||||
static void stopFakeCedar() {
|
||||
fakeCedar.stop(0);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
@@ -53,7 +70,7 @@ class VerifiedPermissionsServiceTest {
|
||||
StorageBackend<String, IdempotencyRecord> idempotency = AccountAwareStorageBackend.inMemory(ACCOUNT);
|
||||
StorageBackend<String, IdentitySource> identitySources = AccountAwareStorageBackend.inMemory(ACCOUNT);
|
||||
CedarSidecarManager manager = mock(CedarSidecarManager.class);
|
||||
when(manager.ensureReady()).thenReturn("http://127.0.0.1:18181");
|
||||
when(manager.ensureReady()).thenReturn("http://127.0.0.1:" + fakeCedar.getAddress().getPort());
|
||||
CedarSidecarClient cedarClient = new CedarSidecarClient(manager, objectMapper);
|
||||
service = new VerifiedPermissionsService(policyStores, aliases, policies, templates, idempotency,
|
||||
identitySources, new RegionResolver(REGION, ACCOUNT), objectMapper, mock(KmsService.class), cedarClient);
|
||||
|
||||
Reference in new issue
Block a user