db

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

package db provides the data access layer for Keymaster. It abstracts the underlying database (e.g., SQLite, PostgreSQL) behind a consistent interface, allowing the rest of the application to interact with the database in a uniform way.

package db provides the data access layer for Keymaster. This file contains the MySQL implementation of the database store. Note: This implementation is considered experimental.

package db provides the data access layer for Keymaster. This file contains the PostgreSQL implementation of the database store. Note: This implementation is considered experimental.

package db provides the data access layer for Keymaster. This file contains the SQLite implementation of the database store.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrDuplicate is returned when a unique constraint is violated.
	ErrDuplicate = errors.New("duplicate entry")
)

Functions

func AddAccount

func AddAccount(username, hostname, label, tags string) (int, error)

AddAccount adds a new account to the database.

func AddKnownHostKey

func AddKnownHostKey(hostname, key string) error

AddKnownHostKey adds a new trusted host key to the database.

func AddPublicKey

func AddPublicKey(algorithm, keyData, comment string, isGlobal bool) error

AddPublicKey adds a new public key to the database.

func AddPublicKeyAndGetModel

func AddPublicKeyAndGetModel(algorithm, keyData, comment string, isGlobal bool) (*model.PublicKey, error)

AddPublicKeyAndGetModel adds a public key to the database if it doesn't already exist (based on the comment) and returns the full key model. If a key with the same comment already exists, it returns (nil, nil) to indicate a duplicate without an error.

func AssignKeyToAccount

func AssignKeyToAccount(keyID, accountID int) error

AssignKeyToAccount creates an association between a key and an account.

func CreateSystemKey

func CreateSystemKey(publicKey, privateKey string) (int, error)

CreateSystemKey adds a new system key to the database. It determines the correct serial automatically.

func DeleteAccount

func DeleteAccount(id int) error

DeleteAccount removes an account from the database by its ID.

func DeleteBootstrapSession added in v1.4.0

func DeleteBootstrapSession(id string) error

DeleteBootstrapSession removes a bootstrap session from the database.

func DeletePublicKey

func DeletePublicKey(id int) error

DeletePublicKey removes a public key and all its associations. The ON DELETE CASCADE constraint handles the associations in account_keys.

func ExportDataForBackup added in v1.4.0

func ExportDataForBackup() (*model.BackupData, error)

ExportDataForBackup retrieves all data from the database for a backup.

func GetAccountsForKey

func GetAccountsForKey(keyID int) ([]model.Account, error)

GetAccountsForKey retrieves all accounts that have a specific public key assigned.

func GetActiveSystemKey

func GetActiveSystemKey() (*model.SystemKey, error)

GetActiveSystemKey retrieves the currently active system key for deployments.

func GetAllAccounts

func GetAllAccounts() ([]model.Account, error)

GetAllAccounts retrieves all accounts from the database.

func GetAllActiveAccounts

func GetAllActiveAccounts() ([]model.Account, error)

GetAllActiveAccounts retrieves all active accounts from the database.

func GetAllAuditLogEntries

func GetAllAuditLogEntries() ([]model.AuditLogEntry, error)

GetAllAuditLogEntries retrieves all entries from the audit log, most recent first.

func GetAllPublicKeys

func GetAllPublicKeys() ([]model.PublicKey, error)

GetAllPublicKeys retrieves all public keys from the database.

func GetBootstrapSession added in v1.4.0

func GetBootstrapSession(id string) (*model.BootstrapSession, error)

GetBootstrapSession retrieves a bootstrap session by ID.

func GetExpiredBootstrapSessions added in v1.4.0

func GetExpiredBootstrapSessions() ([]*model.BootstrapSession, error)

GetExpiredBootstrapSessions returns all expired bootstrap sessions.

func GetGlobalPublicKeys

func GetGlobalPublicKeys() ([]model.PublicKey, error)

GetGlobalPublicKeys retrieves all keys marked as global.

func GetKeysForAccount

func GetKeysForAccount(accountID int) ([]model.PublicKey, error)

GetKeysForAccount retrieves all public keys assigned to a specific account.

func GetKnownHostKey

func GetKnownHostKey(hostname string) (string, error)

GetKnownHostKey retrieves the trusted public key for a given hostname.

func GetOrphanedBootstrapSessions added in v1.4.0

func GetOrphanedBootstrapSessions() ([]*model.BootstrapSession, error)

GetOrphanedBootstrapSessions returns all orphaned bootstrap sessions.

func GetPublicKeyByComment

func GetPublicKeyByComment(comment string) (*model.PublicKey, error)

GetPublicKeyByComment retrieves a single public key by its unique comment.

func GetSystemKeyBySerial

func GetSystemKeyBySerial(serial int) (*model.SystemKey, error)

GetSystemKeyBySerial retrieves a system key by its serial number.

func HasSystemKeys

func HasSystemKeys() (bool, error)

HasSystemKeys checks if any system keys exist in the database.

func ImportDataFromBackup added in v1.4.0

func ImportDataFromBackup(backup *model.BackupData) error

ImportDataFromBackup restores the database from a backup data structure.

func InitDB

func InitDB(dbType, dsn string) error

InitDB initializes the database connection based on the provided type and DSN. It sets the global `store` variable to the appropriate database implementation and runs any pending database migrations.

func IntegrateDataFromBackup added in v1.4.0

func IntegrateDataFromBackup(backup *model.BackupData) error

IntegrateDataFromBackup restores the database from a backup data structure in a non-destructive way.

func LogAction

func LogAction(action string, details string) error

LogAction records an audit trail event.

func RotateSystemKey

func RotateSystemKey(publicKey, privateKey string) (int, error)

RotateSystemKey deactivates all current system keys and adds a new one as active. This should be performed within a transaction to ensure atomicity.

func RunMigrations added in v1.4.0

func RunMigrations(db *sql.DB, dbType string) error

RunMigrations applies the necessary database migrations for a given database connection.

func SaveBootstrapSession added in v1.4.0

func SaveBootstrapSession(id, username, hostname, label, tags, tempPublicKey string, expiresAt time.Time, status string) error

SaveBootstrapSession saves a bootstrap session to the database.

func ToggleAccountStatus

func ToggleAccountStatus(id int) error

ToggleAccountStatus flips the active status of an account.

func TogglePublicKeyGlobal

func TogglePublicKeyGlobal(id int) error

TogglePublicKeyGlobal flips the 'is_global' status of a public key.

func UnassignKeyFromAccount

func UnassignKeyFromAccount(keyID, accountID int) error

UnassignKeyFromAccount removes an association between a key and an account.

func UpdateAccountHostname added in v1.4.0

func UpdateAccountHostname(id int, hostname string) error

UpdateAccountHostname updates the hostname for a given account.

func UpdateAccountLabel

func UpdateAccountLabel(id int, label string) error

UpdateAccountLabel updates the label for a given account.

func UpdateAccountSerial

func UpdateAccountSerial(id, serial int) error

UpdateAccountSerial sets the system key serial for a given account ID. This is typically called after a successful deployment.

func UpdateAccountTags

func UpdateAccountTags(id int, tags string) error

UpdateAccountTags updates the tags for a given account.

func UpdateBootstrapSessionStatus added in v1.4.0

func UpdateBootstrapSessionStatus(id string, status string) error

UpdateBootstrapSessionStatus updates the status of a bootstrap session.

Types

type MySQLStore

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

MySQLStore is the MySQL implementation of the Store interface.

func NewMySQLStore

func NewMySQLStore(dataSourceName string) (*MySQLStore, error)

NewMySQLStore initializes the database connection and creates tables if they don't exist.

func (*MySQLStore) AddAccount

func (s *MySQLStore) AddAccount(username, hostname, label, tags string) (int, error)

func (*MySQLStore) AddKnownHostKey

func (s *MySQLStore) AddKnownHostKey(hostname, key string) error

func (*MySQLStore) AddPublicKey

func (s *MySQLStore) AddPublicKey(algorithm, keyData, comment string, isGlobal bool) error

func (*MySQLStore) AddPublicKeyAndGetModel

func (s *MySQLStore) AddPublicKeyAndGetModel(algorithm, keyData, comment string, isGlobal bool) (*model.PublicKey, error)

func (*MySQLStore) AssignKeyToAccount

func (s *MySQLStore) AssignKeyToAccount(keyID, accountID int) error

func (*MySQLStore) CreateSystemKey

func (s *MySQLStore) CreateSystemKey(publicKey, privateKey string) (int, error)

func (*MySQLStore) DeleteAccount

func (s *MySQLStore) DeleteAccount(id int) error

func (*MySQLStore) DeleteBootstrapSession added in v1.4.0

func (s *MySQLStore) DeleteBootstrapSession(id string) error

DeleteBootstrapSession removes a bootstrap session from the database.

func (*MySQLStore) DeletePublicKey

func (s *MySQLStore) DeletePublicKey(id int) error

func (*MySQLStore) ExportDataForBackup added in v1.4.0

func (s *MySQLStore) ExportDataForBackup() (*model.BackupData, error)

ExportDataForBackup retrieves all data from the database for a backup. It uses a transaction to ensure a consistent snapshot of the data.

func (*MySQLStore) GetAccountsForKey

func (s *MySQLStore) GetAccountsForKey(keyID int) ([]model.Account, error)

func (*MySQLStore) GetActiveSystemKey

func (s *MySQLStore) GetActiveSystemKey() (*model.SystemKey, error)

func (*MySQLStore) GetAllAccounts

func (s *MySQLStore) GetAllAccounts() ([]model.Account, error)

func (*MySQLStore) GetAllActiveAccounts

func (s *MySQLStore) GetAllActiveAccounts() ([]model.Account, error)

func (*MySQLStore) GetAllAuditLogEntries

func (s *MySQLStore) GetAllAuditLogEntries() ([]model.AuditLogEntry, error)

func (*MySQLStore) GetAllPublicKeys

func (s *MySQLStore) GetAllPublicKeys() ([]model.PublicKey, error)

func (*MySQLStore) GetBootstrapSession added in v1.4.0

func (s *MySQLStore) GetBootstrapSession(id string) (*model.BootstrapSession, error)

GetBootstrapSession retrieves a bootstrap session by ID.

func (*MySQLStore) GetExpiredBootstrapSessions added in v1.4.0

func (s *MySQLStore) GetExpiredBootstrapSessions() ([]*model.BootstrapSession, error)

GetExpiredBootstrapSessions returns all expired bootstrap sessions.

func (*MySQLStore) GetGlobalPublicKeys

func (s *MySQLStore) GetGlobalPublicKeys() ([]model.PublicKey, error)

func (*MySQLStore) GetKeysForAccount

func (s *MySQLStore) GetKeysForAccount(accountID int) ([]model.PublicKey, error)

func (*MySQLStore) GetKnownHostKey

func (s *MySQLStore) GetKnownHostKey(hostname string) (string, error)

func (*MySQLStore) GetOrphanedBootstrapSessions added in v1.4.0

func (s *MySQLStore) GetOrphanedBootstrapSessions() ([]*model.BootstrapSession, error)

GetOrphanedBootstrapSessions returns all orphaned bootstrap sessions.

func (*MySQLStore) GetPublicKeyByComment

func (s *MySQLStore) GetPublicKeyByComment(comment string) (*model.PublicKey, error)

func (*MySQLStore) GetSystemKeyBySerial

func (s *MySQLStore) GetSystemKeyBySerial(serial int) (*model.SystemKey, error)

func (*MySQLStore) HasSystemKeys

func (s *MySQLStore) HasSystemKeys() (bool, error)

func (*MySQLStore) ImportDataFromBackup added in v1.4.0

func (s *MySQLStore) ImportDataFromBackup(backup *model.BackupData) error

ImportDataFromBackup restores the database from a backup data structure. It performs a full wipe-and-replace within a single transaction to ensure atomicity.

