@blamejs/core 0.6.37 → 0.6.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,11 +52,13 @@
52
52
  var { URL } = require("url");
53
53
  var nodeCrypto = require("crypto");
54
54
  var C = require("../constants");
55
+ var requestHelpers = require("../request-helpers");
55
56
  var sigv4 = require("./sigv4");
56
57
  var safeXml = require("../parsers/safe-xml");
57
58
  var safeUrl = require("../safe-url");
58
59
  var template = require("../template");
59
60
  var httpClient = require("../http-client");
61
+ var validateOpts = require("../validate-opts");
60
62
  var { ObjectStoreError } = require("../framework-error");
61
63
 
62
64
  var _err = ObjectStoreError.factory;
@@ -269,12 +271,141 @@ function _buildCorsXml(rules) {
269
271
  return body;
270
272
  }
271
273
 
274
+ // ---- Object Lock + Retention + LegalHold validators / XML ----
275
+
276
+ var OBJECT_LOCK_MODES = ["GOVERNANCE", "COMPLIANCE"];
277
+ var LEGAL_HOLD_STATES = ["ON", "OFF"];
278
+
279
+ function _validateObjectLockConfig(cfg) {
280
+ if (!cfg || typeof cfg !== "object") {
281
+ throw _err("INVALID_OBJECT_LOCK",
282
+ "setObjectLockConfiguration: opts must be an object " +
283
+ "with { mode, days|years }", true);
284
+ }
285
+ if (OBJECT_LOCK_MODES.indexOf(cfg.mode) === -1) {
286
+ throw _err("INVALID_OBJECT_LOCK",
287
+ "mode must be one of " + OBJECT_LOCK_MODES.join(", ") +
288
+ "; got " + JSON.stringify(cfg.mode), true);
289
+ }
290
+ var hasDays = cfg.days != null;
291
+ var hasYears = cfg.years != null;
292
+ if (hasDays && hasYears) {
293
+ throw _err("INVALID_OBJECT_LOCK",
294
+ "specify either days OR years, not both (S3 rule)", true);
295
+ }
296
+ if (!hasDays && !hasYears) {
297
+ throw _err("INVALID_OBJECT_LOCK",
298
+ "default retention requires days or years", true);
299
+ }
300
+ if (hasDays) {
301
+ if (typeof cfg.days !== "number" || !Number.isInteger(cfg.days) ||
302
+ cfg.days <= 0) {
303
+ throw _err("INVALID_OBJECT_LOCK",
304
+ "days must be a positive integer; got " + JSON.stringify(cfg.days), true);
305
+ }
306
+ } else {
307
+ if (typeof cfg.years !== "number" || !Number.isInteger(cfg.years) ||
308
+ cfg.years <= 0) {
309
+ throw _err("INVALID_OBJECT_LOCK",
310
+ "years must be a positive integer; got " + JSON.stringify(cfg.years), true);
311
+ }
312
+ }
313
+ }
314
+
315
+ function _buildObjectLockConfigXml(cfg) {
316
+ var body = '<?xml version="1.0" encoding="UTF-8"?>';
317
+ body += '<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
318
+ body += '<ObjectLockEnabled>Enabled</ObjectLockEnabled>';
319
+ body += '<Rule><DefaultRetention>';
320
+ body += '<Mode>' + cfg.mode + '</Mode>';
321
+ if (cfg.days != null) body += '<Days>' + cfg.days + '</Days>';
322
+ if (cfg.years != null) body += '<Years>' + cfg.years + '</Years>';
323
+ body += '</DefaultRetention></Rule>';
324
+ body += '</ObjectLockConfiguration>';
325
+ return body;
326
+ }
327
+
328
+ function _validateRetention(opts) {
329
+ if (!opts || typeof opts !== "object") {
330
+ throw _err("INVALID_RETENTION",
331
+ "setObjectRetention: opts must be an object " +
332
+ "with { mode, retainUntil }", true);
333
+ }
334
+ if (OBJECT_LOCK_MODES.indexOf(opts.mode) === -1) {
335
+ throw _err("INVALID_RETENTION",
336
+ "mode must be one of " + OBJECT_LOCK_MODES.join(", ") +
337
+ "; got " + JSON.stringify(opts.mode), true);
338
+ }
339
+ if (!(opts.retainUntil instanceof Date) || isNaN(opts.retainUntil.getTime())) {
340
+ throw _err("INVALID_RETENTION",
341
+ "retainUntil must be a valid Date instance", true);
342
+ }
343
+ if (opts.retainUntil.getTime() <= Date.now()) {
344
+ throw _err("INVALID_RETENTION",
345
+ "retainUntil must be in the future", true);
346
+ }
347
+ }
348
+
349
+ function _buildRetentionXml(opts) {
350
+ return '<?xml version="1.0" encoding="UTF-8"?>' +
351
+ '<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
352
+ '<Mode>' + opts.mode + '</Mode>' +
353
+ '<RetainUntilDate>' + opts.retainUntil.toISOString() + '</RetainUntilDate>' +
354
+ '</Retention>';
355
+ }
356
+
357
+ function _validateLegalHoldStatus(status) {
358
+ if (LEGAL_HOLD_STATES.indexOf(status) === -1) {
359
+ throw _err("INVALID_LEGAL_HOLD",
360
+ "legal-hold status must be one of " + LEGAL_HOLD_STATES.join(", ") +
361
+ "; got " + JSON.stringify(status), true);
362
+ }
363
+ }
364
+
365
+ function _buildLegalHoldXml(status) {
366
+ return '<?xml version="1.0" encoding="UTF-8"?>' +
367
+ '<LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
368
+ '<Status>' + status + '</Status>' +
369
+ '</LegalHold>';
370
+ }
371
+
372
+ // S3 + MinIO surface "this lock-related state was never set" via two
373
+ // distinct error codes (and HTTP statuses), depending on whether the
374
+ // query is at the bucket level or per-object: bucket-level returns 404 +
375
+ // `ObjectLockConfigurationNotFoundError`; per-object retention/legal-hold
376
+ // returns 4xx + `NoSuchObjectLockConfiguration`. Both translate to the
377
+ // same operator answer: "not set". This helper recognizes both so the
378
+ // `get*` methods can surface a clean default instead of throwing.
379
+ function _isLockNotConfigured(err) {
380
+ if (!err) return false;
381
+ if (err.statusCode !== 404 && err.statusCode !== 400) return false;
382
+ var msg = String(err.message || "");
383
+ return msg.indexOf("ObjectLockConfigurationNotFoundError") !== -1 ||
384
+ msg.indexOf("NoSuchObjectLockConfiguration") !== -1;
385
+ }
386
+
387
+ function _validateObjectKey(key) {
388
+ if (typeof key !== "string" || key.length === 0) {
389
+ throw _err("INVALID_KEY", "object key must be a non-empty string", true);
390
+ }
391
+ if (key.length > 1024) {
392
+ throw _err("INVALID_KEY", "object key exceeds 1024 bytes (S3 limit)", true);
393
+ }
394
+ }
395
+
272
396
  // ---- Public factory ----
