PKI Sync Client
A secure client utility for synchronizing secrets from a PKI (Public Key Infrastructure) server with support for Trusted Execution Environments (TEE) including Intel TDX and AMD SEV-SNP.
Features
- Secure Secret Synchronization: Fetch and save secrets from PKI servers with mutual TLS authentication
- TEE Support: Automatic detection and support for Intel TDX and AMD SEV-SNP environments
- High Availability: Automatic failover across multiple PKI servers
- Certificate Management: Optionally save client certificates for external use
- Flexible Configuration: YAML-based configuration for secrets and server endpoints
- CLI & Programmatic API: Use as a command-line tool or integrate into your application
Installation
As a CLI Tool
npm install -g @super-protocol/pki-sync-client
As a Library
npm install @super-protocol/pki-sync-client
Configuration
Secrets Configuration File
Create a YAML file (e.g., secrets.yaml) that defines which secrets to sync and where to save them:
secrets:
- secretName: database-password
saveTo: /etc/app/db-password.txt
- secretName: api-key
saveTo: /var/secrets/api-key.txt
- secretName: tls-certificate
saveTo: /etc/app/certs/server.crt
Schema:
secretName (string, required): Name of the secret on the PKI server
saveTo (string, required): Absolute path where the secret should be saved
Swarm Environment File
Create a YAML file (e.g., swarm-env.yaml) that defines PKI server connection details:
pki_authority:
networkID: my-network-id
caBundle: |
-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAKL0UG+mRkSvMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
... (certificate content) ...
-----END CERTIFICATE-----
servers:
- pki-server-1.example.com
- pki-server-2.example.com
Schema:
pki_authority.networkID (string, required): Network identifier for PKI authentication
pki_authority.caBundle (string, required): PEM-encoded CA certificate bundle for server verification
pki_authority.servers (array of strings, required): List of PKI server addresses (with automatic failover)
pki-sync-client uses only certificates from pki_authority.caBundle as
trusted CAs. System root certificates are never added to the TLS trust store.
CLI Usage
Basic Sync Command
pki-sync-client sync --config secrets.yaml --swarm-env swarm-env.yaml
Complete Options
pki-sync-client sync [options]
Options
| Option |
Alias |
Type |
Default |
Description |
--config |
-c |
string |
required |
Path to secrets configuration file (YAML) |
--swarm-env |
|
string |
/sp/swarm/swarm-env.yaml |
Path to swarm environment config (YAML) |
--own-challenge |
|
string |
auto |
Authentication challenge type: auto, tdx, sev-snp, or untrusted |
--log-level |
-l |
string |
|
Log level: fatal, error, warn, info, debug, trace |
--retry-attempts |
|
number |
3 |
Number of retry attempts per server for retryable network/DNS errors |
--retry-timeout |
|
number |
10 |
Timeout between retries in seconds for retryable network/DNS errors |
--disable-server-identity-check |
|
boolean |
false |
Disable server identity verification (not recommended) |
--network-type-file |
|
string |
|
Path to file containing local network type (trusted or untrusted). Defaults to untrusted when omitted |
--save-certs-to-dir |
|
string |
|
Directory to save client certificates (key.pem, ca.pem, cert.pem) |
--save-certs-prefix |
|
string |
|
Prefix for certificate filenames (e.g., client → client_key.pem) |
By default, if --log-level is not provided, SyncClient runs with logging disabled.
Examples
Simple sync (no logs):
pki-sync-client sync -c secrets.yaml
Simple sync with explicit log level:
pki-sync-client sync -c secrets.yaml --log-level info
Sync with custom swarm environment:
pki-sync-client sync -c secrets.yaml --swarm-env /custom/path/swarm-env.yaml
Sync with custom retry policy:
pki-sync-client sync -c secrets.yaml --retry-attempts 5 --retry-timeout 3
Sync with explicit local network type file:
pki-sync-client sync -c secrets.yaml --network-type-file /sp/swarm/network-type
Sync and save client certificates:
pki-sync-client sync -c secrets.yaml --save-certs-to-dir /etc/app/certs
Sync with certificate prefix:
pki-sync-client sync -c secrets.yaml \
--save-certs-to-dir /etc/app/certs \
--save-certs-prefix myapp
# Creates: myapp_key.pem, myapp_ca.pem, myapp_cert.pem
Force specific challenge type:
pki-sync-client sync -c secrets.yaml --own-challenge tdx
Development/testing (untrusted mode):
pki-sync-client sync -c secrets.yaml --own-challenge untrusted
Programmatic API
Basic Usage
import { SyncClient, parseConfig, parseSwarmEnv } from '@super-protocol/pki-sync-client';
async function syncSecrets() {
// Parse configuration files
const config = parseConfig('./secrets.yaml');
const swarmEnv = parseSwarmEnv('./swarm-env.yaml');
// Create sync client
const client = new SyncClient({
servers: swarmEnv.pki_authority.servers,
networkID: swarmEnv.pki_authority.networkID,
caBundle: swarmEnv.pki_authority.caBundle,
ownChallenge: 'auto',
logLevel: 'info',
});
// Perform sync
await client.sync(config);
console.log('Secrets synced successfully');
}
syncSecrets().catch(console.error);
Advanced Usage with Certificate Saving
import { SyncClient, SyncConfig } from '@super-protocol/pki-sync-client';
const client = new SyncClient({
servers: ['pki1.example.com', 'pki2.example.com'],
networkID: 'my-network',
caBundle: fs.readFileSync('./ca-bundle.pem', 'utf-8'),
ownChallenge: 'tdx',
logLevel: 'debug',
disableServerIdentityCheck: false,
saveCertsToDir: '/etc/app/certs',
saveCertsPrefix: 'myapp',
});
const config: SyncConfig = {
secrets: [
{ secretName: 'db-password', saveTo: '/etc/app/db-password' },
{ secretName: 'api-key', saveTo: '/etc/app/api-key' },
],
};
await client.sync(config);
API Reference
SyncClient
Constructor Options:
interface SyncClientOptions {
servers: string[]; // List of PKI server addresses
networkID: string; // Network identifier
caBundle: string; // PEM-encoded CA certificate bundle
ownChallenge: string; // Challenge type: 'auto', 'tdx', 'sev-snp', 'untrusted'
retryAttempts?: number; // Retry attempts per server (default: 3)
retryDelaySec?: number; // Delay between retries in seconds (default: 10)
logLevel?: LogLevel; // Log level: fatal | error | warn | info | debug | trace; if omitted logs are disabled
disableServerIdentityCheck?: boolean; // Disable server identity verification (default: false)
networkTypeFile?: string; // File containing local network type: trusted | untrusted; defaults to untrusted
saveCertsToDir?: string; // Directory to save certificates
saveCertsPrefix?: string; // Prefix for certificate filenames
}
Methods:
async sync(config: SyncConfig): Promise<void> - Synchronize secrets from PKI server
parseConfig(configPath: string): SyncConfig
Parse and validate a secrets configuration file.
Throws:
- Error if file not found
- Error if configuration schema is invalid
parseSwarmEnv(swarmEnvPath: string): SwarmEnv
Parse and validate a swarm environment configuration file.
Throws:
- Error if file not found
- Error if configuration schema is invalid
Challenge Types
The sync client supports different authentication challenge types:
auto (Default)
Automatically detects the environment by checking for:
/dev/tdx_guest → Uses TDX challenge
/dev/sev-guest → Uses SEV-SNP challenge
- If the VM is running in Google Cloud and the GCE metadata endpoint responds, TDX/SEV-SNP is temporarily downgraded to
untrusted
- Neither found → Falls back to untrusted mode
tdx
Explicitly use Intel TDX attestation for authentication.
sev-snp
Explicitly use AMD SEV-SNP attestation for authentication.
untrusted
Use a static identifier for non-TEE environments (development/testing only).
How It Works
- Configuration Loading: Parses YAML configuration files for secrets and server details
- Challenge Selection: Determines authentication method based on environment or user specification
- PKI Client Creation: Establishes mutual TLS connection with PKI server
- Secret Retrieval: Requests specified secrets via authenticated API call
- Secret Storage: Saves Base64-decoded secrets to specified file paths with secure permissions (0600)
- Certificate Export (optional): Saves client certificates for external use
- Failover: Automatically tries next server if connection fails
Retry behavior details:
- Retries are performed for retryable network/DNS errors (for example:
ECONNRESET, ECONNREFUSED, ETIMEDOUT, EAI_AGAIN, ENOTFOUND) and for HTTP 429/502 responses (e.g. throttling or temporary upstream failure behind OpenResty)
- Retries are performed against the original hostname (SNI-safe), without replacing hostname with direct IPs
Error Handling
The sync client provides clear error messages for common issues:
try {
await client.sync(config);
} catch (error) {
if (error.message.includes('not found')) {
// Secret doesn't exist on server
} else if (error.message.includes('Failed to sync from all servers')) {
// All servers unreachable
} else {
// Other errors (network, authentication, etc.)
}
}
Common Exit Codes (CLI)
0: Success
1: Sync failed (configuration error, network error, authentication failure, etc.)
File Permissions
- Secrets: Saved with mode
0600 (read/write for owner only)
- Private keys: Saved with mode
0600 (read/write for owner only)
- Certificates: Saved with mode
0644 (readable by all, writable by owner)
Security Considerations
- Server Identity Verification: Always enabled by default. Only disable with
--disable-server-identity-check in trusted development environments.
- Root Certificate Trust Verification: The root certificate network type extension must match the local network type. Trusted roots must include hardware evidence whose mrEnclave is present in the Super Protocol trusted registry.
- Certificate Validation: CA bundle is used to verify server certificates during TLS handshake.
- Mutual TLS: Both client and server authenticate each other using certificates.
- TEE Attestation: In TDX/SEV environments, hardware-backed attestation proves client identity.
- Secure Storage: Secrets are saved with restrictive file permissions to prevent unauthorized access.
Development
Building from Source
npm install
npm run build
Running Without Building
npm start -- sync -c conf/secrets-example.yaml
Linting
npm run lint
npm run lint:fix
Example Configuration Files
Example files are provided in the conf/ directory:
conf/secrets-example.yaml - Example secrets configuration
conf/swarm-env.example.yaml - Example swarm environment configuration
Dependencies
License
ISC
Author
Super Protocol
Related Packages
- @super-protocol/pki-client - PKI client library
- @super-protocol/pki-common - Common PKI utilities
- @super-protocol/pki-authority-service - PKI Authority Service