Skip to content

Commit 5f4879f

Browse files
committed
Remove support for unquoted accountIds with leading zeros (IAM rejects these)
1 parent 72a81bf commit 5f4879f

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

services-custom/iam-policy-builder/src/main/java/software/amazon/awssdk/policybuilder/iam/internal/DefaultIamPolicyReader.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
import software.amazon.awssdk.policybuilder.iam.IamStatement;
3333
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
3434
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
35-
import software.amazon.awssdk.thirdparty.jackson.core.JsonFactory;
36-
import software.amazon.awssdk.thirdparty.jackson.core.StreamReadFeature;
37-
import software.amazon.awssdk.thirdparty.jackson.core.json.JsonReadFeature;
3835
import software.amazon.awssdk.utils.Validate;
3936

4037
/**
@@ -44,16 +41,7 @@
4441
*/
4542
@SdkInternalApi
4643
public final class DefaultIamPolicyReader implements IamPolicyReader {
47-
private static final JsonNodeParser JSON_NODE_PARSER = JsonNodeParser
48-
.builder()
49-
.jsonFactory(JsonFactory
50-
.builder()
51-
.enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
52-
.configure(JsonReadFeature.ALLOW_JAVA_COMMENTS, true)
53-
// required to handle integer accountIDs with leading zeros
54-
.configure(JsonReadFeature.ALLOW_LEADING_ZEROS_FOR_NUMBERS, true)
55-
.build())
56-
.build();
44+
private static final JsonNodeParser JSON_NODE_PARSER = JsonNodeParser.create();
5745

5846
@Override
5947
public IamPolicy read(String policy) {
@@ -225,10 +213,7 @@ private String expectString(JsonNode node, String name) {
225213

226214
private String expectStringOrAccountId(JsonNode node, String name) {
227215
if (node.isNumber()) {
228-
// treat numbers like accountIDs and return a zero padded 12 digit string
229-
if (node.asNumber().length() <= 12) {
230-
return String.format("%012d", Long.parseLong(node.asNumber()));
231-
}
216+
return node.asNumber();
232217
}
233218
Validate.isTrue(node.isString(), "%s was not a string", name);
234219
return node.asString();
@@ -237,10 +222,7 @@ private String expectStringOrAccountId(JsonNode node, String name) {
237222
// condition values are generally String, however in some cases they may be an AWS accountID or a boolean.
238223
private String expectConditionValue(JsonNode node, String name) {
239224
if (node.isNumber()) {
240-
// treat numbers like accountIDs and return a zero padded 12 digit string
241-
if (node.asNumber().length() <= 12) {
242-
return String.format("%012d", Long.parseLong(node.asNumber()));
243-
}
225+
return node.asNumber();
244226
}
245227
if (node.isBoolean()) {
246228
return Boolean.toString(node.asBoolean());

services-custom/iam-policy-builder/src/test/java/software/amazon/awssdk/policybuilder/iam/IamPolicyReaderTest.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import static java.util.Arrays.asList;
1919
import static java.util.Collections.singletonList;
2020
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2123
import static software.amazon.awssdk.policybuilder.iam.IamEffect.ALLOW;
2224

25+
import java.io.UncheckedIOException;
2326
import org.junit.jupiter.api.Test;
2427

2528
class IamPolicyReaderTest {
@@ -97,9 +100,9 @@ class IamPolicyReaderTest {
97100
.builder()
98101
.effect("Allow")
99102
.principals(
100-
IamPrincipal.createAll("AWS", asList("001234567890", "555555555555")))
103+
IamPrincipal.createAll("AWS", asList("123456789012", "555555555555")))
101104
.addCondition(
102-
IamCondition.create("StringNotEquals", "aws:PrincipalAccount", "001234567890"))
105+
IamCondition.create("StringNotEquals", "aws:PrincipalAccount", "123456789012"))
103106
.build()
104107
))
105108
.build();
@@ -210,21 +213,44 @@ public void readPolicyWithIntegerAccountsWorks() {
210213
+ " \"Effect\" : \"Allow\",\n"
211214
+ " \"Principal\" : { \n"
212215
+ " \"AWS\": [ \n"
213-
+ " 001234567890,\n"
216+
+ " 123456789012,\n"
214217
+ " \"555555555555\" \n"
215218
+ " ]\n"
216219
+ " },\n"
217220
+ " \"Condition\" : {\n"
218221
+ " \"StringNotEquals\": {\n"
219222
+ " \"aws:PrincipalAccount\": [\n"
220-
+ " 001234567890\n"
223+
+ " 123456789012\n"
221224
+ " ]\n"
222225
+ " }\n"
223226
+ " }\n"
224227
+ " }\n"
225228
+ "}")).isEqualTo(INTEGER_ACCOUNT_ID_POLICY);
226229
}
227230

231+
@Test
232+
public void readPolicyWithLeadingZeroIntegerFails() {
233+
// while accountIds may have leading zeros, IAM rejects policy documents with
234+
// unquoted accountIDs with leading zeros because these are invalid json.
235+
Exception e = assertThrows(Exception.class, () ->
236+
{
237+
READER.read("{\n"
238+
+ " \"Version\" : \"Version\",\n"
239+
+ " \"Statement\" : {\n"
240+
+ " \"Effect\" : \"Allow\",\n"
241+
+ " \"Condition\" : {\n"
242+
+ " \"StringNotEquals\": {\n"
243+
+ " \"aws:PrincipalAccount\": [\n"
244+
+ " 001234567890\n"
245+
+ " ]\n"
246+
+ " }\n"
247+
+ " }\n"
248+
+ " }\n"
249+
+ "}");
250+
});
251+
assertThat(e.getMessage()).contains("Invalid numeric value: Leading zeroes not allowed");
252+
}
253+
228254
@Test
229255
public void readPolicyWithBooleanConditionWorks() {
230256
assertThat(READER.read("{\n"

0 commit comments

Comments
 (0)