secure

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: Apache-2.0 Imports: 10 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEncryption          = errors.New("encryption error")
	ErrDecryption          = errors.New("decryption error")
	ErrInvalidKey          = errors.New("invalid encryption key")
	ErrEmptyCredential     = errors.New("empty credential")
	ErrSecretsFileNotFound = errors.New("secrets file not found")
)

Functions

func DecodeKey

func DecodeKey(encodedKey string) ([]byte, error)

DecodeKey decodes a base64 encoded key

func EncodeKey

func EncodeKey(key []byte) string

EncodeKey encodes a key as a base64 string for storage

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey generates a random 32-byte key for AES-256

Types

type Credential

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

Credential stores sensitive information (like passwords) in encrypted form in memory

func CredentialFromConfig

func CredentialFromConfig(cfg CredentialConfig, encryptionKey []byte, allowEmpty bool) (*Credential, error)

CredentialFromConfig attempts to parse credentials from a CredentialConfig struct if no valid credentials found, returns error; if environment var is used, it is read only once and then overwritten with an empty value

func CredentialFromEnv

func CredentialFromEnv(envName string, encryptionKey []byte, allowEmpty bool) (*Credential, error)

CredentialFromEnv creates a Credential from an environment variable

func CredentialFromFile

func CredentialFromFile(filename string, encryptionKey []byte, allowEmpty bool) (*Credential, error)

CredentialFromFile creates a Credential from a secrets file

func NewCredential

func NewCredential(plaintext string, encryptionKey []byte, allowEmpty bool) (*Credential, error)

NewCredential creates a new secure credential container The encryption key should be unique per application instance You can use env variables, hardware tokens, etc. as the source of the encryption key

func (*Credential) Clear

func (sc *Credential) Clear()

Clear zeroes out all sensitive data

func (*Credential) Get

func (sc *Credential) Get() (string, error)

Get decrypts and returns the plaintext credential This should be called only when needed to minimize exposure of the sensitive data in memory

func (*Credential) IsEmpty

func (sc *Credential) IsEmpty() bool

IsEmpty returns true if credentials is empty

func (*Credential) Update

func (sc *Credential) Update(plaintext string) error

Update updates the credential with a new plaintext value

type CredentialConfig

type CredentialConfig interface {
	GetPassword() string
	GetEnvVar() string
	GetFileName() string
}

type DefaultCredentialConfig

type DefaultCredentialConfig struct {
	Password       string `json:"password"`       // Password plaintext password; if set, is used instead of the rest
	PasswordEnvVar string `json:"passwordEnvVar"` // PasswordEnvVar name of env var with secret
	PasswordFile   string `json:"passwordFile"`   // PasswordFile name of secrets file, to be read; if none of the above set, this one is used
}

DefaultCredentialConfig misc options for credentials if different field names are required, just implement CredentialConfig interface

func (DefaultCredentialConfig) GetEnvVar

func (c DefaultCredentialConfig) GetEnvVar() string

GetEnvVar fetch environment var name holding the password

func (DefaultCredentialConfig) GetFileName

func (c DefaultCredentialConfig) GetFileName() string

GetFileName fetch file name holding the password

func (DefaultCredentialConfig) GetPassword

func (c DefaultCredentialConfig) GetPassword() string

GetPassword fetch password value

Jump to

Keyboard shortcuts

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