Skip to content

Commit

Permalink
Merge pull request #8 from danopia/patch-1
Browse files Browse the repository at this point in the history
Read string value for Statement Action and Resource as a 1-length array
  • Loading branch information
tdpauw authored Jan 23, 2022
2 parents fd77961 + fb9152e commit 8621a10
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ Supports different principals.
## Limitations
The library only supports the canonical form of IAM Policy JSON documents, i.e.
everywhere a string or an array can be passed, an array is expected.
The library now supports a single string as `Action` and `Resource` value, though it does not yet support this for `Principal` and `Condition` key values.
For `Principal` and `Condition` it still expects the canonical form of an IAM Policy JSON document, i.e. everywhere a string or an array can be passed, an array is expected.
```json
{
Expand All @@ -154,8 +155,8 @@ everywhere a string or an array can be passed, an array is expected.
"Principal": {
"AWS": ["arn:aws:iam::123456789012:user/user-name"]
},
"Action": ["ec2:Describe*"],
"Resource": ["*"],
"Action": "ec2:Describe*",
"Resource": "*",
"Condition": {
"StringEquals": {
"kms:CallerAccount": ["123456789012"]
Expand Down
9 changes: 6 additions & 3 deletions src/statement/deserialiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ export class StatementJSONDeserialiser {
conditions: ConditionJSONDeserialiser.fromJSON(obj.Condition),
});

function parseArray(obj: any): [] {
function parseArray(obj: any): string[] {
if (obj === undefined) {
return [];
}
if (typeof obj === 'string') {
return [obj];
}
if (Array.isArray(obj)) {
if (isArrayOfStrings(obj)) {
return obj as [];
return obj;
}
throw new Error('Unsupported type: expecting an array of strings');
}
throw new Error('Unsupported type: expecting an array');
throw new Error('Unsupported type: expecting an array or a string');
}

function isArrayOfStrings(obj: any[]) {
Expand Down
26 changes: 21 additions & 5 deletions tests/statement/deserialiser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ describe('#StatementDeserialiser', function() {
};
it('should throw an Error', function() {
expect(() => StatementJSONDeserialiser.fromJSON(json)).to.throw(Error)
.with.property('message', 'Unsupported type: expecting an array');
.with.property('message', 'Unsupported type: expecting an array or a string');
});
});

describe('and its value is a string', function() {
const json = {
Action: 'action',
};
it('should throw an Error', function() {
expect(() => StatementJSONDeserialiser.fromJSON(json)).to.throw(Error)
.with.property('message', 'Unsupported type: expecting an array');
it('should return a Statement with actions', function() {
const actual = StatementJSONDeserialiser.fromJSON(json);
const expected = new Statement({
actions: ['action'],
});
expect(actual).to.deep.equal(expected);
});
});

Expand Down Expand Up @@ -60,7 +63,20 @@ describe('#StatementDeserialiser', function() {
};
it('should throw an Error', function() {
expect(() => StatementJSONDeserialiser.fromJSON(json)).to.throw(Error)
.with.property('message', 'Unsupported type: expecting an array');
.with.property('message', 'Unsupported type: expecting an array or a string');
});
});

describe('and its value is a string', function() {
const json = {
Resource: 'resource',
};
it('should return a Statement with resources', function() {
const actual = StatementJSONDeserialiser.fromJSON(json);
const expected = new Statement({
resources: ['resource'],
});
expect(actual).to.deep.equal(expected);
});
});
});
Expand Down

0 comments on commit 8621a10

Please sign in to comment.