Skip to content
Open
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
42 changes: 38 additions & 4 deletions lib/kms/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const vault = require('../auth/vault');
const Cache = require('./Cache');
const {
KmsProtocol,
KmsType,
makeBackend,
isScalityKmsArn,
extractDetailFromArn,
Expand Down Expand Up @@ -283,6 +284,22 @@ function belongsToMigrationSource(storedKey, migrationBackend) {
return !detail.type || getClientIdentifier(detail) === getClientIdentifier(migrationBackend);
}

/**
* Log fields naming the KMS of a failed request, and the key it used.
* A key without arn prefix is named with the arn of the KMS it was sent to.
* Keys of an internal KMS are not named: their KeyId is the master key.
* @param {object} kmsClient - client the request was sent to
* @param {string} [keyId] - KeyId the request used, from getClientForKey
* @returns {object} `{ provider, keyArn }`, without `keyArn` if no key was used or the KMS is internal
*/
function kmsFailureFields(kmsClient, keyId) {
const { type, provider, arnPrefix } = kmsClient.backend;
if (keyId === undefined || type === KmsType.internal) {
return { provider };
}
return { provider, keyArn: `${arnPrefix}${keyId}` };
}

class KMS {
/** Access to client for tests (to create a key without vault check and bucket) */
static get client() {
Expand Down Expand Up @@ -351,7 +368,12 @@ class KMS {
// Otherwise, create a default master encryption key, later its id will be stored at the bucket metadata level.
return client.createBucketKey(bucket.getName(), log, (err, masterKeyId, masterKeyArn) => {
if (err) {
log.debug('error from kms', { implName, error: err });
log.error('KMS failed to create a bucket key', {
implName,
...kmsFailureFields(client),
bucketName: bucket.getName(),
error: err,
});
return cb(err);
}
log.trace('bucket key created in kms');
Expand Down Expand Up @@ -436,7 +458,11 @@ class KMS {
}
return client.destroyBucketKey(key, log, err => {
if (err) {
log.debug('error from kms', { implName, error: err });
log.error('KMS failed to destroy a bucket key', {
implName,
...kmsFailureFields(client, key),
error: err,
});
return cb(err);
}
log.trace('bucket key destroyed in kms');
Expand Down Expand Up @@ -596,7 +622,11 @@ class KMS {
],
(err, cipherBundle) => {
if (err) {
log.error('error processing cipher bundle', { implName, error: err });
log.error('error processing cipher bundle', {
implName,
...kmsFailureFields(client, key),
error: err,
});
}
return cb(err, cipherBundle);
},
Expand Down Expand Up @@ -686,7 +716,11 @@ class KMS {
],
(err, decipherBundle) => {
if (err) {
log.error('error processing decipher bundle', { implName: _impl, error: err });
log.error('error processing decipher bundle', {
implName: _impl,
...kmsFailureFields(client, key),
error: err,
});
return cb(err);
}
return cb(err, decipherBundle);
Expand Down
Loading