func (*MySQLStore) IntegrateDataFromBackup added in v1.4.0

func (s *MySQLStore) IntegrateDataFromBackup(backup *model.BackupData) error

IntegrateDataFromBackup restores data from a backup in a non-destructive way, skipping entries that already exist.

func (*MySQLStore) LogAction

func (s *MySQLStore) LogAction(action string, details string) error

func (*MySQLStore) RotateSystemKey

func (s *MySQLStore) RotateSystemKey(publicKey, privateKey string) (int, error)

func (*MySQLStore) SaveBootstrapSession added in v1.4.0

func (s *MySQLStore) SaveBootstrapSession(id, username, hostname, label, tags, tempPublicKey string, expiresAt time.Time, status string) error

SaveBootstrapSession saves a bootstrap session to the database.

func (*MySQLStore) ToggleAccountStatus

func (s *MySQLStore) ToggleAccountStatus(id int) error

func (*MySQLStore) TogglePublicKeyGlobal

func (s *MySQLStore) TogglePublicKeyGlobal(id int) error

func (*MySQLStore) UnassignKeyFromAccount

func (s *MySQLStore) UnassignKeyFromAccount(keyID, accountID int) error

func (*MySQLStore) UpdateAccountHostname added in v1.4.0

func (s *MySQLStore) UpdateAccountHostname(id int, hostname string) error

func (*MySQLStore) UpdateAccountLabel

func (s *MySQLStore) UpdateAccountLabel(id int, label string) error

func (*MySQLStore) UpdateAccountSerial

func (s *MySQLStore) UpdateAccountSerial(id, serial int) error

func (*MySQLStore) UpdateAccountTags

func (s *MySQLStore) UpdateAccountTags(id int, tags string) error

func (*MySQLStore) UpdateBootstrapSessionStatus added in v1.4.0

func (s *MySQLStore) UpdateBootstrapSessionStatus(id string, status string) error

UpdateBootstrapSessionStatus updates the status of a bootstrap session.

type PostgresStore

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

PostgresStore is the PostgreSQL implementation of the Store interface.

func NewPostgresStore

func NewPostgresStore(dataSourceName string) (*PostgresStore, error)

NewPostgresStore initializes the database connection and creates tables if they don't exist.

func (*PostgresStore) AddAccount

func (s *PostgresStore) AddAccount(username, hostname, label, tags string) (int, error)

func (*PostgresStore) AddKnownHostKey

func (s *PostgresStore) AddKnownHostKey(hostname, key string) error

func (*PostgresStore) AddPublicKey

func (s *PostgresStore) AddPublicKey(algorithm, keyData, comment string, isGlobal bool) error

func (*PostgresStore) AddPublicKeyAndGetModel

func (s *PostgresStore) AddPublicKeyAndGetModel(algorithm, keyData, comment string, isGlobal bool) (*model.PublicKey, error)

func (*PostgresStore) AssignKeyToAccount

func (s *PostgresStore) AssignKeyToAccount(keyID, accountID int) error

func (*PostgresStore) CreateSystemKey

func (s *PostgresStore) CreateSystemKey(publicKey, privateKey string) (int, error)

func (*PostgresStore) DeleteAccount

func (s *PostgresStore) DeleteAccount(id int) error

func (*PostgresStore) DeleteBootstrapSession added in v1.4.0

func (s *PostgresStore) DeleteBootstrapSession(id string) error

DeleteBootstrapSession removes a bootstrap session from the database.

func (*PostgresStore) DeletePublicKey

func (s *PostgresStore) DeletePublicKey(id int) error

func (*PostgresStore) ExportDataForBackup added in v1.4.0

func (s *PostgresStore) ExportDataForBackup() (*model.BackupData, error)

ExportDataForBackup retrieves all data from the database for a backup. It uses a transaction to ensure a consistent snapshot of the data.

func (*PostgresStore) GetAccountsForKey

func (s *PostgresStore) GetAccountsForKey(keyID int) ([]model.Account, error)

func (*PostgresStore) GetActiveSystemKey

