Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as iam from 'aws-cdk-lib/aws-iam';
import { IManagedPolicy, ManagedPolicyReference } from 'aws-cdk-lib/aws-iam';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as deploy from 'aws-cdk-lib/aws-s3-deployment';
import { App, Fn, RemovalPolicy, Stack, UnscopedValidationError } from 'aws-cdk-lib';
import { App, Fn, RemovalPolicy, ResourceEnvironment, Stack, UnscopedValidationError } from 'aws-cdk-lib';
import * as integ from '@aws-cdk/integ-tests-alpha';
import * as cpactions from 'aws-cdk-lib/aws-codepipeline-actions';
import { Node } from 'constructs';
Expand Down Expand Up @@ -56,6 +56,9 @@ function makePolicy(arn: string): IManagedPolicy {
get node(): Node {
throw new UnscopedValidationError('The result of fromAwsManagedPolicyName can not be used in this API');
},
get env(): ResourceEnvironment {
throw new UnscopedValidationError('The result of fromAwsManagedPolicyName can not be used in this API');
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ function makePolicy(arn: string): IManagedPolicy {
get node(): Node {
throw new UnscopedValidationError('The result of fromAwsManagedPolicyName can not be used in this API');
},
get env(): cdk.ResourceEnvironment {
throw new UnscopedValidationError('The result of fromAwsManagedPolicyName can not be used in this API');
},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export abstract class AgentAliasBase extends Resource implements IAgentAlias {
grantee,
actions,
resourceArns: [this.aliasArn],
scope: this,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ export abstract class GuardrailBase extends Resource implements IGuardrail {
grantee,
actions,
resourceArns: [this.guardrailArn],
scope: this,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class ApplicationInferenceProfile extends InferenceProfileBase implements
grantee: grantee,
actions: ['bedrock:GetInferenceProfile', 'bedrock:InvokeModel'],
resourceArns: [this.inferenceProfileArn],
scope: this,
});
}
}
Expand Down Expand Up @@ -157,7 +156,6 @@ export class ApplicationInferenceProfile extends InferenceProfileBase implements
grantee: grantee,
actions: ['bedrock:GetInferenceProfile', 'bedrock:InvokeModel'],
resourceArns: [this.inferenceProfileArn],
scope: this,
});
}
})(cfnApplicationInferenceProfile, id);
Expand Down Expand Up @@ -357,7 +355,6 @@ export class ApplicationInferenceProfile extends InferenceProfileBase implements
grantee: grantee,
actions: ['bedrock:GetInferenceProfile', 'bedrock:InvokeModel'],
resourceArns: [this.inferenceProfileArn],
scope: this,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export abstract class PromptBase extends Resource implements IPrompt {
grantee,
resourceArns: [this.promptArn],
actions: ['bedrock:GetPrompt'],
scope: this,
});
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-appsync/lib/data-source-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ export class AppSyncRdsDataSource extends AppSyncBackedDataSource {
'rds-data:UpdateItems',
],
resourceArns: [clusterArn, `${clusterArn}:*`],
scope: this,
});
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-appsync/lib/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ export class RdsDataSource extends BackedDataSource {
'rds-data:UpdateItems',
],
resourceArns: [clusterArn, `${clusterArn}:*`],
scope: this,
});
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-appsync/lib/eventapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ export abstract class EventApiBase extends ApiBase implements IEventApi {
grantee,
actions,
resourceArns: resources.resourceArns(this),
scope: this,
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-appsync/lib/graphqlapi-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ export abstract class GraphqlApiBase extends Resource implements IGraphqlApi {
grantee,
actions,
resourceArns: resources.resourceArns(this),
scope: this,
});
}

Expand Down
5 changes: 5 additions & 0 deletions packages/aws-cdk-lib/aws-cloudfront/lib/cache-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Duration,
Names,
Resource,
ResourceEnvironment,
Stack,
Token,
UnscopedValidationError,
Expand Down Expand Up @@ -152,6 +153,10 @@ export class CachePolicy extends Resource implements ICachePolicy {
throw new UnscopedValidationError('The result of fromManagedCachePolicy can not be used in this API');
}

public get env(): ResourceEnvironment {
throw new UnscopedValidationError('The result of fromManagedCachePolicy can not be used in this API');
}

public readonly cachePolicyId = managedCachePolicyId;
public readonly cachePolicyRef = {
cachePolicyId: managedCachePolicyId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct, Node } from 'constructs';
import { CfnOriginRequestPolicy, IOriginRequestPolicyRef, OriginRequestPolicyReference } from './cloudfront.generated';
import { Names, Resource, Token, UnscopedValidationError, ValidationError } from '../../core';
import { Names, Resource, ResourceEnvironment, Token, UnscopedValidationError, ValidationError } from '../../core';
import { addConstructMetadata } from '../../core/lib/metadata-resource';
import { propertyInjectable } from '../../core/lib/prop-injectable';

Expand Down Expand Up @@ -92,6 +92,10 @@ export class OriginRequestPolicy extends Resource implements IOriginRequestPolic
throw new UnscopedValidationError('The result of fromManagedOriginRequestPolicy can not be used in this API');
}

