Skip to content
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

[SM-1130] - add install scripts #645

Merged
merged 22 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b39cc93
add install scripts
tangowithfoxtrot Mar 1, 2024
9d72a15
Merge branch 'main' into sm/SM-1130/host-a-generic-install-script
tangowithfoxtrot Jun 5, 2024
25464c8
fix arm arch and fmt error
tangowithfoxtrot Jun 6, 2024
51f1fc8
allow overriding bws version; use Bitwarden for path name
tangowithfoxtrot Jun 6, 2024
c798283
update to 0.5.0
tangowithfoxtrot Jun 6, 2024
276beef
fix erroneous newline
tangowithfoxtrot Jun 6, 2024
1e4a441
add checksum validation
tangowithfoxtrot Jun 6, 2024
3160a61
Merge branch 'main' into sm/SM-1130/host-a-generic-install-script
tangowithfoxtrot Jun 6, 2024
83f53d3
rm todo; grammar
tangowithfoxtrot Jun 6, 2024
f16cf6d
fix macOS arch check; quote vars
tangowithfoxtrot Jun 6, 2024
34e840e
fix macOS arm arch; quote vars
tangowithfoxtrot Jun 6, 2024
a3ff347
Merge branch 'sm/SM-1130/host-a-generic-install-script' of https://gi…
tangowithfoxtrot Jun 11, 2024
c236af2
use PS-friendly names
tangowithfoxtrot Jun 11, 2024
4aa9bc2
add version bumps for install scripts
tangowithfoxtrot Jun 11, 2024
c0e67ba
Merge branch 'main' into sm/SM-1130/host-a-generic-install-script
tangowithfoxtrot Jun 11, 2024
b6e0b3f
fix: match correct checksum for arch
tangowithfoxtrot Jun 12, 2024
0dd2fda
fix: checksum validation for macs without coreutils
tangowithfoxtrot Jun 13, 2024
0c3b568
move to scripts dir
tangowithfoxtrot Jun 14, 2024
24e0e98
add uninstallation arg
tangowithfoxtrot Jun 17, 2024
2dba059
use easier regex matching
tangowithfoxtrot Jun 17, 2024
035ca93
mention install script in docs
tangowithfoxtrot Jun 20, 2024
3ff5cde
Merge branch 'main' into sm/SM-1130/host-a-generic-install-script
tangowithfoxtrot Jun 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/version-bump.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,19 @@ jobs:
run: cargo set-version -p bitwarden-napi ${{ inputs.version_number }}

### bitwarden

- name: Bump bitwarden crate Version
if: ${{ inputs.project == 'bitwarden' }}
run: cargo set-version -p bitwarden ${{ inputs.version_number }}

### bws

- name: Bump bws Version
if: ${{ inputs.project == 'bws' }}
run: cargo set-version -p bws ${{ inputs.version_number }}
run: |
cargo set-version -p bws ${{ inputs.version_number }}
# bump the version in install.sh
sed -i 's/BWS_VERSION="${BWS_VERSION:-[0-9]\+\.[0-9]\+\.[0-9]\+}"/BWS_VERSION="${BWS_VERSION:-${{ inputs.version_number }}}"/' ./crates/bws/install.sh
# bump the version in install.ps1
sed -i 's/{ \$env:bwsVersion } else { "[0-9]\+\.[0-9]\+\.[0-9]\+" }/\{ \$env:bwsVersion } else { "${{ inputs.version_number }}" }/' ./crates/bws/install.ps1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These sed expressions worked when I tested them locally, but there doesn't appear to be a way to test this in the workflow, so I would appreciate some scrutiny here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with simpler regex matches in 2dba059.


### python
- name: Bump python-sdk Version
Expand Down
80 changes: 80 additions & 0 deletions crates/bws/install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
$ErrorActionPreference = "Stop"

$bwsVersion = if ($env:bwsVersion) { $env:bwsVersion } else { "0.5.0" }
$installDir = [Environment]::GetFolderPath([Environment+SpecialFolder]::LocalApplicationData) | Join-Path -ChildPath "Programs" | Join-Path -ChildPath "Bitwarden"

