dsh-config-manager 0.3.0 → 0.3.1
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.
- package/lib/client.js +528 -528
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/sync/s3/oss4-verify.js +21 -6
- package/lib/sync/s3/s3-providers.d.ts +13 -0
- package/lib/sync/s3/s3-providers.js +6 -0
- package/lib/sync/s3/s3-transport.d.ts +22 -0
- package/lib/sync/s3/s3-transport.js +89 -18
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/sync/s3/oss4-verify.ts +22 -6
- package/src/sync/s3/s3-conditional-write.test.ts +153 -3
- package/src/sync/s3/s3-providers.ts +20 -0
- package/src/sync/s3/s3-transport.oss4-server.test.ts +48 -0
- package/src/sync/s3/s3-transport.ts +98 -17
|
@@ -31,6 +31,16 @@ export const S3_INDEX_FILE = 'index.json';
|
|
|
31
31
|
export const S3_BLOBS_SEG = 'blobs';
|
|
32
32
|
export const S3_BLOBS_INDEX_FILE = 'blobs-index.json';
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* 条件写能力(决定「防两台设备同时写」怎么落地):
|
|
36
|
+
* - `etag`:AWS 风格 If-Match / If-None-Match(s3 / minio / cos / kodo);
|
|
37
|
+
* - `forbid-overwrite`:阿里云 OSS —— PutObject **不支持 If-\***(真机会回 400
|
|
38
|
+
* InvalidArgument「A header you provided implies functionality that is not implemented.」,
|
|
39
|
+
* 错误码 0017-00000245;issue #109 第二层),只有 `x-oss-forbid-overwrite: true`
|
|
40
|
+
* 「对象已存在即拒」这一种禁止覆盖语义。
|
|
41
|
+
*/
|
|
42
|
+
export type S3ConditionalWrite = 'etag' | 'forbid-overwrite';
|
|
43
|
+
|
|
34
44
|
export interface S3ProviderVariant {
|
|
35
45
|
id: S3CompatProvider;
|
|
36
46
|
/** 默认签名方言(可被 \`dialectId\` 覆盖) */
|
|
@@ -43,6 +53,8 @@ export interface S3ProviderVariant {
|
|
|
43
53
|
endpointTemplate?: string;
|
|
44
54
|
/** 默认寻址风格:true = path-style(<endpoint>/<bucket>/<key>),false = virtual-host(<bucket>.<endpoint>/<key>) */
|
|
45
55
|
defaultPathStyle: boolean;
|
|
56
|
+
/** 条件写能力(见 S3ConditionalWrite;与签名方言解耦,跟着服务商走而不是跟着 dialectId 走) */
|
|
57
|
+
conditionalWrite: S3ConditionalWrite;
|
|
46
58
|
/**
|
|
47
59
|
* 变体说明的**目录键**(自 t6 起字段是键而不是文案 —— 中文只存在于 `sync/s3/messages.ts` 的本地目录,
|
|
48
60
|
* 经 `s3Msg` / `composeMsg(host, s3Msg)` 解析成当前语言;本字段本身不渲染)。
|
|
@@ -58,25 +70,30 @@ export const S3_PROVIDER_VARIANTS: Record<S3CompatProvider, S3ProviderVariant> =
|
|
|
58
70
|
s3: {
|
|
59
71
|
id: 's3', dialectId: 'aws4', service: 's3', defaultRegion: 'us-east-1',
|
|
60
72
|
endpointTemplate: 'https://s3.{region}.amazonaws.com', defaultPathStyle: false,
|
|
73
|
+
conditionalWrite: 'etag',
|
|
61
74
|
note: 'sync.s3.providerNoteS3',
|
|
62
75
|
},
|
|
63
76
|
oss: {
|
|
64
77
|
id: 'oss', dialectId: 'oss4', service: 'oss',
|
|
65
78
|
endpointTemplate: 'https://oss-{region}.aliyuncs.com', defaultPathStyle: false,
|
|
79
|
+
conditionalWrite: 'forbid-overwrite',
|
|
66
80
|
note: 'sync.s3.providerNoteOss',
|
|
67
81
|
},
|
|
68
82
|
cos: {
|
|
69
83
|
id: 'cos', dialectId: 'aws4', service: 's3',
|
|
70
84
|
endpointTemplate: 'https://cos.{region}.myqcloud.com', defaultPathStyle: false,
|
|
85
|
+
conditionalWrite: 'etag',
|
|
71
86
|
note: 'sync.s3.providerNoteCos',
|
|
72
87
|
},
|
|
73
88
|
minio: {
|
|
74
89
|
id: 'minio', dialectId: 'aws4', service: 's3', defaultRegion: 'us-east-1', defaultPathStyle: true,
|
|
90
|
+
conditionalWrite: 'etag',
|
|
75
91
|
note: 'sync.s3.providerNoteMinio',
|
|
76
92
|
},
|
|
77
93
|
kodo: {
|
|
78
94
|
id: 'kodo', dialectId: 'aws4', service: 's3',
|
|
79
95
|
endpointTemplate: 'https://s3-{region}.qiniucs.com', defaultPathStyle: false,
|
|
96
|
+
conditionalWrite: 'etag',
|
|
80
97
|
note: 'sync.s3.providerNoteKodo',
|
|
81
98
|
},
|
|
82
99
|
};
|
|
@@ -110,6 +127,8 @@ export interface S3Target {
|
|
|
110
127
|
prefix: string;
|
|
111
128
|
pathStyle: boolean;
|
|
112
129
|
dialect: SigV4Dialect;
|
|
130
|
+
/** 条件写能力(来自变体表;dialectId 覆盖不影响它) */
|
|
131
|
+
conditionalWrite: S3ConditionalWrite;
|
|
113
132
|
}
|
|
114
133
|
|
|
115
134
|
export interface S3TargetInput {
|
|
@@ -176,6 +195,7 @@ export function resolveS3Target(input: S3TargetInput): S3TargetResult {
|
|
|
176
195
|
prefix,
|
|
177
196
|
pathStyle: input.pathStyle ?? variant.defaultPathStyle,
|
|
178
197
|
dialect: sigV4Dialect(input.dialectId ?? variant.dialectId),
|
|
198
|
+
conditionalWrite: variant.conditionalWrite,
|
|
179
199
|
},
|
|
180
200
|
};
|
|
181
201
|
}
|
|
@@ -115,3 +115,51 @@ test('OSS 服务端视角验签:Authorization 逐字形状(逗号后无空
|
|
|
115
115
|
}
|
|
116
116
|
assert.deepEqual(server.failed, []);
|
|
117
117
|
});
|
|
118
|
+
test('OSS 服务端视角:首推全程不发 If-*(真机会回 400 0017-00000245),改用 x-oss-forbid-overwrite', async () => {
|
|
119
|
+
const server = makeServer();
|
|
120
|
+
const seen: { method: string; url: string; headers: Record<string, string> }[] = [];
|
|
121
|
+
const wrapped: S3RequestFn = async (method, url, opts = {}) => {
|
|
122
|
+
seen.push({ method, url, headers: { ...(opts.headers ?? {}) } });
|
|
123
|
+
return await server.request(method, url, opts);
|
|
124
|
+
};
|
|
125
|
+
const transport = makeTransport(wrapped);
|
|
126
|
+
|
|
127
|
+
await transport.probe();
|
|
128
|
+
await transport.upload(sampleSnapshot());
|
|
129
|
+
|
|
130
|
+
for (const req of seen) {
|
|
131
|
+
assert.equal(req.headers['if-none-match'], undefined, '绝不发 If-None-Match:' + req.method + ' ' + req.url);
|
|
132
|
+
assert.equal(req.headers['if-match'], undefined, '绝不发 If-Match:' + req.method + ' ' + req.url);
|
|
133
|
+
}
|
|
134
|
+
const indexPut = seen.find((r) => r.method === 'PUT' && r.url.endsWith('/index.json'));
|
|
135
|
+
assert.ok(indexPut, '必须有一次索引 PUT');
|
|
136
|
+
assert.equal(indexPut.headers['x-oss-forbid-overwrite'], 'true', 'OSS 只能靠 forbid-overwrite 防并发首推');
|
|
137
|
+
assert.deepEqual(server.failed, [], '换头之后仍必须逐请求验签通过');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('OSS 服务端视角:验签器照真机拒 If-*(自证盲点回归)', async () => {
|
|
141
|
+
// 这条用例保护的**不是**我们的代码,而是测试替身本身:如果验签器又「自己实现了 if-none-match」,
|
|
142
|
+
// 那 §issue #109 的「14 个请求零失败」就会重演 —— 本地全绿、真机 400。所以这里刻意注入一个
|
|
143
|
+
// If-None-Match(该头不进 OSS4 签名,故签名依旧有效),断言服务端按真机语义拒绝。
|
|
144
|
+
const server = makeServer();
|
|
145
|
+
const wrapped: S3RequestFn = async (method, url, opts = {}) => {
|
|
146
|
+
if (method === 'PUT' && url.endsWith('/index.json')) {
|
|
147
|
+
return await server.request(method, url, { ...opts, headers: { ...(opts.headers ?? {}), 'if-none-match': '*' } });
|
|
148
|
+
}
|
|
149
|
+
return await server.request(method, url, opts);
|
|
150
|
+
};
|
|
151
|
+
const transport = makeTransport(wrapped);
|
|
152
|
+
|
|
153
|
+
await assert.rejects(
|
|
154
|
+
() => transport.upload(sampleSnapshot()),
|
|
155
|
+
(err: unknown) => {
|
|
156
|
+
const message = (err as Error).message;
|
|
157
|
+
assert.match(message, /implies functionality that is not implemented/, '真机报文原文');
|
|
158
|
+
assert.match(message, /If-None-Match/, 'OSS 会回显被拒的头名');
|
|
159
|
+
assert.match(message, /0017-00000245/, '错误码同现场反馈');
|
|
160
|
+
return true;
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
assert.deepEqual(server.failed, [], '签名本身仍然有效 —— 拒的是 API 语义,不是签名');
|
|
164
|
+
assert.ok(!server.objects.has('dsh-config-manager/index.json'), '被拒的索引 PUT 不得落对象');
|
|
165
|
+
});
|
|
@@ -47,7 +47,7 @@ import { composeMsg, s3Msg } from './messages.ts';
|
|
|
47
47
|
import {
|
|
48
48
|
blobIndexKey, blobKey, canonicalObjectPath, indexKey, objectUrl, resolveS3Target, snapshotKey,
|
|
49
49
|
} from './s3-providers.ts';
|
|
50
|
-
import type { S3Target } from './s3-providers.ts';
|
|
50
|
+
import type { S3ConditionalWrite, S3Target } from './s3-providers.ts';
|
|
51
51
|
import type { CloudConfigIssueCode, S3CompatProvider } from '../sync-config.ts';
|
|
52
52
|
import type { FilesSection, SectionData, SectionId } from '../../schema/types.ts';
|
|
53
53
|
|
|
@@ -64,16 +64,34 @@ const REDACTED = '[REDACTED]';
|
|
|
64
64
|
/**
|
|
65
65
|
* 条件写头(cloud-sync 借鉴项 ①):有 ETag → If-Match(远端必须仍是我读到的那一版);
|
|
66
66
|
* 索引本来就不存在 → If-None-Match: *(防两设备同时首推);服务端不给 ETag → 空(无条件写,不改既有行为)。
|
|
67
|
+
*
|
|
68
|
+
* **阿里云 OSS 没有 If-\***(issue #109 第二层):PutObject 收到 If-Match / If-None-Match 一律回
|
|
69
|
+
* 400 InvalidArgument「A header you provided implies functionality that is not implemented.」
|
|
70
|
+
* (错误码 0017-00000245,真机报文里的 `Header: If-None-Match` 即此)。它只提供
|
|
71
|
+
* `x-oss-forbid-overwrite: true`(对象已存在即 409 FileAlreadyExists)。于是按变体的
|
|
72
|
+
* `conditionalWrite` 能力位映射:
|
|
73
|
+
* - 首推(索引本来就不存在)→ 语义完全等价,改用 x-oss-forbid-overwrite;
|
|
74
|
+
* - 索引已存在(拿到了 ETag)→ 没有对应头,退回「写前 HEAD 复核 ETag」
|
|
75
|
+
* (`assertIndexEtagUnchanged`;非原子,尽力而为)。
|
|
67
76
|
*/
|
|
68
|
-
function conditionalHeaders(
|
|
69
|
-
|
|
70
|
-
|
|
77
|
+
function conditionalHeaders(
|
|
78
|
+
etag: string | undefined,
|
|
79
|
+
indexMissing: boolean,
|
|
80
|
+
mode: S3ConditionalWrite,
|
|
81
|
+
): Record<string, string> {
|
|
82
|
+
const guarded = mode === 'forbid-overwrite';
|
|
83
|
+
if (etag !== undefined) return guarded ? {} : { 'if-match': etag };
|
|
84
|
+
if (!indexMissing) return {};
|
|
85
|
+
return guarded ? { 'x-oss-forbid-overwrite': 'true' } : { 'if-none-match': '*' };
|
|
71
86
|
}
|
|
72
87
|
|
|
73
|
-
/**
|
|
74
|
-
|
|
88
|
+
/**
|
|
89
|
+
* 条件写失败判定:412 恒是;409 仅在**确实带了条件写头**时才算
|
|
90
|
+
* (无条件的 409 另有含义,不吞;条件写头的存在由调用方显式告知)。
|
|
91
|
+
*/
|
|
92
|
+
function isConcurrentWrite(status: number, guarded: boolean): boolean {
|
|
75
93
|
if (status === 412) return true;
|
|
76
|
-
return status === 409 &&
|
|
94
|
+
return status === 409 && guarded;
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
/** 请求选项:headers / body / 覆盖默认超时(ms;0 = 不超时) */
|
|
@@ -308,13 +326,15 @@ export class S3Transport implements SyncTransport {
|
|
|
308
326
|
const merged = idxBefore.filter((m) => m.id !== snapshot.id);
|
|
309
327
|
merged.push(meta);
|
|
310
328
|
merged.sort((a, b) => (a.createdAt < b.createdAt ? -1 : a.createdAt > b.createdAt ? 1 : 0));
|
|
311
|
-
const condHeaders = conditionalHeaders(idxEtag, indexMissing);
|
|
329
|
+
const condHeaders = conditionalHeaders(idxEtag, indexMissing, this.target.conditionalWrite);
|
|
330
|
+
await this.reassertIndexEtag(idxEtag, secret);
|
|
312
331
|
const putIdx = await this.send('PUT', indexKey(this.target.prefix), {
|
|
313
332
|
body: JSON.stringify(merged),
|
|
314
333
|
secret,
|
|
315
334
|
headers: condHeaders,
|
|
316
335
|
});
|
|
317
|
-
|
|
336
|
+
const idxFailure = await this.conditionalFailure('PUT', putIdx.url, putIdx.res, Object.keys(condHeaders).length > 0, secret);
|
|
337
|
+
if (idxFailure !== undefined) throw idxFailure;
|
|
318
338
|
if (!putIdx.res.ok) {
|
|
319
339
|
throw new S3TransportError(await this.failText('PUT', putIdx.url, putIdx.res, secret), classifyHttpStatus(putIdx.res.status));
|
|
320
340
|
}
|
|
@@ -361,13 +381,15 @@ export class S3Transport implements SyncTransport {
|
|
|
361
381
|
if (indexMissing) return;
|
|
362
382
|
const remaining = idx.filter((m) => m.id !== id);
|
|
363
383
|
if (remaining.length === idx.length && del.res.status === 404) return; // 对象与索引都没有 → 静默成功
|
|
364
|
-
const condHeaders = conditionalHeaders(idxEtag, false);
|
|
384
|
+
const condHeaders = conditionalHeaders(idxEtag, false, this.target.conditionalWrite);
|
|
385
|
+
await this.reassertIndexEtag(idxEtag, secret);
|
|
365
386
|
const putIdx = await this.send('PUT', indexKey(this.target.prefix), {
|
|
366
387
|
body: JSON.stringify(remaining),
|
|
367
388
|
secret,
|
|
368
389
|
headers: condHeaders,
|
|
369
390
|
});
|
|
370
|
-
|
|
391
|
+
const idxFailure = await this.conditionalFailure('PUT', putIdx.url, putIdx.res, Object.keys(condHeaders).length > 0, secret);
|
|
392
|
+
if (idxFailure !== undefined) throw idxFailure;
|
|
371
393
|
if (!putIdx.res.ok) {
|
|
372
394
|
throw new S3TransportError(await this.failText('PUT', putIdx.url, putIdx.res, secret), classifyHttpStatus(putIdx.res.status));
|
|
373
395
|
}
|
|
@@ -384,6 +406,58 @@ export class S3Transport implements SyncTransport {
|
|
|
384
406
|
);
|
|
385
407
|
}
|
|
386
408
|
|
|
409
|
+
/**
|
|
410
|
+
* OSS 方言的「ETag 条件写」替身(issue #109 第二层):PutObject 既无 If-Match 也无
|
|
411
|
+
* If-None-Match,`x-oss-forbid-overwrite` 只能表达「别覆盖已存在的对象」,表达不了
|
|
412
|
+
* 「远端必须还是我读到的那一版」。所以索引进已存在时改成**写前 HEAD 复核 ETag**:
|
|
413
|
+
* 变了 / 对象没了 → 按并发写中止(零写入)。
|
|
414
|
+
*
|
|
415
|
+
* **不是原子的**:复核与 PUT 之间仍有窗口(服务端不支持条件写就没有真正的解),
|
|
416
|
+
* 比无保护强、比 If-Match 弱 —— 如实登记在 docs/decisions/0003。
|
|
417
|
+
* 其它方言(etag)与「服务端不给 ETag」两种情况一律直接放行,不改既有行为。
|
|
418
|
+
*/
|
|
419
|
+
private async reassertIndexEtag(etag: string | undefined, secret: string): Promise<void> {
|
|
420
|
+
if (this.target.conditionalWrite !== 'forbid-overwrite' || etag === undefined) return;
|
|
421
|
+
const key = indexKey(this.target.prefix);
|
|
422
|
+
const url = objectUrl(this.target, key);
|
|
423
|
+
const head = await this.send('HEAD', key, { secret });
|
|
424
|
+
if (head.res.status === 404) throw this.concurrentWrite(url, 409);
|
|
425
|
+
if (!head.res.ok) {
|
|
426
|
+
throw new S3TransportError(
|
|
427
|
+
await this.failText('HEAD', head.url, head.res, secret),
|
|
428
|
+
classifyHttpStatus(head.res.status),
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
const current = head.res.headers?.['etag'];
|
|
432
|
+
if (typeof current === 'string' && current !== '' && current !== etag) {
|
|
433
|
+
throw this.concurrentWrite(url, 409);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* 条件写失败分类(issue #109 第二层复核):`isConcurrentWrite` 只看「状态码像不像」,
|
|
439
|
+
* 这里再问报文一句确认「对象确实已存在」。OSS 的 **409 不止 FileAlreadyExists** ——
|
|
440
|
+
* FileImmutable(Bucket 数据处于保护状态,删改都会被拒)同为 409,一律按并发写报
|
|
441
|
+
* 「别的设备改过索引」就是假话。报文读不出时仍按并发写处理(宁可让用户重跑一次,
|
|
442
|
+
* 不吞掉真冲突)。412 是 If-Match 语义,恒为并发写。
|
|
443
|
+
*/
|
|
444
|
+
private async conditionalFailure(
|
|
445
|
+
method: string,
|
|
446
|
+
url: string,
|
|
447
|
+
res: S3Response,
|
|
448
|
+
guarded: boolean,
|
|
449
|
+
secret: string,
|
|
450
|
+
): Promise<S3TransportError | undefined> {
|
|
451
|
+
if (!isConcurrentWrite(res.status, guarded)) return undefined;
|
|
452
|
+
if (res.status !== 409) return this.concurrentWrite(url, res.status);
|
|
453
|
+
// 只有 forbid-overwrite 方言的 409 有两种含义(FileAlreadyExists / FileImmutable);
|
|
454
|
+
// etag 方言的 409 由各家自定语义,报文不可当作判据 —— 沿用「带条件头的 409 = 并发写」。
|
|
455
|
+
if (this.target.conditionalWrite !== 'forbid-overwrite') return this.concurrentWrite(url, res.status);
|
|
456
|
+
const body = await this.errorBody(res);
|
|
457
|
+
if (body === '' || body.includes('FileAlreadyExists')) return this.concurrentWrite(url, res.status);
|
|
458
|
+
return new S3TransportError(this.failMessage(method, url, res.status, body, secret), classifyHttpStatus(res.status));
|
|
459
|
+
}
|
|
460
|
+
|
|
387
461
|
/**
|
|
388
462
|
* 连通性探针(cloud-sync 借鉴项 ②):HEAD 桶证明端点/凭据/桶可达,再读一次索引回报快照数。
|
|
389
463
|
* 重定向(301/307/308)时读 x-amz-bucket-region 直接把「region 该填什么」告诉用户 ——
|
|
@@ -486,22 +560,29 @@ export class S3Transport implements SyncTransport {
|
|
|
486
560
|
}
|
|
487
561
|
}
|
|
488
562
|
|
|
489
|
-
/**
|
|
490
|
-
private async
|
|
491
|
-
let body = '';
|
|
563
|
+
/** 读错误响应体(未脱敏原文,截断到 ERR_BODY_MAX;读不出按空串) */
|
|
564
|
+
private async errorBody(res: S3Response): Promise<string> {
|
|
492
565
|
try {
|
|
493
|
-
|
|
566
|
+
return (await res.text()).slice(0, ERR_BODY_MAX);
|
|
494
567
|
} catch {
|
|
495
|
-
|
|
568
|
+
return '';
|
|
496
569
|
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** 构造 HTTP 非 2xx 失败消息:附上脱敏后的响应体片段(截断) */
|
|
573
|
+
private failMessage(method: string, url: string, status: number, body: string, secret: string): string {
|
|
497
574
|
return this.o.msg('sync.s3.requestFailed', {
|
|
498
575
|
method,
|
|
499
576
|
url,
|
|
500
|
-
status: String(
|
|
577
|
+
status: String(status),
|
|
501
578
|
err: this.mask(body, secret),
|
|
502
579
|
});
|
|
503
580
|
}
|
|
504
581
|
|
|
582
|
+
private async failText(method: string, url: string, res: S3Response, secret: string): Promise<string> {
|
|
583
|
+
return this.failMessage(method, url, res.status, await this.errorBody(res), secret);
|
|
584
|
+
}
|
|
585
|
+
|
|
505
586
|
/** 识别超时错误(AbortError / TimeoutError) */
|
|
506
587
|
private isTimeout(err: unknown): boolean {
|
|
507
588
|
const name = (err as Error | undefined)?.name ?? '';
|