Skip to content

Commit

Permalink
fix: handle flatpak usecase for admin commands
Browse files Browse the repository at this point in the history
we need to use flatpak-spawn only after command is set and not at the beginning

else we may try to execute commands with incorrect order of params

related to containers#4456

Signed-off-by: Florent Benoit <fbenoit@redhat.com>
  • Loading branch information
benoitf committed Oct 24, 2023
1 parent 99d0501 commit 2721b04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
34 changes: 34 additions & 0 deletions packages/main/src/plugin/util/exec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,40 @@ describe('exec', () => {
expect(stdout).toContain('Hello, World!');
});

test('should run the command with privileges on flatpak Linux', async () => {
const command = 'echo';
const args = ['Hello, World!'];

(util.isLinux as Mock).mockReturnValue(true);

const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('Hello, World!');
}
}) as unknown as Readable;
const spawnMock = vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
} as any);

// emulate flatpak environment
const { stdout } = await exec.exec(command, args, { env: { FLATPAK_ID: 'true' }, isAdmin: true });

// caller should contains the cwd provided
expect(spawnMock).toHaveBeenCalledWith(
'flatpak-spawn',
expect.arrayContaining(['--host', 'pkexec', 'echo', 'Hello, World!']),
expect.anything(),
);
expect(stdout).toBeDefined();
expect(stdout).toContain('Hello, World!');
});

test('should run the command with privileges using exec on Windows', async () => {
const command = 'echo';
const args = ['Hello, World!'];
Expand Down
8 changes: 5 additions & 3 deletions packages/main/src/plugin/util/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ export class Exec {

if (isMac() || isWindows()) {
env.PATH = getInstallationPath(env.PATH);
} else if (env.FLATPAK_ID) {
args = ['--host', command, ...(args || [])];
command = 'flatpak-spawn';
}

// do we have an admin task ?
Expand Down Expand Up @@ -137,6 +134,11 @@ export class Exec {
}
}

if (env.FLATPAK_ID) {
args = ['--host', command, ...(args || [])];
command = 'flatpak-spawn';
}

let cwd: string;
if (options?.cwd) {
cwd = options.cwd;
Expand Down

0 comments on commit 2721b04

Please sign in to comment.