@blinkhost/cli 2.5.0 → 2.5.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/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  This file records customer-visible CLI changes. BlinkHost uses semantic versioning for the CLI, while security and migration impact are classified separately. A patch may therefore require prompt action, and a major release may coexist with an earlier supported line during its documented migration window.
4
4
 
5
+ ## 2.5.1 — Saved-session concurrency
6
+
7
+ Commands sharing a saved profile now serialize credential refresh and sign-out.
8
+ This prevents concurrent CLI processes from reusing a rotated refresh credential
9
+ and revoking the session. Sign-in verifies the new session using its original
10
+ access token, and temporary refresh errors preserve the saved credential.
11
+
12
+ If a command exits unexpectedly while holding the sign-in lock, the CLI reports
13
+ the lock path. Confirm that no command for that profile is running before
14
+ removing that file. Idam remains limited preview; workspace policy is unchanged.
15
+
5
16
  ## 2.5.0 — Idam limited preview
6
17
 
7
18
  This release adds Idam commands for eligible accounts on an enabled control plane.
package/README.md CHANGED
@@ -15,6 +15,16 @@ Signed release archives, checksums, the CycloneDX SBOM, and Sigstore verificatio
15
15
 
16
16
  ## Current release
17
17
 
18
+ ### 2.5.1 — saved-session concurrency
19
+
20
+ Commands sharing a saved profile serialize credential refresh and sign-out so
21
+ concurrent processes do not reuse a rotated credential. Temporary refresh
22
+ failures preserve the saved credential; rejected sessions still require a fresh
23
+ sign-in. Explicit environment credentials remain the caller's responsibility.
24
+ If a command reports `credential_busy`, let the other command finish. After a
25
+ crash, confirm that no command for that profile is running before removing the
26
+ reported lock file. The file contains process metadata, never credentials.
27
+
18
28
  ### 2.5.0 — Idam limited preview
19
29
 
20
30
  - Added Idam task, source-review, starter, build, export and connected-review commands.
package/dist/api.d.ts CHANGED
@@ -10,6 +10,10 @@ export declare class ApiClient {
10
10
  private accessToken;
11
11
  private constructor();
12
12
  static create(profileName?: string): Promise<ApiClient>;
13
+ private static refresh;
14
+ static logout(profileName?: string): Promise<{
15
+ profile: string;
16
+ }>;
13
17
  static fromAccessToken(profileName: string, profile: Profile, accessToken: string): ApiClient;
14
18
  projectArchive(projectId: string, mode?: 'portable' | 'source'): Promise<Buffer>;
15
19
  request(path: string, init?: RequestInit): Promise<unknown>;
package/dist/api.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { randomUUID } from 'node:crypto';
2
2
  import { activeProfile, writeConfig } from './config.js';
3
3
  import { deleteRefreshCredential, getRefreshCredential, setRefreshCredential } from './credentials.js';
4
+ import { withCredentialLock } from './credential-lock.js';
5
+ import { validateDeviceTokens } from './device-flow.js';
4
6
  import { CliError, EXIT } from './errors.js';
5
7
  import { VERSION } from './version.js';
6
8
  const REQUEST_TIMEOUT_MS = 30_000;
@@ -140,20 +142,51 @@ export class ApiClient {
140
142
  throw new CliError(payload.detail || 'BlinkHost rejected this workload identity.', EXIT.auth, 'ci_exchange_failed');
141
143
  return new ApiClient(selected.name, selected.profile, payload.access_token, true);
142
144
  }
145
+ if (process.env.BLINKHOST_REFRESH_TOKEN)
146
+ return ApiClient.refresh(selected);
147
+ return withCredentialLock(selected.name, () => ApiClient.refresh(selected));
148
+ }
149
+ // Caller holds the profile lock for saved credentials. Explicit environment
150
+ // credentials remain caller-managed and are never persisted or removed.
151
+ static async refresh(selected) {
143
152
  const refresh = process.env.BLINKHOST_REFRESH_TOKEN || await getRefreshCredential(selected.name);
144
153
  if (!refresh)
145
154
  throw new CliError('Sign in with `blinkhost auth login` or provide BLINKHOST_ACCESS_TOKEN for CI.', EXIT.auth, 'not_authenticated');
146
155
  const result = await publicRequest(selected.profile.apiOrigin, '/api/cli/v2/token/refresh/', { method: 'POST', body: JSON.stringify({ refresh_token: refresh, client_version: VERSION }) });
147
156
  if (!result.response.ok) {
148
- if (!process.env.BLINKHOST_REFRESH_TOKEN)
149
- await deleteRefreshCredential(selected.name);
150
- throw new CliError(messageFrom(result.data, 'Your CLI session has expired. Sign in again.'), EXIT.auth, 'session_expired');
157
+ if (result.response.status === 401) {
158
+ if (!process.env.BLINKHOST_REFRESH_TOKEN)
159
+ await deleteRefreshCredential(selected.name);
160
+ throw new CliError(messageFrom(result.data, 'Your CLI session has expired. Sign in again.'), EXIT.auth, 'session_expired');
161
+ }
162
+ throw new CliError('BlinkHost could not refresh this session. The saved credential was preserved.', EXIT.network, 'session_refresh_failed');
151
163
  }
152
- const tokens = result.data;
164
+ const tokens = validateDeviceTokens(result.data);
153
165
  if (!process.env.BLINKHOST_REFRESH_TOKEN)
154
166
  await setRefreshCredential(selected.name, tokens.refresh_token);
155
167
  return new ApiClient(selected.name, selected.profile, tokens.access_token, Boolean(process.env.BLINKHOST_REFRESH_TOKEN));
156
168
  }
