chore: fix installing of branded browsers under non-root (#10640)

This commit is contained in:
Max Schmitt 2021-12-06 14:49:22 -08:00 committed by GitHub
parent c3687a693b
commit 5610974312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 80 deletions

View File

@ -7,26 +7,23 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
if is_user_root; then
maybesudo=""
else
maybesudo="sudo"
fi
# 1. make sure to remove old beta if any.
if dpkg --get-selections | grep -q "^google-chrome-beta[[:space:]]*install$" >/dev/null; then
$maybesudo apt-get remove -y google-chrome-beta
apt-get remove -y google-chrome-beta
fi
# 2. Update apt lists (needed to install curl and chrome dependencies)
apt-get update
# 3. Install curl to download chrome
if ! command -v curl >/dev/null; then
$maybesudo apt-get install -y curl
apt-get install -y curl
fi
# 2. download chrome beta from dl.google.com and install it.
# 4. download chrome beta from dl.google.com and install it.
cd /tmp
curl -O https://dl.google.com/linux/direct/google-chrome-beta_current_amd64.deb
$maybesudo apt-get install -y ./google-chrome-beta_current_amd64.deb
apt-get install -y ./google-chrome-beta_current_amd64.deb
rm -rf ./google-chrome-beta_current_amd64.deb
cd -
google-chrome-beta --version

View File

@ -7,27 +7,23 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
if is_user_root; then
maybesudo=""
else
maybesudo="sudo"
fi
# 1. make sure to remove old stable if any.
if dpkg --get-selections | grep -q "^google-chrome[[:space:]]*install$" >/dev/null; then
$maybesudo apt-get remove -y google-chrome
apt-get remove -y google-chrome
fi
# 2. Update apt lists (needed to install curl and chrome dependencies)
apt-get update
# 3. Install curl to download chrome
if ! command -v curl >/dev/null; then
$maybesudo apt-get install -y curl
apt-get install -y curl
fi
# 2. download chrome stable from dl.google.com and install it.
# 4. download chrome stable from dl.google.com and install it.
cd /tmp
curl -O https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
$maybesudo apt-get install -y ./google-chrome-stable_current_amd64.deb
apt-get install -y ./google-chrome-stable_current_amd64.deb
rm -rf ./google-chrome-stable_current_amd64.deb
cd -
google-chrome --version

View File

@ -8,25 +8,22 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
if is_user_root; then
maybesudo=""
else
maybesudo="sudo"
fi
# 1. make sure to remove old beta if any.
if dpkg --get-selections | grep -q "^microsoft-edge-beta[[:space:]]*install$" >/dev/null; then
$maybesudo apt-get remove -y microsoft-edge-beta
apt-get remove -y microsoft-edge-beta
fi
# 2. Install curl to download Microsoft gpg key
if ! command -v curl >/dev/null; then
$maybesudo apt-get install -y curl
apt-get update
apt-get install -y curl
fi
# 3. Add the GPG key, the apt repo, update the apt cache, and install the package
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg
$maybesudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
$maybesudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
rm /tmp/microsoft.gpg
$maybesudo apt-get update && $maybesudo apt-get install -y microsoft-edge-beta
apt-get update && apt-get install -y microsoft-edge-beta
microsoft-edge-beta --version

View File

@ -8,25 +8,22 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
if is_user_root; then
maybesudo=""
else
maybesudo="sudo"
fi
# 1. make sure to remove old dev if any.
if dpkg --get-selections | grep -q "^microsoft-edge-dev[[:space:]]*install$" >/dev/null; then
$maybesudo apt-get remove -y microsoft-edge-dev
apt-get remove -y microsoft-edge-dev
fi
# 2. Install curl to download Microsoft gpg key
if ! command -v curl >/dev/null; then
$maybesudo apt-get install -y curl
apt-get update
apt-get install -y curl
fi
# 3. Add the GPG key, the apt repo, update the apt cache, and install the package
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg
$maybesudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
$maybesudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-dev.list'
rm /tmp/microsoft.gpg
$maybesudo apt-get update && $maybesudo apt-get install -y microsoft-edge-dev
apt-get update && apt-get install -y microsoft-edge-dev
microsoft-edge-dev --version

View File

@ -8,25 +8,22 @@ if [[ $(arch) == "aarch64" ]]; then
exit 1
fi
is_user_root () { [ "${EUID:-$(id -u)}" -eq 0 ]; }
if is_user_root; then
maybesudo=""
else
maybesudo="sudo"
fi
# 1. make sure to remove old stable if any.
if dpkg --get-selections | grep -q "^microsoft-edge-stable[[:space:]]*install$" >/dev/null; then
$maybesudo apt-get remove -y microsoft-edge-stable
apt-get remove -y microsoft-edge-stable
fi
# 2. Install curl to download Microsoft gpg key
if ! command -v curl >/dev/null; then
$maybesudo apt-get install -y curl
apt-get update
apt-get install -y curl
fi
# 3. Add the GPG key, the apt repo, update the apt cache, and install the package
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg
$maybesudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
$maybesudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-stable.list'
install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/
sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/edge stable main" > /etc/apt/sources.list.d/microsoft-edge-stable.list'
rm /tmp/microsoft.gpg
$maybesudo apt-get update && $maybesudo apt-get install -y microsoft-edge-stable
apt-get update && apt-get install -y microsoft-edge-stable
microsoft-edge-stable --version

View File

@ -70,11 +70,13 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>, dr
commands.push(['apt-get', 'install', '-y', '--no-install-recommends',
...uniqueLibraries,
].join(' '));
const [command, args] = await buildAptProcessArgs(commands);
const { command, args, elevatedPermissions } = await utils.transformCommandsForRoot(commands);
if (dryRun) {
console.log(`${command} ${quoteProcessArgs(args).join(' ')}`); // eslint-disable-line no-console
return;
}
if (elevatedPermissions)
console.log('Switching to root user to install dependencies...'); // eslint-disable-line no-console
const child = childProcess.spawn(command, args, { stdio: 'inherit' });
await new Promise((resolve, reject) => {
child.on('exit', resolve);
@ -82,16 +84,6 @@ export async function installDependenciesLinux(targets: Set<DependencyGroup>, dr
});
}
async function buildAptProcessArgs(commands: string[]): Promise<[string, string[]]> {
const isRoot = process.getuid() === 0;
if (isRoot)
return ['sh', ['-c', `${commands.join('&& ')}`]];
const sudoExists = await utils.spawnAsync('which', ['sudo']);
if (sudoExists.code === 0)
return ['sudo', ['--', 'sh', '-c', `${commands.join('&& ')}`]];
return ['su', ['root', '-c', `${commands.join('&& ')}`]];
}
export async function validateDependenciesWindows(windowsExeAndDllDirectories: string[]) {
const directoryPaths = windowsExeAndDllDirectories;
const lddPaths: string[] = [];

View File

@ -21,7 +21,7 @@ import * as util from 'util';
import * as fs from 'fs';
import lockfile from 'proper-lockfile';
import { getUbuntuVersion } from './ubuntuVersion';
import { getFromENV, getAsBooleanFromENV, calculateSha1, removeFolders, existsAsync, hostPlatform, canAccessFile, spawnAsync, fetchData, wrapInASCIIBox } from './utils';
import { getFromENV, getAsBooleanFromENV, calculateSha1, removeFolders, existsAsync, hostPlatform, canAccessFile, spawnAsync, fetchData, wrapInASCIIBox, transformCommandsForRoot } from './utils';
import { DependencyGroup, installDependenciesLinux, installDependenciesWindows, validateDependenciesLinux, validateDependenciesWindows } from './dependencies';
import { downloadBrowserWithProgressBar, logPolitely } from './browserFetcher';
@ -645,16 +645,25 @@ export class Registry {
const scriptName = scripts[process.platform as 'linux' | 'darwin' | 'win32'];
if (!scriptName)
throw new Error(`Cannot install ${channel} on ${process.platform}`);
const cwd = BIN_PATH;
const isPowerShell = scriptName.endsWith('.ps1');
const shell = isPowerShell ? 'powershell.exe' : 'bash';
if (isPowerShell) {
const args = [
...(isPowerShell ? ['-ExecutionPolicy', 'Bypass', '-File'] : []),
'-ExecutionPolicy', 'Bypass', '-File',
path.join(BIN_PATH, scriptName),
...scriptArgs
];
const { code } = await spawnAsync(shell, args, { cwd: BIN_PATH, stdio: 'inherit' });
const { code } = await spawnAsync('powershell.exe', args, { cwd, stdio: 'inherit' });
if (code !== 0)
throw new Error(`Failed to install ${channel}`);
} else {
const { command, args, elevatedPermissions } = await transformCommandsForRoot([`bash ${path.join(BIN_PATH, scriptName)} ${scriptArgs.join('')}`]);
if (elevatedPermissions)
console.log('Switching to root user to install dependencies...'); // eslint-disable-line no-console
const { code } = await spawnAsync(command, args, { cwd, stdio: 'inherit' });
if (code !== 0)
throw new Error(`Failed to install ${channel}`);
}
}
private async _validateInstallationCache(linksDir: string) {

View File

@ -520,3 +520,13 @@ export function streamToString(stream: stream.Readable): Promise<string> {
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
}
export async function transformCommandsForRoot(commands: string[]): Promise<{ command: string, args: string[], elevatedPermissions: boolean}> {
const isRoot = process.getuid() === 0;
if (isRoot)
return { command: 'sh', args: ['-c', `${commands.join('&& ')}`], elevatedPermissions: false };
const sudoExists = await spawnAsync('which', ['sudo']);
if (sudoExists.code === 0)
return { command: 'sudo', args: ['--', 'sh', '-c', `${commands.join('&& ')}`], elevatedPermissions: true };
return { command: 'su', args: ['root', '-c', `${commands.join('&& ')}`], elevatedPermissions: true };
}