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

(aws-events): Imported Event Bus is not account aware. #13469

Assignees
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@thantos
Copy link
Contributor

thantos commented Mar 8, 2021

When importing an event bus via the fromEventBusArn method, the resource.env is the importing stack's account and not the account of the ARN being imported.

This is important because the integration pattern for a construct consuming a bus could be different for a same account vs cross account bus.

For example, a same account bus cannot be the target of another rule, but a cross account bus MUST be the target of a rule before consuming events in the other account.

Reproduction Steps

const bus = EventBus.fromEventBusArn(scope, 'myEventBus', 'cross account arn');

What did you expect to happen?

const bus = EventBus.fromEventBusArn(scope, 'myEventBus', 'cross account arn');

const compare = Token.compareStrings(Stack.of(this).account, bus.env.account);

// compare == TokenComparison.DIFFERENT;

What actually happened?

const bus = EventBus.fromEventBusArn(scope, 'myEventBus', 'cross account arn');

const compare = Token.compareStrings(Stack.of(this).account, bus.env.account);

// compare == TokenComparison.SAME;

Environment

  • Framework Version: monocdk 1.86
  • Node.js Version: 12
  • OS : WSL2 - Ubuntu
  • Language (Version) Typescript (3.6):

Other

I suspect that event bus should take the account and region from the arn.

super(scope, id);
this.eventBusArn = attrs.eventBusArn;
this.eventBusName = attrs.eventBusName;
this.eventBusPolicy = attrs.eventBusPolicy;
this.eventSourceName = attrs.eventSourceName;
}

const parsedArn = Arn.parse(attrs.eventBusArn);
super(scope, id, {
    account: parsedArn.account, //falls back to stack account
    region: parsedArn.region
});

Or it should accept account and region like s3 does.

/**
* The account this existing bucket belongs to.
*
* @default - it's assumed the bucket belongs to the same account as the scope it's being imported into
*/
readonly account?: string;
/**
* The region this existing bucket is in.
*
* @default - it's assumed the bucket is in the same region as the scope it's being imported into
*/
readonly region?: string;


This is 🐛 Bug Report

@thantos thantos added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Mar 8, 2021
@github-actions github-actions bot added the @aws-cdk/aws-events Related to CloudWatch Events label Mar 8, 2021
@thantos
Copy link
Contributor Author

thantos commented Mar 8, 2021

My work around (it doesn't expose the archive method in the base bus object).

/**
 * An Imported event bus that is aware of the account it is imported from.
 */
export class AccountAwareImportedEventBus extends Resource implements IEventBus {
  constructor(scope: Construct, id: string, eventBusArn: string) {
    const parsedArn = Arn.parse(eventBusArn);
    super(scope, id, {
      account: parsedArn.account,
      region: parsedArn.region
    });

    this.eventBusArn = eventBusArn;
    this.eventBusName = parsedArn.resourceName || '';
    this.eventBusPolicy = '';
  }

  eventBusName: string;
  eventBusArn: string;
  eventBusPolicy: string;
  eventSourceName?: string | undefined;
}

Use

const compareAccount = Token.compareStrings(Stack.of(this).account, eventBus.env.account); // eventBus could be an imported or same account bus.
const sameAccount = compareAccount === TokenComparison.SAME || compareAccount === TokenComparison.BOTH_UNRESOLVED;

// For cross account, we write from the target account bus to a bus provided by this account.
if (!sameAccount) {
  if (!props.eventBus) {
     throw new Error('For a cross account listener, an event bus must be provided.');
  }
  // Create rule policy to write across account
  const coreRule = new Rule().target(/*...cross account bus...*/) // a rule which forwards events from cross account to current account's props.eventbus
}

thantos added a commit to thantos/aws-cdk that referenced this issue Mar 9, 2021
Parses the event bus arn to give the account and region to the underlying Resource.

fixes aws#13469
thantos added a commit to thantos/aws-cdk that referenced this issue Mar 9, 2021
Parses the event bus arn to give the account and region to the underlying Resource.

fixes aws#13469
@mergify mergify bot closed this as completed in #13481 Mar 9, 2021
mergify bot pushed a commit that referenced this issue Mar 9, 2021
…ount (#13481)

Parses the event bus arn to give the account and region to the underlying Resource.

fixes #13469


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Mar 9, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

This was referenced Mar 12, 2021
This was referenced Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment