Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iotevents): add grant method to Input class #18617

Merged
merged 18 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-iotevents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ new iotevents.DetectorModel(this, 'MyDetectorModel', {
initialState: onlineState,
});
```

For grant the permittion to put message to the input, you can use
`grantPutMessage()` as following;
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved

```ts
declare const principal: iam.AnyPrincipal;
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
input.grantPutMessage(principal);
```
77 changes: 73 additions & 4 deletions packages/@aws-cdk/aws-iotevents/lib/input.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as iam from '@aws-cdk/aws-iam';
import { Resource, IResource } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnInput } from './iotevents.generated';
Expand All @@ -11,8 +12,66 @@ export interface IInput extends IResource {
* @attribute
*/
readonly inputName: string;

/**
* The ARN of the input
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The ARN of the input
* The ARN of the input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fix all same case in aws-iotevents 👍🏻

* @attribute
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly inputArn: string;

/**
* Grant the indicated permissions on this input to the given IAM principal (Role/Group/User).
*
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "iotevents:BatchPutMessage")
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
*/
grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant

/**
* Grant the putting message permission to the given IAM principal (Role/Group/User).
*
* @param grantee The principal (no-op if undefined)
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
*/
grantPutMessage(grantee: iam.IGrantable): iam.Grant
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
}


yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
abstract class InputBase extends Resource implements IInput {
/**
* @attribute
*/
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
public abstract readonly inputName: string;
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
/**
* @attribute
*/
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
public abstract readonly inputArn: string;

/**
* Grant the indicated permissions on this input to the given IAM principal (Role/Group/User).
*
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "iotevents:BatchPutMessage")
*/
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
public grant(grantee: iam.IGrantable, ...actions: string[]) {
return iam.Grant.addToPrincipal({
grantee,
actions,
resourceArns: [this.inputArn],
scope: this,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Why are we passing this argument here? I don't think it's required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I copied from DynamoDB Table grant. I removed and confirm the test is passed.

});
}

/**
* Grant the putting message permission to the given IAM principal (Role/Group/User).
*
* @param grantee The principal (no-op if undefined)
*/
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
public grantPutMessage(grantee: iam.IGrantable) {
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
return this.grant(grantee, 'iotevents:BatchPutMessage');
}
}


/**
* Properties for defining an AWS IoT Events input
*/
Expand All @@ -37,18 +96,23 @@ export interface InputProps {
/**
* Defines an AWS IoT Events input in this stack.
*/
export class Input extends Resource implements IInput {
export class Input extends InputBase {
/**
* Import an existing input
*/
public static fromInputName(scope: Construct, id: string, inputName: string): IInput {
class Import extends Resource implements IInput {
return new class Import extends InputBase {
public readonly inputName = inputName;
}
return new Import(scope, id);
public readonly inputArn = this.stack.formatArn({
service: 'iotevents',
resource: 'input',
resourceName: inputName,
});
}(scope, id);
}

public readonly inputName: string;
public readonly inputArn: string;

constructor(scope: Construct, id: string, props: InputProps) {
super(scope, id, {
Expand All @@ -67,5 +131,10 @@ export class Input extends Resource implements IInput {
});

this.inputName = this.getResourceNameAttribute(resource.ref);
this.inputArn = this.stack.formatArn({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this should use the getResourceArnAttribute() helper, similarly like inputName uses getResourceNameAttribute().

See here on how to use that helper.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can 39a1573 (#18617) address this comment? Or using this.stack.formatArn() is better instead of arnForInput()?

service: 'iotevents',
resource: 'input',
resourceName: this.inputName,
});
}
}
88 changes: 79 additions & 9 deletions packages/@aws-cdk/aws-iotevents/test/input.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Template } from '@aws-cdk/assertions';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as iotevents from '../lib';

test('Default property', () => {
const stack = new cdk.Stack();
let stack: cdk.Stack;
beforeEach(() => {
stack = new cdk.Stack();
});

test('Default property', () => {
// WHEN
new iotevents.Input(stack, 'MyInput', {
attributeJsonPaths: ['payload.temperature'],
Expand All @@ -19,7 +23,6 @@ test('Default property', () => {
});

test('can get input name', () => {
const stack = new cdk.Stack();
// GIVEN
const input = new iotevents.Input(stack, 'MyInput', {
attributeJsonPaths: ['payload.temperature'],
Expand All @@ -39,9 +42,41 @@ test('can get input name', () => {
});
});

test('can set physical name', () => {
const stack = new cdk.Stack();
test('can get input ARN', () => {
skinny85 marked this conversation as resolved.
Show resolved Hide resolved
// GIVEN
const input = new iotevents.Input(stack, 'MyInput', {
attributeJsonPaths: ['payload.temperature'],
});

// WHEN
new cdk.CfnResource(stack, 'Res', {
type: 'Test::Resource',
properties: {
InputArn: input.inputArn,
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('Test::Resource', {
InputArn: {
'Fn::Join': [
'',
[
'arn:',
{ Ref: 'AWS::Partition' },
':iotevents:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':input/',
{ Ref: 'MyInput08947B23' },
],
],
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
},
});
});

test('can set physical name', () => {
// WHEN
new iotevents.Input(stack, 'MyInput', {
inputName: 'test_input',
Expand All @@ -55,8 +90,6 @@ test('can set physical name', () => {
});

test('can import a Input by inputName', () => {
const stack = new cdk.Stack();

// WHEN
const inputName = 'test-input-name';
const topicRule = iotevents.Input.fromInputName(stack, 'InputFromInputName', inputName);
Expand All @@ -68,11 +101,48 @@ test('can import a Input by inputName', () => {
});

test('cannot be created with an empty array of attributeJsonPaths', () => {
const stack = new cdk.Stack();

expect(() => {
new iotevents.Input(stack, 'MyInput', {
attributeJsonPaths: [],
});
}).toThrow('attributeJsonPaths property cannot be empty');
});


test('can grant the permission to put message', () => {
const role = iam.Role.fromRoleArn(stack, 'MyRole', 'arn:aws:iam::account-id:role/role-name');
const input = new iotevents.Input(stack, 'MyInput', {
inputName: 'test_input',
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
attributeJsonPaths: ['payload.temperature'],
});

// WHEN
input.grantPutMessage(role);

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
skinny85 marked this conversation as resolved.
Show resolved Hide resolved
PolicyDocument: {
Statement: [
{
Action: 'iotevents:BatchPutMessage',
Effect: 'Allow',
Resource: {
'Fn::Join': [
'',
[
'arn:',
{ Ref: 'AWS::Partition' },
':iotevents:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':input/',
{ Ref: 'MyInput08947B23' },
],
],
yamatatsu marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
},
});
});