11// Docker and locking internals shared by the dev-server entry (index.js) and the detached updater.
22import { spawn } from 'node:child_process' ;
3+ import { access , constants , mkdtemp , rm , symlink } from 'node:fs/promises' ;
34import { createServer } from 'node:net' ;
5+ import { tmpdir } from 'node:os' ;
6+ import { delimiter , join } from 'node:path' ;
47
58export const SYNTAX_AUTH_LOCAL_PORT = 37960 ;
69export const SYNTAX_AUTH_LOCAL_ORIGIN = `http://localhost:${ SYNTAX_AUTH_LOCAL_PORT } ` ;
@@ -29,12 +32,15 @@ export function warn(message) {
2932/**
3033 * @param {string } command
3134 * @param {string[] } args
32- * @param {string } [ input] written to stdin
35+ * @param {{ input?: string, env?: NodeJS.ProcessEnv } } [options]
3336 * @returns {Promise<{ code: number | null, stdout: string, stderr: string }> }
3437 */
35- function run ( command , args , input ) {
38+ function run ( command , args , { input, env } = { } ) {
3639 return new Promise ( ( resolve ) => {
37- const child = spawn ( command , args , { stdio : [ 'pipe' , 'pipe' , 'pipe' ] } ) ;
40+ const child = spawn ( command , args , {
41+ env : env ?? process . env ,
42+ stdio : [ 'pipe' , 'pipe' , 'pipe' ]
43+ } ) ;
3844 let stdout = '' ;
3945 let stderr = '' ;
4046 child . stdout . on ( 'data' , ( chunk ) => ( stdout += chunk ) ) ;
@@ -54,31 +60,73 @@ function is_access_denied(stderr) {
5460 return / u n a u t h o r i z e d | d e n i e d | f o r b i d d e n / i. test ( stderr ) ;
5561}
5662
57- // The image is private to the Syntax team, so reuse the developer's GitHub CLI login for ghcr.io.
58- async function login_with_github_cli ( ) {
59- const token = await run ( 'gh' , [ 'auth' , 'token' , '--hostname' , 'github.com' ] ) ;
60- const user = await run ( 'gh' , [ 'api' , 'user' , '--jq' , '.login' ] ) ;
61- if ( token . code !== 0 || user . code !== 0 ) return false ;
62-
63- const login = await run (
64- 'docker' ,
65- [ 'login' , 'ghcr.io' , '--username' , user . stdout , '--password-stdin' ] ,
66- token . stdout
67- ) ;
68- return login . code === 0 ;
63+ async function find_docker_binary ( ) {
64+ for ( const directory of ( process . env . PATH ?? '' ) . split ( delimiter ) ) {
65+ const candidate = join ( directory , 'docker' ) ;
66+ try {
67+ await access ( candidate , constants . X_OK ) ;
68+ return candidate ;
69+ } catch {
70+ // Not in this PATH entry.
71+ }
72+ }
73+ return null ;
6974}
7075
71- /** @returns {Promise<{ error: string | null, is_denied: boolean }> } */
72- export async function pull_image ( ) {
73- let result = await docker ( [ 'pull' , '--quiet' , IMAGE ] ) ;
76+ // The image is private to the Syntax team. For a syntaxfm member, pull once with their GitHub CLI
77+ // token in a throwaway Docker config. PATH exposes only the docker binary, so Docker cannot fall
78+ // back to the OS keychain: the token is never saved, and any existing ghcr.io login is untouched.
79+ /** @returns {Promise<{ code: number | null, stdout: string, stderr: string } | null> } */
80+ async function pull_as_syntax_team_member ( ) {
81+ const membership = await run ( 'gh' , [ 'api' , 'user/memberships/orgs/syntaxfm' , '--jq' , '.state' ] ) ;
82+ if ( membership . code !== 0 || membership . stdout !== 'active' ) return null ;
83+
84+ const [ user , token , context , docker_binary ] = await Promise . all ( [
85+ run ( 'gh' , [ 'api' , 'user' , '--jq' , '.login' ] ) ,
86+ run ( 'gh' , [ 'auth' , 'token' , '--hostname' , 'github.com' ] ) ,
87+ docker ( [ 'context' , 'inspect' , '--format' , '{{.Endpoints.docker.Host}}' ] ) ,
88+ find_docker_binary ( )
89+ ] ) ;
90+ if ( user . code !== 0 || token . code !== 0 || context . code !== 0 || ! docker_binary ) return null ;
7491
75- if ( result . code !== 0 && is_access_denied ( result . stderr ) && ( await login_with_github_cli ( ) ) ) {
76- result = await docker ( [ 'pull' , '--quiet' , IMAGE ] ) ;
92+ const config_directory = await mkdtemp ( join ( tmpdir ( ) , 'syntax-auth-docker-config-' ) ) ;
93+ const binary_directory = await mkdtemp ( join ( tmpdir ( ) , 'syntax-auth-docker-bin-' ) ) ;
94+ try {
95+ await symlink ( docker_binary , join ( binary_directory , 'docker' ) ) ;
96+ const env = {
97+ ...process . env ,
98+ PATH : binary_directory ,
99+ DOCKER_CONFIG : config_directory ,
100+ DOCKER_HOST : context . stdout
101+ } ;
102+ const login = await run (
103+ 'docker' ,
104+ [ 'login' , 'ghcr.io' , '--username' , user . stdout , '--password-stdin' ] ,
105+ { input : token . stdout , env }
106+ ) ;
107+ if ( login . code !== 0 ) return login ;
108+ return await run ( 'docker' , [ 'pull' , '--quiet' , IMAGE ] , { env } ) ;
109+ } finally {
110+ await rm ( config_directory , { recursive : true , force : true } ) ;
111+ await rm ( binary_directory , { recursive : true , force : true } ) ;
77112 }
113+ }
114+
115+ /**
116+ * @returns {Promise<{ error: string | null, access_problem: 'not_syntax_team' | 'missing_read_packages' | null }> }
117+ */
118+ export async function pull_image ( ) {
119+ const result = await docker ( [ 'pull' , '--quiet' , IMAGE ] ) ;
120+ if ( result . code === 0 ) return { error : null , access_problem : null } ;
121+ if ( ! is_access_denied ( result . stderr ) ) return { error : result . stderr , access_problem : null } ;
122+
123+ const team_result = await pull_as_syntax_team_member ( ) ;
124+ if ( ! team_result ) return { error : result . stderr , access_problem : 'not_syntax_team' } ;
125+ if ( team_result . code === 0 ) return { error : null , access_problem : null } ;
78126
79- return result . code === 0
80- ? { error : null , is_denied : false }
81- : { error : result . stderr , is_denied : is_access_denied ( result . stderr ) } ;
127+ return is_access_denied ( team_result . stderr )
128+ ? { error : team_result . stderr , access_problem : 'missing_read_packages' }
129+ : { error : team_result . stderr , access_problem : null } ;
82130}
83131
84132// True only when Syntax Auth itself answers, never another program that happens to use the port.
0 commit comments