Skip to content

Commit

Permalink
Optimize error handling and logging
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chu <[email protected]>
  • Loading branch information
noCharger committed Oct 11, 2022
1 parent 9f77d5e commit 3b35e77
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/plugins/data_source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ a. Envelope encryption - has multiple benefits including strong protection on da

b. Key derivation algorithm - HKDF with SHA-384, which “helps you avoid accidental reuse of a data encryption key and reduces the risk of overusing a data key.”

c. Signature algorithm - ECDSA with P-384 and SHA-384. Under multiple data source case, data source indices stored on OpenSearch can be modified / replaced by attacker. With ECDSA signature, ciphertext decryption will fail if it’s getting pullted. No one will be able to create another signature that verifies with the public key because the private key has been dropped.
c. Signature algorithm - ECDSA with P-384 and SHA-384. Under multiple data source case, data source documents stored on OpenSearch can be modified / replaced by attacker. With ECDSA signature, ciphertext decryption will fail if it’s getting pullted. No one will be able to create another signature that verifies with the public key because the private key has been dropped.

---

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data_source/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { fileAppenderSchema } from './audit_config';

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.
// Wrapping key size should be 32 bytes, as used in envelope encryption algorithms.
const WRAPPING_KEY_SIZE: number = 32;

export const configSchema = schema.object({
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/data_source/server/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ export const getCredential = async (

const { decryptedText, encryptionContext } = await cryptography
.decodeAndDecrypt(password)
.catch(() => {
throw new Error(
'Encrypted "auth.credentials.password" contaminated. Please delete and create another data source.'
);
.catch((err: any) => {
// Re-throw as DataSourceConfigError
throw new DataSourceConfigError('Unable to decrypt "auth.credentials.password".', err);
});

if (encryptionContext!.endpoint !== endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Cryptography Service', () => {
},
} as DataSourcePluginConfigType;

const expectedErrorMsg = `Wrapping key size shoule be 32 bytes, as used in envelope encryption. Current wrapping key size: '${config.encryption.wrappingKey.length}' bytes`;
const expectedErrorMsg = `Wrapping key size should be 32 bytes, as used in envelope encryption. Current wrapping key size: '${config.encryption.wrappingKey.length}' bytes`;

expect(() => {
service.setup(config);
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/data_source/server/cryptography_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import {
RawAesKeyringNode,
RawAesWrappingSuiteIdentifier,
} from '@aws-crypto/client-node';

import { Logger } from '../../../../src/core/server';

import { DataSourcePluginConfigType } from '../config';

export const ENCODING_STRATEGY: BufferEncoding = 'base64';
Expand Down Expand Up @@ -44,7 +42,7 @@ export class CryptographyService {
const { wrappingKeyName, wrappingKeyNamespace, wrappingKey } = config.encryption;

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`;
const wrappingKeySizeMismatchMsg = `Wrapping key size should be 32 bytes, as used in envelope encryption. Current wrapping key size: '${wrappingKey.length}' bytes`;
this.logger.error(wrappingKeySizeMismatchMsg);
throw new Error(wrappingKeySizeMismatchMsg);
}
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data_source/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
} from '../../../../src/core/server';
import { DataSourcePluginConfigType } from '../config';
import { LoggingAuditor } from './audit/logging_auditor';

import { CryptographyService, CryptographyServiceSetup } from './cryptography_service';
import { DataSourceService, DataSourceServiceSetup } from './data_source_service';
import { DataSourceSavedObjectsClientWrapper, dataSource } from './saved_objects';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ import {
SavedObjectsUpdateOptions,
SavedObjectsUpdateResponse,
} from 'opensearch-dashboards/server';

import { Logger, SavedObjectsErrorHelpers } from '../../../../../src/core/server';

import { DATA_SOURCE_SAVED_OBJECT_TYPE } from '../../common';
import { AuthType } from '../../common/data_sources';

import { EncryptionContext, CryptographyServiceSetup } from '../cryptography_service';

/**
Expand Down Expand Up @@ -296,8 +293,10 @@ export class DataSourceSavedObjectsClientWrapper {
});
attributes = savedObject.attributes;
} catch (err: any) {
// this.logger.error(err);
throw err;
const errMsg = `Fail to fetch existing data source for dataSourceId [${id}]`;
this.logger.error(errMsg);
this.logger.error(err);
throw SavedObjectsErrorHelpers.decorateBadRequestError(err, errMsg);
}

if (!attributes) {
Expand Down Expand Up @@ -350,10 +349,11 @@ export class DataSourceSavedObjectsClientWrapper {

const { encryptionContext } = await this.cryptography
.decodeAndDecrypt(password)
.catch(() => {
throw SavedObjectsErrorHelpers.createBadRequestError(
'Update failed due to deprecated data source: encrypted "auth.credentials.password" contaminated. Please delete and create another data source.'
);
.catch((err: any) => {
const errMsg = `Fail to update existing data source for dataSourceId [${id}]: unable to decrypt "auth.credentials.password"`;
this.logger.error(errMsg);
this.logger.error(err);
throw SavedObjectsErrorHelpers.decorateBadRequestError(err, errMsg);
});

if (encryptionContext.endpoint !== endpoint) {
Expand Down

0 comments on commit 3b35e77

Please sign in to comment.