@blamejs/core 0.7.18 → 0.7.20

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.
Files changed (169) hide show
  1. package/CHANGELOG.md +427 -423
  2. package/README.md +150 -150
  3. package/bin/blamejs.js +0 -0
  4. package/index.js +310 -308
  5. package/lib/api-key.js +660 -660
  6. package/lib/api-snapshot.js +338 -338
  7. package/lib/app-shutdown.js +385 -385
  8. package/lib/app.js +365 -365
  9. package/lib/archive.js +250 -250
  10. package/lib/atomic-file.js +544 -544
  11. package/lib/audit-chain.js +177 -177
  12. package/lib/audit-sign.js +344 -344
  13. package/lib/audit-tools.js +677 -677
  14. package/lib/audit.js +766 -766
  15. package/lib/auth/jwt-external.js +365 -0
  16. package/lib/auth/jwt.js +337 -311
  17. package/lib/auth/lockout.js +436 -436
  18. package/lib/auth/oauth.js +721 -721
  19. package/lib/auth/passkey.js +181 -181
  20. package/lib/auth/password.js +628 -594
  21. package/lib/backup/bundle.js +217 -217
  22. package/lib/backup/crypto.js +176 -176
  23. package/lib/backup/index.js +515 -515
  24. package/lib/backup/manifest.js +282 -282
  25. package/lib/break-glass.js +1338 -1338
  26. package/lib/bundler.js +441 -441
  27. package/lib/cache-redis.js +256 -256
  28. package/lib/cache.js +1206 -1206
  29. package/lib/canonical-json.js +115 -115
  30. package/lib/chain-writer.js +234 -234
  31. package/lib/cli-helpers.js +206 -206
  32. package/lib/cli.js +2334 -2334
  33. package/lib/cluster-provider-db.js +317 -317
  34. package/lib/cluster-storage.js +226 -226
  35. package/lib/cluster.js +703 -703
  36. package/lib/config-drift.js +301 -301
  37. package/lib/consent.js +222 -222
  38. package/lib/constants.js +191 -191
  39. package/lib/cookies.js +350 -315
  40. package/lib/credential-hash.js +322 -322
  41. package/lib/crypto.js +266 -266
  42. package/lib/csv.js +275 -275
  43. package/lib/db-declare-row-policy.js +267 -267
  44. package/lib/db-declare-view.js +420 -420
  45. package/lib/db-query.js +406 -406
  46. package/lib/db-schema.js +319 -319
  47. package/lib/db.js +1288 -1288
  48. package/lib/deprecate.js +222 -222
  49. package/lib/dev.js +335 -335
  50. package/lib/dual-control.js +473 -473
  51. package/lib/error-page.js +420 -420
  52. package/lib/external-db-migrate.js +441 -441
  53. package/lib/external-db.js +1061 -1061
  54. package/lib/file-type.js +273 -273
  55. package/lib/forms.js +422 -422
  56. package/lib/framework-error.js +293 -293
  57. package/lib/framework-schema.js +717 -717
  58. package/lib/handlers.js +350 -350
  59. package/lib/http-client-cookie-jar.js +508 -508
  60. package/lib/http-client.js +1195 -1195
  61. package/lib/i18n.js +878 -878
  62. package/lib/jobs.js +185 -185
  63. package/lib/log-stream-cloudwatch.js +369 -369
  64. package/lib/log-stream-local.js +146 -146
  65. package/lib/log-stream-otlp-grpc.js +410 -410
  66. package/lib/log-stream-otlp.js +286 -286
  67. package/lib/log-stream-syslog.js +302 -302
  68. package/lib/log-stream-webhook.js +199 -199
  69. package/lib/log-stream.js +330 -330
  70. package/lib/log.js +500 -500
  71. package/lib/mail-bounce.js +528 -528
  72. package/lib/mail-dkim.js +369 -369
  73. package/lib/mail.js +981 -981
  74. package/lib/metrics.js +683 -683
  75. package/lib/middleware/api-encrypt.js +936 -936
  76. package/lib/middleware/attach-user.js +157 -157
  77. package/lib/middleware/bearer-auth.js +152 -0
  78. package/lib/middleware/body-parser.js +1170 -1170
  79. package/lib/middleware/bot-guard.js +178 -178
  80. package/lib/middleware/compression.js +452 -452
  81. package/lib/middleware/cors.js +314 -314
  82. package/lib/middleware/csp-nonce.js +348 -348
  83. package/lib/middleware/csrf-protect.js +399 -316
  84. package/lib/middleware/db-role-for.js +264 -264
  85. package/lib/middleware/fetch-metadata.js +129 -0
  86. package/lib/middleware/health.js +392 -392
  87. package/lib/middleware/index.js +85 -79
  88. package/lib/middleware/rate-limit.js +358 -358
  89. package/lib/middleware/request-id.js +61 -61
  90. package/lib/middleware/request-log.js +168 -168
  91. package/lib/middleware/require-auth.js +104 -104
  92. package/lib/middleware/security-headers.js +121 -116
  93. package/lib/middleware/sse.js +166 -166
  94. package/lib/migrations.js +383 -383
  95. package/lib/mtls-ca.js +518 -518
  96. package/lib/mtls-engine-default.js +481 -481
  97. package/lib/network-dns.js +632 -632
  98. package/lib/network-heartbeat.js +290 -290
  99. package/lib/network-nts.js +574 -574
  100. package/lib/network-proxy.js +265 -265
  101. package/lib/network-tls.js +328 -328
  102. package/lib/network.js +233 -233
  103. package/lib/notify.js +612 -612
  104. package/lib/ntp-check.js +229 -229
  105. package/lib/numeric-bounds.js +111 -111
  106. package/lib/object-store/azure-blob-bucket-ops.js +349 -349
  107. package/lib/object-store/azure-blob.js +488 -488
  108. package/lib/object-store/gcs-bucket-ops.js +351 -351
  109. package/lib/object-store/gcs.js +519 -519
  110. package/lib/object-store/http-put.js +153 -153
  111. package/lib/object-store/index.js +197 -197
  112. package/lib/object-store/sigv4-bucket-ops.js +1092 -1092
  113. package/lib/object-store/sigv4.js +903 -903
  114. package/lib/observability.js +151 -151
  115. package/lib/otel-export.js +269 -269
  116. package/lib/pagination.js +464 -464
  117. package/lib/parsers/index.js +80 -80
  118. package/lib/parsers/safe-env.js +642 -642
  119. package/lib/parsers/safe-ini.js +292 -292
  120. package/lib/parsers/safe-toml.js +784 -784
  121. package/lib/parsers/safe-xml.js +390 -390
  122. package/lib/parsers/safe-yaml.js +1015 -1015
  123. package/lib/permissions.js +708 -708
  124. package/lib/pqc-agent.js +87 -87
  125. package/lib/pqc-gate.js +279 -279
  126. package/lib/protobuf-encoder.js +190 -190
  127. package/lib/protocol-dispatcher.js +161 -161
  128. package/lib/pubsub-redis.js +167 -167
  129. package/lib/pubsub.js +429 -429
  130. package/lib/queue-local.js +476 -476
  131. package/lib/queue-redis.js +745 -745
  132. package/lib/queue-sqs.js +319 -319
  133. package/lib/queue.js +695 -695
  134. package/lib/redis-client.js +519 -519
  135. package/lib/request-helpers.js +340 -340
  136. package/lib/restore-bundle.js +237 -237
  137. package/lib/restore-rollback.js +259 -259
  138. package/lib/restore.js +409 -409
  139. package/lib/retry.js +376 -376
  140. package/lib/router.js +748 -748
  141. package/lib/safe-async.js +735 -735
  142. package/lib/safe-buffer.js +237 -237
  143. package/lib/safe-json.js +541 -541
  144. package/lib/safe-schema.js +1266 -1266
  145. package/lib/safe-url.js +159 -159
  146. package/lib/scheduler.js +706 -706
  147. package/lib/security-assert.js +373 -373
  148. package/lib/seeders.js +618 -618
  149. package/lib/session.js +535 -478
  150. package/lib/slug.js +269 -269
  151. package/lib/ssrf-guard.js +401 -401
  152. package/lib/static.js +7 -5
  153. package/lib/storage.js +471 -471
  154. package/lib/subject.js +281 -281
  155. package/lib/template.js +791 -791
  156. package/lib/testing.js +798 -798
  157. package/lib/time.js +310 -310
  158. package/lib/totp.js +302 -302
  159. package/lib/tracing.js +494 -494
  160. package/lib/uuid.js +132 -132
  161. package/lib/validate-opts.js +340 -340
  162. package/lib/vault/index.js +308 -308
  163. package/lib/vault/rotate.js +784 -784
  164. package/lib/vault/wrap.js +296 -296
  165. package/lib/vendor/noble-ciphers.cjs +9 -9
  166. package/lib/webhook.js +595 -595
  167. package/lib/websocket.js +1048 -1048
  168. package/package.json +77 -77
  169. package/sbom.cyclonedx.json +7 -7