func (s *PostgresStore) GetActiveSystemKey() (*model.SystemKey, error)

func (*PostgresStore) GetAllAccounts

func (s *PostgresStore) GetAllAccounts() ([]model.Account, error)

func (*PostgresStore) GetAllActiveAccounts

func (s *PostgresStore) GetAllActiveAccounts() ([]model.Account, error)

func (*PostgresStore) GetAllAuditLogEntries

func (s *PostgresStore) GetAllAuditLogEntries() ([]model.AuditLogEntry, error)

func (*PostgresStore) GetAllPublicKeys

func (s *PostgresStore) GetAllPublicKeys() ([]model.PublicKey, error)

func (*PostgresStore) GetBootstrapSession added in v1.4.0

func (s *PostgresStore) GetBootstrapSession(id string) (*model.BootstrapSession, error)

GetBootstrapSession retrieves a bootstrap session by ID.

func (*PostgresStore) GetExpiredBootstrapSessions added in v1.4.0

func (s *PostgresStore) GetExpiredBootstrapSessions() ([]*model.BootstrapSession, error)

GetExpiredBootstrapSessions returns all expired bootstrap sessions.

func (*PostgresStore) GetGlobalPublicKeys

func (s *PostgresStore) GetGlobalPublicKeys() ([]model.PublicKey, error)

func (*PostgresStore) GetKeysForAccount

func (s *PostgresStore) GetKeysForAccount(accountID int) ([]model.PublicKey, error)

func (*PostgresStore) GetKnownHostKey

func (s *PostgresStore) GetKnownHostKey(hostname string) (string, error)

func (*PostgresStore) GetOrphanedBootstrapSessions added in v1.4.0

func (s *PostgresStore) GetOrphanedBootstrapSessions() ([]*model.BootstrapSession, error)

GetOrphanedBootstrapSessions returns all orphaned bootstrap sessions.

func (*PostgresStore) GetPublicKeyByComment

func (s *PostgresStore) GetPublicKeyByComment(comment string) (*model.PublicKey, error)

func (*PostgresStore) GetSystemKeyBySerial

func (s *PostgresStore) GetSystemKeyBySerial(serial int) (*model.SystemKey, error)

func (*PostgresStore) HasSystemKeys

func (s *PostgresStore) HasSystemKeys() (bool, error)

func (*PostgresStore) ImportDataFromBackup added in v1.4.0

func (s *PostgresStore) ImportDataFromBackup(backup *model.BackupData) error

ImportDataFromBackup restores the database from a backup data structure. It performs a full wipe-and-replace within a single transaction to ensure atomicity.

func (*PostgresStore) IntegrateDataFromBackup added in v1.4.0

func (s *PostgresStore) IntegrateDataFromBackup(backup *model.BackupData) error

IntegrateDataFromBackup restores data from a backup in a non-destructive way, skipping entries that already exist.

func (*PostgresStore) LogAction

func (s *PostgresStore) LogAction(action string, details string) error

func (*PostgresStore) RotateSystemKey

func (s *PostgresStore) RotateSystemKey(publicKey, privateKey string) (int, error)

func (*PostgresStore) SaveBootstrapSession added in v1.4.0

func (s *PostgresStore) SaveBootstrapSession(id, username, hostname, label, tags, tempPublicKey string, expiresAt time.Time, status string) error

SaveBootstrapSession saves a bootstrap session to the database.

func (*PostgresStore) ToggleAccountStatus

func (s *PostgresStore) ToggleAccountStatus(id int) error

func (*PostgresStore) TogglePublicKeyGlobal

func (s *PostgresStore) TogglePublicKeyGlobal(id int) error

func (*PostgresStore) UnassignKeyFromAccount

func (s *PostgresStore) UnassignKeyFromAccount(keyID, accountID int) error

func (*PostgresStore) UpdateAccountHostname added in v1.4.0

func (s *PostgresStore) UpdateAccountHostname(id int, hostname string) error

func (*PostgresStore) UpdateAccountLabel

func (s *PostgresStore) UpdateAccountLabel(id int, label string) error