169
+ static async logout(profileName) {
170
+ const selected = await activeProfile(profileName);
171
+ if (process.env.BLINKHOST_ACCESS_TOKEN || process.env.BLINKHOST_REFRESH_TOKEN
172
+ || (process.env.ACTIONS_ID_TOKEN_REQUEST_URL && process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN)) {
173
+ const client = await ApiClient.create(selected.name);
174
+ await client.request('/api/cli/v2/sessions/current/', { method: 'DELETE' });
175
+ return { profile: selected.name };
176
+ }
177
+ return withCredentialLock(selected.name, async () => {
178
+ try {
179
+ const client = await ApiClient.refresh(selected);
180
+ await client.request('/api/cli/v2/sessions/current/', { method: 'DELETE' });
181
+ }
182
+ catch (error) {
183
+ if (!(error instanceof CliError) || error.code !== 'session_expired')
184
+ throw error;
185
+ }
186
+ await deleteRefreshCredential(selected.name);
187
+ return { profile: selected.name };
188
+ });
189
+ }
157
190
  static fromAccessToken(profileName, profile, accessToken) {
158
191
  return new ApiClient(profileName, profile, accessToken, true);
159
192
  }
package/dist/auth.js CHANGED
@@ -2,7 +2,8 @@ import { createHash, randomBytes } from 'node:crypto';
2
2
  import { hostname } from 'node:os';
3
3
  import { spawn } from 'node:child_process';
4
4
  import { activeProfile, validateApiOrigin, validateProfileName, writeConfig } from './config.js';
5
- import { credentialStoreStatus, deleteRefreshCredential, setRefreshCredential } from './credentials.js';
5
+ import { credentialStoreStatus, setRefreshCredential } from './credentials.js';
6
+ import { withCredentialLock } from './credential-lock.js';
6
7
  import { ApiClient, publicRequest } from './api.js';
7
8
  import { CliError, EXIT } from './errors.js';
8
9
  import { VERSION } from './version.js';
@@ -43,26 +44,21 @@ export async function login(options) {
43
44
  if (options.openBrowser !== false)
44
45
  openBrowser(device.verification_uri_complete);
45
46
  const tokens = validateDeviceTokens(await pollDeviceAuthorization(apiOrigin, device, verifier));
46
- await setRefreshCredential(profileName, tokens.refresh_token);
47
- selected.config.profiles[profileName] = { apiOrigin };
48
- selected.config.activeProfile = profileName;
49
- await writeConfig(selected.config);
50
- const client = await ApiClient.create(profileName);
51
- const capabilities = await client.request('/api/cli/v2/capabilities/');
52
- await client.rememberIdentity(capabilities.actor || {});
53
- return { profile: profileName, api_origin: apiOrigin, account: capabilities.actor, permissions: capabilities.features };
47
+ return withCredentialLock(profileName, async () => {
48
+ await setRefreshCredential(profileName, tokens.refresh_token);
49
+ selected.config.profiles[profileName] = { apiOrigin };
50
+ selected.config.activeProfile = profileName;
51
+ await writeConfig(selected.config);
52
+ const client = ApiClient.fromAccessToken(profileName, { apiOrigin }, tokens.access_token);
53
+ const capabilities = await client.request('/api/cli/v2/capabilities/');
54
+ selected.config.profiles[profileName] = { apiOrigin,
55
+ ...(capabilities.actor?.id ? { userId: capabilities.actor.id } : {}),
56
+ ...(capabilities.actor?.username ? { username: capabilities.actor.username } : {}) };
57
+ await writeConfig(selected.config);
58
+ return { profile: profileName, api_origin: apiOrigin, account: capabilities.actor, permissions: capabilities.features };
59
+ });
54
60
  }
