setup-test-env.ts•4.39 kB
#!/usr/bin/env tsx
/**
* Setup script for test environment
* Helps users configure and verify their test environment
*/
import * as fs from 'fs';
import * as readline from 'readline';
import { config } from 'dotenv';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const prompt = (question: string): Promise<string> => {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
};
async function setupTestEnvironment() {
console.log('🔧 OPNsense MCP Test Environment Setup');
console.log('=======================================\n');
// Check if .env exists
const envPath = '.env';
const envExists = fs.existsSync(envPath);
if (envExists) {
console.log('✅ .env file found');
config();
// Check required variables
const required = ['OPNSENSE_HOST', 'OPNSENSE_API_KEY', 'OPNSENSE_API_SECRET'];
const missing = required.filter(key => !process.env[key]);
if (missing.length === 0) {
console.log('✅ All required environment variables are set\n');
console.log('Current configuration:');
console.log(` Host: ${process.env.OPNSENSE_HOST}`);
console.log(` API Key: ${process.env.OPNSENSE_API_KEY?.substring(0, 8)}...`);
console.log(` DMZ Interface: ${process.env.DMZ_INTERFACE || 'igc3_vlan6 (default)'}\n`);
const reconfigure = await prompt('Would you like to reconfigure? (y/N): ');
if (reconfigure.toLowerCase() !== 'y') {
console.log('\n✅ Environment ready for testing!');
console.log('Run: npm run test:firewall');
rl.close();
return;
}
} else {
console.log(`⚠️ Missing environment variables: ${missing.join(', ')}\n`);
}
} else {
console.log('❌ .env file not found\n');
}
// Interactive setup
console.log('Let\'s set up your test environment:\n');
const host = await prompt('OPNsense Host (e.g., https://192.168.1.1): ');
const apiKey = await prompt('API Key: ');
const apiSecret = await prompt('API Secret: ');
const dmzInterface = await prompt('DMZ Interface (default: igc3_vlan6): ') || 'igc3_vlan6';
const verifySsl = await prompt('Verify SSL? (y/N): ');
// Create .env content
const envContent = `# OPNsense MCP Test Configuration
# Generated by setup-test-env.ts
# OPNsense API Configuration
OPNSENSE_HOST=${host}
OPNSENSE_API_KEY=${apiKey}
OPNSENSE_API_SECRET=${apiSecret}
OPNSENSE_VERIFY_SSL=${verifySsl.toLowerCase() === 'y' ? 'true' : 'false'}
# Network Configuration
DMZ_INTERFACE=${dmzInterface}
# Debug Settings
MCP_DEBUG=true
DEBUG_FIREWALL=true
# Cache Configuration (optional)
ENABLE_CACHE=false
# Database Configuration (optional)
# Uncomment and configure if using PostgreSQL
# POSTGRES_HOST=localhost
# POSTGRES_PORT=5432
# POSTGRES_DB=opnsense_mcp
# POSTGRES_USER=mcp_user
# POSTGRES_PASSWORD=your-password
`;
// Write .env file
fs.writeFileSync(envPath, envContent);
console.log('\n✅ .env file created successfully!');
// Test connection
console.log('\n🔍 Testing connection...');
try {
const { OPNSenseAPIClient } = await import('../src/api/client.js');
const client = new OPNSenseAPIClient({
host,
apiKey,
apiSecret,
verifySsl: verifySsl.toLowerCase() === 'y'
});
// Try to get system info
const response = await client.get('/core/system/status');
if (response) {
console.log('✅ Connection successful!');
console.log(` System: ${response.system || 'OPNsense'}`);
console.log(` Version: ${response.version_data?.product_version || 'Unknown'}`);
}
} catch (error: any) {
console.log('❌ Connection failed:', error?.message || error);
console.log('\nPlease check:');
console.log('1. Host is reachable');
console.log('2. API credentials are correct');
console.log('3. API user has firewall permissions');
}
console.log('\n📋 Next Steps:');
console.log('1. Run the firewall test suite: npm run test:firewall');
console.log('2. Validate NFS connectivity: npm run test:nfs');
console.log('3. Debug persistence issues: tsx debug-firewall-persistence.ts');
rl.close();
}
// Run setup
setupTestEnvironment().catch(error => {
console.error('Setup failed:', error);
rl.close();
process.exit(1);
});