# https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processor#properties
$processorArch = (Get-CimInstance -ClassName Win32_Processor).Architecture
if ($processorArch -eq 9) {
$arch = "x86_64"
} elseif ($processorArch -eq 12) {
$arch = "aarch64"
} else {
throw "Unsupported architecture: $processorArch"
}

function Test-BwsInstallation {
$existingBws = Get-Command bws -ErrorAction SilentlyContinue
if ($null -ne $existingBws) {
$userInput = Read-Host "bws is already installed at $($existingBws.Source). Do you want to overwrite it? (Y/N)"
if ($userInput -ne "Y") {
Write-Host "Installation cancelled by user."
exit
}
}
}

function Invoke-BwsDownload {
Write-Host "Detected architecture: $arch"

$bwsUrl = "https://github.com/bitwarden/sdk/releases/download/bws-v$bwsVersion/bws-$arch-pc-windows-msvc-$bwsVersion.zip"
Write-Host "Downloading bws from: $bwsUrl"
$outputPath = Join-Path $env:TEMP "bws.zip"
Invoke-WebRequest -Uri $bwsUrl -OutFile $outputPath
return $outputPath
}

function Test-Checksum {
param($zipPath)
Write-Host "Validating checksum..."

$checksumUrl = "https://github.com/bitwarden/sdk/releases/download/bws-v$bwsVersion/bws-sha256-checksums-$bwsVersion.txt"
$checksumFile = Join-Path $env:TEMP "bws-checksums.txt"
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumFile

$expectedChecksum = (Get-Content $checksumFile | Where-Object { $_ -match "bws-$arch-pc-windows-msvc-$bwsVersion.zip" }).Split(" ")[0]
$actualChecksum = (Get-FileHash -Algorithm SHA256 -Path $zipPath).Hash

if ($actualChecksum -ne $expectedChecksum) {
throw "Checksum validation failed. Expected: $expectedChecksum, Actual: $actualChecksum"
} else {
Write-Host "Checksum validation successful."
}
}

function Install-Bws {
param($zipPath)
Write-Host "Installing bws..."
New-Item -ItemType Directory -Force -Path $installDir | Out-Null
Expand-Archive -Force $zipPath $installDir
Write-Host "bws installed to $installDir"
setx PATH "$env:PATH;$installDir"
Write-Host "$installDir has been added to your PATH"
Write-Host "Please restart your shell to use bws"
}

function Test-Bws {
Write-Host "Checking bws..."
$bwsPath = Join-Path $installDir "bws.exe"
if (Test-Path $bwsPath) {
Write-Host "bws is installed at $bwsPath"
} else {
throw "bws is not installed"
}
}

Test-BwsInstallation
$zipPath = Invoke-BwsDownload
Test-Checksum -zipPath $zipPath
Install-Bws -zipPath $zipPath
Test-Bws
139 changes: 139 additions & 0 deletions crates/bws/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#!/bin/sh
set -u

##################################################
# An installer for the bws command line utility. #
##################################################

BWS_VERSION="${BWS_VERSION:-0.5.0}"

main() {
check_required
platform_detect
arch_detect
download_bws
validate_checksum
install_bws
}

error() {
echo "$1" >&2
echo "Exiting..." >&2
exit 1
}

check_required() {
coltonhurst marked this conversation as resolved.
Show resolved Hide resolved
if ! command -v curl >/dev/null && ! command -v wget >/dev/null; then
error "curl or wget is required to download bws."
fi

if ! command -v unzip >/dev/null; then
error "unzip is required to install bws."
fi
}

can_sudo() {
if command -v sudo >/dev/null; then
echo "Attempting to install bws with sudo. Please enter your password if prompted."
if sudo -v 2>/dev/null; then
echo "sudo is available and we have the necessary permissions."
echo "Installing bws to /usr/local/bin..."
return 0
else
echo "sudo is available, but we failed to authenticate. Trying to install to your \$HOME directory..."
return 1
fi
else
echo "sudo is not available. Trying to install to your \$HOME directory..."
return 1
fi
}

