# Proxy Setup for Snowflake RG Barry

This document describes how to configure and use the SSH proxy to access Snowflake RG Barry from your local Laravel application.

## 📋 Requirements

1. **Google Cloud CLI** installed: https://cloud.google.com/sdk/docs/install
2. **Access to staging server** (request from william.beltran)
3. **NPM HPTS** installed: `npm install -g http-proxy-to-socks`

## 🔧 Configuration in `.env`

Add these variables to your local `.env` file:

```bash
# ========================================
# Proxy Configuration (Snowflake RG Barry)
# ========================================
PROXY_ENABLED=false
PROXY_TYPE=http
PROXY_HOST=127.0.0.1
PROXY_PORT=8080

# Alternatively, if using SOCKS5 directly:
# PROXY_TYPE=socks5
# PROXY_PORT=5001
```

## 🚀 How to Use

### Option 1: HTTP Proxy (Recommended)

#### 1. Start the local HTTP-to-SOCKS proxy:
```bash
hpts -s 127.0.0.1:5001 -p 8080
```

#### 2. In another terminal, create the SSH tunnel:
```bash
gcloud compute ssh william.beltran@suitex --ssh-flag="-D:5001" --ssh-flag="-N" --ssh-flag="-n"
```

#### 3. Enable proxy in your `.env`:
```bash
PROXY_ENABLED=true
PROXY_TYPE=http
PROXY_HOST=127.0.0.1
PROXY_PORT=8080
```

#### 4. Restart your application (if using `php artisan serve`):
```bash
# Ctrl+C to stop and then restart
php artisan serve
```

### Option 2: Direct SOCKS5

#### 1. Create only the SSH tunnel:
```bash
gcloud compute ssh william.beltran@suitex --ssh-flag="-D:5001" --ssh-flag="-N" --ssh-flag="-n"
```

#### 2. Configure SOCKS5 in your `.env`:
```bash
PROXY_ENABLED=true
PROXY_TYPE=socks5
PROXY_HOST=127.0.0.1
PROXY_PORT=5001
```

#### 3. Restart your application.

## ⚙️ Available Configurations

| Variable | Values | Description |
|----------|--------|-------------|
| `PROXY_ENABLED` | `true` / `false` | Enable or disable proxy |
| `PROXY_TYPE` | `http` / `socks5` / `socks5h` | Proxy type |
| `PROXY_HOST` | IP or hostname | Proxy host (default: `127.0.0.1`) |
| `PROXY_PORT` | Port | Proxy port (default: `8080` for HTTP, `5001` for SOCKS5) |

**Note about `socks5h`:** The 'h' suffix means DNS resolution is done through the proxy (useful for internal domains).

## 🔍 Verification

When the proxy is enabled, you'll see logs like this in `storage/logs/laravel.log`:

```
[DEBUG] 🌐 Proxy enabled
{
  "proxy": "127.0.0.1:8080",
  "type": "http",
  "url": "https://IEDEHNN-YB65632.snowflakecomputing.com/api/v2/statements"
}
```

## 🔄 Switching Between Environments

### For development with Snowflake RG Barry:
```bash
# In .env
PROXY_ENABLED=true
```

### For normal development (other connectors):
```bash
# In .env
PROXY_ENABLED=false
```

**You don't need to change anything else**, just this variable.

## 🐛 Troubleshooting

### Error: "Failed to connect to 127.0.0.1 port 8080"
- **Cause:** Local proxy is not running.
- **Solution:** Start `hpts` with the command from step 1.

### Error: "Failed to connect to 127.0.0.1 port 5001"
- **Cause:** SSH tunnel is not active.
- **Solution:** Create the SSH tunnel with the command from step 2.

### Requests don't work with proxy
- **Verify** that both processes (`hpts` and `gcloud ssh`) are running.
- **Check** logs in `storage/logs/laravel.log` to confirm proxy is being used.
- **Test** with direct `curl` command from terminal with same proxy options.

### .env changes are not applied
- **Solution:** Clear configuration cache:
  ```bash
  php artisan config:clear
  ```

### Script says hpts is not installed but it works in terminal
- **Cause:** You're running the script with `sudo` or `sh` instead of `bash`.
- **Solution:**
  ```bash
  # ❌ DON'T do this
  sudo sh start-proxy.sh

  # ✅ Do this
  chmod +x start-proxy.sh
  ./start-proxy.sh

  # Or this
  bash start-proxy.sh
  ```
- **Explanation:** `npm` installs global packages in your user directory, not in root. When using `sudo`, the script looks in root paths where `hpts` is not installed.

## 📝 Helper Script (Optional)

You can create a bash script to facilitate proxy startup:

```bash
#!/bin/bash
# start-proxy.sh

echo "🚀 Starting proxy for Snowflake RG Barry..."

# Start HPTS in background
echo "📡 Starting HTTP-to-SOCKS proxy..."
hpts -s 127.0.0.1:5001 -p 8080 &
HPTS_PID=$!

# Wait 2 seconds
sleep 2

# Start SSH tunnel
echo "🔐 Starting SSH tunnel..."
gcloud compute ssh william.beltran@suitex --ssh-flag="-D:5001" --ssh-flag="-N" --ssh-flag="-n"

# When interrupted (Ctrl+C), kill HPTS
trap "kill $HPTS_PID" EXIT
```

Usage:
```bash
# Make script executable (once only)
chmod +x start-proxy.sh

# Run script (always)
./start-proxy.sh
```

**⚠️ IMPORTANT:**
- ❌ DO NOT use `sudo sh start-proxy.sh` - will cause PATH errors
- ❌ DO NOT use `sh start-proxy.sh` - script requires bash
- ✅ USE `./start-proxy.sh` or `bash start-proxy.sh`

## 🔐 Snowflake RG Barry Account Details

- **Account Locator:** WA49014
- **Organization ID:** yb65632
- **Username:** netsuite_user

### SANDBOX
- **Database:** DW
- **Schema:** NETSUITE
- **Warehouse:** XS
- **Tables:**
  - `IPAAS_CUSTOMERS_SANDBOX_VW`
  - `IPAAS_VENDORS_SANDBOX_VW`

### PRODUCTION
- **Database:** DW
- **Schema:** NETSUITE
- **Warehouse:** XS
- **Tables:**
  - `IPAAS_CUSTOMERS_RAW_VW`
  - `IPAAS_VENDORS_RAW_VW`

## ⚠️ Important

- **Remember to disable the proxy** (`PROXY_ENABLED=false`) when working with other clients/connectors that don't require the tunnel.
- The SSH tunnel must be active while using the proxy.
- Snowflake JWT tokens expire in 1 hour, regenerate as needed.