public get env(): ResourceEnvironment {
throw new UnscopedValidationError('The result of fromManagedOriginRequestPolicy can not be used in this API');
}

public readonly originRequestPolicyId = managedOriginRequestPolicyId;
public readonly originRequestPolicyRef = {
originRequestPolicyId: managedOriginRequestPolicyId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IResponseHeadersPolicyRef,
ResponseHeadersPolicyReference,
} from './cloudfront.generated';
import { Duration, Names, Resource, Token, UnscopedValidationError, ValidationError, withResolved } from '../../core';
import { Duration, Names, Resource, ResourceEnvironment, Token, UnscopedValidationError, ValidationError, withResolved } from '../../core';
import { addConstructMetadata } from '../../core/lib/metadata-resource';
import { propertyInjectable } from '../../core/lib/prop-injectable';

Expand Down Expand Up @@ -114,6 +114,10 @@ export class ResponseHeadersPolicy extends Resource implements IResponseHeadersP
throw new UnscopedValidationError('The result of fromManagedResponseHeadersPolicy can not be used in this API');
}

public get env(): ResourceEnvironment {
throw new UnscopedValidationError('The result of fromManagedResponseHeadersPolicy can not be used in this API');
}

public readonly responseHeadersPolicyId = managedResponseHeadersPolicyId;
public readonly responseHeadersPolicyRef = {
responseHeadersPolicyId: managedResponseHeadersPolicyId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Construct, Node } from 'constructs';
import * as codepipeline from '../../../aws-codepipeline';
import { Aws, UnscopedValidationError } from '../../../core';
import { Aws, ResourceEnvironment, UnscopedValidationError } from '../../../core';
import { Action } from '../action';
import { deployArtifactBounds } from '../common';

Expand Down Expand Up @@ -57,6 +57,9 @@ export class ElasticBeanstalkDeployAction extends Action {
get node(): Node {
throw new UnscopedValidationError('This object can not be used in this API');
},
get env(): ResourceEnvironment {
throw new UnscopedValidationError('This object can not be used in this API');
},
managedPolicyArn: policyArn,
managedPolicyRef: { policyArn },
});
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-cognito/lib/user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,6 @@ abstract class UserPoolBase extends Resource implements IUserPool {
grantee,
actions,
resourceArns: [this.userPoolArn],
scope: this,
});
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-dynamodb/lib/table-v2-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,6 @@ export abstract class TableBaseV2 extends Resource implements ITableV2, IResourc
grantee,
actions: options.streamActions,
resourceArns: [this.tableStreamArn],
scope: this,
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ export abstract class TableBase extends Resource implements ITable, iam.IResourc
grantee,
actions,
resourceArns: [this.tableStreamArn],
scope: this,
});
}

Expand Down
4 changes: 0 additions & 4 deletions packages/aws-cdk-lib/aws-ecr/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ export abstract class RepositoryBase extends Resource implements IRepository {
grantee,
actions,
resourceArns: [this.repositoryArn],
scope: this,
});
} else {
return iam.Grant.addToPrincipalOrResource({
Expand Down Expand Up @@ -415,7 +414,6 @@ export abstract class RepositoryBase extends Resource implements IRepository {
grantee,
actions: ['ecr:GetAuthorizationToken'],
resourceArns: ['*'],
scope: this,
});

return ret;
Expand All @@ -430,7 +428,6 @@ export abstract class RepositoryBase extends Resource implements IRepository {
grantee,
actions: ['ecr:GetAuthorizationToken'],
resourceArns: ['*'],
scope: this,
});

return ret;
Expand All @@ -448,7 +445,6 @@ export abstract class RepositoryBase extends Resource implements IRepository {
grantee,
actions: ['ecr:GetAuthorizationToken'],
resourceArns: ['*'],
scope: this,
});

