Skip to content

Commit

Permalink
Add awslint rule to force grant() methods to use helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
rix0rrr committed Feb 27, 2019
1 parent 554816d commit cf68f7d
Show file tree
Hide file tree
Showing 19 changed files with 444 additions and 237 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-codepipeline-api/lib/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ export interface IPipeline extends cdk.IConstruct, events.IEventRuleTarget {
*
* @param identity the IAM Identity to grant the permissions to
*/
grantBucketRead(identity?: iam.IPrincipal): void;
grantBucketRead(identity?: iam.IPrincipal): iam.GrantResult;

/**
* Grants read & write permissions to the Pipeline's S3 Bucket to the given Identity.
*
* @param identity the IAM Identity to grant the permissions to
*/
grantBucketReadWrite(identity?: iam.IPrincipal): void;
grantBucketReadWrite(identity?: iam.IPrincipal): iam.GrantResult;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-codepipeline/lib/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ export class Pipeline extends cdk.Construct implements cpapi.IPipeline {
return this.stages.length;
}

public grantBucketRead(identity?: iam.IPrincipal): void {
this.artifactBucket.grantRead(identity);
public grantBucketRead(identity?: iam.IPrincipal): iam.GrantResult {
return this.artifactBucket.grantRead(identity);
}

public grantBucketReadWrite(identity?: iam.IPrincipal): void {
this.artifactBucket.grantReadWrite(identity);
public grantBucketReadWrite(identity?: iam.IPrincipal): iam.GrantResult {
return this.artifactBucket.grantReadWrite(identity);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ export class Table extends Construct {
* @param principal The principal (no-op if undefined)
* @param actions The set of actions to allow (i.e. "dynamodb:PutItem", "dynamodb:GetItem", ...)
*/
public grant(principal?: iam.IPrincipal, ...actions: string[]) {
iam.Permissions.grant({
public grant(principal?: iam.IPrincipal, ...actions: string[]): iam.GrantResult {
return iam.Permissions.grant({
principal,
actions,
resourceArns: [
Expand All @@ -427,7 +427,7 @@ export class Table extends Construct {
* @param actions The set of actions to allow (i.e. "dynamodb:DescribeStream", "dynamodb:GetRecords", ...)
*/
public grantStream(principal?: iam.IPrincipal, ...actions: string[]) {
iam.Permissions.grant({
return iam.Permissions.grant({
principal,
actions,
resourceArns: [this.tableStreamArn],
Expand All @@ -441,7 +441,7 @@ export class Table extends Construct {
* @param principal The principal to grant access to
*/
public grantReadData(principal?: iam.IPrincipal) {
this.grant(principal, ...READ_DATA_ACTIONS);
return this.grant(principal, ...READ_DATA_ACTIONS);
}

/**
Expand All @@ -451,7 +451,7 @@ export class Table extends Construct {
* @param principal The principal to grant access to
*/
public grantStreamRead(principal?: iam.IPrincipal) {
this.grantStream(principal, ...READ_STREAM_DATA_ACTIONS);
return this.grantStream(principal, ...READ_STREAM_DATA_ACTIONS);
}

/**
Expand All @@ -460,7 +460,7 @@ export class Table extends Construct {
* @param principal The principal to grant access to
*/
public grantWriteData(principal?: iam.IPrincipal) {
this.grant(principal, ...WRITE_DATA_ACTIONS);
return this.grant(principal, ...WRITE_DATA_ACTIONS);
}

/**
Expand All @@ -470,15 +470,15 @@ export class Table extends Construct {
* @param principal The principal to grant access to
*/
public grantReadWriteData(principal?: iam.IPrincipal) {
this.grant(principal, ...READ_DATA_ACTIONS, ...WRITE_DATA_ACTIONS);
return this.grant(principal, ...READ_DATA_ACTIONS, ...WRITE_DATA_ACTIONS);
}

/**
* Permits all DynamoDB operations ("dynamodb:*") to an IAM principal.
* @param principal The principal to grant access to
*/
public grantFullAccess(principal?: iam.IPrincipal) {
this.grant(principal, 'dynamodb:*');
return this.grant(principal, 'dynamodb:*');
}

/**
Expand Down
19 changes: 10 additions & 9 deletions packages/@aws-cdk/aws-ecr/lib/repository-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ export interface IRepository extends cdk.IConstruct {
/**
* Grant the given principal identity permissions to perform the actions on this repository
*/
grant(identity?: iam.IPrincipal, ...actions: string[]): void;
grant(identity?: iam.IPrincipal, ...actions: string[]): iam.GrantResult;

/**
* Grant the given identity permissions to pull images in this repository.
*/
grantPull(identity?: iam.IPrincipal): void;
grantPull(identity?: iam.IPrincipal): iam.GrantResult;

/**
* Grant the given identity permissions to pull and push images to this repository.
*/
grantPullPush(identity?: iam.IPrincipal): void;
grantPullPush(identity?: iam.IPrincipal): iam.GrantResult;

This comment has been minimized.

Copy link
@eladb

eladb Feb 27, 2019

Contributor

I think we can call this iam.Grant. Perhaps in the future you'll be able to do some on grants.


/**
* Defines an AWS CloudWatch event rule that can trigger a target when an image is pushed to this
Expand Down Expand Up @@ -207,7 +207,7 @@ export abstract class RepositoryBase extends cdk.Construct implements IRepositor
* Grant the given principal identity permissions to perform the actions on this repository
*/
public grant(principal?: iam.IPrincipal, ...actions: string[]) {
iam.Permissions.grant({
return iam.Permissions.grant({
principal,
actions,
resourceArns: [this.repositoryArn],
Expand All @@ -219,23 +219,24 @@ export abstract class RepositoryBase extends cdk.Construct implements IRepositor
* Grant the given identity permissions to use the images in this repository
*/
public grantPull(principal?: iam.IPrincipal) {
this.grant(principal, "ecr:BatchCheckLayerAvailability", "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage");
const ret = this.grant(principal, "ecr:BatchCheckLayerAvailability", "ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage");

iam.Permissions.grant({
iam.Permissions.tryGrantOnIdentity({
principal,
actions: ["ecr:GetAuthorizationToken", "logs:CreateLogStream", "logs:PutLogEvents"],
actions: ["ecr:GetAuthorizationToken"],
resourceArns: ['*'],
skipResourcePolicy: true,
scope: this,
});

return ret;
}

/**
* Grant the given identity permissions to pull and push images to this repository.
*/
public grantPullPush(identity?: iam.IPrincipal) {
this.grantPull(identity);
this.grant(identity,
return this.grant(identity,
"ecr:PutImage",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
Expand Down
Loading

0 comments on commit cf68f7d

Please sign in to comment.