All files / src/config instances-loader.ts

93.16% Statements 109/117
83.09% Branches 59/71
100% Functions 19/19
92.79% Lines 103/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391                            260x 260x 260x 260x 260x                                               3x   6x 3x 3x                                                   4x 4x               9x       9x 1x     8x       8x 2x 6x 4x     2x 2x 1x   1x       8x                       12x     12x 5x 4x 8x       7x 1x 2x       6x 2x   2x 2x   5x 5x         4x 5x 5x       2x                 32x 32x 2x 2x 1x       1x   1x           260x 77x 77x 77x     77x 9x 9x 9x     8x 10x     8x     10x     8x           1x       1x         68x 12x 12x 12x   11x   22x     11x           1x     1x         56x 43x     43x 43x 1x   43x 1x   43x 1x     43x           43x       43x               13x   13x                               260x         8x 8x 1x   8x 1x   8x 1x     11x           260x 3x           260x 2x                                                                     2x 1x       1x                                                            
/**
 * Instance Configuration Loader
 *
 * Loads GitLab instance configuration from multiple sources:
 * 1. GITLAB_INSTANCES_FILE - Path to YAML/JSON config file
 * 2. GITLAB_INSTANCES - Environment variable (single URL, array, or JSON)
 * 3. GITLAB_API_URL + GITLAB_TOKEN - Legacy single-instance mode
 *
 * Configuration priority (first match wins):
 * 1. GITLAB_INSTANCES_FILE (if set, load from file)
 * 2. GITLAB_INSTANCES (env var - URL, array, or JSON)
 * 3. GITLAB_API_URL (legacy single-instance)
 */
 
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { logInfo, logWarn, logError, logDebug } from '../logger.js';
import {
  GitLabInstanceConfig,
  InstancesConfigFile,
  validateInstancesConfig,
  parseInstanceUrlString,
  applyInstanceDefaults,
} from './instances-schema.js';
 
/**
 * Loaded instances configuration result
 */
export interface LoadedInstancesConfig {
  /** List of configured instances */
  instances: GitLabInstanceConfig[];
  /** Configuration source for logging */
  source: 'file' | 'env' | 'legacy' | 'none';
  /** Source details (file path or env var name) */
  sourceDetails: string;
}
 
/**
 * Load YAML configuration (requires optional yaml package)
 */
async function loadYamlFile(filePath: string): Promise<unknown> {
  try {
    // Dynamic import to avoid requiring yaml as mandatory dependency
    const yaml = await import('yaml');
    const content = fs.readFileSync(filePath, 'utf-8');
    return yaml.parse(content);
  } catch (error) {
    const err = error as NodeJS.ErrnoException & { code?: string; message?: string };
    const code = err.code;
    const message = err.message;
 
    // Check for missing yaml module - Node ESM uses ERR_MODULE_NOT_FOUND
    const isModuleNotFoundCode = code === 'MODULE_NOT_FOUND' || code === 'ERR_MODULE_NOT_FOUND';
    const isYamlNotFoundMessage =
      typeof message === 'string' &&
      (message.includes("Cannot find package 'yaml'") ||
        message.includes("Cannot find module 'yaml'"));
 
    if (isModuleNotFoundCode || isYamlNotFoundMessage) {
      throw new Error(`YAML configuration requires 'yaml' package. Install with: yarn add yaml`, {
        cause: error,
      });
    }
    throw error;
  }
}
 
/**
 * Load JSON configuration
 */
function loadJsonFile(filePath: string): unknown {
  const content = fs.readFileSync(filePath, 'utf-8');
  return JSON.parse(content);
}
 
/**
 * Load configuration from file (YAML or JSON)
 */
async function loadConfigFile(filePath: string): Promise<InstancesConfigFile> {
  // Resolve home directory expansion using os.homedir() for cross-platform support
  const resolvedPath = filePath.startsWith('~')
    ? path.join(os.homedir(), filePath.slice(1))
    : filePath;
 
  if (!fs.existsSync(resolvedPath)) {
    throw new Error(`Configuration file not found: ${resolvedPath}`);
  }
 
  const ext = path.extname(resolvedPath).toLowerCase();
 
  let rawConfig: unknown;
 
  if (ext === '.yaml' || ext === '.yml') {
    rawConfig = await loadYamlFile(resolvedPath);
  } else if (ext === '.json') {
    rawConfig = loadJsonFile(resolvedPath);
  } else {
    // Try to detect format from content
    const content = fs.readFileSync(resolvedPath, 'utf-8').trim();
    if (content.startsWith('{')) {
      rawConfig = JSON.parse(content);
    } else {
      rawConfig = await loadYamlFile(resolvedPath);
    }
  }
 
  return validateInstancesConfig(rawConfig);
}
 