55
61
  export async function logout(profile) {
56
- const selected = await activeProfile(profile);
57
- try {
58
- const client = await ApiClient.create(selected.name);
59
- await client.request('/api/cli/v2/sessions/current/', { method: 'DELETE' });
60
- }
61
- catch (error) {
62
- if (!(error instanceof CliError) || error.code !== 'session_expired')
63
- throw error;
64
- }
65
- await deleteRefreshCredential(selected.name);
66
- return { profile: selected.name };
62
+ return ApiClient.logout(profile);
67
63
  }
68
64
  //# sourceMappingURL=auth.js.map
@@ -0,0 +1,3 @@
1
+ export declare function credentialLockPath(profile: string): string;
2
+ /** Serialize the complete read/rotate/save operation across CLI processes. */
3
+ export declare function withCredentialLock<T>(profile: string, operation: () => Promise<T>, timeoutMs?: number): Promise<T>;
@@ -0,0 +1,52 @@
1
+ import { mkdir, open, unlink } from 'node:fs/promises';
2
+ import { homedir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { setTimeout as delay } from 'node:timers/promises';
5
+ import { validateProfileName } from './config.js';
6
+ import { CliError, EXIT } from './errors.js';
7
+ // The OS credential store keys by profile, independently of CONFIG_HOME and
8
+ // API origin. Commands using different config files must therefore share locks.
9
+ export function credentialLockPath(profile) {
10
+ return join(homedir(), '.blinkhost', 'credential-locks', `${validateProfileName(profile)}.lock`);
11
+ }
12
+ /** Serialize the complete read/rotate/save operation across CLI processes. */
13
+ export async function withCredentialLock(profile, operation, timeoutMs = 45_000) {
14
+ const path = credentialLockPath(profile);
15
+ const directory = join(homedir(), '.blinkhost', 'credential-locks');
16
+ if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 45_000) {
17
+ throw new CliError('The CLI credential lock timeout is invalid.', EXIT.internal, 'credential_lock_invalid');
18
+ }
19
+ try {
20
+ await mkdir(directory, { recursive: true, mode: 0o700 });
21
+ }
22
+ catch {
23
+ throw new CliError('The CLI credential lock directory is unavailable.', EXIT.filesystem, 'credential_lock_failed');
24
+ }
25
+ const deadline = performance.now() + timeoutMs;
26
+ let handle;
27
+ while (!handle) {
28
+ try {
29
+ handle = await open(path, 'wx', 0o600);
30
+ }
31
+ catch (error) {
32
+ if (error.code !== 'EEXIST') {
33
+ throw new CliError('The CLI credential lock is unavailable.', EXIT.filesystem, 'credential_lock_failed');
34
+ }
35
+ if (performance.now() >= deadline) {
36
+ // Never steal an old lock based on time alone: a suspended process may
37
+ // still rotate the credential when it resumes. This file has no secrets.
38
+ throw new CliError('Another CLI command holds this profile’s sign-in lock. Let it finish and retry. If it exited unexpectedly, confirm no CLI command is running before removing the lock file.', EXIT.conflict, 'credential_busy', [path]);
39
+ }
40
+ await delay(Math.min(50, Math.max(1, deadline - performance.now())));
41
+ }
42
+ }
43
+ try {
44
+ await handle.writeFile(JSON.stringify({ pid: process.pid, started_at: new Date().toISOString() }) + '\n');
45
+ return await operation();
46
+ }
47
+ finally {
48
+ await handle.close();
49
+ await unlink(path);
50
+ }
51
+ }
52
+ //# sourceMappingURL=credential-lock.js.map
package/dist/version.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "2.5.0";
1
+ export declare const VERSION = "2.5.1";
2
2
  export declare const DOCUMENTATION_URL = "https://app.blinkhost.me/docs/source-control/cli";
3
3
  export declare const RELEASES_URL = "https://github.com/blinkhost-ltd/blinkhost-cli/releases";
4
4
  export declare function supportedNodeVersion(version?: string): boolean;
package/dist/version.js CHANGED
@@ -1,4 +1,4 @@
1
- export const VERSION = '2.5.0';
1
+ export const VERSION = '2.5.1';
2
2
  export const DOCUMENTATION_URL = 'https://app.blinkhost.me/docs/source-control/cli';
3
3
  export const RELEASES_URL = 'https://github.com/blinkhost-ltd/blinkhost-cli/releases';
4
4
  export function supportedNodeVersion(version = process.versions.node) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blinkhost/cli",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "Secure local and remote developer workflows for BlinkHost",
5
5
  "keywords": ["blinkhost", "deployment", "hosting", "cloud", "cli", "developer-tools"],
6
6
  "homepage": "https://app.blinkhost.me/docs/source-control/cli",