Updated multilang daemon to validate streamArn based on pattern rather than individual section

This commit is contained in:
Ryan Pelaez 2023-06-19 15:43:39 -07:00
parent 9bf6aef453
commit f7c30c5e54
3 changed files with 19 additions and 23 deletions

View file

@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
import software.amazon.awssdk.arns.Arn; import software.amazon.awssdk.arns.Arn;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.regions.Region;
import software.amazon.kinesis.common.StreamIdentifier;
/** /**
* KinesisClientLibConfigurator constructs a KinesisClientLibConfiguration from java properties file. The following * KinesisClientLibConfigurator constructs a KinesisClientLibConfiguration from java properties file. The following
@ -73,17 +74,7 @@ public class KinesisClientLibConfigurator {
if (configuration.getStreamArn() != null && !configuration.getStreamArn().trim().isEmpty()) { if (configuration.getStreamArn() != null && !configuration.getStreamArn().trim().isEmpty()) {
final Arn streamArnObj = Arn.fromString(configuration.getStreamArn()); final Arn streamArnObj = Arn.fromString(configuration.getStreamArn());
StreamIdentifier.validateArn(streamArnObj);
final String resourceType = streamArnObj.resource().resourceType().get();
if (!"stream".equalsIgnoreCase(resourceType)) {
throw new IllegalArgumentException(String.format("StreamArn has unsupported resource type of '%s'. Expected: stream",
resourceType));
}
final String arnService = streamArnObj.service();
if (!"kinesis".equalsIgnoreCase(arnService)) {
throw new IllegalArgumentException(String.format("StreamArn has unsupported service type of '%s'. Expected: kinesis",
arnService));
}
//Parse out the stream Name from the Arn (and/or override existing value for Stream Name) //Parse out the stream Name from the Arn (and/or override existing value for Stream Name)
final String streamNameFromArn = streamArnObj.resource().resource(); final String streamNameFromArn = streamArnObj.resource().resource();
configuration.setStreamName(streamNameFromArn); configuration.setStreamName(streamNameFromArn);

View file

@ -42,7 +42,7 @@ public class MultiLangDaemonConfigTest {
private static final String TEST_STREAM_NAME = "fakeStream"; private static final String TEST_STREAM_NAME = "fakeStream";
private static final String TEST_STREAM_NAME_IN_ARN = "FAKE_STREAM_NAME"; private static final String TEST_STREAM_NAME_IN_ARN = "FAKE_STREAM_NAME";
private static final String TEST_REGION = "us-east-1"; private static final String TEST_REGION = "us-east-1";
private static final String TEST_STREAM_ARN = "arn:aws:kinesis:us-east-2:ACCOUNT_ID:stream/" + TEST_STREAM_NAME_IN_ARN; private static final String TEST_STREAM_ARN = "arn:aws:kinesis:us-east-2:012345678987:stream/" + TEST_STREAM_NAME_IN_ARN;
@Mock @Mock
ClassLoader classLoader; ClassLoader classLoader;
@ -97,18 +97,13 @@ public class MultiLangDaemonConfigTest {
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testConstructorFailsBecauseStreamArnHasInvalidRegion() throws Exception { public void testConstructorFailsBecauseStreamArnIsInvalid2() throws Exception {
setup("", "arn:aws:kinesis:us-east-1:ACCOUNT_ID:stream/streamName", "us-east-1000"); setup("", "arn:aws:kinesis:us-east-2:ACCOUNT_ID:BadFormatting:stream/" + TEST_STREAM_NAME_IN_ARN, TEST_REGION);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testConstructorFailsBecauseStreamArnHasInvalidResourceType() throws Exception { public void testConstructorFailsBecauseInvalidRegion() throws Exception {
setup("", "arn:aws:kinesis:us-EAST-1:ACCOUNT_ID:dynamodb/streamName", TEST_REGION); setup("", TEST_STREAM_ARN, "us-east-1000");
}
@Test(expected = IllegalArgumentException.class)
public void testConstructorFailsBecauseStreamArnHasInvalidService() throws Exception {
setup("", "arn:aws:kinesisFakeService:us-east-1:ACCOUNT_ID:stream/streamName", TEST_REGION);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)

View file

@ -167,12 +167,22 @@ public class StreamIdentifier {
.build(); .build();
} }
private static void validateArn(Arn streamArn) { /**
* Verify the streamArn follows the appropriate formatting.
* Throw an exception if it does not.
* @param streamArn
*/
public static void validateArn(Arn streamArn) {
if (!STREAM_ARN_PATTERN.matcher(streamArn.toString()).matches() || !streamArn.region().isPresent()) { if (!STREAM_ARN_PATTERN.matcher(streamArn.toString()).matches() || !streamArn.region().isPresent()) {
throw new IllegalArgumentException("Unable to create a StreamIdentifier from " + streamArn); throw new IllegalArgumentException("Invalid streamArn " + streamArn);
} }
} }
/**
* Verify creationEpoch is greater than 0.
* Throw an exception if it is not.
* @param creationEpoch
*/
private static void validateCreationEpoch(long creationEpoch) { private static void validateCreationEpoch(long creationEpoch) {
if (creationEpoch <= 0) { if (creationEpoch <= 0) {
throw new IllegalArgumentException( throw new IllegalArgumentException(