/**
 * Parse GITLAB_INSTANCES environment variable
 * Supports formats:
 * - Single URL: "https://gitlab.com"
 * - Bash array: "(https://gitlab.com https://git.corp.io)"
 * - JSON array: '["https://gitlab.com", "https://git.corp.io"]'
 * - JSON object: '{"instances": [...], "defaults": {...}}', the file format, defaults applied
 */
function parseInstancesEnvVar(value: string): GitLabInstanceConfig[] {
  const trimmed = value.trim();
 
  // Check for JSON object format
  if (trimmed.startsWith('{')) {
    const parsed: unknown = JSON.parse(trimmed);
    const config = validateInstancesConfig(parsed);
    return config.instances.map((inst) => applyInstanceDefaults(inst, config.defaults));
  }
 
  // Check for JSON array format
  if (trimmed.startsWith('[')) {
    const parsed = JSON.parse(trimmed) as string[];
    return parsed.map((url: string) => parseInstanceUrlString(url));
  }
 
  // Check for bash array format: (url1 url2 url3)
  if (trimmed.startsWith('(') && trimmed.endsWith(')')) {
    const inner = trimmed.slice(1, -1).trim();
    // Split by whitespace, handling quoted strings
    const urls: string[] = inner.match(/(?:[^\s"]+|"[^"]*")+/g) ?? [];
    return urls.map((url: string) => {
      // Remove surrounding quotes if present
      const cleanUrl = url.startsWith('"') && url.endsWith('"') ? url.slice(1, -1) : url;
      return parseInstanceUrlString(cleanUrl);
    });
  }
 
  // Check for whitespace-separated URLs (space, tab, newline)
  if (/\s/.test(trimmed)) {
    const urls = trimmed.split(/\s+/).filter((url) => url.length > 0);
    return urls.map((url: string) => parseInstanceUrlString(url));
  }
 
  // Single URL format
  return [parseInstanceUrlString(trimmed)];
}
 
/**
 * The instance with its OAuth secret read from the variable `clientSecretEnv` names, so a
 * configuration can name the secret without containing it. An unset variable leaves a
 * public application.
 */
function withSecretFromEnv(instance: GitLabInstanceConfig): GitLabInstanceConfig {
  const oauth = instance.oauth;
  if (!oauth?.clientSecretEnv || oauth.clientSecret) return instance;
  const clientSecret = process.env[oauth.clientSecretEnv];
  if (!clientSecret) {
    logWarn('OAuth secret variable of an instance is not set', {
      instance: instance.url,
      variable: oauth.clientSecretEnv,
    });
    return instance;
  }
  return { ...instance, oauth: { ...oauth, clientSecret } };
}
 
/**
 * Load instances configuration from all sources
 */
export async function loadInstancesConfig(): Promise<LoadedInstancesConfig> {
  const instancesFile = process.env.GITLAB_INSTANCES_FILE;
  const instancesEnv = process.env.GITLAB_INSTANCES;
  const legacyBaseUrl = process.env.GITLAB_API_URL;
 
  // Priority 1: Configuration file
  if (instancesFile) {
    try {
      logDebug('Loading instances from configuration file', { path: instancesFile });
      const config = await loadConfigFile(instancesFile);
 
      // Apply defaults to all instances
      const instances = config.instances.map((inst) =>
        withSecretFromEnv(applyInstanceDefaults(inst, config.defaults)),
      );
 
      logInfo('Loaded GitLab instances from configuration file', {
        path: instancesFile,
        count: instances.length,
        instances: instances.map((i) => i.label ?? i.url),
      });
 
      return {
        instances,
        source: 'file',
        sourceDetails: instancesFile,
      };
    } catch (error) {
      logError('Failed to load instances configuration file', {
        path: instancesFile,
        err: error instanceof Error ? error : new Error(String(error)),
      });
      throw error;
    }
  }
 
  // Priority 2: Environment variable
  if (instancesEnv) {
    try {
      logDebug('Loading instances from GITLAB_INSTANCES env var');
      const instances = parseInstancesEnvVar(instancesEnv).map(withSecretFromEnv);
 
      logInfo('Loaded GitLab instances from environment variable', {
        count: instances.length,
        instances: instances.map((i) => i.label ?? i.url),
      });
 
      return {
        instances,
        source: 'env',
        sourceDetails: 'GITLAB_INSTANCES',
      };
    } catch (error) {
      logError('Failed to parse GITLAB_INSTANCES environment variable', {
        err: error instanceof Error ? error : new Error(String(error)),
      });
      throw error;
    }
  }
 
  // Priority 3: Legacy single-instance mode
  if (legacyBaseUrl) {
    logDebug('Using legacy GITLAB_API_URL configuration');
 
    // Normalize URL (same logic as config.ts normalizeGitLabBaseUrl)
    let normalizedUrl = legacyBaseUrl;
    if (normalizedUrl.endsWith('/')) {
      normalizedUrl = normalizedUrl.slice(0, -1);
    }
    if (normalizedUrl.endsWith('/api/v4')) {
      normalizedUrl = normalizedUrl.slice(0, -7);
    }
    if (normalizedUrl.endsWith('/api/graphql')) {
      normalizedUrl = normalizedUrl.slice(0, -12);
    }
 
    const instance: GitLabInstanceConfig = {
      url: normalizedUrl,
      label: 'Default Instance',
      insecureSkipVerify: process.env.SKIP_TLS_VERIFY === 'true',
    };
 
    logInfo('Using legacy single-instance configuration', {
      url: normalizedUrl,
    });
 
    return {
      instances: [instance],
      source: 'legacy',
      sourceDetails: 'GITLAB_API_URL',
    };
  }
 
  // No configuration - use default gitlab.com
  logWarn('No GitLab instance configuration found, using gitlab.com as default');
 
  return {
    instances: [
      {
        url: 'https://gitlab.com',
        label: 'GitLab.com',
        insecureSkipVerify: false,
      },
    ],
    source: 'none',
    sourceDetails: 'default',
  };
}
 
/**
 * Get a specific instance configuration by URL
 */
export function getInstanceByUrl(
  instances: GitLabInstanceConfig[],
  url: string,
): GitLabInstanceConfig | undefined {
  // Normalize the search URL
  let normalizedSearch = url;
  if (normalizedSearch.endsWith('/')) {
    normalizedSearch = normalizedSearch.slice(0, -1);
  }
  if (normalizedSearch.endsWith('/api/v4')) {
    normalizedSearch = normalizedSearch.slice(0, -7);
  }
  if (normalizedSearch.endsWith('/api/graphql')) {
    normalizedSearch = normalizedSearch.slice(0, -12);
  }
 
  return instances.find((inst) => inst.url === normalizedSearch);
}
 
/**
 * Check if a URL matches a configured instance
 */
export function isKnownInstance(instances: GitLabInstanceConfig[], url: string): boolean {
  return getInstanceByUrl(instances, url) !== undefined;
}
 
/**
 * Create a sample configuration file content
 */
export function generateSampleConfig(format: 'yaml' | 'json'): string {
  const config: InstancesConfigFile = {
    instances: [
      {
        url: 'https://gitlab.com',
        label: 'GitLab.com',
        insecureSkipVerify: false,
      },
      {
        url: 'https://git.corp.io',
        label: 'Corporate GitLab',
        oauth: {
          clientId: 'your_app_id',
          clientSecret: 'your_secret',
          scopes: 'api read_user',
        },
        rateLimit: {
          maxConcurrent: 50,
          queueSize: 200,
          queueTimeout: 30000,
        },
        insecureSkipVerify: false,
      },
    ],
    defaults: {
      rateLimit: {
        maxConcurrent: 100,
        queueSize: 500,
        queueTimeout: 60000,
      },
      oauth: {
        scopes: 'api read_user',
      },
    },
  };
 
  if (format === 'json') {
    return JSON.stringify(config, null, 2);
  }
 
  // YAML format - manual generation to preserve comments
  return `# GitLab MCP Instances Configuration
# Documentation: https://gitlab-mcp.sw.foundation/advanced/multi-instance
 
instances:
  # Minimal configuration (OAuth disabled or uses global credentials)
  - url: https://gitlab.com
    label: "GitLab.com"
 
  # Full configuration with OAuth
  - url: https://git.corp.io
    label: "Corporate GitLab"
    oauth:
      clientId: "your_app_id"
      clientSecret: "your_secret"  # Only for confidential apps
      scopes: "api read_user"      # Optional, default: api read_user
    rateLimit:
      maxConcurrent: 50            # Max parallel requests
      queueSize: 200               # Max queued requests
      queueTimeout: 30000          # Queue wait timeout (ms)
 
# Global defaults (applied to all instances unless overridden)
defaults:
  rateLimit:
    maxConcurrent: 100
    queueSize: 500
    queueTimeout: 60000
  oauth:
    scopes: "api read_user"
`;
}