func (*PostgresStore) UpdateAccountSerial

func (s *PostgresStore) UpdateAccountSerial(id, serial int) error

func (*PostgresStore) UpdateAccountTags

func (s *PostgresStore) UpdateAccountTags(id int, tags string) error

func (*PostgresStore) UpdateBootstrapSessionStatus added in v1.4.0

func (s *PostgresStore) UpdateBootstrapSessionStatus(id string, status string) error

UpdateBootstrapSessionStatus updates the status of a bootstrap session.

type SqliteStore

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

SqliteStore is the SQLite implementation of the Store interface.

func NewSqliteStore

func NewSqliteStore(dataSourceName string) (*SqliteStore, error)

NewSqliteStore initializes the database connection and creates tables if they don't exist.

func (*SqliteStore) AddAccount

func (s *SqliteStore) AddAccount(username, hostname, label, tags string) (int, error)

AddAccount adds a new account to the database.

func (*SqliteStore) AddKnownHostKey

func (s *SqliteStore) AddKnownHostKey(hostname, key string) error

AddKnownHostKey adds a new trusted host key to the database.

func (*SqliteStore) AddPublicKey

func (s *SqliteStore) AddPublicKey(algorithm, keyData, comment string, isGlobal bool) error

AddPublicKey adds a new public key to the database.

func (*SqliteStore) AddPublicKeyAndGetModel

func (s *SqliteStore) AddPublicKeyAndGetModel(algorithm, keyData, comment string, isGlobal bool) (*model.PublicKey, error)

AddPublicKeyAndGetModel adds a public key to the database if it doesn't already exist (based on the comment) and returns the full key model. It returns (nil, nil) if the key is a duplicate.

func (*SqliteStore) AssignKeyToAccount

func (s *SqliteStore) AssignKeyToAccount(keyID, accountID int) error

AssignKeyToAccount creates an association between a key and an account.

func (*SqliteStore) CreateSystemKey

func (s *SqliteStore) CreateSystemKey(publicKey, privateKey string) (int, error)

CreateSystemKey adds a new system key to the database. It determines the correct serial automatically.

func (*SqliteStore) DeleteAccount

func (s *SqliteStore) DeleteAccount(id int) error

DeleteAccount removes an account from the database by its ID.

func (*SqliteStore) DeleteBootstrapSession added in v1.4.0

func (s *SqliteStore) DeleteBootstrapSession(id string) error

DeleteBootstrapSession removes a bootstrap session from the database.

func (*SqliteStore) DeletePublicKey

func (s *SqliteStore) DeletePublicKey(id int) error

DeletePublicKey removes a public key and all its associations. The ON DELETE CASCADE constraint handles the associations in account_keys.

func (*SqliteStore) ExportDataForBackup added in v1.4.0

func (s *SqliteStore) ExportDataForBackup() (*model.BackupData, error)

ExportDataForBackup retrieves all data from the database for a backup. It uses a transaction to ensure a consistent snapshot of the data.

func (*SqliteStore) GetAccountsForKey

func (s *SqliteStore) GetAccountsForKey(keyID int) ([]model.Account, error)

GetAccountsForKey retrieves all accounts that have a specific public key assigned.

func (*SqliteStore) GetActiveSystemKey

func (s *SqliteStore) GetActiveSystemKey() (*model.SystemKey, error)

GetActiveSystemKey retrieves the currently active system key for deployments.

func (*SqliteStore) GetAllAccounts

func (s *SqliteStore) GetAllAccounts() ([]model.Account, error)

GetAllAccounts retrieves all accounts from the database.

func (*SqliteStore) GetAllActiveAccounts

func (s *SqliteStore) GetAllActiveAccounts() ([]model.Account, error)

GetAllActiveAccounts retrieves all active accounts from the database.

func (*SqliteStore) GetAllAuditLogEntries

func (s *SqliteStore) GetAllAuditLogEntries() ([]model.AuditLogEntry, error)

GetAllAuditLogEntries retrieves all entries from the audit log, most recent first.

