Documentation
¶
Index ¶
- Variables
- func DeriveKeyFromSystem() ([]byte, error)
- func GenerateKeyFromPassphrase(passphrase []byte) ([]byte, error)
- func GenerateRandomKey() ([]byte, error)
- func IsErrCredentialsNotFound(err error) bool
- func KeyFileExists() bool
- func LoadKey(keyFile string) ([]byte, error)
- func MigrateCredentials(oldDir, newDir string) (int, error)
- func SaveKey(key []byte, keyFile string) error
- func ValidateKey(key []byte) error
- type Credentials
- type Encryptor
- type EnvProvider
- func (p *EnvProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
- func (p *EnvProvider) ListAgents(ctx context.Context) ([]string, error)
- func (p *EnvProvider) RemoveCredentials(ctx context.Context, agentID string) error
- func (p *EnvProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
- type FallbackProvider
- func (p *FallbackProvider) AddProvider(provider Provider)
- func (p *FallbackProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
- func (p *FallbackProvider) ListAgents(ctx context.Context) ([]string, error)
- func (p *FallbackProvider) RemoveCredentials(ctx context.Context, agentID string) error
- func (p *FallbackProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
- type FileCredentials
- type FileProvider
- func (p *FileProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
- func (p *FileProvider) ListAgents(ctx context.Context) ([]string, error)
- func (p *FileProvider) RemoveCredentials(ctx context.Context, agentID string) error
- func (p *FileProvider) SetPermissions(agentID string) error
- func (p *FileProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
- func (p *FileProvider) ValidatePermissions(agentID string) error
- type KeyringProvider
- func (p *KeyringProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
- func (p *KeyringProvider) IsAvailable() bool
- func (p *KeyringProvider) ListAgents(ctx context.Context) ([]string, error)
- func (p *KeyringProvider) RemoveCredentials(ctx context.Context, agentID string) error
- func (p *KeyringProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
- type Provider
- type SQLiteProvider
- func (p *SQLiteProvider) Close() error
- func (p *SQLiteProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
- func (p *SQLiteProvider) ListAgents(ctx context.Context) ([]string, error)
- func (p *SQLiteProvider) RemoveCredentials(ctx context.Context, agentID string) error
- func (p *SQLiteProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
- type StaticProvider
- func (p *StaticProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
- func (p *StaticProvider) ListAgents(ctx context.Context) ([]string, error)
- func (p *StaticProvider) RemoveCredentials(ctx context.Context, agentID string) error
- func (p *StaticProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCredentialsNotFound is returned when no credentials exist for an agent ErrCredentialsNotFound = errors.New("credentials not found") // ErrCredentialsInvalid is returned when credentials are invalid ErrCredentialsInvalid = errors.New("credentials invalid") ErrProviderUnavailable = errors.New("provider unavailable") )
Common errors
Functions ¶
func DeriveKeyFromSystem ¶
DeriveKeyFromSystem is a placeholder for system keyring integration
Phase 4 will implement actual keyring access using github.com/zalando/go-keyring or similar library.
func GenerateKeyFromPassphrase ¶
GenerateKeyFromPassphrase creates an encryption key from a passphrase using PBKDF2 with a high iteration count
func GenerateRandomKey ¶
GenerateRandomKey generates a new random encryption key
func IsErrCredentialsNotFound ¶
IsErrCredentialsNotFound checks if err is or wraps ErrCredentialsNotFound
func KeyFileExists ¶
func KeyFileExists() bool
KeyFileExists checks if the encryption key file exists
func MigrateCredentials ¶ added in v0.2.0
MigrateCredentials migrates per-agent credential files from an old directory structure to a new one.
Old layout: oldDir/AGENT_ID/credentials.json New layout: newDir/AGENT_ID/credentials.json
Existing credentials at the new location are not overwritten. Returns the number of credentials migrated.
func ValidateKey ¶
ValidateKey checks if a key is valid for AES-256 (must be 32 bytes)
Types ¶
type Credentials ¶
type Credentials struct {
Username string
Password string // Permanent password from registration (64-char hex string)
Empire string // Faction/empire for registration (e.g., "voidborn")
}
Credentials represents authentication credentials for an agent
type Encryptor ¶
type Encryptor struct {
// contains filtered or unexported fields
}
Encryptor handles AES-256-GCM encryption for credential storage
func LoadOrGenerateKey ¶
LoadOrGenerateKey attempts to load an existing encryption key from multiple sources
Key sources (tried in order): 1. System keyring (future implementation) 2. Key file (~/.spacemolt/key) 3. Environment variable (SPACEMOLT_ENCRYPTION_KEY) 4. Generate new random key (saved to keyring or file)
func NewEncryptor ¶
NewEncryptor creates an encryptor with a 32-byte key
func NewEncryptorFromPassphrase ¶
NewEncryptorFromPassphrase derives an encryption key from a passphrase
Uses PBKDF2 with 100,000 iterations to derive a 32-byte key from the passphrase.
type EnvProvider ¶
type EnvProvider struct {
// contains filtered or unexported fields
}
EnvProvider loads credentials from environment variables
Environment variable naming convention:
SPACEMOLT_AGENT_<AGENT_ID>_USERNAME SPACEMOLT_AGENT_<AGENT_ID>_TOKEN SPACEMOLT_AGENT_<AGENT_ID>_EMPIRE
Example for agent "explorer-7":
SPACEMOLT_AGENT_EXPLORER_7_USERNAME="explorer-7" SPACEMOLT_AGENT_EXPLORER_7_TOKEN="abc123" SPACEMOLT_AGENT_EXPLORER_7_EMPIRE="voidborn"
Note: Agent ID is uppercased and hyphens are replaced with underscores to create valid environment variable names.
func NewEnvProvider ¶
func NewEnvProvider(prefix string) *EnvProvider
NewEnvProvider creates a provider that loads credentials from environment variables
func (*EnvProvider) GetCredentials ¶
func (p *EnvProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
GetCredentials retrieves credentials from environment variables for an agent
func (*EnvProvider) ListAgents ¶
func (p *EnvProvider) ListAgents(ctx context.Context) ([]string, error)
ListAgents returns all agent IDs that have credentials in environment variables
func (*EnvProvider) RemoveCredentials ¶
func (p *EnvProvider) RemoveCredentials(ctx context.Context, agentID string) error
RemoveCredentials is a no-op for environment provider
func (*EnvProvider) StoreCredentials ¶
func (p *EnvProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
StoreCredentials is a no-op for environment provider
Environment variables cannot be set from within a running process for security reasons. Returns an error explaining this limitation.
type FallbackProvider ¶
type FallbackProvider struct {
// contains filtered or unexported fields
}
FallbackProvider tries multiple providers in order until one succeeds
Providers are tried in the order they were added. The first provider that successfully returns credentials is used. This allows for flexible credential sourcing with automatic fallback.
Common pattern:
- Try agent-specific file (most specific)
- Try SQLite database (centralized storage)
- Try environment variables (container-friendly)
- Try legacy single-credential file (backward compatibility)
func NewFallbackProvider ¶
func NewFallbackProvider(providers ...Provider) *FallbackProvider
NewFallbackProvider creates a provider that tries multiple sources
func (*FallbackProvider) AddProvider ¶
func (p *FallbackProvider) AddProvider(provider Provider)
AddProvider appends a provider to the fallback chain
Providers are tried in the order they were added.
func (*FallbackProvider) GetCredentials ¶
func (p *FallbackProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
GetCredentials tries each provider in order until credentials are found
Returns the first successful result from any provider. Returns ErrCredentialsNotFound only if all providers fail.
func (*FallbackProvider) ListAgents ¶
func (p *FallbackProvider) ListAgents(ctx context.Context) ([]string, error)
ListAgents aggregates agent lists from all providers
Returns a deduplicated list of all agent IDs across all providers.
func (*FallbackProvider) RemoveCredentials ¶
func (p *FallbackProvider) RemoveCredentials(ctx context.Context, agentID string) error
RemoveCredentials removes credentials from all providers
Attempts to remove from all providers, even if some fail. Returns the first error encountered, but continues trying.
func (*FallbackProvider) StoreCredentials ¶
func (p *FallbackProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
StoreCredentials stores credentials in the first available provider
Tries providers in order until one successfully stores the credentials. Returns an error if all providers fail.
type FileCredentials ¶
type FileCredentials struct {
Username string `json:"username"`
Password string `json:"password"` // New field (v0.38.0+)
Token string `json:"token,omitempty"` // Legacy field for backward compatibility
Empire string `json:"empire"`
}
FileCredentials represents the JSON format of credential files Supports both 'password' (new) and 'token' (legacy) for migration
type FileProvider ¶
type FileProvider struct {
// contains filtered or unexported fields
}
FileProvider loads credentials from agent-specific files
Each agent has its own credentials file in the agent's consolidated directory:
data/agents/<agent-id>/credentials.json
This directory also contains:
- personality.json (agent personality and traits)
- DIARY.md (future: agent's learned knowledge)
File format:
{
"username": "agent-name",
"password": "auth-token",
"empire": "voidborn"
}
Legacy format with "token" instead of "password" is automatically migrated.
func NewFileProvider ¶
func NewFileProvider(agentsDir string) *FileProvider
NewFileProvider creates a new file-based credential provider
agentsDir is the base directory containing agent subdirectories. Each subdirectory should be named with the agent ID and contain:
- personality.json (required)
- credentials.json (auto-generated)
Example: agentsDir = "data/agents" will look for credentials at:
data/agents/<agent-id>/credentials.json
func (*FileProvider) GetCredentials ¶
func (p *FileProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
GetCredentials retrieves credentials for an agent from its credential file
func (*FileProvider) ListAgents ¶
func (p *FileProvider) ListAgents(ctx context.Context) ([]string, error)
ListAgents returns all agent IDs that have credential files
func (*FileProvider) RemoveCredentials ¶
func (p *FileProvider) RemoveCredentials(ctx context.Context, agentID string) error
RemoveCredentials deletes an agent's credential file
func (*FileProvider) SetPermissions ¶
func (p *FileProvider) SetPermissions(agentID string) error
SetPermissions sets secure permissions on credential files
func (*FileProvider) StoreCredentials ¶
func (p *FileProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
StoreCredentials saves credentials for an agent to its credential file
func (*FileProvider) ValidatePermissions ¶
func (p *FileProvider) ValidatePermissions(agentID string) error
ValidatePermissions checks that credential files have secure permissions (0600)
type KeyringProvider ¶
type KeyringProvider struct {
// contains filtered or unexported fields
}
KeyringProvider loads credentials from the system keyring
This provider uses the OS's secure credential storage: - Linux: gnome-keyring, kwallet - macOS: Keychain - Windows: Credential Manager
NOTE: Phase 4 implementation (enhanced providers). Phase 1 provides a stub that returns ErrProviderUnavailable.
func NewKeyringProvider ¶
func NewKeyringProvider(service string) *KeyringProvider
NewKeyringProvider creates a new keyring credential provider
service is the keyring service name to use for storing credentials.
func (*KeyringProvider) GetCredentials ¶
func (p *KeyringProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
GetCredentials retrieves credentials from the system keyring
Phase 4: Implement actual keyring access Phase 1: Returns ErrProviderUnavailable
func (*KeyringProvider) IsAvailable ¶
func (p *KeyringProvider) IsAvailable() bool
IsAvailable checks if the keyring provider is available
Phase 4: Will detect if keyring is accessible
func (*KeyringProvider) ListAgents ¶
func (p *KeyringProvider) ListAgents(ctx context.Context) ([]string, error)
ListAgents returns all agent IDs with stored credentials
func (*KeyringProvider) RemoveCredentials ¶
func (p *KeyringProvider) RemoveCredentials(ctx context.Context, agentID string) error
RemoveCredentials deletes credentials from the system keyring
func (*KeyringProvider) StoreCredentials ¶
func (p *KeyringProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
StoreCredentials saves credentials to the system keyring
type Provider ¶
type Provider interface {
// GetCredentials retrieves credentials for an agent
//
// Returns ErrCredentialsNotFound if no credentials exist for the agent.
// Returns ErrProviderUnavailable if the provider cannot be used.
GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
// StoreCredentials saves credentials for an agent
//
// If credentials already exist, they should be overwritten.
StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
// RemoveCredentials deletes credentials for an agent
//
// Should return ErrCredentialsNotFound if credentials don't exist.
RemoveCredentials(ctx context.Context, agentID string) error
// ListAgents returns all agent IDs that have stored credentials
//
// Returns an empty slice if no credentials are stored.
ListAgents(ctx context.Context) ([]string, error)
}
Provider provides credentials for agents
The Provider interface allows flexibility in where and how credentials are stored, supporting multiple backends (file, SQLite, keyring, etc.)
type SQLiteProvider ¶
type SQLiteProvider struct {
// contains filtered or unexported fields
}
SQLiteProvider loads credentials from a SQLite database
This provider stores credentials in the agent_credentials table, with encrypted tokens for security using AES-256-GCM.
func NewSQLiteProvider ¶
func NewSQLiteProvider(dbPath string, encryptor *Encryptor) (*SQLiteProvider, error)
NewSQLiteProvider creates a new SQLite credential provider
The database must have the agent_credentials table created. See knowledge/sqlite_migrations.go for schema. The encryptor is used to encrypt/decrypt tokens in the database.
func NewSQLiteProviderWithDB ¶
func NewSQLiteProviderWithDB(db *sql.DB, encryptor *Encryptor) *SQLiteProvider
NewSQLiteProviderWithDB creates a provider using an existing DB connection
Useful when sharing a database connection with the knowledge base. The encryptor is used to encrypt/decrypt tokens in the database.
func (*SQLiteProvider) Close ¶
func (p *SQLiteProvider) Close() error
Close closes the database connection
Only call this if the provider was created with NewSQLiteProvider. Do not call if created with NewSQLiteProviderWithDB (connection is shared).
func (*SQLiteProvider) GetCredentials ¶
func (p *SQLiteProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
GetCredentials retrieves credentials from the database
func (*SQLiteProvider) ListAgents ¶
func (p *SQLiteProvider) ListAgents(ctx context.Context) ([]string, error)
ListAgents returns all agent IDs with stored credentials
func (*SQLiteProvider) RemoveCredentials ¶
func (p *SQLiteProvider) RemoveCredentials(ctx context.Context, agentID string) error
RemoveCredentials deletes credentials from the database
func (*SQLiteProvider) StoreCredentials ¶
func (p *SQLiteProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
StoreCredentials saves credentials to the database
type StaticProvider ¶
type StaticProvider struct {
// contains filtered or unexported fields
}
StaticProvider returns a fixed set of credentials for all agents
This is useful for testing or single-credential scenarios.
func NewStaticProvider ¶
func NewStaticProvider(username, password, empire string) *StaticProvider
NewStaticProvider creates a provider that always returns the same credentials
func (*StaticProvider) GetCredentials ¶
func (p *StaticProvider) GetCredentials(ctx context.Context, agentID string) (*Credentials, error)
GetCredentials returns the static credentials for any agent ID
func (*StaticProvider) ListAgents ¶
func (p *StaticProvider) ListAgents(ctx context.Context) ([]string, error)
ListAgents returns nil since static provider doesn't track specific agents
func (*StaticProvider) RemoveCredentials ¶
func (p *StaticProvider) RemoveCredentials(ctx context.Context, agentID string) error
RemoveCredentials is a no-op for static provider
func (*StaticProvider) StoreCredentials ¶
func (p *StaticProvider) StoreCredentials(ctx context.Context, agentID string, creds *Credentials) error
StoreCredentials is a no-op for static provider (credentials cannot be changed)