273
397
 
274
398
  function create(config) {
275
399
  if (!config || typeof config !== "object") {
276
400
  throw _err("INVALID_CONFIG", "bucketOps.create requires a config object", true);
277
401
  }
402
+ validateOpts(config, [
403
+ "protocol", "region", "accessKeyId", "secretAccessKey", "sessionToken",
404
+ "endpoint", "pathStyle", "forcePathStyle",
405
+ "allowedProtocols", "allowInternal", "timeoutMs",
406
+ "ca",
407
+ "audit", "observability", "auditSuccess", "auditFailures",
408
+ ], "bucketOps");
278
409
  if (config.protocol && config.protocol !== "sigv4") {
279
410
  throw _err("INVALID_CONFIG",
280
411
  "bucketOps currently only supports protocol 'sigv4'; got '" +
@@ -298,21 +429,88 @@ function create(config) {
298
429
  var reqOpts = { timeoutMs: config.timeoutMs, allowedProtocols: allowedProtocols };
299
430
  if (allowInternal !== null) reqOpts.allowInternal = allowInternal;
300
431
 
432
+ // Audit + observability are framework-best-practice — wired-on by
433
+ // default (no operator action needed beyond passing `audit: b.audit`),
434
+ // failure-audit always on, success-audit on by default for compliance
435
+ // workloads (SEC 17a-4 / FINRA / HIPAA require a trail of
436
+ // who-changed-the-retention-policy-and-when). Operators with extreme
437
+ // call rates can opt out of success-audit via auditSuccess: false;
438
+ // failures still audit so a forensic reconstruction of "what
439
+ // happened" is always possible.
440
+ var audit = config.audit || null;
441
+ var observability = config.observability || null;
442
+ var auditSuccess = config.auditSuccess !== false;
443
+ var auditFailures = config.auditFailures !== false;
444
+
445
+ function _emit(action, info) {
446
+ if (!audit) return;
447
+ try {
448
+ audit.safeEmit(Object.assign({ action: action }, info));
449
+ } catch (_e) { /* audit best-effort — never break the call */ }
450
+ }
451
+
452
+ function _emitEvent(name, value, labels) {
453
+ if (!observability || typeof observability.event !== "function") return;
454
+ try { observability.event(name, value, labels || {}); }
455
+ catch (_e) { /* observability best-effort */ }
456
+ }
457
+
458
+ function _actor(callerOpts) {
459
+ return requestHelpers.resolveActorWithOverride(callerOpts || {}, null);
460
+ }
461
+
462
+ // S3 subresource queries (`?lifecycle`, `?cors`, `?object-lock`,
463
+ // `?retention`, `?legal-hold`) are *bare* tokens on the wire — the
464
+ // trailing `=` produced by `URLSearchParams.set(k, "")` is interpreted
465
+ // by some S3 implementations (MinIO in particular) as "this is a
466
+ // body-write with a query parameter, not a subresource", which routes
467
+ // the request to the wrong handler. The SigV4 canonicalizer always
468
+ // reads `key=value` (with the empty `=`) per AWS spec, so the signature
469
+ // computation is unaffected; only the wire form differs. To preserve
470
+ // both behaviors, we build the URL string manually and pass it to the
471
+ // URL constructor — the constructor preserves the bare token in
472
+ // `url.search` while `url.searchParams` still presents it as `key=`.
473
+ function _appendQuery(base, query) {
474
+ if (!query) return base;
475
+ var keys = Object.keys(query);
476
+ if (keys.length === 0) return base;
477
+ var parts = keys.map(function (k) {
478
+ var v = query[k];
479
+ if (v === "" || v == null) return encodeURIComponent(k);
480
+ return encodeURIComponent(k) + "=" + encodeURIComponent(v);
481
+ });
482
+ return base + "?" + parts.join("&");
483
+ }
484
+
301
485
  function _bucketUrl(name, query) {
302
- var u;
486
+ var base;
303
487
  if (pathStyle) {
304
- u = new URL(endpoint + "/" + name + "/");
488
+ base = endpoint + "/" + name + "/";
305
489
  } else {
306
- u = new URL(endpoint);
307
- u.hostname = name + "." + u.hostname;
308
- u.pathname = "/";
490
+ var ub = new URL(endpoint);
491
+ ub.hostname = name + "." + ub.hostname;
492
+ ub.pathname = "/";
493
+ base = ub.toString();
494
+ if (base.endsWith("/")) base = base.slice(0, -1) + "/";
309
495
  }
310
- if (query) {
311
- Object.keys(query).forEach(function (k) {
312
- u.searchParams.set(k, query[k] != null ? query[k] : "");
313
- });
496
+ return new URL(_appendQuery(base, query));
497
+ }
498
+
499
+ function _objectUrl(name, key, query) {
500
+ // Each key segment is encoded individually so that legitimate "/"
501
+ // separators in the key are preserved (S3 treats keys with slashes
502
+ // as flat names, not directories).
503
+ var encKey = key.split("/").map(encodeURIComponent).join("/");
504
+ var base;
505
+ if (pathStyle) {
506
+ base = endpoint + "/" + name + "/" + encKey;
507
+ } else {
508
+ var uo = new URL(endpoint);
509
+ uo.hostname = name + "." + uo.hostname;
510
+ uo.pathname = "/" + encKey;
511
+ base = uo.toString();
314
512
  }
315
- return u;
513
+ return new URL(_appendQuery(base, query));
316
514
  }
317
515
 
318
516
  function _serviceUrl(query) {
@@ -357,6 +555,8 @@ function create(config) {
357
555
  function createBucket(name, opts) {
358
556
  _validateBucketName(name);
359
557
  opts = opts || {};
558
+ validateOpts(opts, ["region", "objectLockEnabled", "req", "actor"],
559
+ "bucketOps.create");
360
560
  var targetRegion = opts.region || config.region;
361
561
  var url = _bucketUrl(name);
362
562
  var bodyBuf;
@@ -377,42 +577,112 @@ function create(config) {
377
577
  bodyBuf = Buffer.alloc(0);
378
578
  extra["Content-Length"] = "0";
379
579
  }
580
+ // Object Lock can ONLY be enabled at create time — flipping it on
581
+ // a live bucket isn\'t an S3 API. Setting the header on PutBucket
582
+ // turns on the underlying versioning + write-once-read-many
583
+ // semantics so subsequent setObjectLockConfiguration / Retention
584
+ // / LegalHold calls actually do something.
585
+ if (opts.objectLockEnabled === true) {
586
+ extra["x-amz-bucket-object-lock-enabled"] = "true";
587
+ }
380
588
  var payloadHash = sigv4.sha256Hex(bodyBuf);
381
589
  var headers = _signed("PUT", url, payloadHash, extra);
382
590
  return _request("PUT", url, headers, bodyBuf).then(
383
- function () { return { created: true, name: name, region: targetRegion }; },
591
+ function () {
592
+ if (auditSuccess) {
593
+ _emit("objectstore.bucket.create", {
594
+ actor: _actor(opts),
595
+ resource: { kind: "bucket", id: name },
596
+ metadata: {
597
+ region: targetRegion,
598
+ objectLockEnabled: opts.objectLockEnabled === true,
599
+ },
600
+ });
601
+ }
602
+ _emitEvent("objectstore.bucket.create", 1,
603
+ { outcome: "success", region: targetRegion });
604
+ return { created: true, name: name, region: targetRegion };
605
+ },
384
606
  function (e) {
385
607
  // Map S3 conflict response codes into stable framework codes.
608
+ var mapped = e;
386
609
  if (e.statusCode === 409 && /BucketAlreadyOwnedByYou/.test(e.message || "")) {
387
- throw _err("BUCKET_ALREADY_OWNED",
610
+ mapped = _err("BUCKET_ALREADY_OWNED",
388
611
  "bucket '" + name + "' already exists and is owned by this account", true);
389
- }
390
- if (e.statusCode === 409) {
391
- throw _err("BUCKET_NAME_TAKEN",
612
+ } else if (e.statusCode === 409) {
613
+ mapped = _err("BUCKET_NAME_TAKEN",
392
614
  "bucket name '" + name + "' is taken in S3's global namespace", true);
393
615
  }
394
- throw e;
616
+ if (auditFailures) {
617
+ _emit("objectstore.bucket.create", {
618
+ actor: _actor(opts),
619
+ resource: { kind: "bucket", id: name },
620
+ outcome: "failure",
621
+ reason: mapped.code || "error",
622
+ metadata: { region: targetRegion },
623
+ });
624
+ }
625
+ _emitEvent("objectstore.bucket.create", 1,
626
+ { outcome: "failure", reason: mapped.code || "error" });
627
+ throw mapped;
395
628
  }
396
629
  );
397
630
  }
398
631
 
399
632
  // ---- delete ----
400
633
 
401
- function deleteBucket(name) {
634
+ function deleteBucket(name, opts) {
402
635
  _validateBucketName(name);
636
+ opts = opts || {};
637
+ validateOpts(opts, ["req", "actor"], "bucketOps.delete");
403
638
  var url = _bucketUrl(name);
404
639
  var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
405
640
  var headers = _signed("DELETE", url, payloadHash);
406
641
  return _request("DELETE", url, headers, null).then(
407
- function () { return true; },
642
+ function () {
643
+ if (auditSuccess) {
644
+ _emit("objectstore.bucket.delete", {
645
+ actor: _actor(opts),
646
+ resource: { kind: "bucket", id: name },
647
+ metadata: { existed: true },
648
+ });
649
+ }
650
+ _emitEvent("objectstore.bucket.delete", 1,
651
+ { outcome: "success", existed: "true" });
652
+ return true;
653
+ },
408
654
  function (e) {
409
- if (e.statusCode === 404) return false;
655
+ if (e.statusCode === 404) {
656
+ // Idempotent: missing bucket → false. Audit as success-with-noop
657
+ // so the trail still records "operator attempted delete".
658
+ if (auditSuccess) {
659
+ _emit("objectstore.bucket.delete", {
660
+ actor: _actor(opts),
661
+ resource: { kind: "bucket", id: name },
662
+ metadata: { existed: false },
663
+ });
664
+ }
665
+ _emitEvent("objectstore.bucket.delete", 1,
666
+ { outcome: "success", existed: "false" });
667
+ return false;
668
+ }
669
+ var mapped = e;
410
670
  if (e.statusCode === 409 && /BucketNotEmpty/.test(e.message || "")) {
411
- throw _err("BUCKET_NOT_EMPTY",
671
+ mapped = _err("BUCKET_NOT_EMPTY",
412
672
  "bucket '" + name + "' is not empty; delete all objects + " +
413
673
  "noncurrent versions + delete-markers first", true);
414
674
  }
415
- throw e;
675
+ if (auditFailures) {
676
+ _emit("objectstore.bucket.delete", {
677
+ actor: _actor(opts),
678
+ resource: { kind: "bucket", id: name },
679
+ outcome: "failure",
680
+ reason: mapped.code || "error",
681
+ });
682
+ }
683
+ _emitEvent("objectstore.bucket.delete", 1,
684
+ { outcome: "failure", reason: mapped.code || "error" });
685
+ throw mapped;
416
686
  }
417
687
  );
418
688
  }
@@ -428,8 +698,11 @@ function create(config) {
428
698
  var result = doc.ListAllMyBucketsResult || {};
429
699
  var bucketsContainer = result.Buckets || {};
430
700
  var raw = bucketsContainer.Bucket;
431
- if (!raw) return [];
432
- var arr = Array.isArray(raw) ? raw : [raw];
701
+ var arr;
702
+ if (!raw) arr = [];
703
+ else if (Array.isArray(raw)) arr = raw;
704
+ else arr = [raw];
705
+ _emitEvent("objectstore.bucket.list", arr.length, { outcome: "success" });
433
706
  return arr.map(function (b) {
434
707
  return {
435
708
  name: b.Name,
@@ -442,8 +715,10 @@ function create(config) {
442
715
 
443
716
  // ---- setLifecycle ----
444
717
 
445
- function setLifecycle(name, rules) {
718
+ function setLifecycle(name, rules, opts) {
446
719
  _validateBucketName(name);
720
+ opts = opts || {};
721
+ validateOpts(opts, ["req", "actor"], "bucketOps.setLifecycle");
447
722
  var bodyXml = _buildLifecycleXml(rules);
448
723
  var bodyBuf = Buffer.from(bodyXml, "utf8");
449
724
  var url = _bucketUrl(name, { lifecycle: "" });
@@ -453,15 +728,41 @@ function create(config) {
453
728
  "Content-Length": String(bodyBuf.length),
454
729
  "Content-MD5": _md5Base64(bodyBuf),
455
730
  });
456
- return _request("PUT", url, headers, bodyBuf).then(function () {
457
- return { applied: true, name: name, ruleCount: rules.length };
458
- });
731
+ return _request("PUT", url, headers, bodyBuf).then(
732
+ function () {
733
+ if (auditSuccess) {
734
+ _emit("objectstore.bucket.setLifecycle", {
735
+ actor: _actor(opts),
736
+ resource: { kind: "bucket", id: name },
737
+ metadata: { ruleCount: rules.length },
738
+ });
739
+ }
740
+ _emitEvent("objectstore.bucket.setLifecycle", 1,
741
+ { outcome: "success", ruleCount: String(rules.length) });
742
+ return { applied: true, name: name, ruleCount: rules.length };
743
+ },
744
+ function (e) {
745
+ if (auditFailures) {
746
+ _emit("objectstore.bucket.setLifecycle", {
747
+ actor: _actor(opts),
748
+ resource: { kind: "bucket", id: name },
749
+ outcome: "failure",
750
+ reason: e.code || "error",
751
+ });
752
+ }
753
+ _emitEvent("objectstore.bucket.setLifecycle", 1,
754
+ { outcome: "failure", reason: e.code || "error" });
755
+ throw e;
756
+ }
757
+ );
459
758
  }
460
759
 
461
760
  // ---- setCorsRules ----
462
761
 
463
- function setCorsRules(name, rules) {
762
+ function setCorsRules(name, rules, opts) {
464
763
  _validateBucketName(name);
764
+ opts = opts || {};
765
+ validateOpts(opts, ["req", "actor"], "bucketOps.setCorsRules");
465
766
  var bodyXml = _buildCorsXml(rules);
466
767
  var bodyBuf = Buffer.from(bodyXml, "utf8");
467
768
  var url = _bucketUrl(name, { cors: "" });
@@ -471,18 +772,317 @@ function create(config) {
471
772
  "Content-Length": String(bodyBuf.length),
472
773
  "Content-MD5": _md5Base64(bodyBuf),
473
774
  });
474
- return _request("PUT", url, headers, bodyBuf).then(function () {
475
- return { applied: true, name: name, ruleCount: rules.length };
775
+ return _request("PUT", url, headers, bodyBuf).then(
776
+ function () {
777
+ if (auditSuccess) {
778
+ _emit("objectstore.bucket.setCorsRules", {
779
+ actor: _actor(opts),
780
+ resource: { kind: "bucket", id: name },
781
+ metadata: { ruleCount: rules.length },
782
+ });
783
+ }
784
+ _emitEvent("objectstore.bucket.setCorsRules", 1,
785
+ { outcome: "success", ruleCount: String(rules.length) });
786
+ return { applied: true, name: name, ruleCount: rules.length };
787
+ },
788
+ function (e) {
789
+ if (auditFailures) {
790
+ _emit("objectstore.bucket.setCorsRules", {
791
+ actor: _actor(opts),
792
+ resource: { kind: "bucket", id: name },
793
+ outcome: "failure",
794
+ reason: e.code || "error",
795
+ });
796
+ }
797
+ _emitEvent("objectstore.bucket.setCorsRules", 1,
798
+ { outcome: "failure", reason: e.code || "error" });
799
+ throw e;
800
+ }
801
+ );
802
+ }
803
+
804
+ // ---- Object Lock configuration (bucket-level) ----
805
+
806
+ function setObjectLockConfiguration(name, opts) {
807
+ _validateBucketName(name);
808
+ _validateObjectLockConfig(opts);
809
+ validateOpts(opts, ["mode", "days", "years", "req", "actor"],
810
+ "bucketOps.setObjectLockConfiguration");
811
+ var bodyXml = _buildObjectLockConfigXml(opts);
812
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
813
+ var url = _bucketUrl(name, { "object-lock": "" });
814
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
815
+ var headers = _signed("PUT", url, payloadHash, {
816
+ "Content-Type": "application/xml",
817
+ "Content-Length": String(bodyBuf.length),
818
+ "Content-MD5": _md5Base64(bodyBuf),
819
+ });
820
+ return _request("PUT", url, headers, bodyBuf).then(
821
+ function () {
822
+ // Compliance-critical event — SEC 17a-4 / FINRA / HIPAA require
823
+ // a trail of who-changed-the-default-retention-policy-and-when.
824
+ if (auditSuccess) {
825
+ _emit("objectstore.bucket.setObjectLockConfiguration", {
826
+ actor: _actor(opts),
827
+ resource: { kind: "bucket", id: name },
828
+ metadata: {
829
+ mode: opts.mode,
830
+ days: opts.days != null ? opts.days : null,
831
+ years: opts.years != null ? opts.years : null,
832
+ },
833
+ });
834
+ }
835
+ _emitEvent("objectstore.bucket.setObjectLockConfiguration", 1,
836
+ { outcome: "success", mode: opts.mode });
837
+ return {
838
+ applied: true, name: name,
839
+ mode: opts.mode,
840
+ days: opts.days != null ? opts.days : null,
841
+ years: opts.years != null ? opts.years : null,
842
+ };
843
+ },
844
+ function (e) {
845
+ if (auditFailures) {
846
+ _emit("objectstore.bucket.setObjectLockConfiguration", {
847
+ actor: _actor(opts),
848
+ resource: { kind: "bucket", id: name },
849
+ outcome: "failure",
850
+ reason: e.code || "error",
851
+ metadata: { mode: opts.mode },
852
+ });
853
+ }
854
+ _emitEvent("objectstore.bucket.setObjectLockConfiguration", 1,
855
+ { outcome: "failure", reason: e.code || "error" });
856
+ throw e;
857
+ }
858
+ );
859
+ }
860
+
861
+ function getObjectLockConfiguration(name) {
862
+ _validateBucketName(name);
863
+ _emitEvent("objectstore.bucket.getObjectLockConfiguration", 1,
864
+ { outcome: "success" });
865
+ var url = _bucketUrl(name, { "object-lock": "" });
866
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
867
+ var headers = _signed("GET", url, payloadHash);
868
+ return _request("GET", url, headers, null).then(
869
+ function (res) {
870
+ var doc = safeXml.parse(res.body);
871
+ var olc = doc.ObjectLockConfiguration || {};
872
+ var enabled = olc.ObjectLockEnabled === "Enabled";
873
+ var rule = olc.Rule || {};
874
+ var def = rule.DefaultRetention || {};
875
+ return {
876
+ enabled: enabled,
877
+ mode: def.Mode || null,
878
+ days: def.Days != null ? Number(def.Days) : null,
879
+ years: def.Years != null ? Number(def.Years) : null,
880
+ };
881
+ },
882
+ function (e) {
883
+ // S3 + MinIO return 404 + ObjectLockConfigurationNotFoundError
884
+ // when the bucket was created without `objectLockEnabled: true`.
885
+ // That's a "not configured" state, not an error worth bubbling
886
+ // — operators asking "is this bucket lock-enabled?" want a
887
+ // clean false, not a try/catch.
888
+ if (_isLockNotConfigured(e)) {
889
+ return { enabled: false, mode: null, days: null, years: null };
890
+ }
891
+ throw e;
892
+ }
893
+ );
894
+ }
895
+
896
+ // ---- Per-object retention ----
897
+
898
+ function setObjectRetention(name, key, opts) {
899
+ _validateBucketName(name);
900
+ _validateObjectKey(key);
901
+ _validateRetention(opts);
902
+ validateOpts(opts, ["mode", "retainUntil", "bypassGovernance", "req", "actor"],
903
+ "bucketOps.setObjectRetention");
904
+ var bodyXml = _buildRetentionXml(opts);
905
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
906
+ var url = _objectUrl(name, key, { retention: "" });
907
+ var extra = {
908
+ "Content-Type": "application/xml",
909
+ "Content-Length": String(bodyBuf.length),
910
+ "Content-MD5": _md5Base64(bodyBuf),
911
+ };
912
+ if (opts.bypassGovernance === true) {
913
+ extra["x-amz-bypass-governance-retention"] = "true";
914
+ }
915
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
916
+ var headers = _signed("PUT", url, payloadHash, extra);
917
+ return _request("PUT", url, headers, bodyBuf).then(
918
+ function () {
919
+ // Compliance trail — bypassGovernance is the high-risk op
920
+ // (operator with s3:BypassGovernanceRetention shortened a
921
+ // GOVERNANCE retention) and operators wire alerting on this
922
+ // metadata field.
923
+ if (auditSuccess) {
924
+ _emit("objectstore.object.setRetention", {
925
+ actor: _actor(opts),
926
+ resource: { kind: "object", id: name + "/" + key },
927
+ metadata: {
928
+ bucket: name,
929
+ key: key,
930
+ mode: opts.mode,
931
+ retainUntilIso: opts.retainUntil.toISOString(),
932
+ bypassGovernance: opts.bypassGovernance === true,
933
+ },
934
+ });
935
+ }
936
+ _emitEvent("objectstore.object.setRetention", 1,
937
+ { outcome: "success", mode: opts.mode,
938
+ bypassGovernance: opts.bypassGovernance === true ? "true" : "false" });
939
+ return {
940
+ applied: true,
941
+ name: name,
942
+ key: key,
943
+ mode: opts.mode,
944
+ retainUntil: opts.retainUntil,
945
+ };
946
+ },
947
+ function (e) {
948
+ if (auditFailures) {
949
+ _emit("objectstore.object.setRetention", {
950
+ actor: _actor(opts),
951
+ resource: { kind: "object", id: name + "/" + key },
952
+ outcome: "failure",
953
+ reason: e.code || "error",
954
+ metadata: {
955
+ bucket: name,
956
+ key: key,
957
+ mode: opts.mode,
958
+ bypassGovernance: opts.bypassGovernance === true,
959
+ },
960
+ });
961
+ }
962
+ _emitEvent("objectstore.object.setRetention", 1,
963
+ { outcome: "failure", reason: e.code || "error" });
964
+ throw e;
965
+ }
966
+ );
967
+ }
968
+
969
+ function getObjectRetention(name, key) {
970
+ _validateBucketName(name);
971
+ _validateObjectKey(key);
972
+ _emitEvent("objectstore.object.getRetention", 1, { outcome: "success" });
973
+ var url = _objectUrl(name, key, { retention: "" });
974
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
975
+ var headers = _signed("GET", url, payloadHash);
976
+ return _request("GET", url, headers, null).then(
977
+ function (res) {
978
+ var doc = safeXml.parse(res.body);
979
+ var ret = doc.Retention || {};
980
+ var until = ret.RetainUntilDate ? new Date(ret.RetainUntilDate) : null;
981
+ return {
982
+ mode: ret.Mode || null,
983
+ retainUntil: until,
984
+ };
985
+ },
986
+ function (e) {
987
+ // S3 + MinIO return 4xx + NoSuchObjectLockConfiguration when the
988
+ // object has no per-object retention applied. Same UX choice as
989
+ // getObjectLockConfiguration: surface the not-set state cleanly
990
+ // rather than forcing operators to try/catch on a known-good
991
+ // request shape.
992
+ if (_isLockNotConfigured(e)) {
993
+ return { mode: null, retainUntil: null };
994
+ }
995
+ throw e;
996
+ }
997
+ );
998
+ }
999
+
1000
+ // ---- Per-object legal hold ----
1001
+
1002
+ function setObjectLegalHold(name, key, status, opts) {
1003
+ _validateBucketName(name);
1004
+ _validateObjectKey(key);
1005
+ _validateLegalHoldStatus(status);
1006
+ opts = opts || {};
1007
+ validateOpts(opts, ["req", "actor"], "bucketOps.setObjectLegalHold");
1008
+ var bodyXml = _buildLegalHoldXml(status);
1009
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
1010
+ var url = _objectUrl(name, key, { "legal-hold": "" });
1011
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
1012
+ var headers = _signed("PUT", url, payloadHash, {
1013
+ "Content-Type": "application/xml",
1014
+ "Content-Length": String(bodyBuf.length),
1015
+ "Content-MD5": _md5Base64(bodyBuf),
476
1016
  });
1017
+ return _request("PUT", url, headers, bodyBuf).then(
1018
+ function () {
1019
+ if (auditSuccess) {
1020
+ _emit("objectstore.object.setLegalHold", {
1021
+ actor: _actor(opts),
1022
+ resource: { kind: "object", id: name + "/" + key },
1023
+ metadata: { bucket: name, key: key, status: status },
1024
+ });
1025
+ }
1026
+ _emitEvent("objectstore.object.setLegalHold", 1,
1027
+ { outcome: "success", status: status });
1028
+ return { applied: true, name: name, key: key, status: status };
1029
+ },
1030
+ function (e) {
1031
+ if (auditFailures) {
1032
+ _emit("objectstore.object.setLegalHold", {
1033
+ actor: _actor(opts),
1034
+ resource: { kind: "object", id: name + "/" + key },
1035
+ outcome: "failure",
1036
+ reason: e.code || "error",
1037
+ metadata: { bucket: name, key: key, status: status },
1038
+ });
1039
+ }
1040
+ _emitEvent("objectstore.object.setLegalHold", 1,
1041
+ { outcome: "failure", reason: e.code || "error" });
1042
+ throw e;
1043
+ }
1044
+ );
1045
+ }
1046
+
1047
+ function getObjectLegalHold(name, key) {
1048
+ _validateBucketName(name);
1049
+ _validateObjectKey(key);
1050
+ _emitEvent("objectstore.object.getLegalHold", 1, { outcome: "success" });
1051
+ var url = _objectUrl(name, key, { "legal-hold": "" });
1052
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
1053
+ var headers = _signed("GET", url, payloadHash);
1054
+ return _request("GET", url, headers, null).then(
1055
+ function (res) {
1056
+ var doc = safeXml.parse(res.body);
1057
+ var lh = doc.LegalHold || {};
1058
+ return { status: lh.Status || null };
1059
+ },
1060
+ function (e) {
1061
+ // Object never had a legal hold set — S3 + MinIO surface this
1062
+ // as NoSuchObjectLockConfiguration. Return a clean OFF instead
1063
+ // of throwing; "no hold ever applied" is operationally
1064
+ // identical to "hold is OFF".
1065
+ if (_isLockNotConfigured(e)) {
1066
+ return { status: "OFF" };
1067
+ }
1068
+ throw e;
1069
+ }
1070
+ );
477
1071
  }
478
1072
 
479
1073
  return {
480
- protocol: "sigv4",
481
- create: createBucket,
482
- delete: deleteBucket,
483
- list: listBuckets,
484
- setLifecycle: setLifecycle,
485
- setCorsRules: setCorsRules,
1074
+ protocol: "sigv4",
1075
+ create: createBucket,
1076
+ delete: deleteBucket,
1077
+ list: listBuckets,
1078
+ setLifecycle: setLifecycle,
1079
+ setCorsRules: setCorsRules,
1080
+ setObjectLockConfiguration: setObjectLockConfiguration,
1081
+ getObjectLockConfiguration: getObjectLockConfiguration,
1082
+ setObjectRetention: setObjectRetention,
1083
+ getObjectRetention: getObjectRetention,
1084
+ setObjectLegalHold: setObjectLegalHold,
1085
+ getObjectLegalHold: getObjectLegalHold,
486
1086
  };
487
1087
  }
488
1088
 
@@ -490,7 +1090,7 @@ module.exports = {
490
1090
  create: create,
491
1091
  // Test-only exports for unit-testing the XML builders without
492
1092
  // standing up a fake S3 server.
493
- _buildLifecycleXmlForTest: _buildLifecycleXml,
494
- _buildCorsXmlForTest: _buildCorsXml,
495
- _validateBucketNameForTest: _validateBucketName,
1093
+ _buildLifecycleXmlForTest: _buildLifecycleXml,
1094
+ _buildCorsXmlForTest: _buildCorsXml,
1095
+ _validateBucketNameForTest: _validateBucketName,
496
1096
  };