func (*SqliteStore) GetAllPublicKeys

func (s *SqliteStore) GetAllPublicKeys() ([]model.PublicKey, error)

GetAllPublicKeys retrieves all public keys from the database.

func (*SqliteStore) GetBootstrapSession added in v1.4.0

func (s *SqliteStore) GetBootstrapSession(id string) (*model.BootstrapSession, error)

GetBootstrapSession retrieves a bootstrap session by ID.

func (*SqliteStore) GetExpiredBootstrapSessions added in v1.4.0

func (s *SqliteStore) GetExpiredBootstrapSessions() ([]*model.BootstrapSession, error)

GetExpiredBootstrapSessions returns all expired bootstrap sessions.

func (*SqliteStore) GetGlobalPublicKeys

func (s *SqliteStore) GetGlobalPublicKeys() ([]model.PublicKey, error)

GetGlobalPublicKeys retrieves all keys marked as global.

func (*SqliteStore) GetKeysForAccount

func (s *SqliteStore) GetKeysForAccount(accountID int) ([]model.PublicKey, error)

GetKeysForAccount retrieves all public keys assigned to a specific account.

func (*SqliteStore) GetKnownHostKey

func (s *SqliteStore) GetKnownHostKey(hostname string) (string, error)

GetKnownHostKey retrieves the trusted public key for a given hostname.

func (*SqliteStore) GetOrphanedBootstrapSessions added in v1.4.0

func (s *SqliteStore) GetOrphanedBootstrapSessions() ([]*model.BootstrapSession, error)

GetOrphanedBootstrapSessions returns all orphaned bootstrap sessions.

func (*SqliteStore) GetPublicKeyByComment

func (s *SqliteStore) GetPublicKeyByComment(comment string) (*model.PublicKey, error)

GetPublicKeyByComment retrieves a single public key by its unique comment.

func (*SqliteStore) GetSystemKeyBySerial

func (s *SqliteStore) GetSystemKeyBySerial(serial int) (*model.SystemKey, error)

GetSystemKeyBySerial retrieves a system key by its serial number.

func (*SqliteStore) HasSystemKeys

func (s *SqliteStore) HasSystemKeys() (bool, error)

HasSystemKeys checks if any system keys exist in the database.

func (*SqliteStore) ImportDataFromBackup added in v1.4.0

func (s *SqliteStore) ImportDataFromBackup(backup *model.BackupData) error

ImportDataFromBackup restores the database from a backup data structure. It performs a full wipe-and-replace within a single transaction to ensure atomicity.

func (*SqliteStore) IntegrateDataFromBackup added in v1.4.0

func (s *SqliteStore) IntegrateDataFromBackup(backup *model.BackupData) error

IntegrateDataFromBackup restores data from a backup in a non-destructive way, skipping entries that already exist.

func (*SqliteStore) LogAction

func (s *SqliteStore) LogAction(action string, details string) error

LogAction records an audit trail event.

func (*SqliteStore) RotateSystemKey

func (s *SqliteStore) RotateSystemKey(publicKey, privateKey string) (int, error)

RotateSystemKey deactivates all current system keys and adds a new one as active. This should be performed within a transaction to ensure atomicity.

func (*SqliteStore) SaveBootstrapSession added in v1.4.0

func (s *SqliteStore) SaveBootstrapSession(id, username, hostname, label, tags, tempPublicKey string, expiresAt time.Time, status string) error

SaveBootstrapSession saves a bootstrap session to the database.

func (*SqliteStore) ToggleAccountStatus

func (s *SqliteStore) ToggleAccountStatus(id int) error

ToggleAccountStatus flips the active status of an account.

func (*SqliteStore) TogglePublicKeyGlobal

func (s *SqliteStore) TogglePublicKeyGlobal(id int) error

TogglePublicKeyGlobal flips the 'is_global' status of a public key.

func (*SqliteStore) UnassignKeyFromAccount

func (s *SqliteStore) UnassignKeyFromAccount(keyID, accountID int) error

UnassignKeyFromAccount removes an association between a key and an account.

