Repository navigation
CLDSRV-1056: Fail a self-copy when no known master key decrypts its data #6351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dvasilas
wants to merge
2
commits into
development/9.3
Choose a base branch
from
bugfix/CLDSRV-1056-keep-data-of-damaged-object-on-self-copy
base: development/9.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,64 @@ | ||
| const { PassThrough } = require('stream'); | ||
| const async = require('async'); | ||
| const { jsutil } = require('arsenal'); | ||
|
|
||
| const { data } = require('../../../data/wrapper'); | ||
|
|
||
| /** | ||
| * Sequentially GET the ordered source `dataLocator` parts into a single | ||
| * readable stream, reading them in order through `data.get`. Returns the | ||
| * PassThrough immediately; consumers should pipe it onward and observe | ||
| * `error` for any read failure along the way. | ||
| * | ||
| * @param {Array} dataLocator - ordered source parts | ||
| * @param {object} log - request logger | ||
| * @return {PassThrough} | ||
| */ | ||
| function buildSourcePartsStream(dataLocator, log) { | ||
| const passthrough = new PassThrough(); | ||
| const wrapErr = (err, part) => | ||
| Object.assign(err, { | ||
| copyPart: { key: part.key, dataStoreName: part.dataStoreName, dataStoreType: part.dataStoreType }, | ||
| }); | ||
| async.eachSeries( | ||
| dataLocator, | ||
| (part, cb) => { | ||
| const done = jsutil.once(cb); | ||
| if (part.dataStoreType === 'azure') { | ||
| // Azure's data.get writes part bytes into the provided writable | ||
| // instead of returning a Readable. Pipe a per-part PassThrough | ||
| // into the master passthrough and use its 'end' as the completion | ||
| // signal — same pattern arsenal's data.copyObject uses. | ||
| const perPart = new PassThrough(); | ||
| perPart.once('error', err => done(wrapErr(err, part))); | ||
| perPart.once('end', () => done()); | ||
| perPart.pipe(passthrough, { end: false }); | ||
| return data.get(part, perPart, log, err => { | ||
| if (err) { | ||
| perPart.destroy(err); | ||
| done(wrapErr(err, part)); | ||
| } | ||
| }); | ||
| } | ||
| return data.get(part, null, log, (err, partStream) => { | ||
| if (err) { | ||
| return done(wrapErr(err, part)); | ||
| } | ||
| partStream.once('error', err => done(wrapErr(err, part))); | ||
| partStream.once('end', () => done()); | ||
| partStream.pipe(passthrough, { end: false }); | ||
| return undefined; | ||
| }); | ||
| }, | ||
|
dvasilas marked this conversation as resolved.
Dismissed
|
||
| err => { | ||
| if (err) { | ||
| passthrough.destroy(err); | ||
| } else { | ||
| passthrough.end(); | ||
| } | ||
| }, | ||
| ); | ||
| return passthrough; | ||
| } | ||
|
|
||
| module.exports = { buildSourcePartsStream }; | ||
This file contains hidden or 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,126 @@ | ||
| const async = require('async'); | ||
| const crypto = require('crypto'); | ||
| const { jsutil } = require('arsenal'); | ||
|
|
||
| const kms = require('../../../kms/wrapper'); | ||
| const { buildSourcePartsStream } = require('./sourceChecksum'); | ||
|
|
||
| // `<part number>:<MD5 of the part's plaintext>` | ||
| const partETagPattern = /^\d+:([0-9a-f]{32})$/; | ||
|
|
||
| /** | ||
| * Find the smallest encrypted part of the data that has an MD5. A part is an | ||
| * MPU part, or the whole object if it was not uploaded in parts. | ||
| * | ||
| * An MPU part written by UploadPartCopy can span several locations, and each | ||
| * of them carries the MD5 of the whole part in its dataStoreETag. So a part is | ||
| * the run of consecutive locations that share a dataStoreETag. | ||
| * | ||
| * An empty part decrypts to the same MD5 under any key, so it is picked only if | ||
| * every other part is empty too. | ||
| * | ||
| * @param {Array} dataLocator - source data locations | ||
| * @returns {object|null} the part's locations and MD5, or null if no | ||
| * encrypted part has an MD5 | ||
| */ | ||
| function _smallestPart(dataLocator) { | ||
| const parts = []; | ||
| dataLocator.forEach(location => { | ||
| const part = parts[parts.length - 1]; | ||
| if (part && location.dataStoreETag === part.dataStoreETag) { | ||
| part.locations.push(location); | ||
| part.size += Number(location.size); | ||
| } else { | ||
| parts.push({ | ||
| dataStoreETag: location.dataStoreETag, | ||
| locations: [location], | ||
| size: Number(location.size), | ||
| }); | ||
| } | ||
| }); | ||
| const rank = part => (part.size === 0 ? Infinity : part.size); | ||
| return parts.reduce((smallest, part) => { | ||
| const match = partETagPattern.exec(part.dataStoreETag); | ||
| if (!match || !part.locations.every(location => location.cipheredDataKey)) { | ||
| return smallest; | ||
| } | ||
| if (smallest && rank(smallest) <= rank(part)) { | ||
| return smallest; | ||
| } | ||
| return { locations: part.locations, md5: match[1], size: part.size }; | ||
| }, null); | ||
| } | ||
|
|
||
| /** | ||
| * Compute the MD5 of the locations' data, decrypted with masterKeyId. | ||
| * | ||
| * @param {Array} locations - data locations, in order | ||
| * @param {string} masterKeyId - master key to decrypt the data with | ||
| * @param {object} log - request logger | ||
| * @param {function} cb - cb(err, md5): md5 is the hex digest | ||
| * @returns {undefined} | ||
| */ | ||
| function _md5WithKey(locations, masterKeyId, log, cb) { | ||
| const done = jsutil.once(cb); | ||
| const hash = crypto.createHash('md5'); | ||
| const stream = buildSourcePartsStream( | ||
| locations.map(location => ({ ...location, masterKeyId })), | ||
| log, | ||
| ); | ||
| stream.on('data', chunk => hash.update(chunk)); | ||
| stream.once('error', done); | ||
| stream.once('end', () => done(null, hash.digest('hex'))); | ||
| } | ||
|
dvasilas marked this conversation as resolved.
Dismissed
|
||
|
|
||
| /** | ||
| * Find which master key decrypts the source data correctly: read one part, | ||
| * decrypted with each candidate key in turn, and compare its MD5 with the | ||
| * MD5 recorded for the part. | ||
| * | ||
| * @param {Array} dataLocator - source data locations | ||
| * @param {Array} candidateKeyIds - master key IDs, in the order to try | ||
| * @param {object} log - request logger | ||
| * @param {function} cb - cb(err, masterKeyId, reason): masterKeyId is the | ||
| * first candidate that decrypts the data, or null with the reason when none | ||
| * does or the data has no MD5 to check; err is the error of the read with the | ||
| * first candidate, returned when no other candidate decrypts the data | ||
| * @returns {undefined} | ||
| */ | ||
| function findSourceMasterKey(dataLocator, candidateKeyIds, log, cb) { | ||
| const part = _smallestPart(dataLocator); | ||
| if (!part) { | ||
| return process.nextTick(cb, null, null, 'no MD5 recorded for the encrypted data'); | ||
| } | ||
| const keyIds = candidateKeyIds.filter( | ||
| (keyId, i) => keyId && !candidateKeyIds.slice(0, i).some(other => kms.isSameMasterKey(other, keyId, log)), | ||
| ); | ||
| let firstReadError = null; | ||
| return async.detectSeries( | ||
| keyIds, | ||
| (keyId, next) => | ||
| _md5WithKey(part.locations, keyId, log, (err, md5) => { | ||
| if (err) { | ||
| log.debug('cannot read the source data with a candidate master key', { | ||
| masterKeyId: keyId, | ||
| error: err, | ||
| }); | ||
| if (keyId === keyIds[0]) { | ||
| firstReadError = err; | ||
| } | ||
| return next(null, false); | ||
| } | ||
| return next(null, md5 === part.md5); | ||
| }), | ||
|
dvasilas marked this conversation as resolved.
Dismissed
|
||
| (err, keyId) => { | ||
| if (keyId) { | ||
| return cb(null, keyId); | ||
| } | ||
| if (firstReadError) { | ||
| return cb(firstReadError); | ||
| } | ||
| return cb(null, null, 'no candidate master key decrypts the data'); | ||
| }, | ||
| ); | ||
| } | ||
|
dvasilas marked this conversation as resolved.
Dismissed
|
||
|
|
||
| module.exports = { findSourceMasterKey, _smallestPart }; | ||
This file contains hidden or 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 hidden or 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,45 @@ | ||
| const assert = require('assert'); | ||
|
|
||
| const { _smallestPart } = require('../../../../../lib/api/apiUtils/object/sourceMasterKey'); | ||
|
|
||
| const md5A = 'a'.repeat(32); | ||
| const md5B = 'b'.repeat(32); | ||
| const md5C = 'c'.repeat(32); | ||
| const md5Empty = 'd41d8cd98f00b204e9800998ecf8427e'; | ||
| const encrypted = (key, size, dataStoreETag) => ({ key, size, dataStoreETag, cipheredDataKey: 'ZGF0YWtleQ==' }); | ||
|
|
||
| describe('_smallestPart', () => { | ||
| it('should pick the smallest part, with every location of a part copied from several locations', () => { | ||
| const dataLocator = [ | ||
| encrypted(1, 10, `1:${md5A}`), | ||
| encrypted(2, 4, `2:${md5B}`), | ||
| encrypted(3, 2, `2:${md5B}`), | ||
| encrypted(4, 8, `3:${md5C}`), | ||
| ]; | ||
| assert.deepStrictEqual(_smallestPart(dataLocator), { | ||
| locations: [dataLocator[1], dataLocator[2]], | ||
| md5: md5B, | ||
| size: 6, | ||
| }); | ||
| }); | ||
|
|
||
| it('should skip parts with an unencrypted location or no MD5', () => { | ||
| const dataLocator = [ | ||
| { key: 1, size: 1, dataStoreETag: `1:${md5A}` }, | ||
| encrypted(2, 2, undefined), | ||
| encrypted(3, 3, `${md5B}-2`), | ||
| encrypted(4, 9, `4:${md5C}`), | ||
| ]; | ||
| assert.deepStrictEqual(_smallestPart(dataLocator).locations, [dataLocator[3]]); | ||
| }); | ||
|
|
||
| it('should pick a part with data over a smaller empty part', () => { | ||
| const dataLocator = [encrypted(1, 10, `1:${md5A}`), encrypted(2, 0, `2:${md5Empty}`)]; | ||
| assert.deepStrictEqual(_smallestPart(dataLocator).locations, [dataLocator[0]]); | ||
| }); | ||
|
|
||
| it('should pick an empty part when every part is empty', () => { | ||
| const dataLocator = [encrypted(1, 0, `1:${md5Empty}`)]; | ||
| assert.deepStrictEqual(_smallestPart(dataLocator).locations, [dataLocator[0]]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.