zitinexus-router-script/Router-enrollment-script/ROUTER_ENROLLMENT_SCRIPT_FI...

6.3 KiB

Router Enrollment Script Fixes Summary

Issue Description

The router enrollment script was failing with two main errors:

  1. HTTP 404 Error: API endpoint not found
  2. Unbound Variable Error: CALLBACK_URL: unbound variable

Root Cause Analysis

1. HTTP 404 Error

  • Problem: Script was calling /router/register instead of /api/router/register
  • Cause: Missing /api prefix in URL construction
  • Impact: API calls were hitting non-existent endpoints

2. Unbound Variable Error

  • Problem: Variables were not initialized before use
  • Cause: When API calls failed, variables were never set but cleanup functions tried to use them
  • Impact: Script crashed with "unbound variable" errors

Fixes Implemented

1. API Endpoint URL Fix

File: Router-enrollment-script/enroll-router.sh Line: ~208

Before:

local api_url="${API_ENDPOINT}/router/register"

After:

local api_url="${API_ENDPOINT}/api/router/register"

Impact: Now correctly calls the backend API endpoint that matches the route structure:

  • Backend route: app.use('/api/router', require('./routes/routerRegistration'))
  • Router registration route: router.post('/register', ...)
  • Final endpoint: /api/router/register

2. Default API Endpoint Update

File: Router-enrollment-script/enroll-router.sh Line: ~25

Before:

DEFAULT_API_ENDPOINT="https://your-zitinexus-portal.com/api"

After:

DEFAULT_API_ENDPOINT="https://backend.zitinexus.com"

Impact: Matches the nginx configuration that proxies backend.zitinexus.com to localhost:5000

3. Variable Initialization Fix

File: Router-enrollment-script/enroll-router.sh Lines: ~26-36

Added:

# Initialize variables to prevent unbound variable errors
CALLBACK_URL=""
JWT=""
ROUTER_YAML=""
ROUTER_NAME=""
ROUTER_ID=""
TENANT_ID=""
CONTROLLER_ENDPOINT=""
ROLE_ATTRIBUTES=""
HASH_KEY=""
API_ENDPOINT=""

Impact: Prevents "unbound variable" errors when script fails early

4. Debug Logging Enhancement

File: Router-enrollment-script/enroll-router.sh Line: ~213

Added:

# Debug: Show the URL being called
log "INFO" "API URL: $api_url"

Impact: Makes troubleshooting easier by showing the exact URL being called

Network Architecture Understanding

Nginx Configuration

server {
    listen 443 ssl;
    server_name backend.zitinexus.com;
    
    location / {
        proxy_pass http://localhost:5000;
        # Direct proxy - no path modification
    }
}

Backend Route Structure

// app.js
app.use('/api/router', require('./routes/routerRegistration'));

// routerRegistration.js
router.post('/register', async (req, res) => {
    await controller.registerRouter(req, res);
});

Complete API Flow

  1. Script calls: https://backend.zitinexus.com/api/router/register
  2. Nginx proxies to: http://localhost:5000/api/router/register
  3. Backend routes to: routerRegistration.js/register handler
  4. Controller processes: Router registration with hash key

Expected API Response Structure

The script expects this JSON response format:

{
  "success": true,
  "data": {
    "jwt": "eyJhbGciOiJSUzI1NiIs...",
    "routerConfig": {
      "yaml": "v: 3\nidentity:\n  cert: ...",
      "filename": "router.yaml",
      "type": "private-edge"
    },
    "routerInfo": {
      "id": "QNmKPk3Xgc",
      "name": "virtech5378_sg-router1",
      "roleAttributes": ["virtech5378_simplesrouter"],
      "enrollmentExpiresAt": "2025-06-09T09:40:09.485Z",
      "type": "private-edge"
    },
    "callbackUrl": "http://192.168.50.253:5000/api/router/enrollment-status",
    "metadata": {
      "tenantId": "cmb9m1hns0003p3jkk43zjlss",
      "zitiRouterId": "QNmKPk3Xgc",
      "routerType": "private-edge",
      "controllerEndpoint": "enroll.zitinexus.com:443"
    }
  }
}

Testing

Test Script Created

File: Router-enrollment-script/test-api-fix.sh

This script tests:

  • API URL construction
  • Endpoint connectivity
  • HTTP response codes
  • Error handling

Usage

# On Linux (where the script will actually run):
chmod +x Router-enrollment-script/test-api-fix.sh
./Router-enrollment-script/test-api-fix.sh

# Main script usage:
sudo ./Router-enrollment-script/enroll-router.sh

Verification Steps

  1. API Endpoint Test: The test script verifies the endpoint exists
  2. Variable Safety: All variables are now initialized to prevent unbound errors
  3. Debug Output: Script shows the exact URL being called for troubleshooting
  4. Error Handling: Improved error messages for better debugging

Expected Results

Before Fix

[ERROR] API request failed with HTTP 404: Unknown error
[ERROR] Script failed with exit code 1
./enroll.sh: line 576: CALLBACK_URL: unbound variable

After Fix

[INFO] API URL: https://backend.zitinexus.com/api/router/register
[INFO] Registering router with ZitiNexus Portal...
[SUCCESS] Router registered successfully: virtech5378_sg-router1 (ID: QNmKPk3Xgc)

Files Modified

  1. Router-enrollment-script/enroll-router.sh

    • Fixed API URL construction
    • Updated default endpoint
    • Added variable initialization
    • Enhanced debug logging
  2. Router-enrollment-script/test-api-fix.sh (New)

    • Test script to verify fixes
    • API connectivity testing
    • Error code validation

Compatibility

  • Linux: Full compatibility (target environment)
  • Windows: Script development and testing environment
  • Backend: Compatible with existing nginx and Express.js setup
  • API: Matches existing backend route structure

Next Steps

  1. Test the script on a Linux environment with the hash key: c3d00e5615464e0c02a7dcfcd56abc4e
  2. Verify the backend is running and accessible at https://backend.zitinexus.com
  3. Ensure the router registration endpoint is properly configured
  4. Monitor the enrollment process for any additional issues

Summary

Fixed: HTTP 404 error by adding missing /api prefix
Fixed: Unbound variable error by initializing all variables
Enhanced: Debug logging for better troubleshooting
Updated: Default endpoint to match production configuration
Created: Test script for verification

The router enrollment script should now work correctly with the ZitiNexus backend API.