mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(appsync): preserve resolver callback execution scope (#7512)
This commit is contained in:
1 parent
7bc09c7925
commit
1d6ec2b9d1
6 files changed
+124
-11
No files matched your search
+2
-1
@@ -181,7 +181,8 @@ public class AppSyncExecutionController {
|
||||
parsed.operationName(), denyFields);
|
||||
}
|
||||
try (ResolverCallbackSessions.Session session =
|
||||
callbackSessions.open(apiId, authContext.identity(), authContext.authType())) {
|
||||
callbackSessions.open(apiId, authContext.identity(), authContext.authType(),
|
||||
authContext.accountId(), authContext.region())) {
|
||||
ResolveSpec resolve = new ResolveSpec(reachableEndpoint.baseUrl() + "/_floci/appsync/resolve",
|
||||
session.token(), resolveFields, RESOLVE_MAX_BATCH);
|
||||
Map<String, Object> result = sidecarClient.execute(preparedSdl, scalars, parsed.query(),
|
||||
|
||||
+10
-5
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.appsync.graphql.resolver;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.appsync.model.Resolver;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.Consumes;
|
||||
@@ -79,11 +80,15 @@ public class ResolverCallbackResource {
|
||||
.entity(Map.of("error", "Resolver callback body is missing invocations")).build();
|
||||
}
|
||||
|
||||
List<Map<String, Object>> results = new ArrayList<>();
|
||||
for (JsonNode node : invocations) {
|
||||
results.add(answer(session.get(), node));
|
||||
}
|
||||
return Response.ok(Map.of("results", results)).build();
|
||||
ResolverCallbackSessions.Session execution = session.get();
|
||||
// A callback is a new HTTP request; its bearer token carries the operation's storage scope.
|
||||
return RequestScopes.callAs(execution.accountId(), execution.region(), () -> {
|
||||
List<Map<String, Object>> results = new ArrayList<>();
|
||||
for (JsonNode node : invocations) {
|
||||
results.add(answer(execution, node));
|
||||
}
|
||||
return Response.ok(Map.of("results", results)).build();
|
||||
});
|
||||
}
|
||||
|
||||
private Map<String, Object> answer(ResolverCallbackSessions.Session session, JsonNode node) {
|
||||
|
||||
+16
-3
@@ -38,14 +38,19 @@ public class ResolverCallbackSessions {
|
||||
private final String apiId;
|
||||
private final Object identity;
|
||||
private final String authType;
|
||||
private final String accountId;
|
||||
private final String region;
|
||||
private final List<AppSyncResolverError> appendedErrors = new CopyOnWriteArrayList<>();
|
||||
|
||||
private Session(Map<String, Session> owner, String token, String apiId, Object identity, String authType) {
|
||||
private Session(Map<String, Session> owner, String token, String apiId, Object identity, String authType,
|
||||
String accountId, String region) {
|
||||
this.owner = owner;
|
||||
this.token = token;
|
||||
this.apiId = apiId;
|
||||
this.identity = identity;
|
||||
this.authType = authType;
|
||||
this.accountId = accountId;
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String token() {
|
||||
@@ -64,6 +69,14 @@ public class ResolverCallbackSessions {
|
||||
return authType;
|
||||
}
|
||||
|
||||
public String accountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public String region() {
|
||||
return region;
|
||||
}
|
||||
|
||||
/** Errors from {@code util.appendError}, which are reported beside the data rather than instead of it. */
|
||||
public void appendErrors(List<AppSyncResolverError> errors) {
|
||||
if (errors != null && !errors.isEmpty()) {
|
||||
@@ -81,11 +94,11 @@ public class ResolverCallbackSessions {
|
||||
}
|
||||
}
|
||||
|
||||
public Session open(String apiId, Object identity, String authType) {
|
||||
public Session open(String apiId, Object identity, String authType, String accountId, String region) {
|
||||
byte[] bytes = new byte[TOKEN_BYTES];
|
||||
random.nextBytes(bytes);
|
||||
String token = Base64Url.encode(bytes);
|
||||
Session session = new Session(sessions, token, apiId, identity, authType);
|
||||
Session session = new Session(sessions, token, apiId, identity, authType, accountId, region);
|
||||
sessions.put(token, session);
|
||||
return session;
|
||||
}
|
||||
|
||||
+4
-1
@@ -266,8 +266,11 @@ class AppSyncExecutionControllerTest {
|
||||
when(sidecarClient.execute(any(), any(), any(), any(), any(), any(), any())).thenAnswer(call -> {
|
||||
GraphqlSidecarClient.ResolveSpec spec = call.getArgument(6);
|
||||
token.set(spec.token());
|
||||
ResolverCallbackSessions.Session session = callbackSessions.find(spec.token()).orElseThrow();
|
||||
assertEquals("000000000000", session.accountId());
|
||||
assertEquals("us-east-1", session.region());
|
||||
// What a resolver calling util.appendError does, by way of the callback.
|
||||
callbackSessions.find(spec.token()).orElseThrow().appendErrors(List.of(
|
||||
session.appendErrors(List.of(
|
||||
new AppSyncResolverError("one row was dropped", "Partial", null, Map.of("rows", 1),
|
||||
List.of("getMessages"))));
|
||||
return Map.of("data", Map.of("getMessages", "partial"));
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ class ResolverCallbackResourceTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
resource = new ResolverCallbackResource(sessions, executor, new ObjectMapper());
|
||||
session = sessions.open(API_ID, Map.of("sub", "u-1"), "AWS_IAM");
|
||||
session = sessions.open(API_ID, Map.of("sub", "u-1"), "AWS_IAM", "000000000000", "us-east-1");
|
||||
}
|
||||
|
||||
private String body(String... invocations) {
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.resolver;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.services.appsync.model.Resolver;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class ResolverCallbackScopeIntegrationTest {
|
||||
|
||||
private static final String ACCOUNT = "725018800003";
|
||||
private static final String REGION = "cn-north-1";
|
||||
private static final String API = "callback-scope";
|
||||
private static final String BODY = "{\"invocations\":[{\"id\":\"0\",\"typeName\":\"Mutation\","
|
||||
+ "\"fieldName\":\"publish\",\"accountId\":\"untrusted\",\"region\":\"us-west-2\"}]}";
|
||||
|
||||
@Inject
|
||||
ResolverCallbackSessions sessions;
|
||||
@Inject
|
||||
ResolverCallbackResource resource;
|
||||
@Inject
|
||||
RegionResolver regionResolver;
|
||||
@InjectMock
|
||||
AppSyncResolverExecutor executor;
|
||||
|
||||
@AfterEach
|
||||
void clearMocks() {
|
||||
reset(executor);
|
||||
}
|
||||
|
||||
@Test
|
||||
void callbackHttpRequestUsesTheTokenBoundAccountRegionAndIdentity() {
|
||||
AtomicReference<String> lookup = new AtomicReference<>();
|
||||
AtomicReference<String> execution = new AtomicReference<>();
|
||||
AtomicReference<Object> identity = new AtomicReference<>();
|
||||
Resolver resolver = new Resolver();
|
||||
when(executor.findResolver(eq(API), eq("Mutation"), eq("publish"))).thenAnswer(call -> {
|
||||
lookup.set(scope());
|
||||
return resolver;
|
||||
});
|
||||
when(executor.execute(eq(resolver), any())).thenAnswer(call -> {
|
||||
execution.set(scope());
|
||||
identity.set(((ResolverInvocation) call.getArgument(1)).identity());
|
||||
return new ResolverOutcome("delivered", null, List.of());
|
||||
});
|
||||
Map<String, Object> roleIdentity = Map.of("userArn", "role-identity");
|
||||
try (ResolverCallbackSessions.Session session = sessions.open(API, roleIdentity, "IAM Authorization", ACCOUNT, REGION)) {
|
||||
given().contentType("application/json").header("Authorization", "Bearer " + session.token())
|
||||
.body(BODY).post("/_floci/appsync/resolve")
|
||||
.then().statusCode(200).body("results[0].data", equalTo("delivered"));
|
||||
}
|
||||
assertEquals(ACCOUNT + "|" + REGION + "|aws-cn", lookup.get());
|
||||
assertEquals(lookup.get(), execution.get());
|
||||
assertEquals(roleIdentity, identity.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void callbackRestoresTheSurroundingScopeEvenWhenResolverLookupFails() {
|
||||
when(executor.findResolver(eq(API), any(), any())).thenAnswer(call -> {
|
||||
assertEquals(ACCOUNT + "|" + REGION + "|aws-cn", scope());
|
||||
throw new AwsException("ConcurrentModificationException", "reloading", 409);
|
||||
});
|
||||
try (ResolverCallbackSessions.Session session = sessions.open(API, Map.of(), "IAM Authorization", ACCOUNT, REGION)) {
|
||||
RequestScopes.runAs("725018800004", "eu-west-1", () -> {
|
||||
assertEquals(200, resource.resolve("Bearer " + session.token(), BODY).getStatus());
|
||||
assertEquals("725018800004|eu-west-1|aws", scope());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private String scope() {
|
||||
return regionResolver.getAccountId() + "|" + regionResolver.getRegion() + "|" + regionResolver.getPartition();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user