@aliyunrds/ctxdb-shared 0.0.8 → 0.0.9-beta.0
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/dist/index.js +22 -82
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1196,88 +1196,28 @@ import { randomBytes as randomBytes3 } from "crypto";
|
|
|
1196
1196
|
// src/credentials/local-credential-provider.ts
|
|
1197
1197
|
import { createHash as createHash2, randomBytes as randomBytes2 } from "crypto";
|
|
1198
1198
|
import { homedir as homedir2 } from "os";
|
|
1199
|
-
import { join as
|
|
1199
|
+
import { join as join2 } from "path";
|
|
1200
1200
|
|
|
1201
1201
|
// src/credentials/storage.ts
|
|
1202
1202
|
import { constants, closeSync as closeSync2, existsSync as existsSync2, fstatSync, fsyncSync, lstatSync, mkdirSync as mkdirSync2, openSync as openSync2, readFileSync as readFileSync2, renameSync as renameSync2, unlinkSync, writeFileSync } from "fs";
|
|
1203
1203
|
import { randomBytes, createHash } from "crypto";
|
|
1204
1204
|
import { dirname as dirname2 } from "path";
|
|
1205
1205
|
import { setTimeout as delay2 } from "timers/promises";
|
|
1206
|
-
|
|
1207
|
-
// src/windows-private-storage.ts
|
|
1208
|
-
import { execFileSync } from "child_process";
|
|
1209
|
-
import { join as join2 } from "path";
|
|
1210
|
-
var SCRIPT = `
|
|
1211
|
-
$ErrorActionPreference = 'Stop'
|
|
1212
|
-
$path = $env:CTXDB_PRIVATE_STORAGE_PATH
|
|
1213
|
-
$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().User
|
|
1214
|
-
$item = Get-Item -LiteralPath $path -Force
|
|
1215
|
-
if (($item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne 0) { throw 'reparse' }
|
|
1216
|
-
if ($env:CTXDB_PRIVATE_STORAGE_INITIALIZE -eq '1') {
|
|
1217
|
-
if (-not $item.PSIsContainer) { throw 'not directory' }
|
|
1218
|
-
$acl = [System.Security.AccessControl.DirectorySecurity]::new()
|
|
1219
|
-
$acl.SetOwner($sid)
|
|
1220
|
-
$acl.SetAccessRuleProtection($true, $false)
|
|
1221
|
-
$rule = [System.Security.AccessControl.FileSystemAccessRule]::new($sid, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
|
|
1222
|
-
$acl.AddAccessRule($rule)
|
|
1223
|
-
Set-Acl -LiteralPath $path -AclObject $acl
|
|
1224
|
-
}
|
|
1225
|
-
$acl = Get-Acl -LiteralPath $path
|
|
1226
|
-
if ($acl.GetOwner([System.Security.Principal.SecurityIdentifier]).Value -ne $sid.Value) { throw 'owner' }
|
|
1227
|
-
$self = $false
|
|
1228
|
-
foreach ($rule in $acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])) {
|
|
1229
|
-
if ($rule.AccessControlType -eq 'Allow') {
|
|
1230
|
-
if ($rule.IdentityReference.Value -ne $sid.Value) { throw 'other principal' }
|
|
1231
|
-
if (($rule.FileSystemRights -band [System.Security.AccessControl.FileSystemRights]::FullControl) -eq [System.Security.AccessControl.FileSystemRights]::FullControl) { $self = $true }
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
if (-not $self) { throw 'missing owner access' }
|
|
1235
|
-
Write-Output 'private'
|
|
1236
|
-
`;
|
|
1237
|
-
function checkWindowsPrivateStorage(path, initialize, run = execFileSync) {
|
|
1238
|
-
const systemRoot = process.env.SystemRoot;
|
|
1239
|
-
if (!systemRoot) throw new Error("Private Windows credential storage could not be verified.");
|
|
1240
|
-
try {
|
|
1241
|
-
const result = run(
|
|
1242
|
-
join2(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"),
|
|
1243
|
-
["-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(SCRIPT, "utf16le").toString("base64")],
|
|
1244
|
-
{
|
|
1245
|
-
env: { ...process.env, CTXDB_PRIVATE_STORAGE_PATH: path, CTXDB_PRIVATE_STORAGE_INITIALIZE: initialize ? "1" : "0" },
|
|
1246
|
-
encoding: "utf8",
|
|
1247
|
-
windowsHide: true,
|
|
1248
|
-
timeout: 1e4,
|
|
1249
|
-
maxBuffer: 4096,
|
|
1250
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
1251
|
-
}
|
|
1252
|
-
);
|
|
1253
|
-
if (String(result).trim() !== "private") throw new Error();
|
|
1254
|
-
} catch {
|
|
1255
|
-
throw new Error("Private Windows credential storage could not be verified.");
|
|
1256
|
-
}
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
// src/credentials/storage.ts
|
|
1260
|
-
function privatePath(path, directory = false) {
|
|
1206
|
+
function assertStoragePath(path, directory = false) {
|
|
1261
1207
|
const s = lstatSync(path);
|
|
1262
|
-
if (s.isSymbolicLink() || (directory ? !s.isDirectory() : !s.isFile())) throw new Error("credentials: storage
|
|
1263
|
-
if (process.platform === "win32") {
|
|
1264
|
-
checkWindowsPrivateStorage(path, false);
|
|
1265
|
-
return;
|
|
1266
|
-
}
|
|
1267
|
-
if (s.mode & 63 || process.getuid && s.uid !== process.getuid()) throw new Error("credentials: storage must be private and owner-only (run chmod 600 on credentials.json)");
|
|
1208
|
+
if (s.isSymbolicLink() || (directory ? !s.isDirectory() : !s.isFile())) throw new Error("credentials: invalid storage path");
|
|
1268
1209
|
}
|
|
1269
1210
|
function prepareStorage(path) {
|
|
1270
1211
|
const dir = dirname2(path);
|
|
1271
1212
|
if (!existsSync2(dir)) {
|
|
1272
1213
|
mkdirSync2(dir, { recursive: true, mode: 448 });
|
|
1273
|
-
if (process.platform === "win32") checkWindowsPrivateStorage(dir, true);
|
|
1274
1214
|
}
|
|
1275
|
-
|
|
1215
|
+
assertStoragePath(dir, true);
|
|
1276
1216
|
}
|
|
1277
|
-
function
|
|
1217
|
+
function readCredentialFile(path) {
|
|
1278
1218
|
if (!existsSync2(path)) return void 0;
|
|
1279
|
-
|
|
1280
|
-
|
|
1219
|
+
assertStoragePath(dirname2(path), true);
|
|
1220
|
+
assertStoragePath(path);
|
|
1281
1221
|
const fd = openSync2(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
1282
1222
|
try {
|
|
1283
1223
|
const stat = fstatSync(fd);
|
|
@@ -1289,7 +1229,7 @@ function readPrivate(path) {
|
|
|
1289
1229
|
}
|
|
1290
1230
|
function atomicCredentialWrite(path, value) {
|
|
1291
1231
|
prepareStorage(path);
|
|
1292
|
-
if (existsSync2(path))
|
|
1232
|
+
if (existsSync2(path)) assertStoragePath(path);
|
|
1293
1233
|
const temp = path + "." + randomBytes(12).toString("hex") + ".tmp";
|
|
1294
1234
|
let fd;
|
|
1295
1235
|
try {
|
|
@@ -1371,7 +1311,7 @@ var ACTIVE_CONTEXTDB_CREDENTIAL = "contextdb/active";
|
|
|
1371
1311
|
|
|
1372
1312
|
// src/credentials/local-credential-provider.ts
|
|
1373
1313
|
function defaultCredentialsPath() {
|
|
1374
|
-
return
|
|
1314
|
+
return join2(homedir2(), ".ctxdb", "credentials.json");
|
|
1375
1315
|
}
|
|
1376
1316
|
var object2 = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
1377
1317
|
var fail = (message) => {
|
|
@@ -1407,7 +1347,7 @@ function validate(key, raw) {
|
|
|
1407
1347
|
if (raw.kind === "api-key") validateApi(raw, key === ACTIVE_CONTEXTDB_CREDENTIAL);
|
|
1408
1348
|
}
|
|
1409
1349
|
function readDocument(path) {
|
|
1410
|
-
const bytes =
|
|
1350
|
+
const bytes = readCredentialFile(path);
|
|
1411
1351
|
if (bytes === void 0) return { version: 2, records: {} };
|
|
1412
1352
|
let doc;
|
|
1413
1353
|
try {
|
|
@@ -1592,7 +1532,7 @@ var OAuthCredentialError = class extends Error {
|
|
|
1592
1532
|
login_required: "OAuth session needs login; run ctxdb login for this profile.",
|
|
1593
1533
|
credential_busy: "OAuth credential is busy. If its previous process crashed, run ctxdb login for a new credential.",
|
|
1594
1534
|
invalid_profile: "OAuth credential does not belong to this profile.",
|
|
1595
|
-
unsafe_storage: "OAuth credential storage is
|
|
1535
|
+
unsafe_storage: "OAuth credential storage is inaccessible or invalid."
|
|
1596
1536
|
};
|
|
1597
1537
|
super(messages[code]);
|
|
1598
1538
|
this.name = "OAuthCredentialError";
|
|
@@ -1841,7 +1781,7 @@ function authorization(session) {
|
|
|
1841
1781
|
}
|
|
1842
1782
|
|
|
1843
1783
|
// src/oauth-profile.ts
|
|
1844
|
-
import { dirname as dirname4, join as
|
|
1784
|
+
import { dirname as dirname4, join as join3, resolve } from "path";
|
|
1845
1785
|
|
|
1846
1786
|
// src/auth-config.ts
|
|
1847
1787
|
import { closeSync as closeSync3, existsSync as existsSync3, fsyncSync as fsyncSync2, mkdirSync as mkdirSync3, openSync as openSync3, readFileSync as readFileSync3, renameSync as renameSync3, unlinkSync as unlinkSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
@@ -1993,7 +1933,7 @@ function oauthProfile(reference, configPath, profile, baseUrl) {
|
|
|
1993
1933
|
reference,
|
|
1994
1934
|
profile,
|
|
1995
1935
|
configPath: path,
|
|
1996
|
-
credentialsPath:
|
|
1936
|
+
credentialsPath: join3(dirname4(path), "credentials.json"),
|
|
1997
1937
|
baseUrl: normalizeCoreOrigin(baseUrl)
|
|
1998
1938
|
};
|
|
1999
1939
|
}
|
|
@@ -2060,10 +2000,10 @@ var OAuthProfileProvider = class {
|
|
|
2060
2000
|
|
|
2061
2001
|
// src/login-bindings.ts
|
|
2062
2002
|
import { randomBytes as randomBytes5 } from "crypto";
|
|
2063
|
-
import { dirname as dirname6, join as
|
|
2003
|
+
import { dirname as dirname6, join as join5 } from "path";
|
|
2064
2004
|
|
|
2065
2005
|
// src/data-auth.ts
|
|
2066
|
-
import { dirname as dirname5, join as
|
|
2006
|
+
import { dirname as dirname5, join as join4 } from "path";
|
|
2067
2007
|
function selectDataApiCredential(cfg, env = process.env) {
|
|
2068
2008
|
const accessToken = env.CTXDB_ACCESS_TOKEN?.trim();
|
|
2069
2009
|
if (accessToken) return { type: "access-token", value: accessToken };
|
|
@@ -2081,7 +2021,7 @@ var DataApiAuthorizationProvider = class {
|
|
|
2081
2021
|
const c = this.context, raw = readAuthConfig(c.configPath), section = ownAuthProfile(raw, c.agent);
|
|
2082
2022
|
const fallback = c.inheritDefaultKey && c.agent !== "default" ? ownAuthProfile(raw, "default") : {};
|
|
2083
2023
|
if (c.managedCredentials) {
|
|
2084
|
-
const active = readActiveCredentialSync(c.credentialsPath ??
|
|
2024
|
+
const active = readActiveCredentialSync(c.credentialsPath ?? join4(dirname5(c.configPath), "credentials.json"));
|
|
2085
2025
|
if (active) {
|
|
2086
2026
|
const baseUrl2 = c.baseUrlOverride ?? active.metadata.baseUrl;
|
|
2087
2027
|
if (baseUrl2 !== c.baseUrl) throw new OAuthCredentialError("login_required");
|
|
@@ -2107,7 +2047,7 @@ function publicAuthenticationFromConfig(raw, path, agent, baseUrlOverride) {
|
|
|
2107
2047
|
const oauthCredential = oauthProfileFromConfig(raw, path, agent, baseUrlOverride);
|
|
2108
2048
|
return oauthCredential ? { baseUrl: oauthCredential.baseUrl, oauthCredential } : void 0;
|
|
2109
2049
|
}
|
|
2110
|
-
const r = new LocalCredentialProvider(
|
|
2050
|
+
const r = new LocalCredentialProvider(join4(dirname5(path), "credentials.json")).readStoredRecord(section.credential_ref);
|
|
2111
2051
|
if (!r || r.kind !== "api-key" || r.metadata.state !== "ready" || r.metadata.owner.distribution !== "public" || r.metadata.owner.agent !== agent || r.metadata.owner.loginId) {
|
|
2112
2052
|
throw new AuthConfigError("credential_ref is missing, invalid or belongs to another Agent. Use setup --api-key/--login to choose a valid credential.");
|
|
2113
2053
|
}
|
|
@@ -2287,7 +2227,7 @@ async function selectSetupAuthentication(path, agent, options, env = process.env
|
|
|
2287
2227
|
}
|
|
2288
2228
|
}
|
|
2289
2229
|
function saveSetupKey(path, agent, raw, apiKey, baseUrl) {
|
|
2290
|
-
const provider = new LocalCredentialProvider(
|
|
2230
|
+
const provider = new LocalCredentialProvider(join5(dirname6(path), "credentials.json"));
|
|
2291
2231
|
const previousRef = ownAuthProfile(raw, agent).credential_ref;
|
|
2292
2232
|
const previous = typeof previousRef === "string" && /^[a-f0-9]{32}$/.test(previousRef) ? provider.snapshot(previousRef) : void 0;
|
|
2293
2233
|
const expected = authSnapshot(raw, agent), ref = randomBytes5(16).toString("hex");
|
|
@@ -2339,7 +2279,7 @@ async function resetSetupBaseUrl(path, agent, env) {
|
|
|
2339
2279
|
if (baseUrl === DEFAULT_DATA_BASE_URL) return;
|
|
2340
2280
|
rejectAuthEnvironment(env);
|
|
2341
2281
|
if (stored?.apiKey) {
|
|
2342
|
-
const expected = authSnapshot(raw, agent), provider = new LocalCredentialProvider(
|
|
2282
|
+
const expected = authSnapshot(raw, agent), provider = new LocalCredentialProvider(join5(dirname6(path), "credentials.json"));
|
|
2343
2283
|
provider.transaction((tx) => withAuthConfigLock(path, () => {
|
|
2344
2284
|
const current = readAuthConfig(path);
|
|
2345
2285
|
assertAuthSnapshot(current, agent, expected);
|
|
@@ -2368,7 +2308,7 @@ function keyCandidate(agent, apiKey, baseUrl) {
|
|
|
2368
2308
|
return { kind: "api-key", payload: { api_key: apiKey }, metadata: { base_url: baseUrl, owner: { distribution: "public", agent }, state: "ready", authorization_method: "api-key" } };
|
|
2369
2309
|
}
|
|
2370
2310
|
function updateAuthentication(path, task) {
|
|
2371
|
-
return new LocalCredentialProvider(
|
|
2311
|
+
return new LocalCredentialProvider(join5(dirname6(path), "credentials.json")).transaction((tx) => {
|
|
2372
2312
|
const created = [];
|
|
2373
2313
|
try {
|
|
2374
2314
|
return updateAuthConfig(path, (raw) => {
|
|
@@ -2566,9 +2506,9 @@ var AuthorizationService = class {
|
|
|
2566
2506
|
};
|
|
2567
2507
|
|
|
2568
2508
|
// src/authorization/public-target.ts
|
|
2569
|
-
import { dirname as dirname7, join as
|
|
2509
|
+
import { dirname as dirname7, join as join6 } from "path";
|
|
2570
2510
|
function publicAuthorizationTarget(pending, revoke) {
|
|
2571
|
-
const path =
|
|
2511
|
+
const path = join6(dirname7(pending.path), "credentials.json"), store = new OAuthSessionStore(path), provider = new LocalCredentialProvider(path);
|
|
2572
2512
|
let reference, published = false;
|
|
2573
2513
|
return {
|
|
2574
2514
|
commit(candidate) {
|
package/package.json
CHANGED