-
Notifications
You must be signed in to change notification settings - Fork 551
makes exec_auth spawn the command asynchronously #2046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,10 @@ export interface Credential { | |
export class ExecAuth implements Authenticator { | ||
private readonly tokenCache: { [key: string]: Credential | null } = {}; | ||
private execFn: ( | ||
cmd: string, | ||
args: string[], | ||
opts: child_process.SpawnOptions, | ||
) => child_process.SpawnSyncReturns<Buffer> = child_process.spawnSync; | ||
command: string, | ||
args?: readonly string[], | ||
options?: child_process.SpawnOptionsWithoutStdio, | ||
) => child_process.ChildProcessWithoutNullStreams = child_process.spawn; | ||
|
||
public isAuthProvider(user: User): boolean { | ||
if (!user) { | ||
|
@@ -41,7 +41,7 @@ export class ExecAuth implements Authenticator { | |
} | ||
|
||
public async applyAuthentication(user: User, opts: https.RequestOptions): Promise<void> { | ||
const credential = this.getCredential(user); | ||
const credential = await this.getCredential(user); | ||
if (!credential) { | ||
return; | ||
} | ||
|
@@ -70,7 +70,7 @@ export class ExecAuth implements Authenticator { | |
return null; | ||
} | ||
|
||
private getCredential(user: User): Credential | null { | ||
private async getCredential(user: User): Promise<Credential | null> { | ||
// TODO: Add a unit test for token caching. | ||
const cachedToken = this.tokenCache[user.name]; | ||
if (cachedToken) { | ||
|
@@ -99,15 +99,46 @@ export class ExecAuth implements Authenticator { | |
exec.env.forEach((elt) => (env[elt.name] = elt.value)); | ||
opts = { ...opts, env }; | ||
} | ||
const result = this.execFn(exec.command, exec.args, opts); | ||
if (result.error) { | ||
throw result.error; | ||
} | ||
if (result.status === 0) { | ||
const obj = JSON.parse(result.stdout.toString('utf8')) as Credential; | ||
this.tokenCache[user.name] = obj; | ||
return obj; | ||
} | ||
throw new Error(result.stderr.toString('utf8')); | ||
|
||
return new Promise((resolve, reject) => { | ||
let stdoutData: string = ''; | ||
let stderrData: string = ''; | ||
let savedError: Error | undefined = undefined; | ||
|
||
const subprocess = this.execFn(exec.command, exec.args, opts); | ||
subprocess.stdout.setEncoding('utf8'); | ||
subprocess.stderr.setEncoding('utf8'); | ||
|
||
subprocess.stdout.on('data', (data: Buffer | string) => { | ||
stdoutData += data.toString('utf8'); | ||
feloy marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
}); | ||
|
||
subprocess.stderr.on('data', (data) => { | ||
stderrData += data.toString('utf8'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about dropping the |
||
}); | ||
|
||
subprocess.on('error', (error) => { | ||
savedError = error; | ||
throw error; | ||
feloy marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that it still makes sense to throw here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my mistake, I forgot to remove it |
||
}); | ||
|
||
subprocess.on('close', (code) => { | ||
if (savedError) { | ||
reject(savedError); | ||
return; | ||
} | ||
if (code !== 0) { | ||
reject(new Error(stderrData)); | ||
return; | ||
} | ||
try { | ||
const obj = JSON.parse(stdoutData) as Credential; | ||
this.tokenCache[user.name] = obj; | ||
resolve(obj); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Buffer
part of the type can be dropped now (not sure if TypeScript will complain about it though).