-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·421 lines (358 loc) · 11.8 KB
/
install.sh
File metadata and controls
executable file
·421 lines (358 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env bash
# Socket CLI installation script.
# Downloads and installs the appropriate Socket CLI binary for your platform.
set -euo pipefail
# Colors for output.
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
PURPLE='\033[0;35m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Print colored messages.
info() {
echo -e "${BLUE}ℹ${NC} $1"
}
success() {
echo -e "${GREEN}✓${NC} $1"
}
error() {
echo -e "${RED}✗${NC} $1"
}
warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
step() {
echo -e "${CYAN}→${NC} $1"
}
socket_brand() {
echo -e "${PURPLE}⚡${NC} $1"
}
# Detect if running on musl libc (Alpine Linux, etc.).
detect_musl() {
# Check for Alpine in /etc/os-release.
if [ -f /etc/os-release ]; then
if grep -qi 'alpine' /etc/os-release 2>/dev/null; then
return 0
fi
fi
# Check for musl dynamic linker.
if [ -f /lib/ld-musl-x86_64.so.1 ] || [ -f /lib/ld-musl-aarch64.so.1 ]; then
return 0
fi
# Check ldd output for musl.
if command -v ldd &> /dev/null; then
if ldd --version 2>&1 | grep -qi musl; then
return 0
fi
fi
return 1
}
# Detect platform and architecture.
detect_platform() {
local os
local arch
local libc_suffix=""
# Detect OS.
case "$(uname -s)" in
Linux*)
os="linux"
# Check for musl libc on Linux.
if detect_musl; then
libc_suffix="-musl"
fi
;;
Darwin*)
os="darwin"
;;
MINGW*|MSYS*|CYGWIN*)
os="win32"
;;
*)
error "Unsupported operating system: $(uname -s)"
echo ""
info "Socket CLI supports Linux, macOS, and Windows."
info "If you think this is an error, please open an issue at:"
info "https://github.com/SocketDev/socket-cli/issues"
exit 1
;;
esac
# Detect architecture.
case "$(uname -m)" in
x86_64|amd64)
arch="x64"
;;
aarch64|arm64)
arch="arm64"
;;
*)
error "Unsupported architecture: $(uname -m)"
echo ""
info "Socket CLI supports x64 and arm64 architectures."
info "If you think this is an error, please open an issue at:"
info "https://github.com/SocketDev/socket-cli/issues"
exit 1
;;
esac
echo "${os}-${arch}${libc_suffix}"
}
# Fetch a URL to stdout, enforcing HTTPS.
#
# curl enforces HTTPS via `--proto '=https'`. wget's `--https-only` only
# applies to recursive downloads, so for the single-file fetches we do
# here we disable redirect following (`--max-redirect=0`) — npm's
# registry serves responses directly with no redirect, so this is safe
# AND blocks any MITM attempt to redirect us to http://.
fetch_url() {
local url="$1"
if command -v curl &> /dev/null; then
curl --proto '=https' --tlsv1.2 -fsSL "$url"
elif command -v wget &> /dev/null; then
wget --max-redirect=0 -qO- "$url"
else
error "Neither curl nor wget found on your system"
echo ""
info "Please install curl or wget to continue:"
info " macOS: brew install curl"
info " Ubuntu: sudo apt-get install curl"
info " Fedora: sudo dnf install curl"
exit 1
fi
}
# Download a URL to a file, enforcing HTTPS (see `fetch_url` comment).
fetch_url_to_file() {
local url="$1"
local out="$2"
if command -v curl &> /dev/null; then
curl --proto '=https' --tlsv1.2 -fsSL -o "$out" "$url"
elif command -v wget &> /dev/null; then
wget --max-redirect=0 -qO "$out" "$url"
else
error "Neither curl nor wget found on your system"
exit 1
fi
}
# Parse a JSON string field out of a response body. Tolerates a missing
# field by returning empty, rather than dying under `pipefail`.
parse_json_string() {
local body="$1"
local field="$2"
# Pipe through `cat` so a grep non-match (exit 1) doesn't trip pipefail;
# the final `echo` replaces an empty match with empty string.
printf '%s' "$body" \
| grep -o "\"${field}\": *\"[^\"]*\"" \
| head -1 \
| sed "s/\"${field}\": *\"\\([^\"]*\\)\"/\\1/" \
|| true
}
# Get the latest version from npm registry.
get_latest_version() {
local package_name="$1"
local body version
body=$(fetch_url "https://registry.npmjs.org/${package_name}/latest")
version=$(parse_json_string "$body" "version")
if [ -z "$version" ]; then
error "Failed to fetch latest version from npm registry"
echo ""
info "This might be a temporary network issue. Please try again."
info "If the problem persists, check your internet connection."
exit 1
fi
echo "$version"
}
# Get the npm-published integrity string (SSRI format, e.g. "sha512-...") for
# a specific version.
get_published_integrity() {
local package_name="$1"
local version="$2"
local body
body=$(fetch_url "https://registry.npmjs.org/${package_name}/${version}")
parse_json_string "$body" "integrity"
}
# Compute an SSRI-style hash (e.g. "sha512-<base64>") of a file.
# Requires `openssl` — the tool is ubiquitous (macOS, every mainstream
# Linux distro, Alpine's default image, WSL, Git Bash) and gives us a
# one-step hex-less pipeline so we don't depend on `xxd` (not POSIX).
compute_integrity() {
local file="$1"
local algo="$2"
local digest
if ! command -v openssl &> /dev/null; then
error "openssl not found — required to verify the download integrity"
echo ""
info "Install openssl and re-run:"
info " macOS: already installed (or: brew install openssl)"
info " Alpine: apk add openssl"
info " Debian: sudo apt-get install openssl"
info " Fedora: sudo dnf install openssl"
exit 1
fi
digest=$(openssl dgst "-${algo}" -binary "$file" | openssl base64 -A)
echo "${algo}-${digest}"
}
# Calculate SHA256 hash of a string.
calculate_hash() {
local str="$1"
if command -v sha256sum &> /dev/null; then
echo -n "$str" | sha256sum | cut -d' ' -f1
elif command -v shasum &> /dev/null; then
echo -n "$str" | shasum -a 256 | cut -d' ' -f1
else
error "Neither sha256sum nor shasum found"
exit 1
fi
}
# Download and install Socket CLI.
install_socket_cli() {
local platform
local version
local package_name
local download_url
local dlx_dir
local package_hash
local install_dir
local binary_path
local bin_dir
local symlink_path
step "Detecting your platform..."
platform=$(detect_platform)
success "Platform detected: ${BOLD}$platform${NC}"
# Construct package name.
package_name="@socketbin/cli-${platform}"
step "Fetching latest version from npm..."
version=$(get_latest_version "$package_name")
success "Found version ${BOLD}$version${NC}"
# Construct download URL from npm registry.
download_url="https://registry.npmjs.org/${package_name}/-/cli-${platform}-${version}.tgz"
socket_brand "Downloading Socket CLI..."
# Create DLX directory structure.
dlx_dir="${HOME}/.socket/_dlx"
mkdir -p "$dlx_dir"
# Calculate content hash for the package.
package_hash=$(calculate_hash "${package_name}@${version}")
install_dir="${dlx_dir}/${package_hash}"
# Create installation directory.
mkdir -p "$install_dir"
# Look up the integrity string the registry published for this exact version.
step "Fetching published integrity..."
local expected_integrity
expected_integrity=$(get_published_integrity "$package_name" "$version")
if [ -z "$expected_integrity" ]; then
error "No integrity found in the npm registry metadata for ${package_name}@${version}"
info "Refusing to install without a published checksum to verify against."
exit 1
fi
# Algorithm prefix from the SSRI string (e.g. "sha512-..." -> "sha512").
local integrity_algo="${expected_integrity%%-*}"
# Download tarball to a temporary location outside the install dir so a
# failed verify can't leave a partial blob where future runs might trust it.
local temp_tarball
if command -v mktemp &> /dev/null; then
temp_tarball=$(mktemp -t socket-cli.XXXXXX.tgz 2>/dev/null || mktemp "${TMPDIR:-/tmp}/socket-cli.XXXXXX")
else
temp_tarball="${TMPDIR:-/tmp}/socket-cli.$$.tgz"
fi
trap 'rm -f "$temp_tarball"' EXIT
fetch_url_to_file "$download_url" "$temp_tarball"
# Verify integrity against the value npm published for this version.
step "Verifying integrity..."
local actual_integrity
actual_integrity=$(compute_integrity "$temp_tarball" "$integrity_algo")
if [ "$actual_integrity" != "$expected_integrity" ]; then
error "Integrity check failed for ${package_name}@${version}"
info " expected: ${expected_integrity}"
info " got: ${actual_integrity}"
info "Not installing. Please retry; if this persists, open an issue."
exit 1
fi
success "Integrity verified (${integrity_algo})"
# Extract tarball.
step "Capturing lightning in a bottle ⚡"
tar -xzf "$temp_tarball" -C "$install_dir"
# Get Socket CLI version from extracted package.
local cli_version
if [ -f "${install_dir}/package/package.json" ]; then
cli_version=$(grep -o '"version": *"[^"]*"' "${install_dir}/package/package.json" | head -1 | sed 's/"version": *"\([^"]*\)"/\1/')
if [ -n "$cli_version" ]; then
success "Socket CLI ${BOLD}v${cli_version}${NC} (build ${version})"
fi
fi
# Find the binary (it's in package/bin/socket or package/bin/socket.exe).
if [ "$platform" = "win32-x64" ] || [ "$platform" = "win32-arm64" ]; then
binary_path="${install_dir}/package/bin/socket.exe"
else
binary_path="${install_dir}/package/bin/socket"
fi
if [ ! -f "$binary_path" ]; then
error "Binary not found at expected path: $binary_path"
echo ""
info "This might be a temporary issue with the package. Try again in a moment."
exit 1
fi
# Make binary executable (Unix-like systems).
if [ "$platform" != "win32-x64" ] && [ "$platform" != "win32-arm64" ]; then
chmod +x "$binary_path"
# Clear macOS quarantine attribute.
if [ "$platform" = "darwin-x64" ] || [ "$platform" = "darwin-arm64" ]; then
xattr -d com.apple.quarantine "$binary_path" 2>/dev/null || true
success "Cleared macOS security restrictions"
fi
fi
# Clean up tarball (EXIT trap also handles this in error paths).
rm -f "$temp_tarball"
trap - EXIT
success "Binary ready at ${BOLD}$binary_path${NC}"
# Create symlink in user's local bin directory.
bin_dir="${HOME}/.local/bin"
mkdir -p "$bin_dir"
symlink_path="${bin_dir}/socket"
# Remove existing symlink if present.
if [ -L "$symlink_path" ] || [ -f "$symlink_path" ]; then
step "Replacing existing installation..."
rm "$symlink_path"
fi
# Create symlink.
step "Creating command shortcut..."
ln -s "$binary_path" "$symlink_path"
success "Command ready: ${BOLD}socket${NC}"
echo ""
# Check if ~/.local/bin is in PATH.
if [[ ":$PATH:" != *":${bin_dir}:"* ]]; then
warning "Almost there! One more step needed..."
echo ""
echo " Add ${BOLD}~/.local/bin${NC} to your PATH by adding this line to your shell profile:"
echo " ${BOLD}(~/.bashrc, ~/.zshrc, ~/.bash_profile, or ~/.profile)${NC}"
echo ""
echo " ${CYAN}export PATH=\"\$HOME/.local/bin:\$PATH\"${NC}"
echo ""
echo " Then restart your shell or run: ${CYAN}source ~/.zshrc${NC} (or your shell config)"
echo ""
else
success "Your PATH is already configured perfectly!"
fi
echo ""
if [ -n "$cli_version" ]; then
socket_brand "${BOLD}Socket CLI v${cli_version} installed successfully!${NC}"
else
socket_brand "${BOLD}Socket CLI installed successfully!${NC}"
fi
echo ""
info "Quick start:"
echo -e " ${CYAN}socket --help${NC} Get started with Socket"
echo -e " ${CYAN}socket self-update${NC} Update to the latest version"
echo ""
socket_brand "Happy securing!"
}
# Main execution.
main() {
echo ""
echo -e "${PURPLE}${BOLD}⚡ Socket CLI Installer ⚡${NC}"
echo -e "${BOLD}═══════════════════════════${NC}"
echo ""
echo " Secure your dependencies with Socket Security"
echo ""
install_socket_cli
}
main "$@"