Skip to content

Commit

Permalink
Wsl ca fixes (#291)
Browse files Browse the repository at this point in the history
* Improved current user group detection on win32

* Removed admin requirement to install Lando Development CA on win32 from wsl

* linter

* debuggy1

* debuggy2

* not sure why this stopped working?

* debuggy3

* allow docker-users add to succeed even if user already exists

* tie it all together

* revert win32 group membership discovery changes for now

* revert win32 group membership discovery changes for now part 2
  • Loading branch information
pirog authored Dec 5, 2024
1 parent 602e22f commit 34e5baa
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }})

* Fixed bug causing some `run-elevated` commands to fail on older Powershell versions
* Removed `admin` requirement to install Lando Development CA on `win32` from `wsl`

## v3.23.15 - [December 3, 2024](https://github.com/lando/core/releases/tag/v3.23.15)

* Disabled `DOCKER_CLI_HINTS` on `lando`
Expand Down
4 changes: 2 additions & 2 deletions examples/setup-windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ lando plugin-add "@lando/core@file:../.."
lando setup -y --skip-networking --skip-common-plugins

# Should have installed Docker Desktop
Test-Path "$Env:ProgramFiles\Docker\Docker\Docker Desktop.exe"
& "$Env:ProgramFiles\Docker\Docker\resources\bin\docker.exe" --version
Test-Path "$env:ProgramFiles\\Docker\\Docker\\Docker Desktop.exe"
& "$env:ProgramFiles\\Docker\\Docker\\resources\\bin\\docker.exe" --version

# Should have installed Docker Compose
Get-ChildItem -Path "$HOME/.lando/bin" -Filter "docker-compose-v2*" -Recurse | ForEach-Object { & $_.FullName version }
Expand Down
18 changes: 12 additions & 6 deletions hooks/lando-setup-build-engine-win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,20 @@ module.exports = async (lando, options) => {
// check one last time incase this was added by a dependee or otherwise
if (require('../utils/is-group-member')('docker-users')) return {code: 0};

try {
const command = ['net', 'localgroup', 'docker-users', lando.config.username, '/ADD'];
const response = await require('../utils/run-elevated')(command, {debug});
task.title = `Added ${lando.config.username} to docker-users`;
return response;
} catch (error) {
const command = ['net', 'localgroup', 'docker-users', lando.config.username, '/ADD'];
const {code, stdout, stderr} = await require('../utils/run-elevated')(command, {ignoreReturnCode: true, debug});

// fail on anything except 1378 which is user already exists
if (code !== 0 && (!stderr.includes('1378') || !stderr.includes('already a member'))) {
const error = new Error(`Error adding ${lando.config.username} to the docker-users group!`);
error.code = code;
error.stdout = stdout;
error.stderr = stderr;
throw error;
}

task.title = `Added ${lando.config.username} to docker-users`;
return {code, stdout, stderr};
},
});
};
Expand Down
12 changes: 0 additions & 12 deletions hooks/lando-setup-install-ca-wsl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const os = require('os');
const getWinEnvar = require('../utils/get-win32-envvar-from-wsl');
const path = require('path');

/**
Expand Down Expand Up @@ -118,17 +117,6 @@ module.exports = async (lando, options) => {
}
},
canRun: async () => {
// get windows user
const user = await getWinEnvar('USERNAME');

// Check for admin privileges
if (!await require('../utils/is-admin-user')(user, {platform: 'win32'})) {
throw new Error([
`User "${user}" does not have permission to trust the CA!`,
'Contact your system admin for permission and then rerun setup.',
].join(os.EOL));
}

return true;
},
task: async (ctx, task) => {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions scripts/run-elevated.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ $debug = If ($DebugPreference -eq "Continue") {$true} Else {$false}

# figure out the command and setup fake fds
$command = $cmd.split(',')
$stdoutfile = Join-Path $Env:Temp $(New-Guid)
$stderrfile = Join-Path $Env:Temp $(New-Guid)
$stdoutfile = Join-Path $Env:Temp $([guid]::NewGuid().ToString())
$stderrfile = Join-Path $Env:Temp $([guid]::NewGuid().ToString())

# DEBUG
Write-Debug "running elevated command:"
Expand Down
3 changes: 2 additions & 1 deletion utils/is-admin-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = (user, {platform = process.platform} = {}) => {
|| require('./is-group-member')('wheel', user, platform)
|| require('./is-group-member')('adm', user, platform);
case 'win32':
return require('./is-group-member')('administrators', user, platform);
return require('./is-group-member')('S-1-5-32-544', user, platform)
|| require('./is-group-member')('administrators', user, platform);
default:
return false;
}
Expand Down
7 changes: 5 additions & 2 deletions utils/is-group-member.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ module.exports = (group, user, platform = process.platform) => {

// if windows we have a long command to check
if (platform === 'win32') {
const groups = stdout
const ids = stdout
.split(os.EOL)
.map(group => group.trim())
.filter(group => group !== 'Name' && group !== '----')
.map(group => group.toUpperCase());
return groups.includes(group.toUpperCase());

const matches = ids.filter(id => id === group.toUpperCase() || id.endsWith(group.toUpperCase()));

return matches.length > 0;
}

// otherwise false?
Expand Down
8 changes: 8 additions & 0 deletions utils/run-elevated.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,17 @@ module.exports = (command, options, stdout = '', stderr = '') => {
debug('elevated command %o done with code %o', command, code);
// with run-elevate we want to clean up stderr a bit if we can eg remove the powershell shit
if (options.method === 'run-elevated') {
const raw = stderr;

stderr = stderr.split('. At line')[0];
stderr = stderr.split(`${os.EOL}At `)[0];

// add nse if we have one
if (raw.split('NativeCommandError')[1]) {
const nse = raw.split('NativeCommandError')[1];
stderr = `${stderr}. ${nse.trim()}`;
}

// simplify the UAC cancel error
if (stderr.includes('The operation was canceled by the user.')) {
stderr = 'The operation was canceled by the user.';
Expand Down

0 comments on commit 34e5baa

Please sign in to comment.