Skip to content

Commit bc233dc

Browse files
arvind-choudhary-hHarness
authored andcommitted
Added install command (#114)
* cce005 []: added install command * 4dcb5e []: added install command * b4fce3 []: added install command * afb6ce []: Added install command
1 parent f9bfb63 commit bc233dc

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Or with sudo if you need elevated privileges:
3737
curl -fsSL https://raw.githubusercontent.com/harness/harness-cli/v2/install | sudo sh
3838
```
3939

40-
This script automatically detects your OS and architecture, downloads the appropriate binary, and installs it to `/usr/local/bin`.
40+
This script automatically detects your OS and architecture, downloads the appropriate binary, verifies its checksum for security, and installs it to `/usr/local/bin`.
4141

4242
### Custom Installation Directory
4343

install

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,57 @@ download_file() {
9595
fi
9696
}
9797

98+
# Verify checksum of downloaded file
99+
verify_checksum() {
100+
file="$1"
101+
checksums_file="$2"
102+
filename=$(basename "$file")
103+
104+
# Check if sha256sum or shasum is available
105+
if command -v sha256sum >/dev/null 2>&1; then
106+
expected_checksum=$(grep "$filename" "$checksums_file" | awk '{print $1}')
107+
if [ -z "$expected_checksum" ]; then
108+
warn "Checksum for $filename not found in checksums.txt"
109+
return 1
110+
fi
111+
actual_checksum=$(sha256sum "$file" | awk '{print $1}')
112+
elif command -v shasum >/dev/null 2>&1; then
113+
expected_checksum=$(grep "$filename" "$checksums_file" | awk '{print $1}')
114+
if [ -z "$expected_checksum" ]; then
115+
warn "Checksum for $filename not found in checksums.txt"
116+
return 1
117+
fi
118+
actual_checksum=$(shasum -a 256 "$file" | awk '{print $1}')
119+
else
120+
warn "Neither sha256sum nor shasum found, skipping checksum verification"
121+
return 0
122+
fi
123+
124+
if [ "$expected_checksum" = "$actual_checksum" ]; then
125+
return 0
126+
else
127+
error "Checksum mismatch!"
128+
error "Expected: $expected_checksum"
129+
error "Got: $actual_checksum"
130+
return 1
131+
fi
132+
}
133+
134+
# Validate version tag
135+
validate_version() {
136+
version="$1"
137+
138+
# Check if version starts with "v1"
139+
case "$version" in
140+
v1*)
141+
return 0
142+
;;
143+
*)
144+
error "Invalid version: $version. This installer only supports v1.x releases."
145+
;;
146+
esac
147+
}
148+
98149
# Check if running with sufficient privileges
99150
check_privileges() {
100151
if [ ! -w "$INSTALL_DIR" ]; then
@@ -121,6 +172,10 @@ main() {
121172
if [ -z "$VERSION" ]; then
122173
VERSION="$DEFAULT_VERSION"
123174
fi
175+
176+
# Validate version starts with v1
177+
validate_version "$VERSION"
178+
124179
info "Installing version: $VERSION"
125180

126181
# Construct download URL and filename
@@ -145,6 +200,20 @@ main() {
145200
error "Failed to download from $DOWNLOAD_URL"
146201
fi
147202

203+
# Download checksums file
204+
CHECKSUMS_URL="https://github.com/${REPO}/releases/download/${VERSION}/checksums.txt"
205+
info "Downloading checksums.txt for verification..."
206+
if ! download_file "$CHECKSUMS_URL" "$TMP_DIR/checksums.txt"; then
207+
warn "Failed to download checksums.txt, skipping verification"
208+
else
209+
# Verify checksum
210+
info "Verifying checksum..."
211+
if ! verify_checksum "$TMP_DIR/$FILENAME" "$TMP_DIR/checksums.txt"; then
212+
error "Checksum verification failed! The downloaded file may be corrupted or tampered with."
213+
fi
214+
info "✓ Checksum verified successfully"
215+
fi
216+
148217
# Extract archive
149218
info "Extracting archive..."
150219
cd "$TMP_DIR"

0 commit comments

Comments
 (0)