Skip to content

Commit

Permalink
Add private network support (#39)
Browse files Browse the repository at this point in the history
* Add private network support

* Change model type

* fix example

* fix config

* Fix config for update

* Fix update mapping
  • Loading branch information
shahnami authored Dec 20, 2023
1 parent 4c261ed commit 35ec036
Show file tree
Hide file tree
Showing 38 changed files with 1,390 additions and 100 deletions.
24 changes: 23 additions & 1 deletion examples/defender-test-project/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,34 @@ resources:
forked-networks:
mainnet-fork:
name: mainnet-fork # restricted to alphanumeric characters and dashes (no whitespaces)
forked-network: mainnet # must be of type SupportedNetwork
supported-network: mainnet # must be of type SupportedNetwork
rpc-url: https://rpc.phalcon.xyz/rpc_123
# optional
block-explorer-url: https://scan.phalcon.xyz/fork_123
# optional
api-key: null

private-networks:
my-network:
name: my-private-network # restricted to alphanumeric characters and dashes (no whitespaces)
rpc-url: https://rpc.kaleido.io/
# optional
block-explorer-url: null
# optional
api-key: null
configuration:
symbol: ETH
# optional
safe-contracts:
master: '0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552'
proxy-factory: '0xa6B71E26C5e0845f74c812102Ca7114b6a896AB2'
multi-send-call-only: '0x40A2aCCbd92BCA938b02010E17A5b8929b49130D'
create-call: '0x762fcF49C5EF21510755191BbeD6AA2a702f0348'
# optional
subgraph-url: null
# optional
eips:
isEIP1559: true

plugins:
- '@openzeppelin/defender-as-code'
220 changes: 199 additions & 21 deletions src/cmd/deploy.ts

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions src/cmd/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
TeamKey,
YSecret,
Resources,
DefenderForkedNetwork,
DefenderTenantNetwork,
} from '../types';
import {
Action,
Expand All @@ -39,6 +39,7 @@ import {
Notification,
Category,
ForkedNetworkRequest,
PrivateNetworkRequest,
} from '../types/types/resources.schema';

export default class DefenderInfo {
Expand Down Expand Up @@ -121,20 +122,33 @@ export default class DefenderInfo {
categories: [],
secrets: [],
forkedNetworks: [],
privateNetworks: [],
};

// Forked Networks
const listForkedNetworks = () => getNetworkClient(this.teamKey!).listForkedNetworks();

await this.wrapper<ForkedNetworkRequest, DefenderForkedNetwork>(
await this.wrapper<ForkedNetworkRequest, DefenderTenantNetwork>(
this.serverless,
'Forked Networks',
removeDefenderIdReferences(this.resources?.['forked-networks']),
listForkedNetworks,
(resource: DefenderForkedNetwork) => `${resource.stackResourceId}: ${resource.forkedNetworkId}`,
(resource: DefenderTenantNetwork) => `${resource.stackResourceId}: ${resource.tenantNetworkId}`,
stdOut.forkedNetworks,
);

// Private Networks
const listPrivateNetworks = () => getNetworkClient(this.teamKey!).listPrivateNetworks();

await this.wrapper<PrivateNetworkRequest, DefenderTenantNetwork>(
this.serverless,
'Private Networks',
removeDefenderIdReferences(this.resources?.['private-networks']),
listPrivateNetworks,
(resource: DefenderTenantNetwork) => `${resource.stackResourceId}: ${resource.tenantNetworkId}`,
stdOut.privateNetworks,
);

// Monitors
const listMonitors = () =>
getMonitorClient(this.teamKey!)
Expand Down
40 changes: 33 additions & 7 deletions src/cmd/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
TeamKey,
YSecret,
Resources,
DefenderForkedNetwork,
DefenderTenantNetwork,
} from '../types';
import {
Action,
Expand All @@ -39,6 +39,7 @@ import {
Relayer,
Notification,
ForkedNetworkRequest,
PrivateNetworkRequest,
} from '../types/types/resources.schema';

export default class DefenderRemove {
Expand Down Expand Up @@ -131,7 +132,8 @@ export default class DefenderRemove {
notifications: DefenderNotification[];
categories: DefenderCategory[];
secrets: string[];
forkedNetworks: DefenderForkedNetwork[];
forkedNetworks: DefenderTenantNetwork[];
privateNetworks: DefenderTenantNetwork[];
} = {
stack: stackName,
monitors: [],
Expand All @@ -142,31 +144,55 @@ export default class DefenderRemove {
categories: [],
secrets: [],
forkedNetworks: [],
privateNetworks: [],
};

// Forked Networks
const forkedNetworkClient = getNetworkClient(this.teamKey!);
const listForkedNetworks = () => forkedNetworkClient.listForkedNetworks();
await this.wrapper<ForkedNetworkRequest, DefenderForkedNetwork>(
await this.wrapper<ForkedNetworkRequest, DefenderTenantNetwork>(
this.serverless,
'Forked Networks',
removeDefenderIdReferences(this.resources?.['forked-networks']),
listForkedNetworks,
async (forkedNetworks: DefenderForkedNetwork[]) => {
async (forkedNetworks: DefenderTenantNetwork[]) => {
await Promise.all(
forkedNetworks.map(async (e) => {
this.log.progress(
'component-remove-extra',
`Removing ${e.stackResourceId} (${e.forkedNetworkId}) from Defender`,
`Removing ${e.stackResourceId} (${e.tenantNetworkId}) from Defender`,
);
await forkedNetworkClient.deleteForkedNetwork(e.forkedNetworkId);
this.log.success(`Removed ${e.stackResourceId} (${e.forkedNetworkId})`);
await forkedNetworkClient.deleteForkedNetwork(e.tenantNetworkId);
this.log.success(`Removed ${e.stackResourceId} (${e.tenantNetworkId})`);
}),
);
},
stdOut.forkedNetworks,
);

// Private Networks
const privateNetworkClient = getNetworkClient(this.teamKey!);
const listPrivateNetworks = () => privateNetworkClient.listPrivateNetworks();
await this.wrapper<PrivateNetworkRequest, DefenderTenantNetwork>(
this.serverless,
'Private Networks',
removeDefenderIdReferences(this.resources?.['private-networks']),
listPrivateNetworks,
async (privateNetworks: DefenderTenantNetwork[]) => {
await Promise.all(
privateNetworks.map(async (e) => {
this.log.progress(
'component-remove-extra',
`Removing ${e.stackResourceId} (${e.tenantNetworkId}) from Defender`,
);
await privateNetworkClient.deletePrivateNetwork(e.tenantNetworkId);
this.log.success(`Removed ${e.stackResourceId} (${e.tenantNetworkId})`);
}),
);
},
stdOut.privateNetworks,
);

// Monitors
const monitorClient = getMonitorClient(this.teamKey!);
const listMonitors = () => monitorClient.list().then((i) => i.items);
Expand Down
99 changes: 93 additions & 6 deletions src/types/docs-schemas/definitions.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"type": "string",
"enum": ["x-dfk-avax-chain", "x-dfk-avax-chain-test", "x-security-alliance"]
},
"forked-network": {
"title": "ForkedNetwork",
"tenant-network": {
"title": "TenantNetwork",
"type": "string"
},
"network": {
Expand All @@ -79,7 +79,7 @@
"$ref": "definitions.schema.json#/definitions/supported-network"
},
{
"$ref": "definitions.schema.json#/definitions/forked-network"
"$ref": "definitions.schema.json#/definitions/tenant-network"
}
]
},
Expand Down Expand Up @@ -610,6 +610,17 @@
{ "type": "string", "$ref": "definitions.schema.json#/definitions/defender-id", "format": "uri-reference" }
]
},
"private-network-or-defender-id": {
"title": "PrivateNetworkOrDefenderID",
"anyOf": [
{
"type": "object",
"$ref": "definitions.schema.json#/definitions/private-network-request",
"format": "uri-reference"
},
{ "type": "string", "$ref": "definitions.schema.json#/definitions/defender-id", "format": "uri-reference" }
]
},
"block-explorer-api-key-or-defender-id": {
"title": "BlockExplorerApiKeyOrDefenderID",
"anyOf": [
Expand Down Expand Up @@ -725,17 +736,93 @@
{ "title": "ArrayABI", "type": "array" }
]
},
"safe-contracts": {
"title": "SafeContracts",
"type": "object",
"additionalProperties": false,
"required": ["master", "proxy-factory", "multi-send-call-only"],
"properties": {
"master": {
"$ref": "definitions.schema.json#/definitions/address",
"format": "uri-reference"
},
"proxy-factory": {
"$ref": "definitions.schema.json#/definitions/address",
"format": "uri-reference"
},
"multi-send-call-only": {
"$ref": "definitions.schema.json#/definitions/address",
"format": "uri-reference"
},
"create-call": {
"$ref": "definitions.schema.json#/definitions/address",
"format": "uri-reference"
}
}
},
"tenant-network-eip-configuration": {
"title": "TenantNetworkEIPConfiguration",
"type": "object",
"additionalProperties": false,
"properties": {
"isEIP1559": {
"type": "boolean"
}
}
},
"tenant-network-configuration": {
"title": "TenantNetworkConfiguration",
"type": "object",
"additionalProperties": false,
"required": ["symbol"],
"properties": {
"symbol": {
"type": "string",
"minLength": 3,
"maxLength": 6
},
"eips": {
"$ref": "definitions.schema.json#/definitions/tenant-network-eip-configuration"
},
"safe-contracts": {
"$ref": "definitions.schema.json#/definitions/safe-contracts"
},
"subgraph-url": {
"type": "string",
"pattern": "^(http(s)?://)[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$"
}
}
},
"forked-network-request": {
"title": "ForkedNetworkRequest",
"type": "object",
"additionalProperties": false,
"required": ["name", "forked-network", "rpc-url"],
"required": ["name", "supported-network", "rpc-url"],
"properties": {
"name": { "$ref": "definitions.schema.json#/definitions/forked-network" },
"forked-network": { "$ref": "definitions.schema.json#/definitions/supported-network" },
"name": { "$ref": "definitions.schema.json#/definitions/tenant-network" },
"supported-network": { "$ref": "definitions.schema.json#/definitions/supported-network" },
"rpc-url": {
"type": "string",
"pattern": "^(http|https)://"
},
"api-key": { "type": "string" },
"block-explorer-url": {
"type": "string",
"pattern": "^(http(s)?://)[\\w.-]+(?:\\.[\\w\\.-]+)+[\\w\\-\\._~:/?#[\\]@!\\$&'\\(\\)\\*\\+,;=.]+$"
}
}
},
"private-network-request": {
"title": "PrivateNetworkRequest",
"type": "object",
"additionalProperties": false,
"required": ["name", "configuration", "rpc-url"],
"properties": {
"name": { "$ref": "definitions.schema.json#/definitions/tenant-network" },
"configuration": { "$ref": "definitions.schema.json#/definitions/tenant-network-configuration" },
"rpc-url": {
"type": "string",
"pattern": "^(http|https)://"
},
"api-key": { "type": "string" },
"block-explorer-url": {
Expand Down
8 changes: 8 additions & 0 deletions src/types/docs-schemas/resources.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
"$ref": "definitions.schema.json#/definitions/forked-network-or-defender-id",
"format": "uri-reference"
}
},
"private-networks": {
"title": "Private Networks",
"type": "object",
"additionalProperties": {
"$ref": "definitions.schema.json#/definitions/private-network-or-defender-id",
"format": "uri-reference"
}
}
}
}
10 changes: 10 additions & 0 deletions src/types/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,24 @@

* [Policy](./definitions-definitions-policy.md)`definitions.schema.json#/definitions/policy`

* [Private Networks](./resources-properties-private-networks.md)`resources.schema.json#/properties/private-networks`

* [PrivateNetworkRequest](./definitions-definitions-privatenetworkrequest.md)`definitions.schema.json#/definitions/private-network-request`

* [Relayer](./definitions-definitions-relayer.md)`definitions.schema.json#/definitions/relayer`

* [Relayers](./resources-properties-relayers.md)`resources.schema.json#/properties/relayers`

* [SafeContracts](./definitions-definitions-safecontracts.md)`definitions.schema.json#/definitions/safe-contracts`

* [StackSecrets](./resources-properties-actionsecrets-properties-stacksecrets.md)`resources.schema.json#/properties/secrets/properties/stack`

* [TelegramConfig](./definitions-definitions-telegramconfig.md)`definitions.schema.json#/definitions/telegramBotConfig`

* [TenantNetworkConfiguration](./definitions-definitions-tenantnetworkconfiguration.md)`definitions.schema.json#/definitions/tenant-network-configuration`

* [TenantNetworkEIPConfiguration](./definitions-definitions-tenantnetworkeipconfiguration.md)`definitions.schema.json#/definitions/tenant-network-eip-configuration`

* [Trigger](./definitions-definitions-action-properties-trigger.md)`definitions.schema.json#/definitions/action/properties/trigger`

* [Untitled object in Definitions](./definitions-definitions-relayer-properties-addressfromrelayer-anyof-0.md)`definitions.schema.json#/definitions/relayer/properties/address-from-relayer/anyOf/0`
Expand Down
4 changes: 2 additions & 2 deletions src/types/docs/definitions-definitions-address.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## items Type
## create-call Type

`string` ([Address](definitions-definitions-address.md))

## items Constraints
## create-call Constraints

**pattern**: the string must match the following regular expression:&#x20;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ any of

* [CustomNetwork](definitions-definitions-customnetwork.md "check type definition")

* [ForkedNetwork](definitions-definitions-forkednetwork.md "check type definition")
* [TenantNetwork](definitions-definitions-tenantnetwork.md "check type definition")

### network Constraints

Expand Down
2 changes: 1 addition & 1 deletion src/types/docs/definitions-definitions-blockmonitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ any of

* [CustomNetwork](definitions-definitions-customnetwork.md "check type definition")

* [ForkedNetwork](definitions-definitions-forkednetwork.md "check type definition")
* [TenantNetwork](definitions-definitions-tenantnetwork.md "check type definition")

### network Constraints

Expand Down
2 changes: 1 addition & 1 deletion src/types/docs/definitions-definitions-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ any of

* [CustomNetwork](definitions-definitions-customnetwork.md "check type definition")

* [ForkedNetwork](definitions-definitions-forkednetwork.md "check type definition")
* [TenantNetwork](definitions-definitions-tenantnetwork.md "check type definition")

### network Constraints

Expand Down
3 changes: 0 additions & 3 deletions src/types/docs/definitions-definitions-forkednetwork.md

This file was deleted.

Loading

0 comments on commit 35ec036

Please sign in to comment.