return ret;
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-cdk-lib/aws-efs/lib/efs-file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CfnFileSystem, CfnMountTarget } from './efs.generated';
import * as ec2 from '../../aws-ec2';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { ArnFormat, FeatureFlags, Lazy, Names, RemovalPolicy, Resource, Size, Stack, Tags, Token, ValidationError } from '../../core';
import { ArnFormat, FeatureFlags, IResource, Lazy, Names, RemovalPolicy, Resource, Size, Stack, Tags, Token, ValidationError } from '../../core';
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
import { propertyInjectable } from '../../core/lib/prop-injectable';
import * as cxapi from '../../cx-api';
Expand Down Expand Up @@ -141,7 +141,7 @@ export enum ReplicationOverwriteProtection {
/**
* Represents an Amazon EFS file system
*/
export interface IFileSystem extends ec2.IConnectable, iam.IResourceWithPolicy {
export interface IFileSystem extends ec2.IConnectable, IResource, iam.IResourceWithPolicy {
/**
* The ID of the file system, assigned by Amazon EFS.
*
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-elasticsearch/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,6 @@ abstract class DomainBase extends cdk.Resource implements IDomain {
grantee,
actions: domainActions,
resourceArns,
scope: this,
});

return grant;
Expand Down
30 changes: 18 additions & 12 deletions packages/aws-cdk-lib/aws-iam/lib/grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface GrantWithResourceOptions extends CommonGrantOptions {
* The statement will be added to the resource policy if it couldn't be
* added to the principal policy.
*/
readonly resource: IResourceWithPolicy;
readonly resource: IResourceWithPolicyV2;

/**
* When referring to the resource in a resource policy, use this as ARN.
Expand All @@ -68,7 +68,7 @@ export interface GrantPolicyWithResourceOptions extends GrantWithResourceOptions
* The policy statement to add to the resource's policy
*
* This statement will be passed to the resource's addToResourcePolicy method.
* The actual handling of the statement depends on the specific IResourceWithPolicy
* The actual handling of the statement depends on the specific IResourceWithPolicyV2
* implementation.
*/
readonly statement: PolicyStatement;
Expand All @@ -83,6 +83,7 @@ export interface GrantOnPrincipalOptions extends CommonGrantOptions {
* Construct to report warnings on in case grant could not be registered
*
* @default - the construct in which this construct is defined
* @deprecated The scope argument is currently unused.
*/
readonly scope?: IConstruct;
}
Expand All @@ -97,7 +98,7 @@ export interface GrantOnPrincipalAndResourceOptions extends CommonGrantOptions {
*
* The statement will always be added to the resource policy.
*/
readonly resource: IResourceWithPolicy;
readonly resource: IResourceWithPolicyV2;

/**
* When referring to the resource in a resource policy, use this as ARN.
Expand Down Expand Up @@ -138,10 +139,7 @@ export class Grant implements IDependable {
* resource construct.
*/
public static addToPrincipalOrResource(options: GrantWithResourceOptions): Grant {
const result = Grant.addToPrincipal({
...options,
scope: options.resource,
});
const result = Grant.addToPrincipal(options);

const resourceAndPrincipalAccountComparison = options.grantee.grantPrincipal.principalAccount
? cdk.Token.compareStrings(options.resource.env.account, options.grantee.grantPrincipal.principalAccount)
Expand Down Expand Up @@ -267,10 +265,7 @@ export class Grant implements IDependable {
* Statement will be the resource statement.
*/
public static addToPrincipalAndResource(options: GrantOnPrincipalAndResourceOptions): Grant {
const result = Grant.addToPrincipal({
...options,
scope: options.resource,
});
const result = Grant.addToPrincipal(options);

const statement = new PolicyStatement({
actions: options.actions,
Expand Down Expand Up @@ -426,13 +421,24 @@ interface GrantProps {
/**
* A resource with a resource policy that can be added to
*/
export interface IResourceWithPolicy extends cdk.IResource {
export interface IResourceWithPolicyV2 extends cdk.IEnvironmentAware {
/**
* Add a statement to the resource's resource policy
*/
addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult;
}

/**
* A resource with a resource policy that can be added to
*
* This interface is maintained for backwards compatibility, but should
* not be used in new code. Prefer `IResourceWithPolicyV2` instead.
*
* @deprecated Implement `IResourceWithPolicyV2` instead.
*/
export interface IResourceWithPolicy extends IResourceWithPolicyV2, cdk.IResource {
}

/**
* Result of calling addToResourcePolicy
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/aws-iam/lib/managed-policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AddToPrincipalPolicyResult, IGrantable, IPrincipal, PrincipalPolicyFrag
import { undefinedIfEmpty } from './private/util';
import { IRole } from './role';
import { IUser } from './user';
import { Arn, ArnFormat, Aws, Resource, Stack, UnscopedValidationError } from '../../core';
import { Arn, ArnFormat, Aws, Resource, ResourceEnvironment, Stack, UnscopedValidationError } from '../../core';
import { getCustomizeRolesConfig, PolicySynthesizer } from '../../core/lib/helpers-internal';
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
import { propertyInjectable } from '../../core/lib/prop-injectable';
Expand Down Expand Up @@ -196,6 +196,9 @@ export class ManagedPolicy extends Resource implements IManagedPolicy, IGrantabl
public get node(): Node {
throw new UnscopedValidationError('The result of fromAwsManagedPolicyName can not be used in this API');
}
public get env(): ResourceEnvironment {
throw new UnscopedValidationError('The result of fromAwsManagedPolicyName can not be used in this API');
}
}
return new AwsManagedPolicy();
}
Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-iam/lib/private/imported-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ export class ImportedRole extends Resource implements IRole, IComparablePrincipa
grantee,
actions,
resourceArns: [this.roleArn],
scope: this,
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/aws-cdk-lib/aws-iam/lib/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ export class Role extends Resource implements IRole {
grantee,
actions,
resourceArns: [this.roleArn],
scope: this,
});
}

Expand Down
3 changes: 0 additions & 3 deletions packages/aws-cdk-lib/aws-kinesis/lib/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,7 @@ abstract class StreamBase extends Resource implements IStream {
}
return { statementAdded: false };
},
node: this.node,
stack: this.stack,
env: this.env,
applyRemovalPolicy: x => this.applyRemovalPolicy(x),
},
});
}
Expand Down
Loading
Loading