UNPKG

@brianveltman/sonatype-mcp

Version:

Model Context Protocol server for Sonatype Nexus Repository Manager

163 lines 5.78 kB
import axios from 'axios'; import { handleNexusError, AuthenticationError } from '../utils/errors.js'; function validateFirewallCredentials(config) { return !!(config.firewall?.username && config.firewall?.password); } function createFirewallAuthHeaders(config) { if (!config.firewall?.username || !config.firewall?.password) { return {}; } const credentials = Buffer.from(`${config.firewall.username}:${config.firewall.password}`).toString('base64'); return { 'Authorization': `Basic ${credentials}` }; } export class FirewallClient { client; config; constructor(config) { if (!config.firewall) { throw new AuthenticationError('Firewall configuration is not available. Please provide firewall credentials.'); } this.config = config; const headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' }; if (validateFirewallCredentials(config)) { Object.assign(headers, createFirewallAuthHeaders(config)); } this.client = axios.create({ baseURL: config.firewall.baseUrl, timeout: config.firewall.timeout, headers, validateStatus: () => true, ...(config.firewall.validateSsl === false && { httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }) }) }); this.client.interceptors.request.use((config) => { if (this.config.server?.name) { console.log(`[${this.config.server.name}] Firewall API Request: ${config.method?.toUpperCase()} ${config.url}`); } return config; }, (error) => { console.error('Firewall API Request Error:', error); return Promise.reject(error); }); this.client.interceptors.response.use((response) => { if (this.config.server?.name) { console.log(`[${this.config.server.name}] Firewall API Response: ${response.status} ${response.statusText}`); } return response; }, (error) => { console.error('Firewall API Response Error:', error); return Promise.reject(error); }); } hasCredentials() { return validateFirewallCredentials(this.config); } ensureCredentials() { if (!this.hasCredentials()) { throw new AuthenticationError('Firewall credentials not configured. Please set --firewall-username and --firewall-password arguments or FIREWALL_USERNAME and FIREWALL_PASSWORD environment variables.'); } } async testConnection() { try { const response = await this.client.get('/api/v2/reports/components/quarantined?limit=1'); return response.status < 400; } catch (error) { console.error('Firewall connection test failed:', error); return false; } } async get(endpoint, options = {}) { this.ensureCredentials(); try { const headers = { ...options.headers }; Object.assign(headers, createFirewallAuthHeaders(this.config)); const response = await this.client.get(endpoint, { ...options, headers }); if (response.status >= 400) { throw { response }; } return response.data; } catch (error) { handleNexusError(error); } } async post(endpoint, data, options = {}) { if (this.config.server.readOnly) { throw new AuthenticationError('Write operations are disabled in read-only mode'); } this.ensureCredentials(); try { const response = await this.client.post(endpoint, data, { ...options, headers: { ...createFirewallAuthHeaders(this.config), ...options.headers } }); if (response.status >= 400) { throw { response }; } return response.data; } catch (error) { handleNexusError(error); } } async put(endpoint, data, options = {}) { if (this.config.server.readOnly) { throw new AuthenticationError('Write operations are disabled in read-only mode'); } this.ensureCredentials(); try { const response = await this.client.put(endpoint, data, { ...options, headers: { ...createFirewallAuthHeaders(this.config), ...options.headers } }); if (response.status >= 400) { throw { response }; } return response.data; } catch (error) { handleNexusError(error); } } async delete(endpoint, options = {}) { if (this.config.server.readOnly) { throw new AuthenticationError('Write operations are disabled in read-only mode'); } this.ensureCredentials(); try { const response = await this.client.delete(endpoint, { ...options, headers: { ...createFirewallAuthHeaders(this.config), ...options.headers } }); if (response.status >= 400) { throw { response }; } return response.data; } catch (error) { handleNexusError(error); } } } //# sourceMappingURL=firewall-client.js.map