credentials

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package credentials provides credential management for the OpenCTEM SDK. It includes interfaces for credential storage and retrieval, with implementations for environment variables, files, and external vaults.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCredentialNotFound = fmt.Errorf("credential not found")
	ErrReadOnly           = fmt.Errorf("store is read-only")
	ErrInvalidCredential  = fmt.Errorf("invalid credential")
	ErrInvalidKey         = fmt.Errorf("invalid credential key")
	ErrEncryptionFailed   = fmt.Errorf("encryption failed")
	ErrDecryptionFailed   = fmt.Errorf("decryption failed")
)

Common errors for credential operations.

Functions

func GetValue

func GetValue(ctx context.Context, key string) (string, error)

GetValue retrieves just the value from the default store.

func SecureClear

func SecureClear(cred *Credential)

SecureClear overwrites sensitive credential data with zeros. This helps prevent credential leakage through memory dumps. Note: Due to Go's string immutability, this may not fully clear all copies of the string data, but it clears the primary references.

func SecureCompare

func SecureCompare(a, b string) bool

SecureCompare performs a constant-time comparison of two credential values. This prevents timing attacks when comparing secrets.

func SetDefaultStore

func SetDefaultStore(store Store)

SetDefaultStore sets the global default credential store.

func ValidateKey

func ValidateKey(key string) error

ValidateKey checks if a credential key is valid and safe. Returns ErrInvalidKey if the key contains unsafe characters.

func WithStore

func WithStore(ctx context.Context, store Store) context.Context

WithStore returns a new context with the store attached.

Types

type AESEncryptor

type AESEncryptor struct {
	// contains filtered or unexported fields
}

AESEncryptor implements Encryptor using AES-GCM.

func NewAESEncryptor

func NewAESEncryptor(key []byte) (*AESEncryptor, error)

NewAESEncryptor creates a new AES-GCM encryptor. Key must be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256.

func NewAESEncryptorFromEnv

func NewAESEncryptorFromEnv(envVar string) (*AESEncryptor, error)

NewAESEncryptorFromEnv creates an AES encryptor from an environment variable. The key should be base64-encoded.

func (*AESEncryptor) Decrypt

func (e *AESEncryptor) Decrypt(ciphertext []byte) ([]byte, error)

func (*AESEncryptor) Encrypt

func (e *AESEncryptor) Encrypt(plaintext []byte) ([]byte, error)

type ChainedStore

type ChainedStore struct {
	// contains filtered or unexported fields
}

ChainedStore implements Store by checking multiple stores in order. Useful for layering local overrides on top of a central store.

func NewChainedStore

func NewChainedStore(stores ...Store) *ChainedStore

NewChainedStore creates a new chained credential store. Stores are checked in order - first match wins.

func (*ChainedStore) Delete

func (s *ChainedStore) Delete(ctx context.Context, key string) error

func (*ChainedStore) Exists

func (s *ChainedStore) Exists(ctx context.Context, key string) (bool, error)

func (*ChainedStore) Get

func (s *ChainedStore) Get(ctx context.Context, key string) (*Credential, error)

func (*ChainedStore) List

func (s *ChainedStore) List(ctx context.Context, prefix string) ([]string, error)

func (*ChainedStore) Set

func (s *ChainedStore) Set(ctx context.Context, key string, cred *Credential) error

type Credential

type Credential struct {
	// Key is the credential identifier
	Key string `json:"key"`

	// Type categorizes the credential (api_key, token, password, certificate, etc.)
	Type CredentialType `json:"type"`

	// Value is the actual credential value
	Value string `json:"value"`

	// SecondaryValue holds additional values (e.g., client_secret for OAuth)
	SecondaryValue string `json:"secondary_value,omitempty"`

	// Metadata holds additional credential information
	Metadata map[string]string `json:"metadata,omitempty"`

	// ExpiresAt is the credential expiration time (if applicable)
	ExpiresAt *time.Time `json:"expires_at,omitempty"`

	// CreatedAt is when the credential was stored
	CreatedAt time.Time `json:"created_at"`

	// UpdatedAt is when the credential was last updated
	UpdatedAt time.Time `json:"updated_at"`
}

Credential represents a stored credential.

func Get

func Get(ctx context.Context, key string) (*Credential, error)

Get retrieves a credential from the default store.

func MustGet

func MustGet(ctx context.Context, key string) *Credential

MustGet retrieves a credential from the default store, panicking on error.

func (*Credential) IsExpired

func (c *Credential) IsExpired() bool

IsExpired checks if the credential has expired.

type CredentialType

type CredentialType string

CredentialType represents the type of credential.

const (
	CredentialTypeAPIKey      CredentialType = "api_key"
	CredentialTypeToken       CredentialType = "token"
	CredentialTypePassword    CredentialType = "password"
	CredentialTypeOAuth       CredentialType = "oauth"
	CredentialTypeCertificate CredentialType = "certificate"
	CredentialTypeSSHKey      CredentialType = "ssh_key"
	CredentialTypeSecret      CredentialType = "secret"
)

type EncryptedFileStore

type EncryptedFileStore struct {
	// contains filtered or unexported fields
}

EncryptedFileStore implements Store using an encrypted JSON file. This is a secure alternative to FileStore for storing credentials at rest.

func NewEncryptedFileStore

func NewEncryptedFileStore(filePath string, encryptor Encryptor) (*EncryptedFileStore, error)

NewEncryptedFileStore creates a new encrypted file-based credential store.

func (*EncryptedFileStore) Delete

func (s *EncryptedFileStore) Delete(ctx context.Context, key string) error

func (*EncryptedFileStore) Exists

func (s *EncryptedFileStore) Exists(ctx context.Context, key string) (bool, error)

func (*EncryptedFileStore) Get

func (*EncryptedFileStore) List

func (s *EncryptedFileStore) List(ctx context.Context, prefix string) ([]string, error)

func (*EncryptedFileStore) Set

func (s *EncryptedFileStore) Set(ctx context.Context, key string, cred *Credential) error

type Encryptor

type Encryptor interface {
	// Encrypt encrypts plaintext and returns ciphertext.
	Encrypt(plaintext []byte) ([]byte, error)

	// Decrypt decrypts ciphertext and returns plaintext.
	Decrypt(ciphertext []byte) ([]byte, error)
}

Encryptor provides encryption/decryption for credential values.

type EnvStore

type EnvStore struct {
	// Prefix is prepended to all key lookups (e.g., "OPENCTEM_")
	Prefix string

	// Mapping overrides key-to-env-var mapping
	Mapping map[string]string
}

EnvStore implements Store using environment variables. It's the simplest store implementation and suitable for CI/CD environments.

func NewEnvStore

func NewEnvStore(prefix string) *EnvStore

NewEnvStore creates a new environment variable credential store.

func NewEnvStoreWithMapping

func NewEnvStoreWithMapping(prefix string, mapping map[string]string) *EnvStore

NewEnvStoreWithMapping creates a new environment store with custom mapping.

func (*EnvStore) Delete

func (s *EnvStore) Delete(ctx context.Context, key string) error

func (*EnvStore) Exists

func (s *EnvStore) Exists(ctx context.Context, key string) (bool, error)

func (*EnvStore) Get

func (s *EnvStore) Get(ctx context.Context, key string) (*Credential, error)

func (*EnvStore) List

func (s *EnvStore) List(ctx context.Context, prefix string) ([]string, error)

func (*EnvStore) Set

func (s *EnvStore) Set(ctx context.Context, key string, cred *Credential) error

type FileStore

type FileStore struct {
	// contains filtered or unexported fields
}

FileStore implements Store using a JSON file. Suitable for local development, NOT recommended for production.

func NewFileStore

func NewFileStore(filePath string) (*FileStore, error)

NewFileStore creates a new file-based credential store.

func (*FileStore) Delete

func (s *FileStore) Delete(ctx context.Context, key string) error

func (*FileStore) Exists

func (s *FileStore) Exists(ctx context.Context, key string) (bool, error)

func (*FileStore) Get

func (s *FileStore) Get(ctx context.Context, key string) (*Credential, error)

func (*FileStore) List

func (s *FileStore) List(ctx context.Context, prefix string) ([]string, error)

func (*FileStore) Set

func (s *FileStore) Set(ctx context.Context, key string, cred *Credential) error

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore implements Store using in-memory storage. Useful for testing and development.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates a new in-memory credential store.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(ctx context.Context, key string) error

func (*MemoryStore) Exists

func (s *MemoryStore) Exists(ctx context.Context, key string) (bool, error)

func (*MemoryStore) Get

func (s *MemoryStore) Get(ctx context.Context, key string) (*Credential, error)

func (*MemoryStore) List

func (s *MemoryStore) List(ctx context.Context, prefix string) ([]string, error)

func (*MemoryStore) Set

func (s *MemoryStore) Set(ctx context.Context, key string, cred *Credential) error

type Store

type Store interface {
	// Get retrieves a credential by key.
	Get(ctx context.Context, key string) (*Credential, error)

	// Set stores a credential.
	Set(ctx context.Context, key string, cred *Credential) error

	// Delete removes a credential.
	Delete(ctx context.Context, key string) error

	// List returns all credential keys matching a prefix.
	List(ctx context.Context, prefix string) ([]string, error)

	// Exists checks if a credential exists.
	Exists(ctx context.Context, key string) (bool, error)
}

Store is the interface for credential storage and retrieval. Implement this interface to use custom credential backends (Vault, AWS Secrets Manager, etc.).

func GetDefaultStore

func GetDefaultStore() Store

GetDefaultStore returns the global default credential store.

func StoreFromContext

func StoreFromContext(ctx context.Context) Store

StoreFromContext returns the store from the context, or the default.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL