Skip to content
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

S3UTILS-146: Recover from specific listing parsing errors #302

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions bucketVersionsStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { doWhilst } = require('async');
const { Logger } = require('werelogs');

const parseOlderThan = require('./utils/parseOlderThan');
const { safeListObjectVersions } = require('./utils/safeList');

const log = new Logger('s3utils::bucketVersionsStats');
const { ENDPOINT } = process.env;
Expand Down Expand Up @@ -166,7 +167,7 @@ const logProgressInterval = setInterval(
);

function _listObjectVersions(bucket, KeyMarker, VersionIdMarker, cb) {
return s3.listObjectVersions({
return safeListObjectVersions(s3, {
Bucket: bucket,
MaxKeys: LISTING_LIMIT,
Prefix: TARGET_PREFIX,
Expand All @@ -191,8 +192,11 @@ function listBucket(bucket, cb) {
return done(err);
}
for (const version of data.Versions) {
if (_OLDER_THAN_TIMESTAMP && new Date(version.LastModified) > _OLDER_THAN_TIMESTAMP) {
continue;
if (_OLDER_THAN_TIMESTAMP) {
const parsed = new Date(version.LastModified);
if (Number.isNaN(parsed.getTime()) || parsed > _OLDER_THAN_TIMESTAMP) {
continue;
}
}
const statObj = version.IsLatest ? stats.current : stats.noncurrent;
statObj.count += 1;
Expand Down
15 changes: 12 additions & 3 deletions cleanupNoncurrentVersions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { Logger } = require('werelogs');

const BackbeatClient = require('./BackbeatClient');
const parseOlderThan = require('./utils/parseOlderThan');
const { safeListObjectVersions } = require('./utils/safeList');

const log = new Logger('s3utils::cleanupNoncurrentVersions');

Expand Down Expand Up @@ -243,7 +244,7 @@ const logProgressInterval = setInterval(
);

function _listObjectVersions(bucket, VersionIdMarker, KeyMarker, cb) {
return s3.listObjectVersions({
return safeListObjectVersions(s3, {
Bucket: bucket,
MaxKeys: LISTING_LIMIT,
Prefix: TARGET_PREFIX,
Expand Down Expand Up @@ -272,11 +273,19 @@ function _getMetadata(bucket, key, versionId, cb) {
}

function _lastModifiedIsEligible(lastModifiedString) {
return !OLDER_THAN || (new Date(lastModifiedString) < OLDER_THAN);
const ts = new Date(lastModifiedString);
if (Number.isNaN(ts.getTime())) {
return false;
}
return !OLDER_THAN || (ts < OLDER_THAN);
}

function _deleteMarkerIsEligible(lastModifiedString) {
return !DELETED_BEFORE || (new Date(lastModifiedString) < DELETED_BEFORE);
const ts = new Date(lastModifiedString);
if (Number.isNaN(ts.getTime())) {
return false;
}
return !DELETED_BEFORE || (ts < DELETED_BEFORE);
}

let deleteQueue = [];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s3utils",
"version": "1.14.2",
"version": "1.14.3",
"engines": {
"node": ">= 16"
},
Expand Down Expand Up @@ -42,6 +42,7 @@
"node-uuid": "^1.4.8",
"vaultclient": "scality/vaultclient#8.2.6",
"werelogs": "scality/werelogs#8.1.2",
"xml2js": "^0.6.2",
"zenkoclient": "scality/zenkoclient#1.2.1"
},
"resolutions": {
Expand Down
311 changes: 311 additions & 0 deletions tests/unit/utils/safeList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
const xml2js = require('xml2js');
const { recoverListing } = require('../../../utils/safeList');


describe('test safeListObjectVersions', () => {
const testCases = [
{
description: 'should parse valid empty response',
response: {
ListVersionsResult: {
Name: 'my-bucket',
IsTruncated: false,
MaxKeys: 1000,
},
},
expectedData: {
Name: 'my-bucket',
IsTruncated: false,
MaxKeys: 1000,
Versions: [],
DeleteMarkers: [],
CommonPrefixes: [],
},
},
{
description: 'should return error for invalid empty response',
response: {},
expectedError: new Error('im an original error'),
},
{
description: 'should parse truncated response',
response: {
ListVersionsResult: {
Name: 'my-bucket',
IsTruncated: true,
MaxKeys: 1000,
NextKeyMarker: 'my-next-key-marker',
NextVersionIdMarker: 'my-next-version-id-marker',
Version: [
{
Key: 'my-key',
VersionId: 'my-version-id',
IsLatest: true,
LastModified: '2021-01-01T00:00:00.000Z',
ETag: 'my-etag',
Size: 123,
StorageClass: 'STANDARD',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
DeleteMarker: [
{
Key: 'my-deleted-key',
VersionId: 'my-deleted-version-id',
IsLatest: true,
LastModified: '2021-01-01T00:00:00.000Z',
ETag: 'my-etag',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
CommonPrefix: [
{
Prefix: 'my-prefix',
},
],
},
},
expectedData: {
Name: 'my-bucket',
IsTruncated: true,
MaxKeys: 1000,
NextKeyMarker: 'my-next-key-marker',
NextVersionIdMarker: 'my-next-version-id-marker',
Versions: [
{
Key: 'my-key',
VersionId: 'my-version-id',
IsLatest: true,
LastModified: new Date('2021-01-01T00:00:00.000Z'),
ETag: 'my-etag',
Size: 123,
StorageClass: 'STANDARD',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
DeleteMarkers: [
{
Key: 'my-deleted-key',
VersionId: 'my-deleted-version-id',
IsLatest: true,
LastModified: new Date('2021-01-01T00:00:00.000Z'),
ETag: 'my-etag',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
CommonPrefixes: [
{
Prefix: 'my-prefix',
},
],
},
},
{
description: 'should recover bad last-modified date',
response: {
ListVersionsResult: {
Name: 'my-bucket',
IsTruncated: true,
MaxKeys: 1000,
NextKeyMarker: 'my-next-key-marker',
NextVersionIdMarker: 'my-next-version-id-marker',
Version: [
{
Key: 'my-key',
VersionId: 'my-version-id',
IsLatest: true,
LastModified: 'undefined',
ETag: 'my-etag',
Size: 123,
StorageClass: 'STANDARD',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
DeleteMarker: [
// LastModified is missing
{
Key: 'my-deleted-key',
VersionId: 'my-deleted-version-id',
IsLatest: true,
ETag: 'my-etag',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
CommonPrefix: [],
},

},
expectedData: {
Name: 'my-bucket',
IsTruncated: true,
MaxKeys: 1000,
NextKeyMarker: 'my-next-key-marker',
NextVersionIdMarker: 'my-next-version-id-marker',
Versions: [
{
Key: 'my-key',
VersionId: 'my-version-id',
IsLatest: true,
LastModified: 'undefined',
ETag: 'my-etag',
Size: 123,
StorageClass: 'STANDARD',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
DeleteMarkers: [
{
Key: 'my-deleted-key',
VersionId: 'my-deleted-version-id',
IsLatest: true,
ETag: 'my-etag',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
CommonPrefixes: [],
},
},
{
description: 'should recover bad content-length',
response: {
ListVersionsResult: {
Name: 'my-bucket',
IsTruncated: true,
MaxKeys: 1000,
NextKeyMarker: 'my-next-key-marker',
NextVersionIdMarker: 'my-next-version-id-marker',
Version: [
{
Key: 'my-key',
VersionId: 'my-version-id',
IsLatest: true,
LastModified: '2021-01-01T00:00:00.000Z',
ETag: 'my-etag',
Size: 'foobar',
StorageClass: 'STANDARD',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
DeleteMarker: [
{
Key: 'my-deleted-key',
VersionId: 'my-deleted-version-id',
IsLatest: true,
LastModified: '2021-01-01T00:00:00.000Z',
ETag: 'my-etag',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
CommonPrefix: [],
},
},
expectedData: {
Name: 'my-bucket',
IsTruncated: true,
MaxKeys: 1000,
NextKeyMarker: 'my-next-key-marker',
NextVersionIdMarker: 'my-next-version-id-marker',
Versions: [
{
Key: 'my-key',
VersionId: 'my-version-id',
IsLatest: true,
LastModified: new Date('2021-01-01T00:00:00.000Z'),
ETag: 'my-etag',
Size: 'foobar',
StorageClass: 'STANDARD',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
DeleteMarkers: [
{
Key: 'my-deleted-key',
VersionId: 'my-deleted-version-id',
IsLatest: true,
LastModified: new Date('2021-01-01T00:00:00.000Z'),
ETag: 'my-etag',
Owner: {
ID: 'my-id',
DisplayName: 'my-display-name',
},
},
],
CommonPrefixes: [],
},
},
{
description: 'should pass through original error is response is not recoverable',
response: {},
expectedError: new Error('im an original error'),
},
];


testCases.forEach(testCase => {
it(testCase.description, done => {
const xmlBuilder = new xml2js.Builder();
const body = Buffer.from(xmlBuilder.buildObject(testCase.response));
const resp = {
httpResponse: {
body,
},
error: testCase.expectedError || null,
};

recoverListing(resp, (err, data) => {
expect(err).toStrictEqual(testCase.expectedError || null);
expect(data).toStrictEqual(testCase.expectedData);
done();
});
});
});

it('should pass through original error if the XML fails to parse', done => {
const error = new Error('im an original error');
const resp = {
httpResponse: {
body: Buffer.from('im not xml'),
},
error,
};

recoverListing(resp, (err, data) => {
expect(err).toStrictEqual(error);
expect(data).toStrictEqual(undefined);
done();
});
});
});
Loading
Loading