Skip to content

Commit

Permalink
Use getent passwd
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmarti committed Jul 6, 2023
1 parent c263250 commit 893dbb4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/spec-common/injectHeadless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ export async function getContainerProperties(options: {
remoteExecAsRoot = remoteExec;
}
const osRelease = await getOSRelease(shellServer);
const passwdUser = await getUserFromEtcPasswd(shellServer, containerUser);
const passwdUser = await getUserFromPasswdDB(shellServer, containerUser);
if (!passwdUser) {
params.output.write(toWarningText(`User ${containerUser} not found in /etc/passwd.`));
params.output.write(toWarningText(`User ${containerUser} not found with 'getent passwd'.`));
}
const shell = await getUserShell(containerEnv, passwdUser);
const homeFolder = await getHomeFolder(containerEnv, passwdUser);
Expand Down Expand Up @@ -283,9 +283,9 @@ async function getUserShell(containerEnv: NodeJS.ProcessEnv, passwdUser: PasswdU
return containerEnv.SHELL || (passwdUser && passwdUser.shell) || '/bin/sh';
}

export async function getUserFromEtcPasswd(shellServer: ShellServer, userNameOrId: string) {
const { stdout } = await shellServer.exec('cat /etc/passwd', { logOutput: false });
return findUserInEtcPasswd(stdout, userNameOrId);
export async function getUserFromPasswdDB(shellServer: ShellServer, userNameOrId: string) {
const { stdout } = await shellServer.exec(`getent passwd ${userNameOrId}`, { logOutput: false });
return findUserInPasswdDB(stdout, userNameOrId);
}

export interface PasswdUser {
Expand All @@ -296,7 +296,7 @@ export interface PasswdUser {
shell: string;
}

export function findUserInEtcPasswd(etcPasswd: string, nameOrId: string): PasswdUser | undefined {
function findUserInPasswdDB(etcPasswd: string, nameOrId: string): PasswdUser | undefined {
const users = etcPasswd
.split(/\r?\n/)
.map(line => line.split(':'))
Expand Down

0 comments on commit 893dbb4

Please sign in to comment.