func (*SqliteStore) UpdateAccountHostname added in v1.4.0

func (s *SqliteStore) UpdateAccountHostname(id int, hostname string) error

UpdateAccountHostname updates the hostname for a given account. This is primarily used for testing to point an account to a mock server.

func (*SqliteStore) UpdateAccountLabel

func (s *SqliteStore) UpdateAccountLabel(id int, label string) error

UpdateAccountLabel updates the label for a given account.

func (*SqliteStore) UpdateAccountSerial

func (s *SqliteStore) UpdateAccountSerial(id, serial int) error

UpdateAccountSerial sets the serial for a given account ID to a specific value.

func (*SqliteStore) UpdateAccountTags

func (s *SqliteStore) UpdateAccountTags(id int, tags string) error

UpdateAccountTags updates the tags for a given account.

func (*SqliteStore) UpdateBootstrapSessionStatus added in v1.4.0

func (s *SqliteStore) UpdateBootstrapSessionStatus(id string, status string) error

UpdateBootstrapSessionStatus updates the status of a bootstrap session.

type Store

type Store interface {
	// Account methods
	GetAllAccounts() ([]model.Account, error)
	AddAccount(username, hostname, label, tags string) (int, error)
	DeleteAccount(id int) error
	UpdateAccountSerial(id, serial int) error
	ToggleAccountStatus(id int) error
	UpdateAccountLabel(id int, label string) error
	UpdateAccountHostname(id int, hostname string) error
	UpdateAccountTags(id int, tags string) error
	GetAllActiveAccounts() ([]model.Account, error)

	// Public Key methods
	AddPublicKey(algorithm, keyData, comment string, isGlobal bool) error
	GetAllPublicKeys() ([]model.PublicKey, error)
	GetPublicKeyByComment(comment string) (*model.PublicKey, error)
	AddPublicKeyAndGetModel(algorithm, keyData, comment string, isGlobal bool) (*model.PublicKey, error)
	TogglePublicKeyGlobal(id int) error
	GetGlobalPublicKeys() ([]model.PublicKey, error)
	DeletePublicKey(id int) error

	// Host Key methods
	GetKnownHostKey(hostname string) (string, error)
	AddKnownHostKey(hostname, key string) error

	// System Key methods
	CreateSystemKey(publicKey, privateKey string) (int, error)
	RotateSystemKey(publicKey, privateKey string) (int, error)
	GetActiveSystemKey() (*model.SystemKey, error)
	GetSystemKeyBySerial(serial int) (*model.SystemKey, error)
	HasSystemKeys() (bool, error)

	// Assignment methods
	AssignKeyToAccount(keyID, accountID int) error
	UnassignKeyFromAccount(keyID, accountID int) error
	GetKeysForAccount(accountID int) ([]model.PublicKey, error)
	GetAccountsForKey(keyID int) ([]model.Account, error)

	// Audit Log methods
	GetAllAuditLogEntries() ([]model.AuditLogEntry, error)
	LogAction(action string, details string) error

	// Bootstrap Session methods
	SaveBootstrapSession(id, username, hostname, label, tags, tempPublicKey string, expiresAt time.Time, status string) error
	GetBootstrapSession(id string) (*model.BootstrapSession, error)
	DeleteBootstrapSession(id string) error
	UpdateBootstrapSessionStatus(id string, status string) error
	GetExpiredBootstrapSessions() ([]*model.BootstrapSession, error)
	GetOrphanedBootstrapSessions() ([]*model.BootstrapSession, error)

	// Backup/Restore methods
	ExportDataForBackup() (*model.BackupData, error)
	ImportDataFromBackup(*model.BackupData) error
	IntegrateDataFromBackup(*model.BackupData) error
}

Store defines the interface for all database operations in Keymaster. This allows for multiple database backends to be implemented.

func NewStore added in v1.4.0

func NewStore(dbType string, db *sql.DB) (Store, error)

NewStore creates and returns a new store instance for the given database type and connection. This is used by InitDB and the migrate command.

Jump to

Keyboard shortcuts

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