Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -15,6 +15,13 @@ export interface IFirewallRuleGroup extends IResource {
* @attribute
*/
readonly firewallRuleGroupId: string;

/**
* The name of the rule group
*
* @attribute
*/
readonly firewallRuleGroupName?: string;
}

/**
Expand Down Expand Up @@ -156,7 +163,7 @@ export abstract class DnsBlockResponse {
*/
export class FirewallRuleGroup extends Resource implements IFirewallRuleGroup {
/**
* Import an existing Firewall Rule Group
* Import an existing Firewall Rule Group by ID
*/
public static fromFirewallRuleGroupId(scope: Construct, id: string, firewallRuleGroupId: string): IFirewallRuleGroup {
class Import extends Resource implements IFirewallRuleGroup {
Expand All @@ -165,8 +172,29 @@ export class FirewallRuleGroup extends Resource implements IFirewallRuleGroup {
return new Import(scope, id);
}

/**
* Import an existing Firewall Rule Group by Name
*/
public static fromFirewallRuleGroupName(scope: Construct, id: string, firewallRuleGroupName: string): IFirewallRuleGroup {
const firewallRuleGroups: { [name: string]: string } = scope.node.tryGetContext('firewallRuleGroups') || {};
const firewallRuleGroupId = firewallRuleGroups[firewallRuleGroupName];

if (!firewallRuleGroupId) {
throw new Error(`Firewall Rule Group with name "${firewallRuleGroupName}" not found in context.`);
}

class Import extends Resource implements IFirewallRuleGroup {
public readonly firewallRuleGroupId = firewallRuleGroupId;
public readonly firewallRuleGroupName = firewallRuleGroupName;
}

return new Import(scope, id);
}

public readonly firewallRuleGroupId: string;

public readonly firewallRuleGroupName?: string;

/**
* The ARN (Amazon Resource Name) of the rule group
* @attribute
Expand Down Expand Up @@ -231,6 +259,9 @@ export class FirewallRuleGroup extends Resource implements IFirewallRuleGroup {

this.rules = props.rules ?? [];

// Store the name of the rule group
this.firewallRuleGroupName = props.name;

const ruleGroup = new CfnFirewallRuleGroup(this, 'Resource', {
name: props.name,
firewallRules: Lazy.any({ produce: () => this.rules.map(renderRule) }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,24 @@ test('throws when associating with a priority not between 100-9,000', () => {
vpc,
})).toThrow(/Priority must be greater than 100 and less than 9000/);
});

test('fromFirewallRuleGroupName return correct imported resource when mapping provided', () => {
const stackWithContext = new Stack();
stackWithContext.node.setContext('firewallRuleGroups', {
TestGroup: 'fwr-123456',
});

const importedGroup = FirewallRuleGroup.fromFirewallRuleGroupName(stackWithContext, 'ImportedGroup', 'TestGroup');

expect(importedGroup.firewallRuleGroupId).toEqual('fwr-123456');
expect(importedGroup.firewallRuleGroupName).toEqual('TestGroup');
});

test('fromFirewallRuleGroupName throws an error when the name is not in context', () => {
const stackWithoutMapping = new Stack();
stackWithoutMapping.node.setContext('firewallRuleGroups', {});

expect(() => {
FirewallRuleGroup.fromFirewallRuleGroupName(stackWithoutMapping, 'ImportedGroup', 'NonExistentGroup');
}).toThrow(/Firewall Rule Group with name "NonExistentGroup" not found in context./);
});
Loading