feat(redshift): add Lambda batch invoker for scalar external functions (#9285)

Adds LambdaUdfInvoker, the batch invoker for scalar Redshift Lambda UDFs, on top of LambdaService. It sends the request Redshift documents (request_id, cluster, user, database, external_function, query_id, num_records, arguments) from the cluster's Region, splits rows by the batch row and size limits, parses the response and maps PostgreSQL text values to and from JSON across the documented scalar types. It retries TooManyRequestsException with backoff, and refusals and failures surface as SQL errors.

Whether the role may invoke the function is decided by a new iam.RoleResourceAccessAuthorizer, which holds the identity, boundary, SCP and resource policy evaluation Step Functions' LambdaTaskAuthorizer used, and which that class now calls too.

Part of #7815.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-11-24 00:40:48 -04:00
1 parent c997a3c2ba
commit cd0436a53f
7 files changed
+1551 -177

No files matched your search

@@ -0,0 +1,94 @@
package io.floci.aws.services.iam;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.services.iam.ResourcePolicyProvider.ResourcePolicy;
import io.floci.aws.services.iam.model.CallerContext;
import io.floci.aws.services.iam.model.IamRole;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class RoleResourceAccessAuthorizerTest {
private static final String ACCOUNT = "111122223333";
private static final String OTHER_ACCOUNT = "444455556666";
private static final String ROLE = "arn:aws:iam::" + ACCOUNT + ":role/exec";
private static final String ACTION = "lambda:InvokeFunction";
private static final String OWN_FUNCTION = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:sum";
private static final String OTHER_FUNCTION = "arn:aws:lambda:eu-west-1:" + OTHER_ACCOUNT + ":function:sum";
private static final String INVOKE_ANYTHING = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"lambda:InvokeFunction","Resource":"*"}]}""";
private ResourcePolicyProvider functionPolicies;
private IamService iamService;
private RoleResourceAccessAuthorizer authorizer;
@BeforeEach
@SuppressWarnings("unchecked")
void setUp() {
iamService = mock(IamService.class);
functionPolicies = mock(ResourcePolicyProvider.class);
when(functionPolicies.getResourcePolicies(anyString(), anyString(), anyString())).thenReturn(List.of());
Instance<ResourcePolicyProvider> providers = mock(Instance.class);
when(providers.iterator()).thenAnswer(call -> List.of(functionPolicies).iterator());
Instance<ScpProvider> scps = mock(Instance.class);
IamRole role = new IamRole();
role.setArn(ROLE);
when(iamService.findRole(ACCOUNT, "exec")).thenReturn(Optional.of(role));
when(iamService.resolvePrincipalContext(anyString()))
.thenReturn(CallerContext.of(List.of(INVOKE_ANYTHING)));
authorizer = new RoleResourceAccessAuthorizer(iamService, new IamPolicyEvaluator(new ObjectMapper()),
providers, scps);
}
@Test
void aRoleItsOwnPoliciesAllowInvokesAFunctionOfItsAccountWithoutAResourcePolicy() {
assertThat(authorizer.refusal(ROLE, ACTION, OWN_FUNCTION, "us-east-1", Map.of()), equalTo(Optional.empty()));
}
@Test
void aFunctionOfAnotherAccountNeedsItsResourcePolicyToGrantTheRole() {
when(functionPolicies.getResourcePolicies("lambda", ACTION, OTHER_FUNCTION))
.thenReturn(List.of(new ResourcePolicy(null, OTHER_ACCOUNT)));
Optional<String> refusal = authorizer.refusal(ROLE, ACTION, OTHER_FUNCTION, "eu-west-1", Map.of());
assertThat(refusal.orElseThrow(), startsWith("User: arn:aws:sts::" + ACCOUNT + ":assumed-role/exec"));
assertThat(refusal.orElseThrow(), containsString("no resource-based policy allows the " + ACTION));
}
@Test
void anExplicitDenyInTheResourcePolicyRefusesARoleItsOwnPoliciesAllow() {
String deny = """
{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":{"AWS":"%s"},
"Action":"lambda:InvokeFunction","Resource":"%s"}]}""".formatted(ROLE, OWN_FUNCTION);
when(functionPolicies.getResourcePolicies("lambda", ACTION, OWN_FUNCTION))
.thenReturn(List.of(new ResourcePolicy(deny, ACCOUNT)));
Optional<String> refusal = authorizer.refusal(ROLE, ACTION, OWN_FUNCTION, "us-east-1", Map.of());
assertThat(refusal.orElseThrow(), containsString("with an explicit deny in a resource-based policy"));
}
@Test
void aRoleThatDoesNotExistIsRefused() {
String missing = "arn:aws:iam::" + ACCOUNT + ":role/gone";
Optional<String> refusal = authorizer.refusal(missing, ACTION, OWN_FUNCTION, "us-east-1", Map.of());
assertThat(refusal.orElseThrow(), containsString("no identity-based policy allows the " + ACTION));
}
}
@@ -0,0 +1,129 @@
package io.floci.aws.services.redshift.spectrum;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ExternalFunctionValuesTest {
private static final JsonNodeFactory JSON = JsonNodeFactory.instance;
@Test
void integerTypesTravelAsJsonNumbers() {
assertThat(ExternalFunctionValues.toJson("integer", "42").isIntegralNumber(), is(true));
assertThat(ExternalFunctionValues.toJson("bigint", "-9223372036854775808").longValue(),
equalTo(Long.MIN_VALUE));
}
@Test
void numericKeepsItsScaleAndFloatsAreNumbers() {
assertThat(ExternalFunctionValues.toJson("numeric(10,2)", "1.50").decimalValue().toPlainString(),
equalTo("1.50"));
assertThat(ExternalFunctionValues.toJson("double precision", "2.5").doubleValue(), equalTo(2.5));
}
@Test
void anInfiniteFloatTravelsAsItsPostgresText() {
assertThat(ExternalFunctionValues.toJson("real", "Infinity").textValue(), equalTo("Infinity"));
}
@Test
void booleanCharacterDateAndTimestampValuesMapToTheirJsonTypes() {
assertThat(ExternalFunctionValues.toJson("boolean", "t").booleanValue(), is(true));
assertThat(ExternalFunctionValues.toJson("boolean", "false").booleanValue(), is(false));
assertThat(ExternalFunctionValues.toJson("varchar(20)", "abc").textValue(), equalTo("abc"));
assertThat(ExternalFunctionValues.toJson("date", "2026-11-23").textValue(), equalTo("2026-11-23"));
assertThat(ExternalFunctionValues.toJson("timestamp", "2026-11-23 10:15:00").textValue(),
equalTo("2026-11-23 10:15:00"));
}
@Test
void aSqlNullIsAJsonNullBothWays() {
assertThat(ExternalFunctionValues.toJson("integer", null).isNull(), is(true));
assertThat(ExternalFunctionValues.fromJson("integer", JSON.nullNode()), is(nullValue()));
assertThat(ExternalFunctionValues.fromJson("integer", null), is(nullValue()));
}
@Test
void resultsAreReadBackAsTheTextOfTheReturnType() {
assertThat(ExternalFunctionValues.fromJson("smallint", JSON.numberNode(7)), equalTo("7"));
assertThat(ExternalFunctionValues.fromJson("numeric(5,2)", JSON.numberNode(new BigDecimal("3.10"))),
equalTo("3.10"));
assertThat(ExternalFunctionValues.fromJson("boolean", JSON.booleanNode(true)), equalTo("true"));
assertThat(ExternalFunctionValues.fromJson("date", JSON.textNode("2026-11-23")), equalTo("2026-11-23"));
assertThat(ExternalFunctionValues.fromJson("timestamp", JSON.textNode("2026-11-23T10:15:00")),
equalTo("2026-11-23 10:15:00"));
assertThat(ExternalFunctionValues.fromJson("varchar", JSON.textNode("x")), equalTo("x"));
}
@Test
void anIntegerResultMayCarryAZeroFraction() {
assertThat(ExternalFunctionValues.fromJson("integer", JSON.numberNode(new BigDecimal("3.0"))), equalTo("3"));
assertThat(ExternalFunctionValues.fromJson("bigint", JSON.numberNode(new BigDecimal("1E+3"))),
equalTo("1000"));
}
@Test
void theNonFiniteValuesAFloatTravelsAsComeBack() {
assertThat(ExternalFunctionValues.fromJson("double precision", JSON.textNode("Infinity")), equalTo("Infinity"));
assertThat(ExternalFunctionValues.fromJson("real", JSON.textNode("NaN")), equalTo("NaN"));
assertThrows(SpectrumSqlException.class,
() -> ExternalFunctionValues.fromJson("real", JSON.textNode("a lot")));
}
@Test
void aNumericNanRoundTripsAsItsText() {
assertThat(ExternalFunctionValues.toJson("numeric", "NaN").textValue(), equalTo("NaN"));
assertThat(ExternalFunctionValues.fromJson("numeric", JSON.textNode("NaN")), equalTo("NaN"));
assertThat(ExternalFunctionValues.fromJson("numeric(10,2)", JSON.textNode("nan")), equalTo("NaN"));
assertThrows(SpectrumSqlException.class,
() -> ExternalFunctionValues.fromJson("numeric", JSON.textNode("Infinity")));
assertThrows(SpectrumSqlException.class,
() -> ExternalFunctionValues.fromJson("numeric", JSON.textNode("not-a-number")));
}
@Test
void anArgumentThatIsNotAValueOfItsTypeIsAnSqlError() {
for (String[] bad : new String[][] {{"integer", "abc"}, {"numeric", "1.2.3"}, {"double precision", "x"},
{"boolean", "yes"}}) {
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> ExternalFunctionValues.toJson(bad[0], bad[1]));
assertThat(failed.sqlState(), equalTo("22P02"));
}
}
@Test
void aResultOfTheWrongJsonTypeFailsTheCall() {
SpectrumSqlException integerFromString = assertThrows(SpectrumSqlException.class,
() -> ExternalFunctionValues.fromJson("integer", JSON.textNode("1")));
assertThat(integerFromString.sqlState(), equalTo("22P02"));
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("integer",
JSON.numberNode(1.5)));
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("boolean",
JSON.textNode("true")));
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("varchar",
JSON.numberNode(1)));
}
@Test
void aResultOutsideTheRangeOfTheReturnTypeFailsTheCall() {
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("smallint",
JSON.numberNode(40_000)));
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("integer",
JSON.numberNode(3_000_000_000L)));
}
@Test
void aMalformedDateOrTimestampFailsTheCall() {
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("date",
JSON.textNode("yesterday")));
assertThrows(SpectrumSqlException.class, () -> ExternalFunctionValues.fromJson("timestamp",
JSON.textNode("noon")));
}
}
@@ -0,0 +1,580 @@
package io.floci.aws.services.redshift.spectrum;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.iam.RoleResourceAccessAuthorizer;
import io.floci.aws.services.iam.ServiceRoleAuthorizer;
import io.floci.aws.services.lambda.LambdaService;
import io.floci.aws.services.lambda.model.InvocationType;
import io.floci.aws.services.lambda.model.InvokeResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class LambdaUdfInvokerTest {
private static final String ACCOUNT = "000000000000";
private static final String ROLE = "arn:aws:iam::000000000000:role/udf";
private static final String FUNCTION_ARN = "arn:aws:lambda:us-east-1:000000000000:function:sum";
private static final SpectrumSession SESSION = new SpectrumSession(ACCOUNT, ACCOUNT + ":cluster-1", "dev",
List.of(), false);
private static final ObjectMapper JSON = new ObjectMapper();
private static final String REGION = "us-east-1";
private LambdaService lambda;
private ServiceRoleAuthorizer authorizer;
private RoleResourceAccessAuthorizer roleAccess;
private EmulatorConfig config;
private AtomicLong now;
private List<Long> sleeps;
private LambdaUdfInvoker invoker;
@BeforeEach
void setUp() {
lambda = mock(LambdaService.class);
authorizer = mock(ServiceRoleAuthorizer.class);
when(authorizer.refusal(anyString(), anyString(), anyString(), any(), anyList()))
.thenReturn(Optional.empty());
roleAccess = mock(RoleResourceAccessAuthorizer.class);
when(roleAccess.refusal(anyString(), anyString(), anyString(), any(), any())).thenReturn(Optional.empty());
config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.services().iam().enforcementEnabled()).thenReturn(true);
RegionResolver regions = mock(RegionResolver.class);
when(regions.getDefaultRegion()).thenReturn("us-east-1");
when(regions.partitionForRegion(anyString())).thenReturn("aws");
now = new AtomicLong(1_000);
sleeps = new ArrayList<>();
Clock clock = new Clock() {
@Override
public ZoneId getZone() {
return ZoneOffset.UTC;
}
@Override
public Clock withZone(ZoneId zone) {
return this;
}
@Override
public Instant instant() {
return Instant.ofEpochMilli(now.get());
}
};
invoker = new LambdaUdfInvoker(lambda, authorizer, roleAccess,
config, regions, clock, millis -> {
sleeps.add(millis);
now.addAndGet(millis);
});
}
private static ExternalFunctionBinding function(List<String> args, String returns, int maxRows, long maxBytes,
long retryMillis) {
return new ExternalFunctionBinding(ACCOUNT, SESSION.clusterKey(), "dev", "public", "exfunc_sum", args,
returns, "STABLE", "sum", ROLE, retryMillis, maxRows, maxBytes);
}
private static ExternalFunctionBinding twoInts() {
return function(List.of("integer", "integer"), "integer", Integer.MAX_VALUE, 5L * 1024 * 1024, 20_000);
}
private static InvokeResult ok(String body) {
return new InvokeResult(200, null, body.getBytes(StandardCharsets.UTF_8), null, "req");
}
private static List<List<String>> rows(String[]... rows) {
return Arrays.stream(rows).map(Arrays::asList).toList();
}
private JsonNode lastRequest() throws Exception {
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(lambda, atLeastOnce()).invokeArn(eq(FUNCTION_ARN), payload.capture(),
eq(InvocationType.RequestResponse));
return JSON.readTree(payload.getValue());
}
@Test
void sendsTheDocumentedRequestAndReadsTheResultsInOrder() throws Exception {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":3,\"results\":[3,null,10]}"));
List<String> results = invoker.invoke(twoInts(), SESSION, REGION, "analyst", 5678234L,
rows(new String[] {"1", "2"}, new String[] {"3", null}, new String[] {"4", "6"}));
assertThat(results, equalTo(Arrays.asList("3", null, "10")));
JsonNode request = lastRequest();
assertThat(request.get("cluster").textValue(),
equalTo("arn:aws:redshift:us-east-1:000000000000:cluster:cluster-1"));
assertThat(request.get("user").textValue(), equalTo("analyst"));
assertThat(request.get("database").textValue(), equalTo("dev"));
assertThat(request.get("external_function").textValue(), equalTo("public.exfunc_sum"));
assertThat(request.get("query_id").longValue(), equalTo(5678234L));
assertThat(request.get("num_records").intValue(), equalTo(3));
assertThat(request.get("request_id").textValue().length(), equalTo(36));
assertThat(request.get("arguments").toString(), equalTo("[[1,2],[3,null],[4,6]]"));
}
@Test
void callsABareFunctionNameAndNamesTheClusterInTheClustersOwnRegion() throws Exception {
String euFunctionArn = "arn:aws:lambda:eu-west-1:000000000000:function:sum";
when(lambda.invokeArn(eq(euFunctionArn), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[3]}"));
invoker.invoke(twoInts(), SESSION, "eu-west-1", "u", 1, rows(new String[] {"1", "2"}));
ArgumentCaptor<byte[]> payload = ArgumentCaptor.forClass(byte[].class);
verify(lambda).invokeArn(eq(euFunctionArn), payload.capture(), eq(InvocationType.RequestResponse));
assertThat(JSON.readTree(payload.getValue()).get("cluster").textValue(),
equalTo("arn:aws:redshift:eu-west-1:000000000000:cluster:cluster-1"));
}
@Test
void aFunctionOfOneArgumentGetsOneElementArrays() throws Exception {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":2,\"results\":[\"A\",null]}"));
List<String> results = invoker.invoke(
function(List.of("character varying"), "varchar", 100, 1024, 0), SESSION, REGION, "u", 1,
rows(new String[] {"a"}, new String[] {null}));
assertThat(results, equalTo(Arrays.asList("A", null)));
assertThat(lastRequest().get("arguments").toString(), equalTo("[[\"a\"],[null]]"));
}
@Test
void aJsonEncodedStringResponseIsAccepted() throws Exception {
String encoded = JSON.writeValueAsString("{\"success\":true,\"num_records\":2,\"results\":[3,10]}");
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(ok(encoded));
List<String> results = invoker.invoke(twoInts(), SESSION, REGION, "u", 1,
rows(new String[] {"1", "2"}, new String[] {"4", "6"}));
assertThat(results, equalTo(List.of("3", "10")));
}
@Test
void splitsRowsIntoBatchesOfAtMostMaxBatchRows() throws Exception {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":2,\"results\":[1,2]}"),
ok("{\"success\":true,\"num_records\":1,\"results\":[3]}"));
List<String> results = invoker.invoke(
function(List.of("integer"), "integer", 2, 5L * 1024 * 1024, 0), SESSION, REGION, "u", 1,
rows(new String[] {"1"}, new String[] {"2"}, new String[] {"3"}));
assertThat(results, equalTo(List.of("1", "2", "3")));
ArgumentCaptor<byte[]> payloads = ArgumentCaptor.forClass(byte[].class);
verify(lambda, times(2)).invokeArn(eq(FUNCTION_ARN), payloads.capture(), any());
assertThat(JSON.readTree(payloads.getAllValues().get(0)).get("arguments").toString(), equalTo("[[1],[2]]"));
assertThat(JSON.readTree(payloads.getAllValues().get(1)).get("arguments").toString(), equalTo("[[3]]"));
}
@Test
void splitsRowsIntoBatchesByTheirSizeAndSendsAnOversizedRowAlone() throws Exception {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenAnswer(call -> {
JsonNode request = JSON.readTree((byte[]) call.getArgument(1));
int count = request.get("num_records").intValue();
return ok("{\"success\":true,\"num_records\":" + count + ",\"results\":["
+ String.join(",", Collections.nCopies(count, "0")) + "]}");
});
// the first row alone is over the size limit; the two small ones fit together in the next batch
invoker.invoke(function(List.of("varchar"), "integer", 100, 1024, 0), SESSION, REGION, "u", 1,
rows(new String[] {"x".repeat(2000)}, new String[] {"y"}, new String[] {"z"}));
ArgumentCaptor<byte[]> payloads = ArgumentCaptor.forClass(byte[].class);
verify(lambda, times(2)).invokeArn(eq(FUNCTION_ARN), payloads.capture(), any());
assertThat(JSON.readTree(payloads.getAllValues().get(0)).get("num_records").intValue(), equalTo(1));
assertThat(JSON.readTree(payloads.getAllValues().get(1)).get("arguments").toString(),
equalTo("[[\"y\"],[\"z\"]]"));
}
@Test
void batchesCountArgumentsArrayBracketsAndCommasAgainstMaxBatchSize() throws Exception {
long limit = 20;
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenAnswer(call -> {
JsonNode request = JSON.readTree((byte[]) call.getArgument(1));
int count = request.get("num_records").intValue();
return ok("{\"success\":true,\"num_records\":" + count + ",\"results\":["
+ String.join(",", Collections.nCopies(count, "0")) + "]}");
});
// each row is "[1]" (3 bytes); 4 rows serialize to "[[1],[1],[1],[1]]" (17 bytes <= 20), 5 rows would be 21
List<List<String>> input = Collections.nCopies(10, List.of("1"));
invoker.invoke(function(List.of("integer"), "integer", 100, limit, 0), SESSION, REGION, "u", 1, input);
ArgumentCaptor<byte[]> payloads = ArgumentCaptor.forClass(byte[].class);
verify(lambda, times(3)).invokeArn(eq(FUNCTION_ARN), payloads.capture(), any());
for (byte[] payload : payloads.getAllValues()) {
String argumentsJson = JSON.readTree(payload).get("arguments").toString();
assertThat((long) argumentsJson.getBytes(StandardCharsets.UTF_8).length, lessThanOrEqualTo(limit));
}
}
@Test
void anArnFunctionIsInvokedAsGiven() {
String arn = "arn:aws:lambda:us-east-1:000000000000:function:other:7";
ExternalFunctionBinding function = new ExternalFunctionBinding(ACCOUNT, SESSION.clusterKey(), "dev",
"public", "f", List.of("integer"), "integer", "STABLE", arn, ROLE, 0, 10, 1024);
when(lambda.invokeArn(eq(arn), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[5]}"));
assertThat(invoker.invoke(function, SESSION, REGION, "u", 1, rows(new String[] {"1"})), equalTo(List.of("5")));
}
@Test
void noRowsMeansNoCall() {
assertThat(invoker.invoke(twoInts(), SESSION, REGION, "u", 1, List.of()), hasSize(0));
verify(lambda, never()).invokeArn(anyString(), any(), any());
}
@Test
void aRoleThatMayNotInvokeTheFunctionIsRefusedBeforeAnyCall() {
when(authorizer.refusal(eq(ROLE), eq(ACCOUNT), anyString(), isNull(), anyList()))
.thenReturn(Optional.of("The role does not allow lambda:InvokeFunction."));
SpectrumSqlException refused = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(refused.sqlState(), equalTo("42501"));
verify(lambda, never()).invokeArn(anyString(), any(), any());
}
@Test
void successFalseSurfacesTheErrorMessage() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":false,\"error_msg\":\"my function is not working\"}"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("my function is not working"));
}
@Test
void aFunctionErrorSurfacesTheRuntimeMessage() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(new InvokeResult(200, "Unhandled",
"{\"errorMessage\":\"boom\",\"errorType\":\"Error\"}".getBytes(StandardCharsets.UTF_8), null, "r"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("boom"));
}
@Test
void aTimedOutInvocationFailsTheCall() {
InvokeResult timedOut = ok("");
timedOut.setTimedOut(true);
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(timedOut);
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("timed out"));
}
@Test
void aResponseWithTheWrongNumberOfResultsFailsTheCall() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[1]}"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1,
rows(new String[] {"1", "2"}, new String[] {"3", "4"})));
assertThat(failed.getMessage(), containsString("returned 1 results"));
}
@Test
void aNumRecordsThatDisagreesWithTheRowsFailsEvenWhenTheResultsFit() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":5,\"results\":[1]}"),
ok("{\"success\":true,\"results\":[1]}"),
ok("{\"success\":true,\"num_records\":\"1\",\"results\":[1]}"));
for (int attempt = 0; attempt < 3; attempt++) {
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
}
}
@Test
void successMustBeAJsonBoolean() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":\"true\",\"num_records\":1,\"results\":[1]}"),
ok("{\"success\":1,\"num_records\":1,\"results\":[1]}"));
for (int attempt = 0; attempt < 2; attempt++) {
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
}
}
@Test
void successFalseWithoutAnErrorMessageStillFails() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(ok("{\"success\":false}"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("no error_msg"));
}
@Test
void aResponseThatIsNotJsonOrHasNoResultsFailsTheCall() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(ok("not json"),
ok("{\"success\":true,\"num_records\":1}"));
SpectrumSqlException notJson = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
SpectrumSqlException noResults = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(notJson.getMessage(), containsString("not JSON"));
assertThat(noResults.getMessage(), containsString("no results array"));
}
@Test
void aResponseThatIsNotAJsonObjectFailsTheCall() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(ok("[]"), ok(""));
for (int attempt = 0; attempt < 2; attempt++) {
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
}
}
@Test
void aFunctionErrorWithAnEmptyPayloadReportsTheErrorType() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any()))
.thenReturn(new InvokeResult(200, "Unhandled", new byte[0], null, "r"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("Unhandled"));
}
@Test
void aNumericResultKeepsEveryDigitThroughTheJsonParser() {
ExternalFunctionBinding function = function(List.of("integer"), "numeric(30,10)", 10, 1024, 0);
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(ok(
"{\"success\":true,\"num_records\":2,\"results\":[12345678901234567890.1234567890,3.10]}"));
List<String> results = invoker.invoke(function, SESSION, REGION, "u", 1,
rows(new String[] {"1"}, new String[] {"2"}));
assertThat(results, equalTo(List.of("12345678901234567890.1234567890", "3.10")));
}
@Test
void aRowWithTheWrongNumberOfValuesFailsBeforeAnyCall() {
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1"})));
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2", "3"})));
verify(lambda, never()).invokeArn(anyString(), any(), any());
}
@Test
void anArgumentThatIsNotAValueOfItsTypeFailsBeforeAnyCall() {
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"abc", "2"})));
assertThat(failed.sqlState(), equalTo("22P02"));
verify(lambda, never()).invokeArn(anyString(), any(), any());
}
@Test
void aFunctionWithoutArgumentsGetsEmptyRecords() throws Exception {
ExternalFunctionBinding function = function(List.of(), "integer", 10, 1024, 0);
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":2,\"results\":[1,1]}"));
invoker.invoke(function, SESSION, REGION, "u", 1, rows(new String[] {}, new String[] {}));
assertThat(lastRequest().get("arguments").toString(), equalTo("[[],[]]"));
}
@Test
void aFunctionGivenAsAccountFunctionNameIsInvokedInThatAccount() {
String arn = "arn:aws:lambda:us-east-1:111111111111:function:sum";
ExternalFunctionBinding function = new ExternalFunctionBinding(ACCOUNT, SESSION.clusterKey(), "dev",
"public", "f", List.of("integer"), "integer", "STABLE", "111111111111:function:sum", ROLE, 0, 10,
1024);
when(lambda.invokeArn(eq(arn), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[5]}"));
assertThat(invoker.invoke(function, SESSION, REGION, "u", 1, rows(new String[] {"1"})), equalTo(List.of("5")));
verify(authorizer).refusal(eq(ROLE), eq(ACCOUNT), anyString(), isNull(), anyList());
verify(roleAccess).refusal(eq(ROLE), eq("lambda:InvokeFunction"), eq(arn), eq("us-east-1"), any());
}
@Test
void aRoleTheFunctionsAccessPoliciesRefuseIsRefusedBeforeAnyCall() {
when(roleAccess.refusal(eq(ROLE), eq("lambda:InvokeFunction"), eq(FUNCTION_ARN), anyString(), any()))
.thenReturn(Optional.of("not authorized to perform: lambda:InvokeFunction"));
SpectrumSqlException refused = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(refused.sqlState(), equalTo("42501"));
assertThat(refused.getMessage(), containsString("not authorized to perform: lambda:InvokeFunction"));
verify(lambda, never()).invokeArn(anyString(), any(), any());
}
@Test
void theFunctionsAccessPoliciesAreNotConsultedWhenEnforcementIsOff() {
when(config.services().iam().enforcementEnabled()).thenReturn(false);
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[5]}"));
assertThat(invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})),
equalTo(List.of("5")));
verify(roleAccess, never()).refusal(anyString(), anyString(), anyString(), any(), any());
}
@Test
void aFunctionNameWithAQualifierIsAnArnOfThatVersion() {
String arn = "arn:aws:lambda:us-east-1:000000000000:function:sum:7";
ExternalFunctionBinding function = new ExternalFunctionBinding(ACCOUNT, SESSION.clusterKey(), "dev",
"public", "f", List.of("integer"), "integer", "STABLE", "sum:7", ROLE, 0, 10, 1024);
when(lambda.invokeArn(eq(arn), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[5]}"));
assertThat(invoker.invoke(function, SESSION, REGION, "u", 1, rows(new String[] {"1"})), equalTo(List.of("5")));
}
@Test
void anArnThatIsNotALambdaFunctionFailsBeforeAnyCall() {
for (String reference : List.of("arn:foo", "arn:aws:sqs:us-east-1:000000000000:queue")) {
ExternalFunctionBinding function = new ExternalFunctionBinding(ACCOUNT, SESSION.clusterKey(), "dev",
"public", "f", List.of("integer"), "integer", "STABLE", reference, ROLE, 0, 10, 1024);
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(function, SESSION, REGION, "u", 1, rows(new String[] {"1"})));
}
verify(lambda, never()).invokeArn(anyString(), any(), any());
}
@Test
void anUnexpectedFailureOfTheLambdaServiceIsAnSqlError() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenThrow(new IllegalStateException("executor down"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("executor down"));
}
@Test
void aHugeRetryTimeoutDoesNotOverflowIntoNoRetry() {
AwsException throttled = new AwsException("TooManyRequestsException", "Rate Exceeded.", 429);
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenThrow(throttled).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[3]}"));
List<String> results = invoker.invoke(
function(List.of("integer"), "integer", 10, 1024, Long.MAX_VALUE), SESSION, REGION, "u", 1,
rows(new String[] {"1"}));
assertThat(results, equalTo(List.of("3")));
}
@Test
void aMistypedResultFailsTheCall() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[\"three\"]}"));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.sqlState(), equalTo("22P02"));
}
@Test
void aThrottledCallIsRetriedWithGrowingPausesUntilItSucceeds() {
AwsException throttled = new AwsException("TooManyRequestsException", "Rate Exceeded.", 429);
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any())).thenThrow(throttled, throttled).thenReturn(
ok("{\"success\":true,\"num_records\":1,\"results\":[3]}"));
List<String> results = invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"}));
assertThat(results, equalTo(List.of("3")));
assertThat(sleeps, equalTo(List.of(50L, 100L)));
}
@Test
void aCallThatStaysThrottledFailsOnceTheRetryTimeoutIsSpent() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any()))
.thenThrow(new AwsException("TooManyRequestsException", "Rate Exceeded.", 429));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(function(List.of("integer"), "integer", 10, 1024, 300), SESSION, REGION, "u", 1,
rows(new String[] {"1"})));
assertThat(failed.getMessage(), containsString("throttled"));
// the last pause is cut to what is left of the 300 ms, and one more attempt follows it
assertThat(sleeps, equalTo(List.of(50L, 100L, 150L)));
verify(lambda, times(4)).invokeArn(anyString(), any(), any());
}
@Test
void aThrottledCallIsNotRetriedWithAZeroRetryTimeout() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any()))
.thenThrow(new AwsException("TooManyRequestsException", "Rate Exceeded.", 429));
assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(function(List.of("integer"), "integer", 10, 1024, 0), SESSION, REGION, "u", 1,
rows(new String[] {"1"})));
assertThat(sleeps, hasSize(0));
}
@Test
void anotherLambdaErrorFailsTheCallWithoutRetry() {
when(lambda.invokeArn(eq(FUNCTION_ARN), any(), any()))
.thenThrow(new AwsException("ResourceNotFoundException", "Function not found", 404));
SpectrumSqlException failed = assertThrows(SpectrumSqlException.class,
() -> invoker.invoke(twoInts(), SESSION, REGION, "u", 1, rows(new String[] {"1", "2"})));
assertThat(failed.getMessage(), containsString("Function not found"));
assertThat(sleeps, hasSize(0));
verify(lambda, times(1)).invokeArn(anyString(), any(), any());
}
}