-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·287 lines (235 loc) · 9.92 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·287 lines (235 loc) · 9.92 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
#!/bin/bash
set -euo pipefail
# Credential Proxy installer
# Builds the native macOS app (Swift HTTP server) + thin MCP relay (stdio→HTTP bridge)
# All credential handling happens in compiled Swift — the agent cannot modify it
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
APP_NAME="Credential Proxy.app"
INSTALL_DIR="/Applications"
APP_PATH="$INSTALL_DIR/$APP_NAME"
MCP_RELAY_DIR="$APP_PATH/Contents/Resources/mcp-relay"
APP_PORT=11111
RED='\033[0;31m'
GREEN='\033[0;32m'
DIM='\033[2m'
RESET='\033[0m'
info() { echo -e "${DIM}$1${RESET}"; }
success() { echo -e "${GREEN}✓ $1${RESET}"; }
error() { echo -e "${RED}✗ $1${RESET}"; exit 1; }
# Register credential-proxy MCP server in a JSON config file.
# Usage: register_mcp <config_path> <relay_index_path> [extra_fields_json]
register_mcp() {
local config_path="$1"
local relay_index="$2"
local extra="${3:-}"
: "${extra:="{}"}"
node - "$config_path" "$relay_index" "$APP_PORT" "$extra" <<'REGISTER_SCRIPT'
const fs = require('fs');
const path = require('path');
const [,, configPath, relayIndex, portStr, extraJson] = process.argv;
const port = parseInt(portStr, 10);
const extra = JSON.parse(extraJson);
fs.mkdirSync(path.dirname(configPath), { recursive: true });
let config = {};
try { config = JSON.parse(fs.readFileSync(configPath, 'utf8')); } catch {}
const key = config['mcp-servers'] && !config.mcpServers ? 'mcp-servers' : 'mcpServers';
if (!config[key]) config[key] = {};
const entry = {
...config[key]['credential-proxy'],
command: 'node',
args: [relayIndex],
env: { CREDENTIAL_PROXY_APP_URL: 'http://127.0.0.1:' + port },
...extra,
};
config[key]['credential-proxy'] = entry;
fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n');
REGISTER_SCRIPT
}
# --- Preflight checks ---
command -v node >/dev/null 2>&1 || error "Node.js is required. Install it from https://nodejs.org"
command -v npm >/dev/null 2>&1 || error "npm is required"
NODE_VERSION=$(node -v | sed 's/v//' | cut -d. -f1)
if [ "$NODE_VERSION" -lt 20 ]; then
error "Node.js 20+ is required (found v$(node -v))"
fi
# --- Guard against clobbering existing installation ---
DATA_DIR="$HOME/Library/Application Support/credential-proxy"
if [ -d "$APP_PATH" ] && [ -f "$DATA_DIR/seal.salt" ]; then
echo ""
echo -e "${RED}An existing Credential Proxy installation with credentials was detected.${RESET}"
echo ""
echo " Running install.sh would replace the binary, which can make"
echo " existing credentials undecryptable."
echo ""
echo " To update safely: ./update.sh"
echo " To force reinstall: ./install.sh --force"
echo ""
if [[ "${1:-}" != "--force" ]]; then
exit 1
fi
echo -e "${DIM}--force specified, proceeding with reinstall...${RESET}"
fi
echo ""
echo "Installing Credential Proxy..."
echo ""
# --- Step 1: Build Node.js MCP relay ---
info "Building MCP relay (stdio → HTTP bridge)..."
cd "$SCRIPT_DIR"
if [ ! -d node_modules ]; then
npm install --silent 2>/dev/null
fi
npm run build --silent 2>/dev/null
success "MCP relay built"
# --- Step 2: Locate pre-built macOS binary ---
PREBUILT_BIN="$SCRIPT_DIR/macos/bin/CredentialProxy"
if [ ! -f "$PREBUILT_BIN" ]; then
error "Pre-built binary not found at $PREBUILT_BIN. Run 'cd macos && swift build -c release' and copy the binary to macos/bin/ first."
fi
success "Pre-built macOS binary found"
# --- Step 3: Create .app bundle ---
info "Creating app bundle..."
mkdir -p "$INSTALL_DIR"
# Remove old installation (current + legacy ~/Applications location)
pkill -x CredentialProxy 2>/dev/null || true
sleep 0.5
rm -rf "$APP_PATH"
rm -rf "$HOME/Applications/$APP_NAME"
# Create bundle structure
mkdir -p "$APP_PATH/Contents/MacOS"
mkdir -p "$APP_PATH/Contents/Resources"
mkdir -p "$MCP_RELAY_DIR"
# Copy app binary (includes native HTTP server — no Node.js server needed)
cp "$PREBUILT_BIN" "$APP_PATH/Contents/MacOS/CredentialProxy"
# Copy Info.plist
cp "$SCRIPT_DIR/macos/Info.plist" "$APP_PATH/Contents/Info.plist"
# Copy MCP relay files (thin stdio→HTTP bridge, never touches secrets)
cp -R "$SCRIPT_DIR/dist/"* "$MCP_RELAY_DIR/"
cp "$SCRIPT_DIR/package.json" "$MCP_RELAY_DIR/"
if [ -f "$SCRIPT_DIR/package-lock.json" ]; then
cp "$SCRIPT_DIR/package-lock.json" "$MCP_RELAY_DIR/"
fi
# Install production dependencies for relay (just @modelcontextprotocol/sdk)
cd "$MCP_RELAY_DIR"
npm ci --omit=dev --silent 2>/dev/null
cd "$SCRIPT_DIR"
# Ad-hoc sign the binary — Keychain ACLs are bound to this signature.
# A rebuilt binary gets a different signature, so Keychain will prompt
# the user before granting access. This prevents agents from silently
# rebuilding to exfiltrate secrets.
codesign -s - -f "$APP_PATH/Contents/MacOS/CredentialProxy" 2>/dev/null
success "App bundle created and signed at $APP_PATH"
# --- Step 4: MCP registration ---
RELAY_INDEX="$MCP_RELAY_DIR/index.js"
# Claude Code (~/.claude.json)
register_mcp "$HOME/.claude.json" "$RELAY_INDEX"
# Pi MCP adapter (~/.pi/agent/mcp.json) — keep-alive + directTools for causeway
PI_MCP="$HOME/.pi/agent/mcp.json"
if [ -d "$HOME/.pi/agent" ] || [ -f "$PI_MCP" ]; then
register_mcp "$PI_MCP" "$RELAY_INDEX" '{"lifecycle":"keep-alive","directTools":true}'
fi
success "MCP server registered"
# --- Step 5: Migrate existing secrets to Keychain ---
OLD_DATA_DIR="$HOME/.local/share/credential-proxy"
NEW_DATA_DIR="$HOME/Library/Application Support/credential-proxy"
mkdir -p "$NEW_DATA_DIR"
chmod 700 "$NEW_DATA_DIR"
# Only migrate from legacy location if new data dir has no secrets.json yet
if [ -f "$NEW_DATA_DIR/secrets.json" ]; then
info "Existing credentials found — skipping migration"
elif [ -f "$OLD_DATA_DIR/secrets.json" ] && [ -f "$OLD_DATA_DIR/secrets.key" ]; then
info "Migrating encrypted secrets from legacy location..."
node -e "
const fs = require('fs');
const crypto = require('crypto');
const { execFileSync } = require('child_process');
const secretsFile = '$OLD_DATA_DIR/secrets.json';
const keyFile = '$OLD_DATA_DIR/secrets.key';
const store = JSON.parse(fs.readFileSync(secretsFile, 'utf8'));
const masterKey = fs.readFileSync(keyFile, 'utf8').trim();
function decrypt(encryptedStr, key) {
const data = JSON.parse(encryptedStr);
const derivedKey = crypto.scryptSync(key, Buffer.from(data.salt, 'hex'), 32);
const decipher = crypto.createDecipheriv('aes-256-gcm', derivedKey, Buffer.from(data.iv, 'hex'));
decipher.setAuthTag(Buffer.from(data.authTag, 'hex'));
return decipher.update(data.encrypted, 'hex', 'utf8') + decipher.final('utf8');
}
let migrated = 0;
for (const [name, meta] of Object.entries(store.secrets || {})) {
const source = meta.source || { type: 'encrypted', encryptedValue: meta.encryptedValue };
if (source.type !== 'encrypted') continue;
try {
const value = decrypt(source.encryptedValue, masterKey);
// Store in Keychain using security command
try {
execFileSync('security', ['delete-generic-password', '-s', 'com.credential-proxy.secrets', '-a', name], { stdio: 'ignore' });
} catch {}
execFileSync('security', ['add-generic-password', '-s', 'com.credential-proxy.secrets', '-a', name, '-w', value, '-l', 'Credential Proxy: ' + name]);
migrated++;
} catch (e) {
console.error(' Failed to migrate ' + name + ': ' + e.message);
}
}
// Copy metadata (without encrypted values) to new data dir
const metadata = { version: store.version || 2, secrets: {} };
for (const [name, meta] of Object.entries(store.secrets || {})) {
metadata.secrets[name] = {
source: meta.source?.type === '1password' ? meta.source : { type: 'keychain' },
allowedDomains: meta.allowedDomains,
allowedPlacements: meta.allowedPlacements,
allowedCommands: meta.allowedCommands,
createdAt: meta.createdAt,
lastUsed: meta.lastUsed,
usageCount: meta.usageCount
};
}
fs.writeFileSync('$NEW_DATA_DIR/secrets.json', JSON.stringify(metadata, null, 2), { mode: 0o600 });
if (migrated > 0) console.log(' Migrated ' + migrated + ' secret(s) to Keychain');
" 2>&1 && success "Secrets migrated to macOS Keychain" || info "No secrets to migrate"
# Copy logs
if [ -d "$OLD_DATA_DIR/logs" ]; then
cp -Rp "$OLD_DATA_DIR/logs" "$NEW_DATA_DIR/" 2>/dev/null || true
fi
fi
# --- Step 6: Set up Launch Agent ---
LAUNCH_AGENTS="$HOME/Library/LaunchAgents"
PLIST_PATH="$LAUNCH_AGENTS/com.credential-proxy.app.plist"
mkdir -p "$LAUNCH_AGENTS"
cat > "$PLIST_PATH" << PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.credential-proxy.app</string>
<key>ProgramArguments</key>
<array>
<string>open</string>
<string>-a</string>
<string>$APP_PATH</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>
PLIST
success "Launch agent configured (starts at login)"
# --- Step 7: Launch the app ---
info "Indexing for Spotlight..."
mdimport "$APP_PATH" 2>/dev/null || true
info "Launching Credential Proxy..."
open "$APP_PATH"
success "Credential Proxy is running"
echo ""
echo -e "${GREEN}Installation complete!${RESET}"
echo ""
echo " App: $APP_PATH"
echo " Data: $NEW_DATA_DIR"
echo " MCP: Auto-registered on first launch"
echo ""
echo " 1. Look for the key icon in your menu bar"
echo " 2. Set a PIN (first time) or enter your PIN to unlock"
echo " 3. Restart Claude Code to load the MCP server"
echo ""
echo " You can search for \"Credential Proxy\" in Spotlight to launch it."