All files / commands apikeys.js

0% Statements 0/153
0% Branches 0/74
0% Functions 0/9
0% Lines 0/150

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * API Keys Command - API key management
 */
 
const { Command } = require('commander');
const chalk = require('chalk');
const ora = require('ora');
const inquirer = require('inquirer');
const { table } = require('table');
const logger = require('../utils/logger');
const APIClient = require('../services/api-client');
 
const apiKeysCommand = new Command('api-keys')
  .description('šŸ”‘ Manage API keys for integrations');
 
// List API keys
apiKeysCommand
  .command('list')
  .description('List all API keys')
  .option('--show-keys', 'show actual key values (use with caution)')
  .action(async (options) => {
    const spinner = ora('Fetching API keys...').start();
 
    try {
      logger.command('api-keys list', { showKeys: options.showKeys }, true);
      logger.security('API keys list accessed', { show_keys: !!options.showKeys });
 
      const apiClient = new APIClient();
      const response = await apiClient.get('/api/v1/api-keys');
 
      spinner.succeed('API keys retrieved');
 
      const keys = response.data.api_keys || [];
 
      console.log(chalk.bold.blue('\nšŸ”‘ API Keys\n'));
 
      if (keys.length === 0) {
        console.log(chalk.gray('No API keys found'));
        console.log(chalk.blue('Create one with: vaultace api-keys create'));
        return;
      }
 
      const keyData = [
        ['Name', 'Key', 'Permissions', 'Created', 'Last Used', 'Status']
      ];
 
      keys.forEach(key => {
        const keyValue = options.showKeys ? key.key : `${key.key.substring(0, 8)}...`;
        const status = key.is_active ? '🟢 Active' : 'šŸ”“ Inactive';
 
        keyData.push([
          key.name,
          keyValue,
          key.permissions.join(', '),
          new Date(key.created_at).toLocaleDateString(),
          key.last_used ? new Date(key.last_used).toLocaleDateString() : 'Never',
          status
        ]);
      });
 
      console.log(table(keyData));
 
      if (options.showKeys) {
        console.log(chalk.red('\nāš ļø  API keys are sensitive - keep them secure!'));
      }
 
      console.log(chalk.gray(`\nTotal: ${keys.length} API keys`));
 
    } catch (error) {
      spinner.fail('Failed to fetch API keys');
      logger.error('API keys list error', { error: error.message });
 
      if (error.response?.status === 401) {
        console.log(chalk.yellow('\nAuthentication required. Run: vaultace auth login'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Create new API key
apiKeysCommand
  .command('create')
  .description('Create new API key')
  .argument('[name]', 'API key name')
  .option('--permissions <perms...>', 'key permissions (scan|fix|read|write)', ['scan', 'read'])
  .option('--expires <days>', 'expiration in days (0 for no expiration)', '0')
  .action(async (name, options) => {
    try {
      logger.command('api-keys create', { name, permissions: options.permissions }, true);
 
      let keyName = name;
      if (!keyName) {
        const { inputName } = await inquirer.prompt([
          {
            type: 'input',
            name: 'inputName',
            message: 'API key name:',
            validate: (input) => input.length > 0 || 'Name is required'
          }
        ]);
        keyName = inputName;
      }
 
      const availablePermissions = ['scan', 'fix', 'read', 'write', 'admin'];
      const { selectedPermissions } = await inquirer.prompt([
        {
          type: 'checkbox',
          name: 'selectedPermissions',
          message: 'Select permissions:',
          choices: availablePermissions,
          default: options.permissions
        }
      ]);
 
      const { confirm } = await inquirer.prompt([
        {
          type: 'confirm',
          name: 'confirm',
          message: `Create API key "${keyName}" with permissions: ${selectedPermissions.join(', ')}?`,
          default: true
        }
      ]);
 
      if (!confirm) {
        console.log(chalk.yellow('API key creation cancelled'));
        return;
      }
 
      const spinner = ora('Creating API key...').start();
 
      const apiClient = new APIClient();
      const response = await apiClient.post('/api/v1/api-keys', {
        name: keyName,
        permissions: selectedPermissions,
        expires_in_days: parseInt(options.expires) || null
      });
 
      spinner.succeed('API key created successfully');
      logger.security('API key created', { name: keyName, permissions: selectedPermissions });
 
      console.log(chalk.green('\nāœ… API Key Created'));
      console.log(chalk.bold('Name:'), keyName);
      console.log(chalk.bold('Key:'), chalk.yellow(response.data.api_key));
      console.log(chalk.bold('Permissions:'), selectedPermissions.join(', '));
 
      if (response.data.expires_at) {
        console.log(chalk.bold('Expires:'), new Date(response.data.expires_at).toLocaleDateString());
      }
 
      console.log(chalk.red('\nāš ļø  Save this key securely - it won\'t be shown again!'));
 
    } catch (error) {
      if (error.spinner) error.spinner.fail('Failed to create API key');
      logger.error('API key create error', { error: error.message, name });
      console.error(chalk.red(`Error: ${error.message}`));
      process.exit(1);
    }
  });
 
// Revoke API key
apiKeysCommand
  .command('revoke')
  .description('Revoke API key')
  .argument('<key-id>', 'API key ID or name')
  .option('--force', 'skip confirmation prompt')
  .action(async (keyId, options) => {
    try {
      logger.command('api-keys revoke', { keyId }, true);
 
      if (!options.force) {
        const { confirm } = await inquirer.prompt([
          {
            type: 'confirm',
            name: 'confirm',
            message: `Revoke API key "${keyId}"? This cannot be undone.`,
            default: false
          }
        ]);
 
        if (!confirm) {
          console.log(chalk.yellow('Revocation cancelled'));
          return;
        }
      }
 
      const spinner = ora('Revoking API key...').start();
 
      const apiClient = new APIClient();
      await apiClient.delete(`/api/v1/api-keys/${keyId}`);
 
      spinner.succeed('API key revoked');
      logger.security('API key revoked', { key_id: keyId });
 
      console.log(chalk.green(`\nāœ… API key "${keyId}" revoked`));
 
    } catch (error) {
      if (error.spinner) error.spinner.fail('Failed to revoke API key');
      logger.error('API key revoke error', { error: error.message, keyId });
 
      if (error.response?.status === 404) {
        console.error(chalk.red('API key not found'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Update API key
apiKeysCommand
  .command('update')
  .description('Update API key permissions or name')
  .argument('<key-id>', 'API key ID or name')
  .option('--name <name>', 'new name for the key')
  .option('--permissions <perms...>', 'new permissions')
  .option('--activate', 'activate the key')
  .option('--deactivate', 'deactivate the key')
  .action(async (keyId, options) => {
    try {
      logger.command('api-keys update', { keyId, options }, true);
 
      const updateData = {};
 
      if (options.name) {
        updateData.name = options.name;
      }
 
      if (options.permissions) {
        updateData.permissions = options.permissions;
      }
 
      if (options.activate) {
        updateData.is_active = true;
      } else if (options.deactivate) {
        updateData.is_active = false;
      }
 
      if (Object.keys(updateData).length === 0) {
        console.log(chalk.yellow('No updates specified'));
        console.log('Use --name, --permissions, --activate, or --deactivate');
        return;
      }
 
      const { confirm } = await inquirer.prompt([
        {
          type: 'confirm',
          name: 'confirm',
          message: `Update API key "${keyId}"?`,
          default: true
        }
      ]);
 
      if (!confirm) {
        console.log(chalk.yellow('Update cancelled'));
        return;
      }
 
      const spinner = ora('Updating API key...').start();
 
      const apiClient = new APIClient();
      await apiClient.patch(`/api/v1/api-keys/${keyId}`, updateData);
 
      spinner.succeed('API key updated');
      logger.security('API key updated', { key_id: keyId, updates: Object.keys(updateData) });
 
      console.log(chalk.green(`\nāœ… API key "${keyId}" updated`));
 
    } catch (error) {
      if (error.spinner) error.spinner.fail('Failed to update API key');
      logger.error('API key update error', { error: error.message, keyId });
 
      if (error.response?.status === 404) {
        console.error(chalk.red('API key not found'));
      } else {
        console.error(chalk.red(`Error: ${error.message}`));
      }
      process.exit(1);
    }
  });
 
// Show API key usage
apiKeysCommand
  .command('usage')
  .description('Show API key usage statistics')
  .argument('[key-id]', 'API key ID (shows all if not specified)')
  .option('--days <number>', 'number of days to show', '30')
  .action(async (keyId, options) => {
    const spinner = ora('Fetching usage statistics...').start();
 
    try {
      logger.command('api-keys usage', { keyId, days: options.days }, true);
 
      const apiClient = new APIClient();
      const endpoint = keyId ? `/api/v1/api-keys/${keyId}/usage` : '/api/v1/api-keys/usage';
 
      const response = await apiClient.get(endpoint, {
        params: { days: parseInt(options.days) }
      });
 
      spinner.succeed('Usage statistics retrieved');
 
      const usage = response.data;
 
      console.log(chalk.bold.cyan('\nšŸ“Š API Key Usage\n'));
 
      if (keyId) {
        // Single key usage
        console.log(chalk.bold('Key:'), usage.key_name);
        console.log(chalk.bold('Total Requests:'), usage.total_requests || 0);
        console.log(chalk.bold('Successful:'), usage.successful_requests || 0);
        console.log(chalk.bold('Failed:'), usage.failed_requests || 0);
        console.log(chalk.bold('Rate Limited:'), usage.rate_limited || 0);
        console.log(chalk.bold('Last Used:'), usage.last_used || 'Never');
 
        if (usage.daily_usage && usage.daily_usage.length > 0) {
          console.log(chalk.bold('\nšŸ“ˆ Daily Usage:'));
          usage.daily_usage.slice(-7).forEach(day => {
            console.log(`  ${day.date}: ${day.requests} requests`);
          });
        }
      } else {
        // All keys summary
        const usageData = [
          ['Key Name', 'Requests', 'Success Rate', 'Last Used']
        ];
 
        usage.keys.forEach(key => {
          const successRate = key.total_requests > 0 ?
            Math.round((key.successful_requests / key.total_requests) * 100) + '%' :
            'N/A';
 
          usageData.push([
            key.name,
            key.total_requests || 0,
            successRate,
            key.last_used || 'Never'
          ]);
        });
 
        console.log(table(usageData));
      }
 
    } catch (error) {
      spinner.fail('Failed to fetch usage statistics');
      logger.error('API key usage error', { error: error.message, keyId });
      console.error(chalk.red(`Error: ${error.message}`));
      process.exit(1);
    }
  });
 
module.exports = apiKeysCommand;