platform_detect() {
if [ "$(uname -s)" = "Linux" ]; then
PLATFORM="unknown-linux-gnu"
elif [ "$(uname -s)" = "Darwin" ]; then
PLATFORM="apple-darwin"
else
error "Unsupported platform: $(uname -s)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could potentially point people to the GitHub SDK repository if they want to create an issue for us to look into supporting their platform.

fi
}

arch_detect() {
if [ "$(uname -m)" = "x86_64" ]; then
ARCH="x86_64"
elif [ "$(uname -m)" = "aarch64" ]; then # Linux uname output
ARCH="aarch64"
elif [ "$(uname -m)" = "arm64" ]; then # Darwin uname output
ARCH="aarch64"
else
error "Unsupported architecture: $(uname -m)"
fi
}

downloader() {
if command -v curl >/dev/null; then
curl -L -o "$2" "$1"
else
wget -O "$2" "$1"
fi
}

extract() {
unzip -o "$1" -d "$2"
}

download_bws() {
bws_url="https://github.com/bitwarden/sdk/releases/download/bws-v${BWS_VERSION}/bws-${ARCH}-${PLATFORM}-${BWS_VERSION}.zip"
echo "Downloading bws from: $bws_url"
tmp_dir="$(mktemp -d)"
downloader "$bws_url" "$tmp_dir/bws.zip"
}

validate_checksum() {
checksum_url="https://github.com/bitwarden/sdk/releases/download/bws-v${BWS_VERSION}/bws-sha256-checksums-${BWS_VERSION}.txt"
echo "Downloading checksum file from: $checksum_url"
checksum_file="$tmp_dir/bws-checksums.txt"
downloader "$checksum_url" "$checksum_file"

expected_checksum="$(grep "bws-${ARCH}-${PLATFORM}-${BWS_VERSION}.zip" "$checksum_file" | awk '{print $1}')"
actual_checksum="$(sha256sum "$tmp_dir/bws.zip" | awk '{print $1}')"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this get the expected and actual checksum from the same downloaded zip?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This greps for the string "bws-${ARCH}-${PLATFORM}-${BWS_VERSION}.zip" in the checksum file and uses awk to return just the checksum value.

Like this:

bash-5.2$ grep bws-aarch64-apple-darwin-0.5.0.zip ~/Downloads/bws-sha256-checksums-0.5.0.txt
aeac01edbd7cdfb8e75e9143d13d69221e6e0bff0fd9f8de69b85c3108a4e986 bws-aarch64-apple-darwin-0.5.0.zip
bash-5.2$ grep bws-aarch64-apple-darwin-0.5.0.zip ~/Downloads/bws-sha256-checksums-0.5.0.txt | awk '{print $1}'
aeac01edbd7cdfb8e75e9143d13d69221e6e0bff0fd9f8de69b85c3108a4e986


if [ "$actual_checksum" != "$expected_checksum" ]; then
error "Checksum validation failed. Expected: $expected_checksum, Actual: $actual_checksum"
else
echo "Checksum validation successful."
fi
}

install_bws() {
echo "Installing bws..."
extract "$tmp_dir/bws.zip" "$tmp_dir"
chmod +x "$tmp_dir/bws"

if can_sudo; then
sudo install -m 755 "$tmp_dir/bws" /usr/local/bin/bws

if ! command -v bws >/dev/null; then
error "Installation failed. bws was not found in /usr/local/bin"
fi

echo "bws installed to /usr/local/bin/bws"
else
user_bin_dir="${HOME}/.local/bin"
mkdir -p "${user_bin_dir}"
install -m 755 "$tmp_dir/bws" "${user_bin_dir}/bws"

if ! command -v "${user_bin_dir}/bws" >/dev/null; then
error "Installation failed. bws was not found in ${user_bin_dir}"
fi

echo "bws installed at ${user_bin_dir}/bws"
echo "Please add ${user_bin_dir} to your PATH by adding the following line to your ~/.profile or shell rc file:"
echo "export PATH=\"\$PATH:${user_bin_dir}\""
fi

rm -rf "$tmp_dir"
}

main
Loading