Skip to content

Commit

Permalink
make supportedLifecycleRules configurable through the config
Browse files Browse the repository at this point in the history
This will help when unifying Cloudserver, as currently the value
of the rules is a constant in Arsenal that is different between the
7.x and 8.x branches.

Issue: CLDSRV-588
  • Loading branch information
Kerkesni committed Dec 12, 2024
1 parent a240104 commit 8a0659d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
7 changes: 7 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ const constants = {
// if requester is not bucket owner, bucket policy actions should be denied with
// MethodNotAllowed error
onlyOwnerAllowed: ['bucketDeletePolicy', 'bucketGetPolicy', 'bucketPutPolicy'],
supportedLifecycleRules: [
'expiration',
'noncurrentVersionExpiration',
'abortIncompleteMultipartUpload',
'transitions',
'noncurrentVersionTransition',
],
};

module.exports = constants;
23 changes: 21 additions & 2 deletions lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ const { isValidBucketName } = s3routes.routesUtils;
const validateAuthConfig = arsenalAuth.inMemory.validateAuthConfig;
const { buildAuthDataAccount } = require('./auth/in_memory/builder');
const validExternalBackends = require('../constants').externalBackends;
const { azureAccountNameRegex, base64Regex,
allowedUtapiEventFilterFields, allowedUtapiEventFilterStates,
const {
azureAccountNameRegex,
base64Regex,
allowedUtapiEventFilterFields,
allowedUtapiEventFilterStates,
supportedLifecycleRules,
} = require('../constants');
const { utapiVersion } = require('utapi');
const { scaleMsPerDay } = s3middleware.objectUtils;
Expand Down Expand Up @@ -148,6 +152,18 @@ function parseRedisConfig(redisConfig) {
return joi.attempt(redisConfig, joiSchema, 'bad config');
}

function parseSupportedLifecycleRules(supportedLifecycleRulesConfig) {
const supportedLifecycleRulesSchema = joi.array()
.items(joi.string().valid(...supportedLifecycleRules))
.default(supportedLifecycleRules)
.min(1);
return joi.attempt(
supportedLifecycleRulesConfig,
supportedLifecycleRulesSchema,
'bad supported lifecycle rules config',
);
}

function restEndpointsAssert(restEndpoints, locationConstraints) {
assert(typeof restEndpoints === 'object',
'bad config: restEndpoints must be an object of endpoints');
Expand Down Expand Up @@ -1713,6 +1729,8 @@ class Config extends EventEmitter {
'bad config: maxScannedLifecycleListingEntries must be greater than 2');
this.maxScannedLifecycleListingEntries = config.maxScannedLifecycleListingEntries;
}

this.supportedLifecycleRules = parseSupportedLifecycleRules(config.supportedLifecycleRules);
}

_setTimeOptions() {
Expand Down Expand Up @@ -1978,4 +1996,5 @@ module.exports = {
azureGetStorageAccountName,
azureGetLocationCredentials,
azureArchiveLocationConstraintAssert,
parseSupportedLifecycleRules,
};
3 changes: 1 addition & 2 deletions lib/api/apiUtils/object/expirationHeaders.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { supportedLifecycleRules } = require('arsenal').constants;
const { LifecycleConfiguration } = require('arsenal').models;
const {
LifecycleDateTime,
Expand All @@ -19,7 +18,7 @@ const lifecycleDateTime = new LifecycleDateTime({
timeProgressionFactor,
});

const lifecycleUtils = new LifecycleUtils(supportedLifecycleRules, lifecycleDateTime, timeProgressionFactor);
const lifecycleUtils = new LifecycleUtils(config.supportedLifecycleRules, lifecycleDateTime, timeProgressionFactor);

function calculateDate(objDate, expDays, datetime) {
return new Date(datetime.getTimestamp(objDate) + (expDays * scaledMsPerDay));
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/Config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const assert = require('assert');
const {
azureArchiveLocationConstraintAssert,
parseSupportedLifecycleRules,
ConfigObject: ConfigObjectForTest,
} = require('../../lib/Config');

const {
supportedLifecycleRules,
} = require('../../constants');

describe('Config', () => {
const envToRestore = [];
const setEnv = (key, value) => {
Expand Down Expand Up @@ -668,4 +673,30 @@ describe('Config', () => {
assert.throws(() => azureArchiveLocationConstraintAssert(locationObj));
});
});

describe('parseSupportedLifecycleRules', () => {
it('should throw when an empty array is provided', () => {
assert.throws(() => parseSupportedLifecycleRules([]));
});

it('should use default values when no value is provided', () => {
const rules = parseSupportedLifecycleRules(undefined);
assert.deepStrictEqual(rules, [...supportedLifecycleRules]);
});

it('should throw when rule name is not valid', () => {
const rules = ['invalid'];
assert.throws(() => parseSupportedLifecycleRules(rules));
});

it('should return the rules provided when they are valid', () => {
const rules = [
'expiration',
'noncurrentVersionExpiration',
'abortIncompleteMultipartUpload',
];
const parsedRules = parseSupportedLifecycleRules(rules);
assert.deepStrictEqual(parsedRules, rules);
});
});
});

0 comments on commit 8a0659d

Please sign in to comment.