Skip to content

Commit

Permalink
Add grant helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Hoermann committed Jul 19, 2020
1 parent 4399cd9 commit cc9b1bb
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-elasticsearch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ Helper methods also exist for managing access to the domain.

```ts
const lambda = new lambda.Function(this, 'Lambda', { /* ... */ });
// Grant the lambda functiomn read and write access to app-search index
domain.grantReadWriteForIndex(lambda, 'app-search');
// Grant the lambda functiomn read access to app-search index
domain.grantIndex(lambda, 'app-search', 'es:HttpGet');
```

### Encryption
Expand Down
85 changes: 85 additions & 0 deletions packages/@aws-cdk/aws-elasticsearch/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,35 @@ export interface IDomain extends cdk.IResource {
*/
readonly domainEndpoint: string;

/**
* Adds an IAM policy statement associated with this domain to an IAM
* principal's policy.
*
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "es:HttpGet", "es:HttpPut", ...)
*/
grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant;

/**
* Adds an IAM policy statement associated with an index in this domain to an IAM
* principal's policy.
*
* @param index The index to grant permissions for
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "es:HttpGet", "es:HttpPut", ...)
*/
grantIndex(index: string, grantee: iam.IGrantable, ...actions: string[]): iam.Grant;

/**
* Adds an IAM policy statement associated with a path in this domain to an IAM
* principal's policy.
*
* @param path The path to grant permissions for
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "es:HttpGet", "es:HttpPut", ...)
*/
grantPath(path: string, grantee: iam.IGrantable, ...actions: string[]): iam.Grant;

/**
* Return the given named metric for this Domain.
*/
Expand Down Expand Up @@ -592,6 +621,62 @@ export class Domain extends cdk.Resource implements IDomain {
this.domainEndpoint = this.domain.getAtt('DomainEndpoint').toString();
}

/**
* Adds an IAM policy statement associated with this domain to an IAM
* principal's policy.
*
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "es:HttpGet", "es:HttpPut", ...)
*/
public grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions,
resourceArns: [
this.domainArn,
`${this.domainArn}/*`,
],
scope: this,
});
}

/**
* Adds an IAM policy statement associated with an index in this domain to an IAM
* principal's policy.
*
* @param index The index to grant permissions for
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "es:HttpGet", "es:HttpPut", ...)
*/
public grantIndex(index: string, grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions,
resourceArns: [
`${this.domainArn}/${index}`,
`${this.domainArn}/${index}/*`,
],
scope: this,
});
}

/**
* Adds an IAM policy statement associated with a path in this domain to an IAM
* principal's policy.
*
* @param path The path to grant permissions for
* @param grantee The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "es:HttpGet", "es:HttpPut", ...)
*/
public grantPath(path: string, grantee: iam.IGrantable, ...actions: string[]): iam.Grant {
return iam.Grant.addToPrincipal({
grantee,
actions,
resourceArns: [`${this.domainArn}/${path}`],
scope: this,
});
}

/**
* Return the given named metric for this Domain.
*/
Expand Down

0 comments on commit cc9b1bb

Please sign in to comment.