getMTLSConfig function

MTLSConfig? getMTLSConfig()

Get mTLS configuration from environment variables.

Implementation

MTLSConfig? getMTLSConfig() {
  if (_mtlsConfigCacheInitialized) return _mtlsConfigCache;
  _mtlsConfigCacheInitialized = true;

  String? cert;
  String? key;
  String? passphrase;

  // Client certificate
  final certPath = Platform.environment['MAGE_CLIENT_CERT'];
  if (certPath != null && certPath.isNotEmpty) {
    try {
      cert = File(certPath).readAsStringSync();
    } catch (error) {
      stderr.writeln('mTLS: Failed to load client certificate: $error');
    }
  }

  // Client key
  final keyPath = Platform.environment['MAGE_CLIENT_KEY'];
  if (keyPath != null && keyPath.isNotEmpty) {
    try {
      key = File(keyPath).readAsStringSync();
    } catch (error) {
      stderr.writeln('mTLS: Failed to load client key: $error');
    }
  }

  // Key passphrase
  final passphraseVal = Platform.environment['MAGE_CLIENT_KEY_PASSPHRASE'];
  if (passphraseVal != null && passphraseVal.isNotEmpty) {
    passphrase = passphraseVal;
  }

  if (cert == null && key == null && passphrase == null) {
    _mtlsConfigCache = null;
    return null;
  }

  _mtlsConfigCache = MTLSConfig(cert: cert, key: key, passphrase: passphrase);
  return _mtlsConfigCache;
}