-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add encrypt/decrypt module on data source plugin
Signed-off-by: Louis Chu <[email protected]>
- Loading branch information
Showing
13 changed files
with
487 additions
and
20 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
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,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * as Credential from './types'; |
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,27 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectAttributes } from 'src/core/types'; | ||
|
||
export type SharedAuthType = 'shared'; | ||
export type UsernamePasswordType = 'username_password'; | ||
export type NoAuthType = 'no_auth'; | ||
|
||
export interface CredentialSavedObjectAttributes extends SavedObjectAttributes { | ||
title: string; | ||
authType: SharedAuthType; | ||
credentialMaterials: CredentialMaterials; | ||
description?: string; | ||
} | ||
|
||
export interface CredentialMaterials extends SavedObjectAttributes { | ||
credentialMaterialsType: UsernamePasswordType | NoAuthType; | ||
credentialMaterialsContent?: UsernamePasswordTypedContent; | ||
} | ||
|
||
export interface UsernamePasswordTypedContent extends SavedObjectAttributes { | ||
username: string; | ||
password: string; | ||
} |
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,31 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema, TypeOf } from '@osd/config-schema'; | ||
|
||
const KEY_NAME_MIN_LENGTH: number = 1; | ||
const KEY_NAME_MAX_LENGTH: number = 100; | ||
// Wrapping key size shoule be 32 bytes, as used in envelope encryption algorithms. | ||
const WRAPPING_KEY_SIZE: number = 32; | ||
|
||
export const configSchema = schema.object({ | ||
enabled: schema.boolean({ defaultValue: false }), | ||
wrappingKeyName: schema.string({ | ||
minLength: KEY_NAME_MIN_LENGTH, | ||
maxLength: KEY_NAME_MAX_LENGTH, | ||
defaultValue: 'wrappingKeyName', | ||
}), | ||
wrappingKeyNamespace: schema.string({ | ||
minLength: KEY_NAME_MIN_LENGTH, | ||
maxLength: KEY_NAME_MAX_LENGTH, | ||
defaultValue: 'wrappingKeyNamespace', | ||
}), | ||
wrappingKey: schema.arrayOf(schema.number(), { | ||
minSize: WRAPPING_KEY_SIZE, | ||
maxSize: WRAPPING_KEY_SIZE, | ||
}), | ||
}); | ||
|
||
export type DataSourcePluginConfigType = TypeOf<typeof configSchema>; |
20 changes: 20 additions & 0 deletions
20
src/plugins/data_source/server/cryptography/cryptography_client.test.ts
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,20 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { CryptographyClient } from './cryptography_client'; | ||
import { randomBytes } from 'crypto'; | ||
|
||
const dummyWrappingKeyName = 'dummy_wrapping_key_name'; | ||
const dummyWrappingKeyNamespace = 'dummy_wrapping_key_namespace'; | ||
|
||
test('Invalid wrapping key size throws error', () => { | ||
const dummyRandomBytes = [...randomBytes(31)]; | ||
const expectedErrorMsg = `Wrapping key size shoule be 32 bytes, as used in envelope encryption. Current wrapping key size: '${dummyRandomBytes.length}' bytes`; | ||
expect(() => { | ||
new CryptographyClient(dummyWrappingKeyName, dummyWrappingKeyNamespace, dummyRandomBytes); | ||
}).toThrowError(new Error(expectedErrorMsg)); | ||
}); | ||
|
||
// TODO: Add more test cases for encrypt / decrypt https://github.com/opensearch-project/OpenSearch-Dashboards/issues/2068 |
70 changes: 70 additions & 0 deletions
70
src/plugins/data_source/server/cryptography/cryptography_client.ts
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,70 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
buildClient, | ||
CommitmentPolicy, | ||
RawAesKeyringNode, | ||
RawAesWrappingSuiteIdentifier, | ||
} from '@aws-crypto/client-node'; | ||
|
||
export const ENCODING_STRATEGY: BufferEncoding = 'base64'; | ||
export const WRAPPING_KEY_SIZE: number = 32; | ||
|
||
export class CryptographyClient { | ||
private readonly _commitmentPolicy = CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT; | ||
private readonly _wrappingSuite = RawAesWrappingSuiteIdentifier.AES256_GCM_IV12_TAG16_NO_PADDING; | ||
|
||
private readonly _keyring: RawAesKeyringNode; | ||
|
||
private readonly _encrypt; | ||
private readonly _decrypt; | ||
|
||
/** | ||
* @param {string} wrappingKeyName | ||
* @param {string} wrappingKeyNamespace | ||
* @param {number[]} wrappingKey 32 Bytes raw wrapping key used to perform envelope encryption | ||
*/ | ||
constructor(wrappingKeyName: string, wrappingKeyNamespace: string, wrappingKey: number[]) { | ||
if (wrappingKey.length !== WRAPPING_KEY_SIZE) { | ||
const wrappingKeySizeMismatchMsg = `Wrapping key size shoule be 32 bytes, as used in envelope encryption. Current wrapping key size: '${wrappingKey.length}' bytes`; | ||
throw new Error(wrappingKeySizeMismatchMsg); | ||
} | ||
|
||
// Create raw AES keyring | ||
this._keyring = new RawAesKeyringNode({ | ||
keyName: wrappingKeyName, | ||
keyNamespace: wrappingKeyNamespace, | ||
unencryptedMasterKey: new Uint8Array(wrappingKey), | ||
wrappingSuite: this._wrappingSuite, | ||
}); | ||
|
||
// Destructuring encrypt and decrypt functions from client | ||
const { encrypt, decrypt } = buildClient(this._commitmentPolicy); | ||
|
||
this._encrypt = encrypt; | ||
this._decrypt = decrypt; | ||
} | ||
|
||
/** | ||
* Input text content and output encrypted string encoded with ENCODING_STRATEGY | ||
* @param {string} plainText | ||
* @returns {Promise} | ||
*/ | ||
public async encrypt(plainText: string | Buffer): Promise<string> { | ||
const result = await this._encrypt(this._keyring, plainText); | ||
return result.result.toString(ENCODING_STRATEGY); | ||
} | ||
|
||
/** | ||
* Input encrypted content and output decrypted string | ||
* @param {string} encrypted | ||
* @returns {Promise} | ||
*/ | ||
public async decrypt(encrypted: string): Promise<string> { | ||
const result = await this._decrypt(this._keyring, Buffer.from(encrypted, ENCODING_STRATEGY)); | ||
return result.plaintext.toString(); | ||
} | ||
} |
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,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { CryptographyClient } from './cryptography_client'; |
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
Oops, something went wrong.