@@ -1,1092 +1,1092 @@
1
- "use strict";
2
- /**
3
- * sigv4-bucket-ops — bucket-level operations for SigV4 backends.
4
- *
5
- * Per-object ops (put / get / list / delete / multipart) live in
6
- * lib/object-store/sigv4.js and are bound to a single bucket at
7
- * create() time. Bucket lifecycle ops are at a different level —
8
- * they need a service-scoped client that addresses arbitrary
9
- * buckets — so they get their own factory.
10
- *
11
- * Operators with multi-cloud ambitions reach for Terraform / CDK /
12
- * Pulumi. The framework's bucket-ops surface is the operator-from-app
13
- * path: create the bucket your app needs at boot, attach a lifecycle
14
- * rule that aborts incomplete multiparts after a week, etc. Niche ops
15
- * (Object Lock, Replication, Inventory, Notification) are deferred —
16
- * they're well into Terraform territory.
17
- *
18
- * Public API:
19
- *
20
- * var ops = b.objectStore.bucketOps.create({
21
- * protocol: "sigv4",
22
- * region: "us-east-1",
23
- * accessKeyId: env("AWS_ACCESS_KEY_ID"),
24
- * secretAccessKey: env("AWS_SECRET_ACCESS_KEY"),
25
- * endpoint: "https://s3.us-east-1.amazonaws.com", // optional
26
- * pathStyle: false,
27
- * timeoutMs: 30000,
28
- * });
29
- *
30
- * await ops.create("my-bucket", { region: "eu-west-1" });
31
- * await ops.delete("my-bucket");
32
- * var buckets = await ops.list(); // [{ name, creationDate }]
33
- * await ops.setLifecycle("my-bucket", [{
34
- * id: "abort-stale-multiparts",
35
- * status: "Enabled",
36
- * prefix: "",
37
- * abortIncompleteMultipartUpload: { daysAfterInitiation: 7 },
38
- * }]);
39
- * await ops.setCorsRules("my-bucket", [{
40
- * allowedOrigins: ["https://app.example.com"],
41
- * allowedMethods: ["GET", "PUT", "POST"],
42
- * allowedHeaders: ["*"],
43
- * exposeHeaders: ["ETag"],
44
- * maxAgeSeconds: 3600,
45
- * }]);
46
- *
47
- * Validation rejects every bad input shape at the call site rather
48
- * than producing a server-side 400. Errors surface as ObjectStoreError
49
- * with codes (BUCKET_INVALID_NAME, INVALID_LIFECYCLE, INVALID_CORS_RULE,
50
- * BUCKET_ALREADY_OWNED, BUCKET_NOT_EMPTY, etc.).
51
- */
52
- var nodeCrypto = require("crypto");
53
- var C = require("../constants");
54
- var requestHelpers = require("../request-helpers");
55
- var sigv4 = require("./sigv4");
56
- var sharedRequest = require("./http-request");
57
- var safeXml = require("../parsers/safe-xml");
58
- var safeUrl = require("../safe-url");
59
- var template = require("../template");
60
- var validateOpts = require("../validate-opts");
61
- var { ObjectStoreError } = require("../framework-error");
62
-
63
- var _err = ObjectStoreError.factory;
64
-
65
- // Internal URL builder — endpoint + path string from validated config.
66
- // Routes through safeUrl.parse so the protocol allowlist + length cap
67
- // apply uniformly. Cap is generous since presigned-style URLs with many
68
- // query params can approach 8 KB.
69
- function _internalUrl(input, allowedProtocols) {
70
- return safeUrl.parse(input, {
71
- allowedProtocols: allowedProtocols || safeUrl.ALLOW_HTTP_TLS,
72
- errorClass: ObjectStoreError,
73
- maxUrlLength: C.BYTES.kib(32),
74
- });
75
- }
76
-
77
- // S3 bucket-name rules (general purpose). Source: AWS docs
78
- // "Bucket naming rules". Lowercase letters, digits, hyphens; 3..63
79
- // chars; no consecutive dots; cannot end in -s3alias / -ol-s3 etc.
80
- // We catch the common-mistake cases at config time; AWS catches the
81
- // rest at request time.
82
- var BUCKET_NAME_RE = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
83
-
84
- function _validateBucketName(name) {
85
- if (typeof name !== "string" || name.length < 3 || name.length > 63) {
86
- throw _err("BUCKET_INVALID_NAME",
87
- "bucket name must be a string of length 3..63, got " +
88
- (typeof name === "string" ? "length " + name.length : typeof name), true);
89
- }
90
- // Length is bounded above (3..63) before the regex runs so the
91
- // pattern can't be driven against an unbounded input.
92
- if (name.length > 63 || !BUCKET_NAME_RE.test(name)) {
93
- throw _err("BUCKET_INVALID_NAME",
94
- "bucket name '" + name + "' violates S3 naming rules " +
95
- "(lowercase, digits, hyphens, dots — no leading/trailing punct)", true);
96
- }
97
- if (name.indexOf("..") !== -1) {
98
- throw _err("BUCKET_INVALID_NAME",
99
- "bucket name '" + name + "' contains consecutive dots", true);
100
- }
101
- }
102
-
103
- // XML body strings flow through template.escapeHtml — `&#x27;` (which
104
- // it emits for the apostrophe) is a numeric character reference and
105
- // is valid in both XML and HTML, where `&apos;` is XML-only. AWS S3
106
- // accepts both; using the shared HTML escape keeps the framework
107
- // down to one canonical escape primitive.
108
- var _xmlEscape = template.escapeHtml;
109
-
110
- // AWS PutBucketLifecycle / PutBucketCors require a Content-MD5 header
111
- // for body integrity (legacy AWS API requirement; SigV4 already covers
112
- // integrity via x-amz-content-sha256 but the API still validates this).
113
- // MD5 here is NOT a credential or security primitive — it's an AWS API
114
- // shape. b.credentialHash is the wrong tool.
115
- function _md5Base64(buf) {
116
- return nodeCrypto.createHash("md5").update(buf).digest("base64");
117
- }
118
-
119
- // ---- Lifecycle XML ----
120
-
121
- var ALLOWED_STORAGE_CLASSES = [
122
- "STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA",
123
- "INTELLIGENT_TIERING", "GLACIER", "DEEP_ARCHIVE", "GLACIER_IR",
124
- "EXPRESS_ONEZONE",
125
- ];
126
-
127
- function _buildLifecycleXml(rules) {
128
- if (!Array.isArray(rules) || rules.length === 0) {
129
- throw _err("INVALID_LIFECYCLE",
130
- "setLifecycle: rules must be a non-empty array", true);
131
- }
132
- // S3 spec hard cap on lifecycle rules per bucket.
133
- var MAX_LIFECYCLE_RULES = 1000;
134
- if (rules.length > MAX_LIFECYCLE_RULES) {
135
- throw _err("INVALID_LIFECYCLE",
136
- "setLifecycle: maximum " + MAX_LIFECYCLE_RULES + " rules per bucket (S3 spec)", true);
137
- }
138
- var body = '<?xml version="1.0" encoding="UTF-8"?>';
139
- body += '<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
140
- for (var i = 0; i < rules.length; i++) {
141
- var rule = rules[i];
142
- if (!rule || typeof rule !== "object") {
143
- throw _err("INVALID_LIFECYCLE",
144
- "rules[" + i + "] must be an object", true);
145
- }
146
- var status = rule.status || "Enabled";
147
- if (status !== "Enabled" && status !== "Disabled") {
148
- throw _err("INVALID_LIFECYCLE",
149
- "rules[" + i + "].status must be 'Enabled' or 'Disabled'", true);
150
- }
151
- if (!rule.expiration && !rule.transition && !rule.abortIncompleteMultipartUpload) {
152
- throw _err("INVALID_LIFECYCLE",
153
- "rules[" + i + "] must specify at least one of " +
154
- "expiration / transition / abortIncompleteMultipartUpload", true);
155
- }
156
- body += "<Rule>";
157
- if (rule.id !== undefined) {
158
- if (typeof rule.id !== "string" || rule.id.length === 0) {
159
- throw _err("INVALID_LIFECYCLE",
160
- "rules[" + i + "].id must be a non-empty string when set", true);
161
- }
162
- body += "<ID>" + _xmlEscape(rule.id) + "</ID>";
163
- }
164
- body += "<Filter><Prefix>" + _xmlEscape(rule.prefix || "") + "</Prefix></Filter>";
165
- body += "<Status>" + status + "</Status>";
166
- if (rule.expiration) {
167
- body += "<Expiration>";
168
- if (rule.expiration.days !== undefined) {
169
- if (typeof rule.expiration.days !== "number" || rule.expiration.days < 1) {
170
- throw _err("INVALID_LIFECYCLE",
171
- "rules[" + i + "].expiration.days must be a positive integer", true);
172
- }
173
- body += "<Days>" + rule.expiration.days + "</Days>";
174
- }
175
- if (rule.expiration.date !== undefined) {
176
- body += "<Date>" + _xmlEscape(rule.expiration.date) + "</Date>";
177
- }
178
- if (rule.expiration.expiredObjectDeleteMarker !== undefined) {
179
- body += "<ExpiredObjectDeleteMarker>" +
180
- (rule.expiration.expiredObjectDeleteMarker ? "true" : "false") +
181
- "</ExpiredObjectDeleteMarker>";
182
- }
183
- body += "</Expiration>";
184
- }
185
- if (rule.transition) {
186
- if (ALLOWED_STORAGE_CLASSES.indexOf(rule.transition.storageClass) === -1) {
187
- throw _err("INVALID_LIFECYCLE",
188
- "rules[" + i + "].transition.storageClass must be one of: " +
189
- ALLOWED_STORAGE_CLASSES.join(", "), true);
190
- }
191
- body += "<Transition>";
192
- if (rule.transition.days !== undefined) {
193
- body += "<Days>" + rule.transition.days + "</Days>";
194
- }
195
- if (rule.transition.date !== undefined) {
196
- body += "<Date>" + _xmlEscape(rule.transition.date) + "</Date>";
197
- }
198
- body += "<StorageClass>" + rule.transition.storageClass + "</StorageClass>";
199
- body += "</Transition>";
200
- }
201
- if (rule.abortIncompleteMultipartUpload) {
202
- var dai = rule.abortIncompleteMultipartUpload.daysAfterInitiation;
203
- if (typeof dai !== "number" || dai < 1) {
204
- throw _err("INVALID_LIFECYCLE",
205
- "rules[" + i + "].abortIncompleteMultipartUpload.daysAfterInitiation " +
206
- "must be a positive integer", true);
207
- }
208
- body += "<AbortIncompleteMultipartUpload>";
209
- body += "<DaysAfterInitiation>" + dai + "</DaysAfterInitiation>";
210
- body += "</AbortIncompleteMultipartUpload>";
211
- }
212
- body += "</Rule>";
213
- }
214
- body += "</LifecycleConfiguration>";
215
- return body;
216
- }
217
-
218
- // ---- CORS XML ----
219
-
220
- var ALLOWED_CORS_METHODS = ["GET", "PUT", "POST", "DELETE", "HEAD"];
221
-
222
- function _buildCorsXml(rules) {
223
- if (!Array.isArray(rules) || rules.length === 0) {
224
- throw _err("INVALID_CORS_RULE",
225
- "setCorsRules: rules must be a non-empty array", true);
226
- }
227
- if (rules.length > 100) {
228
- throw _err("INVALID_CORS_RULE",
229
- "setCorsRules: maximum 100 rules per bucket (S3 spec)", true);
230
- }
231
- var body = '<?xml version="1.0" encoding="UTF-8"?>';
232
- body += '<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
233
- for (var i = 0; i < rules.length; i++) {
234
- var rule = rules[i];
235
- if (!rule || typeof rule !== "object") {
236
- throw _err("INVALID_CORS_RULE",
237
- "rules[" + i + "] must be an object", true);
238
- }
239
- if (!Array.isArray(rule.allowedOrigins) || rule.allowedOrigins.length === 0) {
240
- throw _err("INVALID_CORS_RULE",
241
- "rules[" + i + "].allowedOrigins must be a non-empty array", true);
242
- }
243
- if (!Array.isArray(rule.allowedMethods) || rule.allowedMethods.length === 0) {
244
- throw _err("INVALID_CORS_RULE",
245
- "rules[" + i + "].allowedMethods must be a non-empty array", true);
246
- }
247
- for (var m = 0; m < rule.allowedMethods.length; m++) {
248
- if (ALLOWED_CORS_METHODS.indexOf(rule.allowedMethods[m]) === -1) {
249
- throw _err("INVALID_CORS_RULE",
250
- "rules[" + i + "].allowedMethods[" + m + "] must be one of: " +
251
- ALLOWED_CORS_METHODS.join(", "), true);
252
- }
253
- }
254
- body += "<CORSRule>";
255
- if (rule.id !== undefined) body += "<ID>" + _xmlEscape(rule.id) + "</ID>";
256
- rule.allowedOrigins.forEach(function (o) {
257
- body += "<AllowedOrigin>" + _xmlEscape(o) + "</AllowedOrigin>";
258
- });
259
- rule.allowedMethods.forEach(function (m) {
260
- body += "<AllowedMethod>" + _xmlEscape(m) + "</AllowedMethod>";
261
- });
262
- if (Array.isArray(rule.allowedHeaders)) {
263
- rule.allowedHeaders.forEach(function (h) {
264
- body += "<AllowedHeader>" + _xmlEscape(h) + "</AllowedHeader>";
265
- });
266
- }
267
- if (Array.isArray(rule.exposeHeaders)) {
268
- rule.exposeHeaders.forEach(function (h) {
269
- body += "<ExposeHeader>" + _xmlEscape(h) + "</ExposeHeader>";
270
- });
271
- }
272
- if (rule.maxAgeSeconds !== undefined) {
273
- if (typeof rule.maxAgeSeconds !== "number" || rule.maxAgeSeconds < 0) {
274
- throw _err("INVALID_CORS_RULE",
275
- "rules[" + i + "].maxAgeSeconds must be a non-negative number", true);
276
- }
277
- body += "<MaxAgeSeconds>" + rule.maxAgeSeconds + "</MaxAgeSeconds>";
278
- }
279
- body += "</CORSRule>";
280
- }
281
- body += "</CORSConfiguration>";
282
- if (Buffer.byteLength(body, "utf8") > C.BYTES.kib(64)) {
283
- throw _err("INVALID_CORS_RULE",
284
- "CORS configuration exceeds 64 KB (S3 spec)", true);
285
- }
286
- return body;
287
- }
288
-
289
- // ---- Object Lock + Retention + LegalHold validators / XML ----
290
-
291
- var OBJECT_LOCK_MODES = ["GOVERNANCE", "COMPLIANCE"];
292
- var LEGAL_HOLD_STATES = ["ON", "OFF"];
293
-
294
- function _validateObjectLockConfig(cfg) {
295
- if (!cfg || typeof cfg !== "object") {
296
- throw _err("INVALID_OBJECT_LOCK",
297
- "setObjectLockConfiguration: opts must be an object " +
298
- "with { mode, days|years }", true);
299
- }
300
- if (OBJECT_LOCK_MODES.indexOf(cfg.mode) === -1) {
301
- throw _err("INVALID_OBJECT_LOCK",
302
- "mode must be one of " + OBJECT_LOCK_MODES.join(", ") +
303
- "; got " + JSON.stringify(cfg.mode), true);
304
- }
305
- var hasDays = cfg.days != null;
306
- var hasYears = cfg.years != null;
307
- if (hasDays && hasYears) {
308
- throw _err("INVALID_OBJECT_LOCK",
309
- "specify either days OR years, not both (S3 rule)", true);
310
- }
311
- if (!hasDays && !hasYears) {
312
- throw _err("INVALID_OBJECT_LOCK",
313
- "default retention requires days or years", true);
314
- }
315
- if (hasDays) {
316
- if (typeof cfg.days !== "number" || !Number.isInteger(cfg.days) ||
317
- cfg.days <= 0) {
318
- throw _err("INVALID_OBJECT_LOCK",
319
- "days must be a positive integer; got " + JSON.stringify(cfg.days), true);
320
- }
321
- } else {
322
- if (typeof cfg.years !== "number" || !Number.isInteger(cfg.years) ||
323
- cfg.years <= 0) {
324
- throw _err("INVALID_OBJECT_LOCK",
325
- "years must be a positive integer; got " + JSON.stringify(cfg.years), true);
326
- }
327
- }
328
- }
329
-
330
- function _buildObjectLockConfigXml(cfg) {
331
- var body = '<?xml version="1.0" encoding="UTF-8"?>';
332
- body += '<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
333
- body += '<ObjectLockEnabled>Enabled</ObjectLockEnabled>';
334
- body += '<Rule><DefaultRetention>';
335
- body += '<Mode>' + cfg.mode + '</Mode>';
336
- if (cfg.days != null) body += '<Days>' + cfg.days + '</Days>';
337
- if (cfg.years != null) body += '<Years>' + cfg.years + '</Years>';
338
- body += '</DefaultRetention></Rule>';
339
- body += '</ObjectLockConfiguration>';
340
- return body;
341
- }
342
-
343
- function _validateRetention(opts) {
344
- if (!opts || typeof opts !== "object") {
345
- throw _err("INVALID_RETENTION",
346
- "setObjectRetention: opts must be an object " +
347
- "with { mode, retainUntil }", true);
348
- }
349
- if (OBJECT_LOCK_MODES.indexOf(opts.mode) === -1) {
350
- throw _err("INVALID_RETENTION",
351
- "mode must be one of " + OBJECT_LOCK_MODES.join(", ") +
352
- "; got " + JSON.stringify(opts.mode), true);
353
- }
354
- if (!(opts.retainUntil instanceof Date) || isNaN(opts.retainUntil.getTime())) {
355
- throw _err("INVALID_RETENTION",
356
- "retainUntil must be a valid Date instance", true);
357
- }
358
- if (opts.retainUntil.getTime() <= Date.now()) {
359
- throw _err("INVALID_RETENTION",
360
- "retainUntil must be in the future", true);
361
- }
362
- }
363
-
364
- function _buildRetentionXml(opts) {
365
- return '<?xml version="1.0" encoding="UTF-8"?>' +
366
- '<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
367
- '<Mode>' + opts.mode + '</Mode>' +
368
- '<RetainUntilDate>' + opts.retainUntil.toISOString() + '</RetainUntilDate>' +
369
- '</Retention>';
370
- }
371
-
372
- function _validateLegalHoldStatus(status) {
373
- if (LEGAL_HOLD_STATES.indexOf(status) === -1) {
374
- throw _err("INVALID_LEGAL_HOLD",
375
- "legal-hold status must be one of " + LEGAL_HOLD_STATES.join(", ") +
376
- "; got " + JSON.stringify(status), true);
377
- }
378
- }
379
-
380
- function _buildLegalHoldXml(status) {
381
- return '<?xml version="1.0" encoding="UTF-8"?>' +
382
- '<LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
383
- '<Status>' + status + '</Status>' +
384
- '</LegalHold>';
385
- }
386
-
387
- // S3 + MinIO surface "this lock-related state was never set" via two
388
- // distinct error codes (and HTTP statuses), depending on whether the
389
- // query is at the bucket level or per-object: bucket-level returns 404 +
390
- // `ObjectLockConfigurationNotFoundError`; per-object retention/legal-hold
391
- // returns 4xx + `NoSuchObjectLockConfiguration`. Both translate to the
392
- // same operator answer: "not set". This helper recognizes both so the
393
- // `get*` methods can surface a clean default instead of throwing.
394
- function _isLockNotConfigured(err) {
395
- if (!err) return false;
396
- if (err.statusCode !== 404 && err.statusCode !== 400) return false;
397
- var msg = String(err.message || "");
398
- return msg.indexOf("ObjectLockConfigurationNotFoundError") !== -1 ||
399
- msg.indexOf("NoSuchObjectLockConfiguration") !== -1;
400
- }
401
-
402
- function _validateObjectKey(key) {
403
- if (typeof key !== "string" || key.length === 0) {
404
- throw _err("INVALID_KEY", "object key must be a non-empty string", true);
405
- }
406
- if (key.length > C.BYTES.kib(1)) {
407
- throw _err("INVALID_KEY", "object key exceeds 1024 bytes (S3 limit)", true);
408
- }
409
- }
410
-
411
- // ---- Public factory ----
412
-
413
- function create(config) {
414
- if (!config || typeof config !== "object") {
415
- throw _err("INVALID_CONFIG", "bucketOps.create requires a config object", true);
416
- }
417
- validateOpts(config, [
418
- "protocol", "region", "accessKeyId", "secretAccessKey", "sessionToken",
419
- "endpoint", "pathStyle", "forcePathStyle",
420
- "allowedProtocols", "allowInternal", "timeoutMs",
421
- "ca",
422
- "audit", "observability", "auditSuccess", "auditFailures",
423
- ], "bucketOps");
424
- if (config.protocol && config.protocol !== "sigv4") {
425
- throw _err("INVALID_CONFIG",
426
- "bucketOps currently only supports protocol 'sigv4'; got '" +
427
- config.protocol + "'. GCS and Azure bucket lifecycle differs " +
428
- "substantially per cloud and is operator-managed (Terraform / " +
429
- "CDK / Pulumi).", true);
430
- }
431
- if (!config.region) throw _err("INVALID_CONFIG", "bucketOps: region is required", true);
432
- if (!config.accessKeyId) throw _err("INVALID_CONFIG", "bucketOps: accessKeyId is required", true);
433
- if (!config.secretAccessKey) throw _err("INVALID_CONFIG", "bucketOps: secretAccessKey is required", true);
434
-
435
- var endpoint = config.endpoint || ("https://s3." + config.region + ".amazonaws.com");
436
- if (endpoint.endsWith("/")) endpoint = endpoint.slice(0, -1);
437
- var pathStyle = !!(config.pathStyle || config.forcePathStyle);
438
- var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
439
- var allowInternal = config.allowInternal != null ? config.allowInternal : null;
440
- safeUrl.parse(endpoint, {
441
- allowedProtocols: allowedProtocols,
442
- errorClass: ObjectStoreError,
443
- });
444
- var reqOpts = { timeoutMs: config.timeoutMs, allowedProtocols: allowedProtocols };
445
- if (allowInternal !== null) reqOpts.allowInternal = allowInternal;
446
-
447
- // Audit + observability are framework-best-practice — wired-on by
448
- // default (no operator action needed beyond passing `audit: b.audit`),
449
- // failure-audit always on, success-audit on by default for compliance
450
- // workloads (SEC 17a-4 / FINRA / HIPAA require a trail of
451
- // who-changed-the-retention-policy-and-when). Operators with extreme
452
- // call rates can opt out of success-audit via auditSuccess: false;
453
- // failures still audit so a forensic reconstruction of "what
454
- // happened" is always possible.
455
- var audit = config.audit || null;
456
- var observability = config.observability || null;
457
- var auditSuccess = config.auditSuccess !== false;
458
- var auditFailures = config.auditFailures !== false;
459
-
460
- var _emit = validateOpts.makeAuditEmitter(audit);
461
-
462
- function _emitEvent(name, value, labels) {
463
- if (observability) observability.safeEvent(name, value, labels || {});
464
- }
465
-
466
- function _actor(callerOpts) {
467
- return requestHelpers.resolveActorWithOverride(callerOpts || {}, null);
468
- }
469
-
470
- // S3 subresource queries (`?lifecycle`, `?cors`, `?object-lock`,
471
- // `?retention`, `?legal-hold`) are *bare* tokens on the wire — the
472
- // trailing `=` produced by `URLSearchParams.set(k, "")` is interpreted
473
- // by some S3 implementations (MinIO in particular) as "this is a
474
- // body-write with a query parameter, not a subresource", which routes
475
- // the request to the wrong handler. The SigV4 canonicalizer always
476
- // reads `key=value` (with the empty `=`) per AWS spec, so the signature
477
- // computation is unaffected; only the wire form differs. To preserve
478
- // both behaviors, we build the URL string manually and pass it to the
479
- // URL constructor — the constructor preserves the bare token in
480
- // `url.search` while `url.searchParams` still presents it as `key=`.
481
- function _appendQuery(base, query) {
482
- if (!query) return base;
483
- var keys = Object.keys(query);
484
- if (keys.length === 0) return base;
485
- var parts = keys.map(function (k) {
486
- var v = query[k];
487
- if (v === "" || v == null) return encodeURIComponent(k);
488
- return encodeURIComponent(k) + "=" + encodeURIComponent(v);
489
- });
490
- return base + "?" + parts.join("&");
491
- }
492
-
493
- function _bucketUrl(name, query) {
494
- var ub = _internalUrl(endpoint, allowedProtocols);
495
- if (pathStyle) {
496
- ub.pathname = "/" + name + "/";
497
- } else {
498
- ub.hostname = name + "." + ub.hostname;
499
- ub.pathname = "/";
500
- }
501
- var base = ub.toString();
502
- return _internalUrl(_appendQuery(base, query), allowedProtocols);
503
- }
504
-
505
- function _objectUrl(name, key, query) {
506
- // Each key segment is encoded individually so that legitimate "/"
507
- // separators in the key are preserved (S3 treats keys with slashes
508
- // as flat names, not directories).
509
- var encKey = key.split("/").map(encodeURIComponent).join("/");
510
- var uo = _internalUrl(endpoint, allowedProtocols);
511
- if (pathStyle) {
512
- uo.pathname = "/" + name + "/" + encKey;
513
- } else {
514
- uo.hostname = name + "." + uo.hostname;
515
- uo.pathname = "/" + encKey;
516
- }
517
- var base = uo.toString();
518
- return _internalUrl(_appendQuery(base, query), allowedProtocols);
519
- }
520
-
521
- function _serviceUrl(query) {
522
- // ListBuckets — service-level, no bucket prefix.
523
- var u = _internalUrl(endpoint, allowedProtocols);
524
- u.pathname = "/";
525
- if (query) {
526
- Object.keys(query).forEach(function (k) { u.searchParams.set(k, query[k]); });
527
- }
528
- return u;
529
- }
530
-
531
- function _signed(method, url, payloadHash, extraHeaders) {
532
- var signed = sigv4.signRequest({
533
- method: method,
534
- url: url,
535
- headers: extraHeaders || {},
536
- payloadHash: payloadHash,
537
- region: config.region,
538
- accessKeyId: config.accessKeyId,
539
- secretAccessKey: config.secretAccessKey,
540
- sessionToken: config.sessionToken,
541
- });
542
- return signed.headers;
543
- }
544
-
545
- function _request(method, url, headers, body) {
546
- return sharedRequest(method, url, headers, body, reqOpts);
547
- }
548
-
549
- // ---- create ----
550
-
551
- function createBucket(name, opts) {
552
- _validateBucketName(name);
553
- opts = opts || {};
554
- validateOpts(opts, ["region", "objectLockEnabled", "req", "actor"],
555
- "bucketOps.create");
556
- var targetRegion = opts.region || config.region;
557
- var url = _bucketUrl(name);
558
- var bodyBuf;
559
- var extra = {};
560
- // us-east-1 takes an empty body. Other regions need
561
- // CreateBucketConfiguration with a LocationConstraint.
562
- if (targetRegion && targetRegion !== "us-east-1") {
563
- bodyBuf = Buffer.from(
564
- '<?xml version="1.0" encoding="UTF-8"?>' +
565
- '<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
566
- '<LocationConstraint>' + _xmlEscape(targetRegion) + '</LocationConstraint>' +
567
- '</CreateBucketConfiguration>',
568
- "utf8"
569
- );
570
- extra["Content-Type"] = "application/xml";
571
- extra["Content-Length"] = String(bodyBuf.length);
572
- } else {
573
- bodyBuf = Buffer.alloc(0);
574
- extra["Content-Length"] = "0";
575
- }
576
- // Object Lock can ONLY be enabled at create time — flipping it on
577
- // a live bucket isn\'t an S3 API. Setting the header on PutBucket
578
- // turns on the underlying versioning + write-once-read-many
579
- // semantics so subsequent setObjectLockConfiguration / Retention
580
- // / LegalHold calls actually do something.
581
- if (opts.objectLockEnabled === true) {
582
- extra["x-amz-bucket-object-lock-enabled"] = "true";
583
- }
584
- var payloadHash = sigv4.sha256Hex(bodyBuf);
585
- var headers = _signed("PUT", url, payloadHash, extra);
586
- return _request("PUT", url, headers, bodyBuf).then(
587
- function () {
588
- if (auditSuccess) {
589
- _emit("objectstore.bucket.create", {
590
- actor: _actor(opts),
591
- resource: { kind: "bucket", id: name },
592
- metadata: {
593
- region: targetRegion,
594
- objectLockEnabled: opts.objectLockEnabled === true,
595
- },
596
- });
597
- }
598
- _emitEvent("objectstore.bucket.create", 1,
599
- { outcome: "success", region: targetRegion });
600
- return { created: true, name: name, region: targetRegion };
601
- },
602
- function (e) {
603
- // Map S3 conflict response codes into stable framework codes.
604
- var mapped = e;
605
- if (e.statusCode === 409 && /BucketAlreadyOwnedByYou/.test(e.message || "")) {
606
- mapped = _err("BUCKET_ALREADY_OWNED",
607
- "bucket '" + name + "' already exists and is owned by this account", true);
608
- } else if (e.statusCode === 409) {
609
- mapped = _err("BUCKET_NAME_TAKEN",
610
- "bucket name '" + name + "' is taken in S3's global namespace", true);
611
- }
612
- if (auditFailures) {
613
- _emit("objectstore.bucket.create", {
614
- actor: _actor(opts),
615
- resource: { kind: "bucket", id: name },
616
- outcome: "failure",
617
- reason: mapped.code || "error",
618
- metadata: { region: targetRegion },
619
- });
620
- }
621
- _emitEvent("objectstore.bucket.create", 1,
622
- { outcome: "failure", reason: mapped.code || "error" });
623
- throw mapped;
624
- }
625
- );
626
- }
627
-
628
- // ---- delete ----
629
-
630
- function deleteBucket(name, opts) {
631
- _validateBucketName(name);
632
- opts = opts || {};
633
- validateOpts(opts, ["req", "actor"], "bucketOps.delete");
634
- var url = _bucketUrl(name);
635
- var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
636
- var headers = _signed("DELETE", url, payloadHash);
637
- return _request("DELETE", url, headers, null).then(
638
- function () {
639
- if (auditSuccess) {
640
- _emit("objectstore.bucket.delete", {
641
- actor: _actor(opts),
642
- resource: { kind: "bucket", id: name },
643
- metadata: { existed: true },
644
- });
645
- }
646
- _emitEvent("objectstore.bucket.delete", 1,
647
- { outcome: "success", existed: "true" });
648
- return true;
649
- },
650
- function (e) {
651
- if (e.statusCode === 404) {
652
- // Idempotent: missing bucket → false. Audit as success-with-noop
653
- // so the trail still records "operator attempted delete".
654
- if (auditSuccess) {
655
- _emit("objectstore.bucket.delete", {
656
- actor: _actor(opts),
657
- resource: { kind: "bucket", id: name },
658
- metadata: { existed: false },
659
- });
660
- }
661
- _emitEvent("objectstore.bucket.delete", 1,
662
- { outcome: "success", existed: "false" });
663
- return false;
664
- }
665
- var mapped = e;
666
- if (e.statusCode === 409 && /BucketNotEmpty/.test(e.message || "")) {
667
- mapped = _err("BUCKET_NOT_EMPTY",
668
- "bucket '" + name + "' is not empty; delete all objects + " +
669
- "noncurrent versions + delete-markers first", true);
670
- }
671
- if (auditFailures) {
672
- _emit("objectstore.bucket.delete", {
673
- actor: _actor(opts),
674
- resource: { kind: "bucket", id: name },
675
- outcome: "failure",
676
- reason: mapped.code || "error",
677
- });
678
- }
679
- _emitEvent("objectstore.bucket.delete", 1,
680
- { outcome: "failure", reason: mapped.code || "error" });
681
- throw mapped;
682
- }
683
- );
684
- }
685
-
686
- // ---- list ----
687
-
688
- function listBuckets() {
689
- var url = _serviceUrl();
690
- var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
691
- var headers = _signed("GET", url, payloadHash);
692
- return _request("GET", url, headers, null).then(function (res) {
693
- var doc = safeXml.parse(res.body);
694
- var result = doc.ListAllMyBucketsResult || {};
695
- var bucketsContainer = result.Buckets || {};
696
- var raw = bucketsContainer.Bucket;
697
- var arr;
698
- if (!raw) arr = [];
699
- else if (Array.isArray(raw)) arr = raw;
700
- else arr = [raw];
701
- _emitEvent("objectstore.bucket.list", arr.length, { outcome: "success" });
702
- return arr.map(function (b) {
703
- return {
704
- name: b.Name,
705
- creationDate: b.CreationDate ? Date.parse(b.CreationDate) : null,
706
- region: b.BucketRegion || null,
707
- };
708
- });
709
- });
710
- }
711
-
712
- // ---- setLifecycle ----
713
-
714
- function setLifecycle(name, rules, opts) {
715
- _validateBucketName(name);
716
- opts = opts || {};
717
- validateOpts(opts, ["req", "actor"], "bucketOps.setLifecycle");
718
- var bodyXml = _buildLifecycleXml(rules);
719
- var bodyBuf = Buffer.from(bodyXml, "utf8");
720
- var url = _bucketUrl(name, { lifecycle: "" });
721
- var payloadHash = sigv4.sha256Hex(bodyBuf);
722
- var headers = _signed("PUT", url, payloadHash, {
723
- "Content-Type": "application/xml",
724
- "Content-Length": String(bodyBuf.length),
725
- "Content-MD5": _md5Base64(bodyBuf),
726
- });
727
- return _request("PUT", url, headers, bodyBuf).then(
728
- function () {
729
- if (auditSuccess) {
730
- _emit("objectstore.bucket.setLifecycle", {
731
- actor: _actor(opts),
732
- resource: { kind: "bucket", id: name },
733
- metadata: { ruleCount: rules.length },
734
- });
735
- }
736
- _emitEvent("objectstore.bucket.setLifecycle", 1,
737
- { outcome: "success", ruleCount: String(rules.length) });
738
- return { applied: true, name: name, ruleCount: rules.length };
739
- },
740
- function (e) {
741
- if (auditFailures) {
742
- _emit("objectstore.bucket.setLifecycle", {
743
- actor: _actor(opts),
744
- resource: { kind: "bucket", id: name },
745
- outcome: "failure",
746
- reason: e.code || "error",
747
- });
748
- }
749
- _emitEvent("objectstore.bucket.setLifecycle", 1,
750
- { outcome: "failure", reason: e.code || "error" });
751
- throw e;
752
- }
753
- );
754
- }
755
-
756
- // ---- setCorsRules ----
757
-
758
- function setCorsRules(name, rules, opts) {
759
- _validateBucketName(name);
760
- opts = opts || {};
761
- validateOpts(opts, ["req", "actor"], "bucketOps.setCorsRules");
762
- var bodyXml = _buildCorsXml(rules);
763
- var bodyBuf = Buffer.from(bodyXml, "utf8");
764
- var url = _bucketUrl(name, { cors: "" });
765
- var payloadHash = sigv4.sha256Hex(bodyBuf);
766
- var headers = _signed("PUT", url, payloadHash, {
767
- "Content-Type": "application/xml",
768
- "Content-Length": String(bodyBuf.length),
769
- "Content-MD5": _md5Base64(bodyBuf),
770
- });
771
- return _request("PUT", url, headers, bodyBuf).then(
772
- function () {
773
- if (auditSuccess) {
774
- _emit("objectstore.bucket.setCorsRules", {
775
- actor: _actor(opts),
776
- resource: { kind: "bucket", id: name },
777
- metadata: { ruleCount: rules.length },
778
- });
779
- }
780
- _emitEvent("objectstore.bucket.setCorsRules", 1,
781
- { outcome: "success", ruleCount: String(rules.length) });
782
- return { applied: true, name: name, ruleCount: rules.length };
783
- },
784
- function (e) {
785
- if (auditFailures) {
786
- _emit("objectstore.bucket.setCorsRules", {
787
- actor: _actor(opts),
788
- resource: { kind: "bucket", id: name },
789
- outcome: "failure",
790
- reason: e.code || "error",
791
- });
792
- }
793
- _emitEvent("objectstore.bucket.setCorsRules", 1,
794
- { outcome: "failure", reason: e.code || "error" });
795
- throw e;
796
- }
797
- );
798
- }
799
-
800
- // ---- Object Lock configuration (bucket-level) ----
801
-
802
- function setObjectLockConfiguration(name, opts) {
803
- _validateBucketName(name);
804
- _validateObjectLockConfig(opts);
805
- validateOpts(opts, ["mode", "days", "years", "req", "actor"],
806
- "bucketOps.setObjectLockConfiguration");
807
- var bodyXml = _buildObjectLockConfigXml(opts);
808
- var bodyBuf = Buffer.from(bodyXml, "utf8");
809
- var url = _bucketUrl(name, { "object-lock": "" });
810
- var payloadHash = sigv4.sha256Hex(bodyBuf);
811
- var headers = _signed("PUT", url, payloadHash, {
812
- "Content-Type": "application/xml",
813
- "Content-Length": String(bodyBuf.length),
814
- "Content-MD5": _md5Base64(bodyBuf),
815
- });
816
- return _request("PUT", url, headers, bodyBuf).then(
817
- function () {
818
- // Compliance-critical event — SEC 17a-4 / FINRA / HIPAA require
819
- // a trail of who-changed-the-default-retention-policy-and-when.
820
- if (auditSuccess) {
821
- _emit("objectstore.bucket.setObjectLockConfiguration", {
822
- actor: _actor(opts),
823
- resource: { kind: "bucket", id: name },
824
- metadata: {
825
- mode: opts.mode,
826
- days: opts.days != null ? opts.days : null,
827
- years: opts.years != null ? opts.years : null,
828
- },
829
- });
830
- }
831
- _emitEvent("objectstore.bucket.setObjectLockConfiguration", 1,
832
- { outcome: "success", mode: opts.mode });
833
- return {
834
- applied: true, name: name,
835
- mode: opts.mode,
836
- days: opts.days != null ? opts.days : null,
837
- years: opts.years != null ? opts.years : null,
838
- };
839
- },
840
- function (e) {
841
- if (auditFailures) {
842
- _emit("objectstore.bucket.setObjectLockConfiguration", {
843
- actor: _actor(opts),
844
- resource: { kind: "bucket", id: name },
845
- outcome: "failure",
846
- reason: e.code || "error",
847
- metadata: { mode: opts.mode },
848
- });
849
- }
850
- _emitEvent("objectstore.bucket.setObjectLockConfiguration", 1,
851
- { outcome: "failure", reason: e.code || "error" });
852
- throw e;
853
- }
854
- );
855
- }
856
-
857
- function getObjectLockConfiguration(name) {
858
- _validateBucketName(name);
859
- _emitEvent("objectstore.bucket.getObjectLockConfiguration", 1,
860
- { outcome: "success" });
861
- var url = _bucketUrl(name, { "object-lock": "" });
862
- var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
863
- var headers = _signed("GET", url, payloadHash);
864
- return _request("GET", url, headers, null).then(
865
- function (res) {
866
- var doc = safeXml.parse(res.body);
867
- var olc = doc.ObjectLockConfiguration || {};
868
- var enabled = olc.ObjectLockEnabled === "Enabled";
869
- var rule = olc.Rule || {};
870
- var def = rule.DefaultRetention || {};
871
- return {
872
- enabled: enabled,
873
- mode: def.Mode || null,
874
- days: def.Days != null ? Number(def.Days) : null,
875
- years: def.Years != null ? Number(def.Years) : null,
876
- };
877
- },
878
- function (e) {
879
- // S3 + MinIO return 404 + ObjectLockConfigurationNotFoundError
880
- // when the bucket was created without `objectLockEnabled: true`.
881
- // That's a "not configured" state, not an error worth bubbling
882
- // — operators asking "is this bucket lock-enabled?" want a
883
- // clean false, not a try/catch.
884
- if (_isLockNotConfigured(e)) {
885
- return { enabled: false, mode: null, days: null, years: null };
886
- }
887
- throw e;
888
- }
889
- );
890
- }
891
-
892
- // ---- Per-object retention ----
893
-
894
- function setObjectRetention(name, key, opts) {
895
- _validateBucketName(name);
896
- _validateObjectKey(key);
897
- _validateRetention(opts);
898
- validateOpts(opts, ["mode", "retainUntil", "bypassGovernance", "req", "actor"],
899
- "bucketOps.setObjectRetention");
900
- var bodyXml = _buildRetentionXml(opts);
901
- var bodyBuf = Buffer.from(bodyXml, "utf8");
902
- var url = _objectUrl(name, key, { retention: "" });
903
- var extra = {
904
- "Content-Type": "application/xml",
905
- "Content-Length": String(bodyBuf.length),
906
- "Content-MD5": _md5Base64(bodyBuf),
907
- };
908
- if (opts.bypassGovernance === true) {
909
- extra["x-amz-bypass-governance-retention"] = "true";
910
- }
911
- var payloadHash = sigv4.sha256Hex(bodyBuf);
912
- var headers = _signed("PUT", url, payloadHash, extra);
913
- return _request("PUT", url, headers, bodyBuf).then(
914
- function () {
915
- // Compliance trail — bypassGovernance is the high-risk op
916
- // (operator with s3:BypassGovernanceRetention shortened a
917
- // GOVERNANCE retention) and operators wire alerting on this
918
- // metadata field.
919
- if (auditSuccess) {
920
- _emit("objectstore.object.setRetention", {
921
- actor: _actor(opts),
922
- resource: { kind: "object", id: name + "/" + key },
923
- metadata: {
924
- bucket: name,
925
- key: key,
926
- mode: opts.mode,
927
- retainUntilIso: opts.retainUntil.toISOString(),
928
- bypassGovernance: opts.bypassGovernance === true,
929
- },
930
- });
931
- }
932
- _emitEvent("objectstore.object.setRetention", 1,
933
- { outcome: "success", mode: opts.mode,
934
- bypassGovernance: opts.bypassGovernance === true ? "true" : "false" });
935
- return {
936
- applied: true,
937
- name: name,
938
- key: key,
939
- mode: opts.mode,
940
- retainUntil: opts.retainUntil,
941
- };
942
- },
943
- function (e) {
944
- if (auditFailures) {
945
- _emit("objectstore.object.setRetention", {
946
- actor: _actor(opts),
947
- resource: { kind: "object", id: name + "/" + key },
948
- outcome: "failure",
949
- reason: e.code || "error",
950
- metadata: {
951
- bucket: name,
952
- key: key,
953
- mode: opts.mode,
954
- bypassGovernance: opts.bypassGovernance === true,
955
- },
956
- });
957
- }
958
- _emitEvent("objectstore.object.setRetention", 1,
959
- { outcome: "failure", reason: e.code || "error" });
960
- throw e;
961
- }
962
- );
963
- }
964
-
965
- function getObjectRetention(name, key) {
966
- _validateBucketName(name);
967
- _validateObjectKey(key);
968
- _emitEvent("objectstore.object.getRetention", 1, { outcome: "success" });
969
- var url = _objectUrl(name, key, { retention: "" });
970
- var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
971
- var headers = _signed("GET", url, payloadHash);
972
- return _request("GET", url, headers, null).then(
973
- function (res) {
974
- var doc = safeXml.parse(res.body);
975
- var ret = doc.Retention || {};
976
- var until = ret.RetainUntilDate ? new Date(ret.RetainUntilDate) : null;
977
- return {
978
- mode: ret.Mode || null,
979
- retainUntil: until,
980
- };
981
- },
982
- function (e) {
983
- // S3 + MinIO return 4xx + NoSuchObjectLockConfiguration when the
984
- // object has no per-object retention applied. Same UX choice as
985
- // getObjectLockConfiguration: surface the not-set state cleanly
986
- // rather than forcing operators to try/catch on a known-good
987
- // request shape.
988
- if (_isLockNotConfigured(e)) {
989
- return { mode: null, retainUntil: null };
990
- }
991
- throw e;
992
- }
993
- );
994
- }
995
-
996
- // ---- Per-object legal hold ----
997
-
998
- function setObjectLegalHold(name, key, status, opts) {
999
- _validateBucketName(name);
1000
- _validateObjectKey(key);
1001
- _validateLegalHoldStatus(status);
1002
- opts = opts || {};
1003
- validateOpts(opts, ["req", "actor"], "bucketOps.setObjectLegalHold");
1004
- var bodyXml = _buildLegalHoldXml(status);
1005
- var bodyBuf = Buffer.from(bodyXml, "utf8");
1006
- var url = _objectUrl(name, key, { "legal-hold": "" });
1007
- var payloadHash = sigv4.sha256Hex(bodyBuf);
1008
- var headers = _signed("PUT", url, payloadHash, {
1009
- "Content-Type": "application/xml",
1010
- "Content-Length": String(bodyBuf.length),
1011
- "Content-MD5": _md5Base64(bodyBuf),
1012
- });
1013
- return _request("PUT", url, headers, bodyBuf).then(
1014
- function () {
1015
- if (auditSuccess) {
1016
- _emit("objectstore.object.setLegalHold", {
1017
- actor: _actor(opts),
1018
- resource: { kind: "object", id: name + "/" + key },
1019
- metadata: { bucket: name, key: key, status: status },
1020
- });
1021
- }
1022
- _emitEvent("objectstore.object.setLegalHold", 1,
1023
- { outcome: "success", status: status });
1024
- return { applied: true, name: name, key: key, status: status };
1025
- },
1026
- function (e) {
1027
- if (auditFailures) {
1028
- _emit("objectstore.object.setLegalHold", {
1029
- actor: _actor(opts),
1030
- resource: { kind: "object", id: name + "/" + key },
1031
- outcome: "failure",
1032
- reason: e.code || "error",
1033
- metadata: { bucket: name, key: key, status: status },
1034
- });
1035
- }
1036
- _emitEvent("objectstore.object.setLegalHold", 1,
1037
- { outcome: "failure", reason: e.code || "error" });
1038
- throw e;
1039
- }
1040
- );
1041
- }
1042
-
1043
- function getObjectLegalHold(name, key) {
1044
- _validateBucketName(name);
1045
- _validateObjectKey(key);
1046
- _emitEvent("objectstore.object.getLegalHold", 1, { outcome: "success" });
1047
- var url = _objectUrl(name, key, { "legal-hold": "" });
1048
- var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
1049
- var headers = _signed("GET", url, payloadHash);
1050
- return _request("GET", url, headers, null).then(
1051
- function (res) {
1052
- var doc = safeXml.parse(res.body);
1053
- var lh = doc.LegalHold || {};
1054
- return { status: lh.Status || null };
1055
- },
1056
- function (e) {
1057
- // Object never had a legal hold set — S3 + MinIO surface this
1058
- // as NoSuchObjectLockConfiguration. Return a clean OFF instead
1059
- // of throwing; "no hold ever applied" is operationally
1060
- // identical to "hold is OFF".
1061
- if (_isLockNotConfigured(e)) {
1062
- return { status: "OFF" };
1063
- }
1064
- throw e;
1065
- }
1066
- );
1067
- }
1068
-
1069
- return {
1070
- protocol: "sigv4",
1071
- create: createBucket,
1072
- delete: deleteBucket,
1073
- list: listBuckets,
1074
- setLifecycle: setLifecycle,
1075
- setCorsRules: setCorsRules,
1076
- setObjectLockConfiguration: setObjectLockConfiguration,
1077
- getObjectLockConfiguration: getObjectLockConfiguration,
1078
- setObjectRetention: setObjectRetention,
1079
- getObjectRetention: getObjectRetention,
1080
- setObjectLegalHold: setObjectLegalHold,
1081
- getObjectLegalHold: getObjectLegalHold,
1082
- };
1083
- }
1084
-
1085
- module.exports = {
1086
- create: create,
1087
- // Test-only exports for unit-testing the XML builders without
1088
- // standing up a fake S3 server.
1089
- _buildLifecycleXmlForTest: _buildLifecycleXml,
1090
- _buildCorsXmlForTest: _buildCorsXml,
1091
- _validateBucketNameForTest: _validateBucketName,
1092
- };
1
+ "use strict";
2
+ /**
3
+ * sigv4-bucket-ops — bucket-level operations for SigV4 backends.
4
+ *
5
+ * Per-object ops (put / get / list / delete / multipart) live in
6
+ * lib/object-store/sigv4.js and are bound to a single bucket at
7
+ * create() time. Bucket lifecycle ops are at a different level —
8
+ * they need a service-scoped client that addresses arbitrary
9
+ * buckets — so they get their own factory.
10
+ *
11
+ * Operators with multi-cloud ambitions reach for Terraform / CDK /
12
+ * Pulumi. The framework's bucket-ops surface is the operator-from-app
13
+ * path: create the bucket your app needs at boot, attach a lifecycle
14
+ * rule that aborts incomplete multiparts after a week, etc. Niche ops
15
+ * (Object Lock, Replication, Inventory, Notification) are deferred —
16
+ * they're well into Terraform territory.
17
+ *
18
+ * Public API:
19
+ *
20
+ * var ops = b.objectStore.bucketOps.create({
21
+ * protocol: "sigv4",
22
+ * region: "us-east-1",
23
+ * accessKeyId: env("AWS_ACCESS_KEY_ID"),
24
+ * secretAccessKey: env("AWS_SECRET_ACCESS_KEY"),
25
+ * endpoint: "https://s3.us-east-1.amazonaws.com", // optional
26
+ * pathStyle: false,
27
+ * timeoutMs: 30000,
28
+ * });
29
+ *
30
+ * await ops.create("my-bucket", { region: "eu-west-1" });
31
+ * await ops.delete("my-bucket");
32
+ * var buckets = await ops.list(); // [{ name, creationDate }]
33
+ * await ops.setLifecycle("my-bucket", [{
34
+ * id: "abort-stale-multiparts",
35
+ * status: "Enabled",
36
+ * prefix: "",
37
+ * abortIncompleteMultipartUpload: { daysAfterInitiation: 7 },
38
+ * }]);
39
+ * await ops.setCorsRules("my-bucket", [{
40
+ * allowedOrigins: ["https://app.example.com"],
41
+ * allowedMethods: ["GET", "PUT", "POST"],
42
+ * allowedHeaders: ["*"],
43
+ * exposeHeaders: ["ETag"],
44
+ * maxAgeSeconds: 3600,
45
+ * }]);
46
+ *
47
+ * Validation rejects every bad input shape at the call site rather
48
+ * than producing a server-side 400. Errors surface as ObjectStoreError
49
+ * with codes (BUCKET_INVALID_NAME, INVALID_LIFECYCLE, INVALID_CORS_RULE,
50
+ * BUCKET_ALREADY_OWNED, BUCKET_NOT_EMPTY, etc.).
51
+ */
52
+ var nodeCrypto = require("crypto");
53
+ var C = require("../constants");
54
+ var requestHelpers = require("../request-helpers");
55
+ var sigv4 = require("./sigv4");
56
+ var sharedRequest = require("./http-request");
57
+ var safeXml = require("../parsers/safe-xml");
58
+ var safeUrl = require("../safe-url");
59
+ var template = require("../template");
60
+ var validateOpts = require("../validate-opts");
61
+ var { ObjectStoreError } = require("../framework-error");
62
+
63
+ var _err = ObjectStoreError.factory;
64
+
65
+ // Internal URL builder — endpoint + path string from validated config.
66
+ // Routes through safeUrl.parse so the protocol allowlist + length cap
67
+ // apply uniformly. Cap is generous since presigned-style URLs with many
68
+ // query params can approach 8 KB.
69
+ function _internalUrl(input, allowedProtocols) {
70
+ return safeUrl.parse(input, {
71
+ allowedProtocols: allowedProtocols || safeUrl.ALLOW_HTTP_TLS,
72
+ errorClass: ObjectStoreError,
73
+ maxUrlLength: C.BYTES.kib(32),
74
+ });
75
+ }
76
+
77
+ // S3 bucket-name rules (general purpose). Source: AWS docs
78
+ // "Bucket naming rules". Lowercase letters, digits, hyphens; 3..63
79
+ // chars; no consecutive dots; cannot end in -s3alias / -ol-s3 etc.
80
+ // We catch the common-mistake cases at config time; AWS catches the
81
+ // rest at request time.
82
+ var BUCKET_NAME_RE = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
83
+
84
+ function _validateBucketName(name) {
85
+ if (typeof name !== "string" || name.length < 3 || name.length > 63) {
86
+ throw _err("BUCKET_INVALID_NAME",
87
+ "bucket name must be a string of length 3..63, got " +
88
+ (typeof name === "string" ? "length " + name.length : typeof name), true);
89
+ }
90
+ // Length is bounded above (3..63) before the regex runs so the
91
+ // pattern can't be driven against an unbounded input.
92
+ if (name.length > 63 || !BUCKET_NAME_RE.test(name)) {
93
+ throw _err("BUCKET_INVALID_NAME",
94
+ "bucket name '" + name + "' violates S3 naming rules " +
95
+ "(lowercase, digits, hyphens, dots — no leading/trailing punct)", true);
96
+ }
97
+ if (name.indexOf("..") !== -1) {
98
+ throw _err("BUCKET_INVALID_NAME",
99
+ "bucket name '" + name + "' contains consecutive dots", true);
100
+ }
101
+ }
102
+
103
+ // XML body strings flow through template.escapeHtml — `&#x27;` (which
104
+ // it emits for the apostrophe) is a numeric character reference and
105
+ // is valid in both XML and HTML, where `&apos;` is XML-only. AWS S3
106
+ // accepts both; using the shared HTML escape keeps the framework
107
+ // down to one canonical escape primitive.
108
+ var _xmlEscape = template.escapeHtml;
109
+
110
+ // AWS PutBucketLifecycle / PutBucketCors require a Content-MD5 header
111
+ // for body integrity (legacy AWS API requirement; SigV4 already covers
112
+ // integrity via x-amz-content-sha256 but the API still validates this).
113
+ // MD5 here is NOT a credential or security primitive — it's an AWS API
114
+ // shape. b.credentialHash is the wrong tool.
115
+ function _md5Base64(buf) {
116
+ return nodeCrypto.createHash("md5").update(buf).digest("base64");
117
+ }
118
+
119
+ // ---- Lifecycle XML ----
120
+
121
+ var ALLOWED_STORAGE_CLASSES = [
122
+ "STANDARD", "REDUCED_REDUNDANCY", "STANDARD_IA", "ONEZONE_IA",
123
+ "INTELLIGENT_TIERING", "GLACIER", "DEEP_ARCHIVE", "GLACIER_IR",
124
+ "EXPRESS_ONEZONE",
125
+ ];
126
+
127
+ function _buildLifecycleXml(rules) {
128
+ if (!Array.isArray(rules) || rules.length === 0) {
129
+ throw _err("INVALID_LIFECYCLE",
130
+ "setLifecycle: rules must be a non-empty array", true);
131
+ }
132
+ // S3 spec hard cap on lifecycle rules per bucket.
133
+ var MAX_LIFECYCLE_RULES = 1000;
134
+ if (rules.length > MAX_LIFECYCLE_RULES) {
135
+ throw _err("INVALID_LIFECYCLE",
136
+ "setLifecycle: maximum " + MAX_LIFECYCLE_RULES + " rules per bucket (S3 spec)", true);
137
+ }
138
+ var body = '<?xml version="1.0" encoding="UTF-8"?>';
139
+ body += '<LifecycleConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
140
+ for (var i = 0; i < rules.length; i++) {
141
+ var rule = rules[i];
142
+ if (!rule || typeof rule !== "object") {
143
+ throw _err("INVALID_LIFECYCLE",
144
+ "rules[" + i + "] must be an object", true);
145
+ }
146
+ var status = rule.status || "Enabled";
147
+ if (status !== "Enabled" && status !== "Disabled") {
148
+ throw _err("INVALID_LIFECYCLE",
149
+ "rules[" + i + "].status must be 'Enabled' or 'Disabled'", true);
150
+ }
151
+ if (!rule.expiration && !rule.transition && !rule.abortIncompleteMultipartUpload) {
152
+ throw _err("INVALID_LIFECYCLE",
153
+ "rules[" + i + "] must specify at least one of " +
154
+ "expiration / transition / abortIncompleteMultipartUpload", true);
155
+ }
156
+ body += "<Rule>";
157
+ if (rule.id !== undefined) {
158
+ if (typeof rule.id !== "string" || rule.id.length === 0) {
159
+ throw _err("INVALID_LIFECYCLE",
160
+ "rules[" + i + "].id must be a non-empty string when set", true);
161
+ }
162
+ body += "<ID>" + _xmlEscape(rule.id) + "</ID>";
163
+ }
164
+ body += "<Filter><Prefix>" + _xmlEscape(rule.prefix || "") + "</Prefix></Filter>";
165
+ body += "<Status>" + status + "</Status>";
166
+ if (rule.expiration) {
167
+ body += "<Expiration>";
168
+ if (rule.expiration.days !== undefined) {
169
+ if (typeof rule.expiration.days !== "number" || rule.expiration.days < 1) {
170
+ throw _err("INVALID_LIFECYCLE",
171
+ "rules[" + i + "].expiration.days must be a positive integer", true);
172
+ }
173
+ body += "<Days>" + rule.expiration.days + "</Days>";
174
+ }
175
+ if (rule.expiration.date !== undefined) {
176
+ body += "<Date>" + _xmlEscape(rule.expiration.date) + "</Date>";
177
+ }
178
+ if (rule.expiration.expiredObjectDeleteMarker !== undefined) {
179
+ body += "<ExpiredObjectDeleteMarker>" +
180
+ (rule.expiration.expiredObjectDeleteMarker ? "true" : "false") +
181
+ "</ExpiredObjectDeleteMarker>";
182
+ }
183
+ body += "</Expiration>";
184
+ }
185
+ if (rule.transition) {
186
+ if (ALLOWED_STORAGE_CLASSES.indexOf(rule.transition.storageClass) === -1) {
187
+ throw _err("INVALID_LIFECYCLE",
188
+ "rules[" + i + "].transition.storageClass must be one of: " +
189
+ ALLOWED_STORAGE_CLASSES.join(", "), true);
190
+ }
191
+ body += "<Transition>";
192
+ if (rule.transition.days !== undefined) {
193
+ body += "<Days>" + rule.transition.days + "</Days>";
194
+ }
195
+ if (rule.transition.date !== undefined) {
196
+ body += "<Date>" + _xmlEscape(rule.transition.date) + "</Date>";
197
+ }
198
+ body += "<StorageClass>" + rule.transition.storageClass + "</StorageClass>";
199
+ body += "</Transition>";
200
+ }
201
+ if (rule.abortIncompleteMultipartUpload) {
202
+ var dai = rule.abortIncompleteMultipartUpload.daysAfterInitiation;
203
+ if (typeof dai !== "number" || dai < 1) {
204
+ throw _err("INVALID_LIFECYCLE",
205
+ "rules[" + i + "].abortIncompleteMultipartUpload.daysAfterInitiation " +
206
+ "must be a positive integer", true);
207
+ }
208
+ body += "<AbortIncompleteMultipartUpload>";
209
+ body += "<DaysAfterInitiation>" + dai + "</DaysAfterInitiation>";
210
+ body += "</AbortIncompleteMultipartUpload>";
211
+ }
212
+ body += "</Rule>";
213
+ }
214
+ body += "</LifecycleConfiguration>";
215
+ return body;
216
+ }
217
+
218
+ // ---- CORS XML ----
219
+
220
+ var ALLOWED_CORS_METHODS = ["GET", "PUT", "POST", "DELETE", "HEAD"];
221
+
222
+ function _buildCorsXml(rules) {
223
+ if (!Array.isArray(rules) || rules.length === 0) {
224
+ throw _err("INVALID_CORS_RULE",
225
+ "setCorsRules: rules must be a non-empty array", true);
226
+ }
227
+ if (rules.length > 100) {
228
+ throw _err("INVALID_CORS_RULE",
229
+ "setCorsRules: maximum 100 rules per bucket (S3 spec)", true);
230
+ }
231
+ var body = '<?xml version="1.0" encoding="UTF-8"?>';
232
+ body += '<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
233
+ for (var i = 0; i < rules.length; i++) {
234
+ var rule = rules[i];
235
+ if (!rule || typeof rule !== "object") {
236
+ throw _err("INVALID_CORS_RULE",
237
+ "rules[" + i + "] must be an object", true);
238
+ }
239
+ if (!Array.isArray(rule.allowedOrigins) || rule.allowedOrigins.length === 0) {
240
+ throw _err("INVALID_CORS_RULE",
241
+ "rules[" + i + "].allowedOrigins must be a non-empty array", true);
242
+ }
243
+ if (!Array.isArray(rule.allowedMethods) || rule.allowedMethods.length === 0) {
244
+ throw _err("INVALID_CORS_RULE",
245
+ "rules[" + i + "].allowedMethods must be a non-empty array", true);
246
+ }
247
+ for (var m = 0; m < rule.allowedMethods.length; m++) {
248
+ if (ALLOWED_CORS_METHODS.indexOf(rule.allowedMethods[m]) === -1) {
249
+ throw _err("INVALID_CORS_RULE",
250
+ "rules[" + i + "].allowedMethods[" + m + "] must be one of: " +
251
+ ALLOWED_CORS_METHODS.join(", "), true);
252
+ }
253
+ }
254
+ body += "<CORSRule>";
255
+ if (rule.id !== undefined) body += "<ID>" + _xmlEscape(rule.id) + "</ID>";
256
+ rule.allowedOrigins.forEach(function (o) {
257
+ body += "<AllowedOrigin>" + _xmlEscape(o) + "</AllowedOrigin>";
258
+ });
259
+ rule.allowedMethods.forEach(function (m) {
260
+ body += "<AllowedMethod>" + _xmlEscape(m) + "</AllowedMethod>";
261
+ });
262
+ if (Array.isArray(rule.allowedHeaders)) {
263
+ rule.allowedHeaders.forEach(function (h) {
264
+ body += "<AllowedHeader>" + _xmlEscape(h) + "</AllowedHeader>";
265
+ });
266
+ }
267
+ if (Array.isArray(rule.exposeHeaders)) {
268
+ rule.exposeHeaders.forEach(function (h) {
269
+ body += "<ExposeHeader>" + _xmlEscape(h) + "</ExposeHeader>";
270
+ });
271
+ }
272
+ if (rule.maxAgeSeconds !== undefined) {
273
+ if (typeof rule.maxAgeSeconds !== "number" || rule.maxAgeSeconds < 0) {
274
+ throw _err("INVALID_CORS_RULE",
275
+ "rules[" + i + "].maxAgeSeconds must be a non-negative number", true);
276
+ }
277
+ body += "<MaxAgeSeconds>" + rule.maxAgeSeconds + "</MaxAgeSeconds>";
278
+ }
279
+ body += "</CORSRule>";
280
+ }
281
+ body += "</CORSConfiguration>";
282
+ if (Buffer.byteLength(body, "utf8") > C.BYTES.kib(64)) {
283
+ throw _err("INVALID_CORS_RULE",
284
+ "CORS configuration exceeds 64 KB (S3 spec)", true);
285
+ }
286
+ return body;
287
+ }
288
+
289
+ // ---- Object Lock + Retention + LegalHold validators / XML ----
290
+
291
+ var OBJECT_LOCK_MODES = ["GOVERNANCE", "COMPLIANCE"];
292
+ var LEGAL_HOLD_STATES = ["ON", "OFF"];
293
+
294
+ function _validateObjectLockConfig(cfg) {
295
+ if (!cfg || typeof cfg !== "object") {
296
+ throw _err("INVALID_OBJECT_LOCK",
297
+ "setObjectLockConfiguration: opts must be an object " +
298
+ "with { mode, days|years }", true);
299
+ }
300
+ if (OBJECT_LOCK_MODES.indexOf(cfg.mode) === -1) {
301
+ throw _err("INVALID_OBJECT_LOCK",
302
+ "mode must be one of " + OBJECT_LOCK_MODES.join(", ") +
303
+ "; got " + JSON.stringify(cfg.mode), true);
304
+ }
305
+ var hasDays = cfg.days != null;
306
+ var hasYears = cfg.years != null;
307
+ if (hasDays && hasYears) {
308
+ throw _err("INVALID_OBJECT_LOCK",
309
+ "specify either days OR years, not both (S3 rule)", true);
310
+ }
311
+ if (!hasDays && !hasYears) {
312
+ throw _err("INVALID_OBJECT_LOCK",
313
+ "default retention requires days or years", true);
314
+ }
315
+ if (hasDays) {
316
+ if (typeof cfg.days !== "number" || !Number.isInteger(cfg.days) ||
317
+ cfg.days <= 0) {
318
+ throw _err("INVALID_OBJECT_LOCK",
319
+ "days must be a positive integer; got " + JSON.stringify(cfg.days), true);
320
+ }
321
+ } else {
322
+ if (typeof cfg.years !== "number" || !Number.isInteger(cfg.years) ||
323
+ cfg.years <= 0) {
324
+ throw _err("INVALID_OBJECT_LOCK",
325
+ "years must be a positive integer; got " + JSON.stringify(cfg.years), true);
326
+ }
327
+ }
328
+ }
329
+
330
+ function _buildObjectLockConfigXml(cfg) {
331
+ var body = '<?xml version="1.0" encoding="UTF-8"?>';
332
+ body += '<ObjectLockConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';
333
+ body += '<ObjectLockEnabled>Enabled</ObjectLockEnabled>';
334
+ body += '<Rule><DefaultRetention>';
335
+ body += '<Mode>' + cfg.mode + '</Mode>';
336
+ if (cfg.days != null) body += '<Days>' + cfg.days + '</Days>';
337
+ if (cfg.years != null) body += '<Years>' + cfg.years + '</Years>';
338
+ body += '</DefaultRetention></Rule>';
339
+ body += '</ObjectLockConfiguration>';
340
+ return body;
341
+ }
342
+
343
+ function _validateRetention(opts) {
344
+ if (!opts || typeof opts !== "object") {
345
+ throw _err("INVALID_RETENTION",
346
+ "setObjectRetention: opts must be an object " +
347
+ "with { mode, retainUntil }", true);
348
+ }
349
+ if (OBJECT_LOCK_MODES.indexOf(opts.mode) === -1) {
350
+ throw _err("INVALID_RETENTION",
351
+ "mode must be one of " + OBJECT_LOCK_MODES.join(", ") +
352
+ "; got " + JSON.stringify(opts.mode), true);
353
+ }
354
+ if (!(opts.retainUntil instanceof Date) || isNaN(opts.retainUntil.getTime())) {
355
+ throw _err("INVALID_RETENTION",
356
+ "retainUntil must be a valid Date instance", true);
357
+ }
358
+ if (opts.retainUntil.getTime() <= Date.now()) {
359
+ throw _err("INVALID_RETENTION",
360
+ "retainUntil must be in the future", true);
361
+ }
362
+ }
363
+
364
+ function _buildRetentionXml(opts) {
365
+ return '<?xml version="1.0" encoding="UTF-8"?>' +
366
+ '<Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
367
+ '<Mode>' + opts.mode + '</Mode>' +
368
+ '<RetainUntilDate>' + opts.retainUntil.toISOString() + '</RetainUntilDate>' +
369
+ '</Retention>';
370
+ }
371
+
372
+ function _validateLegalHoldStatus(status) {
373
+ if (LEGAL_HOLD_STATES.indexOf(status) === -1) {
374
+ throw _err("INVALID_LEGAL_HOLD",
375
+ "legal-hold status must be one of " + LEGAL_HOLD_STATES.join(", ") +
376
+ "; got " + JSON.stringify(status), true);
377
+ }
378
+ }
379
+
380
+ function _buildLegalHoldXml(status) {
381
+ return '<?xml version="1.0" encoding="UTF-8"?>' +
382
+ '<LegalHold xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
383
+ '<Status>' + status + '</Status>' +
384
+ '</LegalHold>';
385
+ }
386
+
387
+ // S3 + MinIO surface "this lock-related state was never set" via two
388
+ // distinct error codes (and HTTP statuses), depending on whether the
389
+ // query is at the bucket level or per-object: bucket-level returns 404 +
390
+ // `ObjectLockConfigurationNotFoundError`; per-object retention/legal-hold
391
+ // returns 4xx + `NoSuchObjectLockConfiguration`. Both translate to the
392
+ // same operator answer: "not set". This helper recognizes both so the
393
+ // `get*` methods can surface a clean default instead of throwing.
394
+ function _isLockNotConfigured(err) {
395
+ if (!err) return false;
396
+ if (err.statusCode !== 404 && err.statusCode !== 400) return false;
397
+ var msg = String(err.message || "");
398
+ return msg.indexOf("ObjectLockConfigurationNotFoundError") !== -1 ||
399
+ msg.indexOf("NoSuchObjectLockConfiguration") !== -1;
400
+ }
401
+
402
+ function _validateObjectKey(key) {
403
+ if (typeof key !== "string" || key.length === 0) {
404
+ throw _err("INVALID_KEY", "object key must be a non-empty string", true);
405
+ }
406
+ if (key.length > C.BYTES.kib(1)) {
407
+ throw _err("INVALID_KEY", "object key exceeds 1024 bytes (S3 limit)", true);
408
+ }
409
+ }
410
+
411
+ // ---- Public factory ----
412
+
413
+ function create(config) {
414
+ if (!config || typeof config !== "object") {
415
+ throw _err("INVALID_CONFIG", "bucketOps.create requires a config object", true);
416
+ }
417
+ validateOpts(config, [
418
+ "protocol", "region", "accessKeyId", "secretAccessKey", "sessionToken",
419
+ "endpoint", "pathStyle", "forcePathStyle",
420
+ "allowedProtocols", "allowInternal", "timeoutMs",
421
+ "ca",
422
+ "audit", "observability", "auditSuccess", "auditFailures",
423
+ ], "bucketOps");
424
+ if (config.protocol && config.protocol !== "sigv4") {
425
+ throw _err("INVALID_CONFIG",
426
+ "bucketOps currently only supports protocol 'sigv4'; got '" +
427
+ config.protocol + "'. GCS and Azure bucket lifecycle differs " +
428
+ "substantially per cloud and is operator-managed (Terraform / " +
429
+ "CDK / Pulumi).", true);
430
+ }
431
+ if (!config.region) throw _err("INVALID_CONFIG", "bucketOps: region is required", true);
432
+ if (!config.accessKeyId) throw _err("INVALID_CONFIG", "bucketOps: accessKeyId is required", true);
433
+ if (!config.secretAccessKey) throw _err("INVALID_CONFIG", "bucketOps: secretAccessKey is required", true);
434
+
435
+ var endpoint = config.endpoint || ("https://s3." + config.region + ".amazonaws.com");
436
+ if (endpoint.endsWith("/")) endpoint = endpoint.slice(0, -1);
437
+ var pathStyle = !!(config.pathStyle || config.forcePathStyle);
438
+ var allowedProtocols = config.allowedProtocols || safeUrl.ALLOW_HTTP_TLS;
439
+ var allowInternal = config.allowInternal != null ? config.allowInternal : null;
440
+ safeUrl.parse(endpoint, {
441
+ allowedProtocols: allowedProtocols,
442
+ errorClass: ObjectStoreError,
443
+ });
444
+ var reqOpts = { timeoutMs: config.timeoutMs, allowedProtocols: allowedProtocols };
445
+ if (allowInternal !== null) reqOpts.allowInternal = allowInternal;
446
+
447
+ // Audit + observability are framework-best-practice — wired-on by
448
+ // default (no operator action needed beyond passing `audit: b.audit`),
449
+ // failure-audit always on, success-audit on by default for compliance
450
+ // workloads (SEC 17a-4 / FINRA / HIPAA require a trail of
451
+ // who-changed-the-retention-policy-and-when). Operators with extreme
452
+ // call rates can opt out of success-audit via auditSuccess: false;
453
+ // failures still audit so a forensic reconstruction of "what
454
+ // happened" is always possible.
455
+ var audit = config.audit || null;
456
+ var observability = config.observability || null;
457
+ var auditSuccess = config.auditSuccess !== false;
458
+ var auditFailures = config.auditFailures !== false;
459
+
460
+ var _emit = validateOpts.makeAuditEmitter(audit);
461
+
462
+ function _emitEvent(name, value, labels) {
463
+ if (observability) observability.safeEvent(name, value, labels || {});
464
+ }
465
+
466
+ function _actor(callerOpts) {
467
+ return requestHelpers.resolveActorWithOverride(callerOpts || {}, null);
468
+ }
469
+
470
+ // S3 subresource queries (`?lifecycle`, `?cors`, `?object-lock`,
471
+ // `?retention`, `?legal-hold`) are *bare* tokens on the wire — the
472
+ // trailing `=` produced by `URLSearchParams.set(k, "")` is interpreted
473
+ // by some S3 implementations (MinIO in particular) as "this is a
474
+ // body-write with a query parameter, not a subresource", which routes
475
+ // the request to the wrong handler. The SigV4 canonicalizer always
476
+ // reads `key=value` (with the empty `=`) per AWS spec, so the signature
477
+ // computation is unaffected; only the wire form differs. To preserve
478
+ // both behaviors, we build the URL string manually and pass it to the
479
+ // URL constructor — the constructor preserves the bare token in
480
+ // `url.search` while `url.searchParams` still presents it as `key=`.
481
+ function _appendQuery(base, query) {
482
+ if (!query) return base;
483
+ var keys = Object.keys(query);
484
+ if (keys.length === 0) return base;
485
+ var parts = keys.map(function (k) {
486
+ var v = query[k];
487
+ if (v === "" || v == null) return encodeURIComponent(k);
488
+ return encodeURIComponent(k) + "=" + encodeURIComponent(v);
489
+ });
490
+ return base + "?" + parts.join("&");
491
+ }
492
+
493
+ function _bucketUrl(name, query) {
494
+ var ub = _internalUrl(endpoint, allowedProtocols);
495
+ if (pathStyle) {
496
+ ub.pathname = "/" + name + "/";
497
+ } else {
498
+ ub.hostname = name + "." + ub.hostname;
499
+ ub.pathname = "/";
500
+ }
501
+ var base = ub.toString();
502
+ return _internalUrl(_appendQuery(base, query), allowedProtocols);
503
+ }
504
+
505
+ function _objectUrl(name, key, query) {
506
+ // Each key segment is encoded individually so that legitimate "/"
507
+ // separators in the key are preserved (S3 treats keys with slashes
508
+ // as flat names, not directories).
509
+ var encKey = key.split("/").map(encodeURIComponent).join("/");
510
+ var uo = _internalUrl(endpoint, allowedProtocols);
511
+ if (pathStyle) {
512
+ uo.pathname = "/" + name + "/" + encKey;
513
+ } else {
514
+ uo.hostname = name + "." + uo.hostname;
515
+ uo.pathname = "/" + encKey;
516
+ }
517
+ var base = uo.toString();
518
+ return _internalUrl(_appendQuery(base, query), allowedProtocols);
519
+ }
520
+
521
+ function _serviceUrl(query) {
522
+ // ListBuckets — service-level, no bucket prefix.
523
+ var u = _internalUrl(endpoint, allowedProtocols);
524
+ u.pathname = "/";
525
+ if (query) {
526
+ Object.keys(query).forEach(function (k) { u.searchParams.set(k, query[k]); });
527
+ }
528
+ return u;
529
+ }
530
+
531
+ function _signed(method, url, payloadHash, extraHeaders) {
532
+ var signed = sigv4.signRequest({
533
+ method: method,
534
+ url: url,
535
+ headers: extraHeaders || {},
536
+ payloadHash: payloadHash,
537
+ region: config.region,
538
+ accessKeyId: config.accessKeyId,
539
+ secretAccessKey: config.secretAccessKey,
540
+ sessionToken: config.sessionToken,
541
+ });
542
+ return signed.headers;
543
+ }
544
+
545
+ function _request(method, url, headers, body) {
546
+ return sharedRequest(method, url, headers, body, reqOpts);
547
+ }
548
+
549
+ // ---- create ----
550
+
551
+ function createBucket(name, opts) {
552
+ _validateBucketName(name);
553
+ opts = opts || {};
554
+ validateOpts(opts, ["region", "objectLockEnabled", "req", "actor"],
555
+ "bucketOps.create");
556
+ var targetRegion = opts.region || config.region;
557
+ var url = _bucketUrl(name);
558
+ var bodyBuf;
559
+ var extra = {};
560
+ // us-east-1 takes an empty body. Other regions need
561
+ // CreateBucketConfiguration with a LocationConstraint.
562
+ if (targetRegion && targetRegion !== "us-east-1") {
563
+ bodyBuf = Buffer.from(
564
+ '<?xml version="1.0" encoding="UTF-8"?>' +
565
+ '<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">' +
566
+ '<LocationConstraint>' + _xmlEscape(targetRegion) + '</LocationConstraint>' +
567
+ '</CreateBucketConfiguration>',
568
+ "utf8"
569
+ );
570
+ extra["Content-Type"] = "application/xml";
571
+ extra["Content-Length"] = String(bodyBuf.length);
572
+ } else {
573
+ bodyBuf = Buffer.alloc(0);
574
+ extra["Content-Length"] = "0";
575
+ }
576
+ // Object Lock can ONLY be enabled at create time — flipping it on
577
+ // a live bucket isn\'t an S3 API. Setting the header on PutBucket
578
+ // turns on the underlying versioning + write-once-read-many
579
+ // semantics so subsequent setObjectLockConfiguration / Retention
580
+ // / LegalHold calls actually do something.
581
+ if (opts.objectLockEnabled === true) {
582
+ extra["x-amz-bucket-object-lock-enabled"] = "true";
583
+ }
584
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
585
+ var headers = _signed("PUT", url, payloadHash, extra);
586
+ return _request("PUT", url, headers, bodyBuf).then(
587
+ function () {
588
+ if (auditSuccess) {
589
+ _emit("objectstore.bucket.create", {
590
+ actor: _actor(opts),
591
+ resource: { kind: "bucket", id: name },
592
+ metadata: {
593
+ region: targetRegion,
594
+ objectLockEnabled: opts.objectLockEnabled === true,
595
+ },
596
+ });
597
+ }
598
+ _emitEvent("objectstore.bucket.create", 1,
599
+ { outcome: "success", region: targetRegion });
600
+ return { created: true, name: name, region: targetRegion };
601
+ },
602
+ function (e) {
603
+ // Map S3 conflict response codes into stable framework codes.
604
+ var mapped = e;
605
+ if (e.statusCode === 409 && /BucketAlreadyOwnedByYou/.test(e.message || "")) {
606
+ mapped = _err("BUCKET_ALREADY_OWNED",
607
+ "bucket '" + name + "' already exists and is owned by this account", true);
608
+ } else if (e.statusCode === 409) {
609
+ mapped = _err("BUCKET_NAME_TAKEN",
610
+ "bucket name '" + name + "' is taken in S3's global namespace", true);
611
+ }
612
+ if (auditFailures) {
613
+ _emit("objectstore.bucket.create", {
614
+ actor: _actor(opts),
615
+ resource: { kind: "bucket", id: name },
616
+ outcome: "failure",
617
+ reason: mapped.code || "error",
618
+ metadata: { region: targetRegion },
619
+ });
620
+ }
621
+ _emitEvent("objectstore.bucket.create", 1,
622
+ { outcome: "failure", reason: mapped.code || "error" });
623
+ throw mapped;
624
+ }
625
+ );
626
+ }
627
+
628
+ // ---- delete ----
629
+
630
+ function deleteBucket(name, opts) {
631
+ _validateBucketName(name);
632
+ opts = opts || {};
633
+ validateOpts(opts, ["req", "actor"], "bucketOps.delete");
634
+ var url = _bucketUrl(name);
635
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
636
+ var headers = _signed("DELETE", url, payloadHash);
637
+ return _request("DELETE", url, headers, null).then(
638
+ function () {
639
+ if (auditSuccess) {
640
+ _emit("objectstore.bucket.delete", {
641
+ actor: _actor(opts),
642
+ resource: { kind: "bucket", id: name },
643
+ metadata: { existed: true },
644
+ });
645
+ }
646
+ _emitEvent("objectstore.bucket.delete", 1,
647
+ { outcome: "success", existed: "true" });
648
+ return true;
649
+ },
650
+ function (e) {
651
+ if (e.statusCode === 404) {
652
+ // Idempotent: missing bucket → false. Audit as success-with-noop
653
+ // so the trail still records "operator attempted delete".
654
+ if (auditSuccess) {
655
+ _emit("objectstore.bucket.delete", {
656
+ actor: _actor(opts),
657
+ resource: { kind: "bucket", id: name },
658
+ metadata: { existed: false },
659
+ });
660
+ }
661
+ _emitEvent("objectstore.bucket.delete", 1,
662
+ { outcome: "success", existed: "false" });
663
+ return false;
664
+ }
665
+ var mapped = e;
666
+ if (e.statusCode === 409 && /BucketNotEmpty/.test(e.message || "")) {
667
+ mapped = _err("BUCKET_NOT_EMPTY",
668
+ "bucket '" + name + "' is not empty; delete all objects + " +
669
+ "noncurrent versions + delete-markers first", true);
670
+ }
671
+ if (auditFailures) {
672
+ _emit("objectstore.bucket.delete", {
673
+ actor: _actor(opts),
674
+ resource: { kind: "bucket", id: name },
675
+ outcome: "failure",
676
+ reason: mapped.code || "error",
677
+ });
678
+ }
679
+ _emitEvent("objectstore.bucket.delete", 1,
680
+ { outcome: "failure", reason: mapped.code || "error" });
681
+ throw mapped;
682
+ }
683
+ );
684
+ }
685
+
686
+ // ---- list ----
687
+
688
+ function listBuckets() {
689
+ var url = _serviceUrl();
690
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
691
+ var headers = _signed("GET", url, payloadHash);
692
+ return _request("GET", url, headers, null).then(function (res) {
693
+ var doc = safeXml.parse(res.body);
694
+ var result = doc.ListAllMyBucketsResult || {};
695
+ var bucketsContainer = result.Buckets || {};
696
+ var raw = bucketsContainer.Bucket;
697
+ var arr;
698
+ if (!raw) arr = [];
699
+ else if (Array.isArray(raw)) arr = raw;
700
+ else arr = [raw];
701
+ _emitEvent("objectstore.bucket.list", arr.length, { outcome: "success" });
702
+ return arr.map(function (b) {
703
+ return {
704
+ name: b.Name,
705
+ creationDate: b.CreationDate ? Date.parse(b.CreationDate) : null,
706
+ region: b.BucketRegion || null,
707
+ };
708
+ });
709
+ });
710
+ }
711
+
712
+ // ---- setLifecycle ----
713
+
714
+ function setLifecycle(name, rules, opts) {
715
+ _validateBucketName(name);
716
+ opts = opts || {};
717
+ validateOpts(opts, ["req", "actor"], "bucketOps.setLifecycle");
718
+ var bodyXml = _buildLifecycleXml(rules);
719
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
720
+ var url = _bucketUrl(name, { lifecycle: "" });
721
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
722
+ var headers = _signed("PUT", url, payloadHash, {
723
+ "Content-Type": "application/xml",
724
+ "Content-Length": String(bodyBuf.length),
725
+ "Content-MD5": _md5Base64(bodyBuf),
726
+ });
727
+ return _request("PUT", url, headers, bodyBuf).then(
728
+ function () {
729
+ if (auditSuccess) {
730
+ _emit("objectstore.bucket.setLifecycle", {
731
+ actor: _actor(opts),
732
+ resource: { kind: "bucket", id: name },
733
+ metadata: { ruleCount: rules.length },
734
+ });
735
+ }
736
+ _emitEvent("objectstore.bucket.setLifecycle", 1,
737
+ { outcome: "success", ruleCount: String(rules.length) });
738
+ return { applied: true, name: name, ruleCount: rules.length };
739
+ },
740
+ function (e) {
741
+ if (auditFailures) {
742
+ _emit("objectstore.bucket.setLifecycle", {
743
+ actor: _actor(opts),
744
+ resource: { kind: "bucket", id: name },
745
+ outcome: "failure",
746
+ reason: e.code || "error",
747
+ });
748
+ }
749
+ _emitEvent("objectstore.bucket.setLifecycle", 1,
750
+ { outcome: "failure", reason: e.code || "error" });
751
+ throw e;
752
+ }
753
+ );
754
+ }
755
+
756
+ // ---- setCorsRules ----
757
+
758
+ function setCorsRules(name, rules, opts) {
759
+ _validateBucketName(name);
760
+ opts = opts || {};
761
+ validateOpts(opts, ["req", "actor"], "bucketOps.setCorsRules");
762
+ var bodyXml = _buildCorsXml(rules);
763
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
764
+ var url = _bucketUrl(name, { cors: "" });
765
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
766
+ var headers = _signed("PUT", url, payloadHash, {
767
+ "Content-Type": "application/xml",
768
+ "Content-Length": String(bodyBuf.length),
769
+ "Content-MD5": _md5Base64(bodyBuf),
770
+ });
771
+ return _request("PUT", url, headers, bodyBuf).then(
772
+ function () {
773
+ if (auditSuccess) {
774
+ _emit("objectstore.bucket.setCorsRules", {
775
+ actor: _actor(opts),
776
+ resource: { kind: "bucket", id: name },
777
+ metadata: { ruleCount: rules.length },
778
+ });
779
+ }
780
+ _emitEvent("objectstore.bucket.setCorsRules", 1,
781
+ { outcome: "success", ruleCount: String(rules.length) });
782
+ return { applied: true, name: name, ruleCount: rules.length };
783
+ },
784
+ function (e) {
785
+ if (auditFailures) {
786
+ _emit("objectstore.bucket.setCorsRules", {
787
+ actor: _actor(opts),
788
+ resource: { kind: "bucket", id: name },
789
+ outcome: "failure",
790
+ reason: e.code || "error",
791
+ });
792
+ }
793
+ _emitEvent("objectstore.bucket.setCorsRules", 1,
794
+ { outcome: "failure", reason: e.code || "error" });
795
+ throw e;
796
+ }
797
+ );
798
+ }
799
+
800
+ // ---- Object Lock configuration (bucket-level) ----
801
+
802
+ function setObjectLockConfiguration(name, opts) {
803
+ _validateBucketName(name);
804
+ _validateObjectLockConfig(opts);
805
+ validateOpts(opts, ["mode", "days", "years", "req", "actor"],
806
+ "bucketOps.setObjectLockConfiguration");
807
+ var bodyXml = _buildObjectLockConfigXml(opts);
808
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
809
+ var url = _bucketUrl(name, { "object-lock": "" });
810
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
811
+ var headers = _signed("PUT", url, payloadHash, {
812
+ "Content-Type": "application/xml",
813
+ "Content-Length": String(bodyBuf.length),
814
+ "Content-MD5": _md5Base64(bodyBuf),
815
+ });
816
+ return _request("PUT", url, headers, bodyBuf).then(
817
+ function () {
818
+ // Compliance-critical event — SEC 17a-4 / FINRA / HIPAA require
819
+ // a trail of who-changed-the-default-retention-policy-and-when.
820
+ if (auditSuccess) {
821
+ _emit("objectstore.bucket.setObjectLockConfiguration", {
822
+ actor: _actor(opts),
823
+ resource: { kind: "bucket", id: name },
824
+ metadata: {
825
+ mode: opts.mode,
826
+ days: opts.days != null ? opts.days : null,
827
+ years: opts.years != null ? opts.years : null,
828
+ },
829
+ });
830
+ }
831
+ _emitEvent("objectstore.bucket.setObjectLockConfiguration", 1,
832
+ { outcome: "success", mode: opts.mode });
833
+ return {
834
+ applied: true, name: name,
835
+ mode: opts.mode,
836
+ days: opts.days != null ? opts.days : null,
837
+ years: opts.years != null ? opts.years : null,
838
+ };
839
+ },
840
+ function (e) {
841
+ if (auditFailures) {
842
+ _emit("objectstore.bucket.setObjectLockConfiguration", {
843
+ actor: _actor(opts),
844
+ resource: { kind: "bucket", id: name },
845
+ outcome: "failure",
846
+ reason: e.code || "error",
847
+ metadata: { mode: opts.mode },
848
+ });
849
+ }
850
+ _emitEvent("objectstore.bucket.setObjectLockConfiguration", 1,
851
+ { outcome: "failure", reason: e.code || "error" });
852
+ throw e;
853
+ }
854
+ );
855
+ }
856
+
857
+ function getObjectLockConfiguration(name) {
858
+ _validateBucketName(name);
859
+ _emitEvent("objectstore.bucket.getObjectLockConfiguration", 1,
860
+ { outcome: "success" });
861
+ var url = _bucketUrl(name, { "object-lock": "" });
862
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
863
+ var headers = _signed("GET", url, payloadHash);
864
+ return _request("GET", url, headers, null).then(
865
+ function (res) {
866
+ var doc = safeXml.parse(res.body);
867
+ var olc = doc.ObjectLockConfiguration || {};
868
+ var enabled = olc.ObjectLockEnabled === "Enabled";
869
+ var rule = olc.Rule || {};
870
+ var def = rule.DefaultRetention || {};
871
+ return {
872
+ enabled: enabled,
873
+ mode: def.Mode || null,
874
+ days: def.Days != null ? Number(def.Days) : null,
875
+ years: def.Years != null ? Number(def.Years) : null,
876
+ };
877
+ },
878
+ function (e) {
879
+ // S3 + MinIO return 404 + ObjectLockConfigurationNotFoundError
880
+ // when the bucket was created without `objectLockEnabled: true`.
881
+ // That's a "not configured" state, not an error worth bubbling
882
+ // — operators asking "is this bucket lock-enabled?" want a
883
+ // clean false, not a try/catch.
884
+ if (_isLockNotConfigured(e)) {
885
+ return { enabled: false, mode: null, days: null, years: null };
886
+ }
887
+ throw e;
888
+ }
889
+ );
890
+ }
891
+
892
+ // ---- Per-object retention ----
893
+
894
+ function setObjectRetention(name, key, opts) {
895
+ _validateBucketName(name);
896
+ _validateObjectKey(key);
897
+ _validateRetention(opts);
898
+ validateOpts(opts, ["mode", "retainUntil", "bypassGovernance", "req", "actor"],
899
+ "bucketOps.setObjectRetention");
900
+ var bodyXml = _buildRetentionXml(opts);
901
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
902
+ var url = _objectUrl(name, key, { retention: "" });
903
+ var extra = {
904
+ "Content-Type": "application/xml",
905
+ "Content-Length": String(bodyBuf.length),
906
+ "Content-MD5": _md5Base64(bodyBuf),
907
+ };
908
+ if (opts.bypassGovernance === true) {
909
+ extra["x-amz-bypass-governance-retention"] = "true";
910
+ }
911
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
912
+ var headers = _signed("PUT", url, payloadHash, extra);
913
+ return _request("PUT", url, headers, bodyBuf).then(
914
+ function () {
915
+ // Compliance trail — bypassGovernance is the high-risk op
916
+ // (operator with s3:BypassGovernanceRetention shortened a
917
+ // GOVERNANCE retention) and operators wire alerting on this
918
+ // metadata field.
919
+ if (auditSuccess) {
920
+ _emit("objectstore.object.setRetention", {
921
+ actor: _actor(opts),
922
+ resource: { kind: "object", id: name + "/" + key },
923
+ metadata: {
924
+ bucket: name,
925
+ key: key,
926
+ mode: opts.mode,
927
+ retainUntilIso: opts.retainUntil.toISOString(),
928
+ bypassGovernance: opts.bypassGovernance === true,
929
+ },
930
+ });
931
+ }
932
+ _emitEvent("objectstore.object.setRetention", 1,
933
+ { outcome: "success", mode: opts.mode,
934
+ bypassGovernance: opts.bypassGovernance === true ? "true" : "false" });
935
+ return {
936
+ applied: true,
937
+ name: name,
938
+ key: key,
939
+ mode: opts.mode,
940
+ retainUntil: opts.retainUntil,
941
+ };
942
+ },
943
+ function (e) {
944
+ if (auditFailures) {
945
+ _emit("objectstore.object.setRetention", {
946
+ actor: _actor(opts),
947
+ resource: { kind: "object", id: name + "/" + key },
948
+ outcome: "failure",
949
+ reason: e.code || "error",
950
+ metadata: {
951
+ bucket: name,
952
+ key: key,
953
+ mode: opts.mode,
954
+ bypassGovernance: opts.bypassGovernance === true,
955
+ },
956
+ });
957
+ }
958
+ _emitEvent("objectstore.object.setRetention", 1,
959
+ { outcome: "failure", reason: e.code || "error" });
960
+ throw e;
961
+ }
962
+ );
963
+ }
964
+
965
+ function getObjectRetention(name, key) {
966
+ _validateBucketName(name);
967
+ _validateObjectKey(key);
968
+ _emitEvent("objectstore.object.getRetention", 1, { outcome: "success" });
969
+ var url = _objectUrl(name, key, { retention: "" });
970
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
971
+ var headers = _signed("GET", url, payloadHash);
972
+ return _request("GET", url, headers, null).then(
973
+ function (res) {
974
+ var doc = safeXml.parse(res.body);
975
+ var ret = doc.Retention || {};
976
+ var until = ret.RetainUntilDate ? new Date(ret.RetainUntilDate) : null;
977
+ return {
978
+ mode: ret.Mode || null,
979
+ retainUntil: until,
980
+ };
981
+ },
982
+ function (e) {
983
+ // S3 + MinIO return 4xx + NoSuchObjectLockConfiguration when the
984
+ // object has no per-object retention applied. Same UX choice as
985
+ // getObjectLockConfiguration: surface the not-set state cleanly
986
+ // rather than forcing operators to try/catch on a known-good
987
+ // request shape.
988
+ if (_isLockNotConfigured(e)) {
989
+ return { mode: null, retainUntil: null };
990
+ }
991
+ throw e;
992
+ }
993
+ );
994
+ }
995
+
996
+ // ---- Per-object legal hold ----
997
+
998
+ function setObjectLegalHold(name, key, status, opts) {
999
+ _validateBucketName(name);
1000
+ _validateObjectKey(key);
1001
+ _validateLegalHoldStatus(status);
1002
+ opts = opts || {};
1003
+ validateOpts(opts, ["req", "actor"], "bucketOps.setObjectLegalHold");
1004
+ var bodyXml = _buildLegalHoldXml(status);
1005
+ var bodyBuf = Buffer.from(bodyXml, "utf8");
1006
+ var url = _objectUrl(name, key, { "legal-hold": "" });
1007
+ var payloadHash = sigv4.sha256Hex(bodyBuf);
1008
+ var headers = _signed("PUT", url, payloadHash, {
1009
+ "Content-Type": "application/xml",
1010
+ "Content-Length": String(bodyBuf.length),
1011
+ "Content-MD5": _md5Base64(bodyBuf),
1012
+ });
1013
+ return _request("PUT", url, headers, bodyBuf).then(
1014
+ function () {
1015
+ if (auditSuccess) {
1016
+ _emit("objectstore.object.setLegalHold", {
1017
+ actor: _actor(opts),
1018
+ resource: { kind: "object", id: name + "/" + key },
1019
+ metadata: { bucket: name, key: key, status: status },
1020
+ });
1021
+ }
1022
+ _emitEvent("objectstore.object.setLegalHold", 1,
1023
+ { outcome: "success", status: status });
1024
+ return { applied: true, name: name, key: key, status: status };
1025
+ },
1026
+ function (e) {
1027
+ if (auditFailures) {
1028
+ _emit("objectstore.object.setLegalHold", {
1029
+ actor: _actor(opts),
1030
+ resource: { kind: "object", id: name + "/" + key },
1031
+ outcome: "failure",
1032
+ reason: e.code || "error",
1033
+ metadata: { bucket: name, key: key, status: status },
1034
+ });
1035
+ }
1036
+ _emitEvent("objectstore.object.setLegalHold", 1,
1037
+ { outcome: "failure", reason: e.code || "error" });
1038
+ throw e;
1039
+ }
1040
+ );
1041
+ }
1042
+
1043
+ function getObjectLegalHold(name, key) {
1044
+ _validateBucketName(name);
1045
+ _validateObjectKey(key);
1046
+ _emitEvent("objectstore.object.getLegalHold", 1, { outcome: "success" });
1047
+ var url = _objectUrl(name, key, { "legal-hold": "" });
1048
+ var payloadHash = sigv4.sha256Hex(Buffer.alloc(0));
1049
+ var headers = _signed("GET", url, payloadHash);
1050
+ return _request("GET", url, headers, null).then(
1051
+ function (res) {
1052
+ var doc = safeXml.parse(res.body);
1053
+ var lh = doc.LegalHold || {};
1054
+ return { status: lh.Status || null };
1055
+ },
1056
+ function (e) {
1057
+ // Object never had a legal hold set — S3 + MinIO surface this
1058
+ // as NoSuchObjectLockConfiguration. Return a clean OFF instead
1059
+ // of throwing; "no hold ever applied" is operationally
1060
+ // identical to "hold is OFF".
1061
+ if (_isLockNotConfigured(e)) {
1062
+ return { status: "OFF" };
1063
+ }
1064
+ throw e;
1065
+ }
1066
+ );
1067
+ }
1068
+
1069
+ return {
1070
+ protocol: "sigv4",
1071
+ create: createBucket,
1072
+ delete: deleteBucket,
1073
+ list: listBuckets,
1074
+ setLifecycle: setLifecycle,
1075
+ setCorsRules: setCorsRules,
1076
+ setObjectLockConfiguration: setObjectLockConfiguration,
1077
+ getObjectLockConfiguration: getObjectLockConfiguration,
1078
+ setObjectRetention: setObjectRetention,
1079
+ getObjectRetention: getObjectRetention,
1080
+ setObjectLegalHold: setObjectLegalHold,
1081
+ getObjectLegalHold: getObjectLegalHold,
1082
+ };
1083
+ }
1084
+
1085
+ module.exports = {
1086
+ create: create,
1087
+ // Test-only exports for unit-testing the XML builders without
1088
+ // standing up a fake S3 server.
1089
+ _buildLifecycleXmlForTest: _buildLifecycleXml,
1090
+ _buildCorsXmlForTest: _buildCorsXml,
1091
+ _validateBucketNameForTest: _validateBucketName,
1092
+ };