-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for kvstore client (#495)
* add package * include kvstore client in defender-sdk index * add kv store local example * include action credentials when initializing client
- Loading branch information
1 parent
c510cad
commit cb0ee8e
Showing
24 changed files
with
1,376 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { Defender } = require('@openzeppelin/defender-sdk'); | ||
|
||
async function main() { | ||
const store = Defender.localKVStoreClient({ path: './store.json' }); | ||
await store.put('key', 'value!'); | ||
|
||
const value = await store.get('key'); | ||
console.log(value); | ||
|
||
await store.del('key'); | ||
} | ||
|
||
if (require.main === module) { | ||
main().catch(console.error); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@openzeppelin/defender-sdk-example-key-value-store-local", | ||
"version": "1.14.2", | ||
"private": true, | ||
"main": "index.js", | ||
"author": "OpenZeppelin Defender <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@openzeppelin/defender-sdk": "1.14.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Defender Key-Value Store for Actions | ||
|
||
The [Defender Actions](https://docs.openzeppelin.com/defender/v2/module/actions) service allows you to run small code snippets on a regular basis or via webhooks that can make calls to the Ethereum network or to external APIs. Thanks to tight integration to Defender Relayers, you can use Actions to automate regular operations on your contracts. | ||
|
||
This client allows you to access a simple key-value data store from your Actions code, so you can persist data throughout executions and across different Actions. | ||
|
||
_Note that this package will not work outisde the Actions environment._ | ||
|
||
## Installation | ||
|
||
This package is included in the latest Actions runtime environment, so you do not need to bundle it in your code. To install it for local development and typescript type completion, run: | ||
|
||
```bash | ||
npm install @openzeppelin/defender-sdk-key-value-store | ||
``` | ||
|
||
```bash | ||
yarn add @openzeppelin/defender-sdk-key-value-store | ||
``` | ||
|
||
## Usage | ||
|
||
You can interact with your key-value store through an instance of `Defender`, which is initialized with the payload injected in the your Action `handler` function. Once initialized, you can call `kvstore.get`, `kvstore.put`, or `kvstore.del`. | ||
|
||
```js | ||
const { Defender } = require('@openzeppelin/defender-sdk'); | ||
|
||
exports.handler = async function (event) { | ||
// Creates an instance of the key-value store client | ||
const client = new Defender(event); | ||
|
||
// Associates myValue to myKey | ||
await client.keyValueStore.put('myKey', 'myValue'); | ||
|
||
// Returns myValue associated to myKey | ||
const value = await client.keyValueStore.get('myKey'); | ||
|
||
// Deletes the entry for myKey | ||
await client.keyValueStore.del('myKey'); | ||
}; | ||
``` | ||
|
||
## Local development | ||
|
||
The Defender key-value store is only accessible from within an Action. To simplify local development, you can create an instance using `Defender.localKVStoreClient` providing an object with a `path` property. The client will use a local json file at that path for all operations. | ||
|
||
```js | ||
const { Defender } = require('@openzeppelin/defender-sdk'); | ||
|
||
async function local() { | ||
// Creates an instance of the client that will write to a local file | ||
const store = Defender.localKVStoreClient({ path: '/tmp/foo/store.json' }); | ||
|
||
// The store.json file will contain { myKey: myValue } | ||
await store.put('myKey', 'myValue'); | ||
} | ||
``` | ||
|
||
## Considerations | ||
|
||
- All data in the key-value store is persisted as strings, both keys and values. | ||
- Keys are limited to 1kb in size, and values to 300kb. | ||
- The data store is shared across all your Actions; consider prefixing the keys with a namespace if you want to have different data buckets. | ||
- A key-value entry is expired after 90 days of the last time it was `put` into the store. | ||
- The total number of key-value records in your store is determined by your Defender plan. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const Lambda = jest.fn(() => ({ | ||
invoke: jest.fn(() => | ||
Promise.resolve({ | ||
Payload: { | ||
transformToString: () => JSON.stringify({ result: 'result' }), | ||
}, | ||
}), | ||
), | ||
})); | ||
|
||
export { Lambda }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const mock = jest.fn(() => ({ | ||
invoke: jest.fn(() => ({ | ||
promise: jest.fn(() => | ||
Promise.resolve({ | ||
Payload: JSON.stringify({ result: 'result' }), | ||
}), | ||
), | ||
})), | ||
})); | ||
|
||
export default mock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../../jest.config'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@openzeppelin/defender-sdk-key-value-store-client", | ||
"version": "1.14.2", | ||
"description": "", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"scripts": { | ||
"build": "rm -rf lib && tsc", | ||
"test": "npm run test:unit", | ||
"test:unit": "jest --verbose --passWithNoTests --forceExit", | ||
"watch": "tsc -w" | ||
}, | ||
"files": [ | ||
"lib", | ||
"!*.test.js", | ||
"!*.test.js.map", | ||
"!*.test.d.ts", | ||
"!*__mocks__" | ||
], | ||
"author": "OpenZeppelin Defender <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@openzeppelin/defender-sdk-base-client": "^1.14.2", | ||
"axios": "^1.7.2", | ||
"lodash": "^4.17.21", | ||
"fs-extra": "^11.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-extra": "^11.0.4" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { KeyValueStoreActionClient } from './action'; | ||
import Lambda from '../__mocks__/aws-sdk/clients/lambda'; | ||
import { Lambda as LambdaV3 } from '../__mocks__/@aws-sdk/client-lambda'; | ||
jest.mock('node:process', () => ({ | ||
...jest.requireActual('node:process'), | ||
version: 'v16.0.3', | ||
})); | ||
|
||
type TestClient = Omit<KeyValueStoreActionClient, 'lambda'> & { lambda: typeof Lambda }; | ||
|
||
describe('KeyValueStoreAutotaskClient', () => { | ||
const credentials = { | ||
AccessKeyId: 'keyId', | ||
SecretAccessKey: 'accessKey', | ||
SessionToken: 'token', | ||
}; | ||
|
||
let client: TestClient; | ||
|
||
beforeEach(async function () { | ||
jest.mock('aws-sdk/clients/lambda', () => Lambda); | ||
jest.mock('@aws-sdk/client-lambda', () => ({ Lambda: LambdaV3 })); | ||
client = new KeyValueStoreActionClient({ | ||
credentials: JSON.stringify(credentials), | ||
kvstoreARN: 'arn', | ||
}) as unknown as TestClient; | ||
}); | ||
|
||
describe('get', () => { | ||
test('calls kvstore function', async () => { | ||
((client.lambda as any).invoke as jest.Mock).mockImplementationOnce(() => ({ | ||
promise: () => Promise.resolve({ Payload: JSON.stringify('myvalue') }), | ||
})); | ||
|
||
const result = await client.get('mykey'); | ||
|
||
expect(result).toEqual('myvalue'); | ||
expect((client.lambda as any).invoke).toBeCalledWith({ | ||
FunctionName: 'arn', | ||
InvocationType: 'RequestResponse', | ||
Payload: '{"action":"get","key":"mykey"}', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('del', () => { | ||
test('calls kvstore function', async () => { | ||
await client.del('mykey'); | ||
expect((client.lambda as any).invoke).toBeCalledWith({ | ||
FunctionName: 'arn', | ||
InvocationType: 'RequestResponse', | ||
Payload: '{"action":"del","key":"mykey"}', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('put', () => { | ||
test('calls kvstore function', async () => { | ||
await client.put('mykey', 'myvalue'); | ||
expect((client.lambda as any).invoke).toBeCalledWith({ | ||
FunctionName: 'arn', | ||
InvocationType: 'RequestResponse', | ||
Payload: '{"action":"put","key":"mykey","value":"myvalue"}', | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { IKeyValueStoreClient, KeyValueStoreCreateParams, KeyValueStoreRequest } from './types'; | ||
import { BaseActionClient } from '@openzeppelin/defender-sdk-base-client'; | ||
|
||
export class KeyValueStoreActionClient extends BaseActionClient implements IKeyValueStoreClient { | ||
public constructor(params: KeyValueStoreCreateParams) { | ||
super(params.credentials, params.kvstoreARN); | ||
} | ||
|
||
public async get(key: string): Promise<string | undefined> { | ||
const request: KeyValueStoreRequest = { action: 'get', key }; | ||
return this.execute(request); | ||
} | ||
|
||
public async put(key: string, value: string): Promise<void> { | ||
const request: KeyValueStoreRequest = { action: 'put', key, value }; | ||
return this.execute(request); | ||
} | ||
|
||
public async del(key: string): Promise<void> { | ||
const request: KeyValueStoreRequest = { action: 'del', key }; | ||
return this.execute(request); | ||
} | ||
} |
Oops, something went wrong.