database

package
v0.0.0-...-9e55594 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package database provides connection management for the integrated Credentials Config Service database. It supports PostgreSQL, MySQL, and SQLite backends, selected via the config.Database.Type field.

Index

Constants

View Source
const (
	// DriverTypePostgres selects the PostgreSQL driver.
	DriverTypePostgres = "postgres"
	// DriverTypeMySQL selects the MySQL/MariaDB driver.
	DriverTypeMySQL = "mysql"
	// DriverTypeSQLite selects the pure-Go SQLite driver.
	DriverTypeSQLite = "sqlite"
)

Supported database driver type constants.

Variables

View Source
var (
	// ErrServiceNotFound is returned when a service ID does not exist.
	ErrServiceNotFound = errors.New("service not found")
	// ErrServiceAlreadyExists is returned on a duplicate service ID insert.
	ErrServiceAlreadyExists = errors.New("service already exists")
)

Sentinel errors returned by ServiceRepository methods.

Functions

func Close

func Close(db *sql.DB)

Close gracefully closes the database connection pool. It logs any error but does not return it, making it convenient for deferred calls.

func InitSchema

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

InitSchema creates the service and scope_entry tables if they do not already exist. The DDL is database-type-aware: PostgreSQL uses BIGSERIAL, SQLite uses INTEGER PRIMARY KEY AUTOINCREMENT, and MySQL uses BIGINT AUTO_INCREMENT. The function is idempotent — calling it multiple times is safe.

func NewConnection

func NewConnection(cfg config.Database) (*sql.DB, error)

NewConnection opens a database connection pool based on the provided configuration. The returned *sql.DB is ready to use and has been verified with a ping. Callers are responsible for closing it when done.

func RowToService

func RowToService(row ServiceRow, scopeRows []ScopeEntryRow) (config.ConfiguredService, error)

RowToService assembles a config.ConfiguredService from a ServiceRow and its associated ScopeEntryRow values, unmarshalling JSON text columns back into typed Go structs.

Types

type ScopeEntryRow

type ScopeEntryRow struct {
	// ID is the auto-generated primary key.
	ID int64
	// ServiceID is the foreign key referencing service.id.
	ServiceID string
	// ScopeKey is the OIDC scope name (map key in ServiceScopes).
	ScopeKey string
	// Credentials is a JSON-encoded array of config.Credential objects.
	Credentials string
	// PresentationDefinition is a JSON-encoded config.PresentationDefinition; may be nil.
	PresentationDefinition *string
	// FlatClaims indicates whether claims should be flattened in the JWT.
	FlatClaims bool
	// DcqlQuery is a JSON-encoded config.DCQL object; may be nil.
	DcqlQuery *string
}

ScopeEntryRow represents a row in the scope_entry table.

func ScopeEntryToRows

func ScopeEntryToRows(serviceID string, scopes map[string]config.ScopeEntry) ([]ScopeEntryRow, error)

ScopeEntryToRows converts the ServiceScopes map from a ConfiguredService into a slice of ScopeEntryRow values, marshalling the complex fields to JSON text. An error is returned if any JSON serialisation fails.

type ServiceRepository

type ServiceRepository interface {
	// CreateService persists a new service together with all its scope entries.
	// Returns ErrServiceAlreadyExists if a service with the same ID exists.
	CreateService(ctx context.Context, service config.ConfiguredService) error

	// GetService retrieves a single service by ID, including all scope entries.
	// Returns ErrServiceNotFound if the ID does not exist.
	GetService(ctx context.Context, id string) (config.ConfiguredService, error)

	// GetAllServices returns a page of services ordered by ID and the total
	// count across all pages. page is zero-based.
	GetAllServices(ctx context.Context, page, pageSize int) ([]config.ConfiguredService, int, error)

	// UpdateService replaces the service row and all its scope entries.
	// Returns ErrServiceNotFound if the ID does not exist. Returns the
	// updated service (re-read from DB) for response purposes.
	UpdateService(ctx context.Context, id string, service config.ConfiguredService) (config.ConfiguredService, error)

	// DeleteService removes a service and its scope entries (via CASCADE).
	// Returns ErrServiceNotFound if the ID does not exist.
	DeleteService(ctx context.Context, id string) error

	// GetServiceScopes returns the credential types required for a scope.
	// When oidcScope is nil, the service's default scope is used.
	// Returns ErrServiceNotFound when the service does not exist, or
	// config.ErrorNoSuchScope when the resolved scope is not configured.
	GetServiceScopes(ctx context.Context, id string, oidcScope *string) ([]string, error)

	// ServiceExists checks whether a service with the given ID exists.
	ServiceExists(ctx context.Context, id string) (bool, error)
}

ServiceRepository defines the data-access operations for CCS services and their scope entries. Implementations must be safe for concurrent use.

type ServiceRow

type ServiceRow struct {
	// ID is the unique service identifier (primary key).
	ID string
	// DefaultOidcScope is the default OIDC scope name; may be nil.
	DefaultOidcScope *string
	// AuthorizationType describes the authorization mode; may be nil.
	AuthorizationType *string
}

ServiceRow represents a row in the service table.

func ServiceToRow

func ServiceToRow(service config.ConfiguredService) ServiceRow

ServiceToRow converts a config.ConfiguredService into a ServiceRow. The scope entries are handled separately via ScopeEntryToRows.

type SqlServiceRepository

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

SqlServiceRepository is a ServiceRepository backed by database/sql.

func NewServiceRepository

func NewServiceRepository(db *sql.DB, dbType string) *SqlServiceRepository

NewServiceRepository creates a new SqlServiceRepository for the provided database connection and driver type. The dbType must be one of the DriverType* constants and is used to adapt SQL placeholder syntax.

func (*SqlServiceRepository) CreateService

func (r *SqlServiceRepository) CreateService(ctx context.Context, service config.ConfiguredService) error

CreateService persists a new service and its scope entries within a single transaction. Returns ErrServiceAlreadyExists on duplicate ID.

func (*SqlServiceRepository) DeleteService

func (r *SqlServiceRepository) DeleteService(ctx context.Context, id string) error

DeleteService removes a service. Scope entries are cascade-deleted.

func (*SqlServiceRepository) GetAllServices

func (r *SqlServiceRepository) GetAllServices(ctx context.Context, page, pageSize int) ([]config.ConfiguredService, int, error)

GetAllServices returns a page of services and the total service count.

func (*SqlServiceRepository) GetService

GetService retrieves a single service by ID.

func (*SqlServiceRepository) GetServiceScopes

func (r *SqlServiceRepository) GetServiceScopes(ctx context.Context, id string, oidcScope *string) ([]string, error)

GetServiceScopes returns the credential type names required for the given scope. When oidcScope is nil the service's default scope is used.

func (*SqlServiceRepository) ServiceExists

func (r *SqlServiceRepository) ServiceExists(ctx context.Context, id string) (bool, error)

ServiceExists returns true if a service with the given ID exists.

func (*SqlServiceRepository) UpdateService

UpdateService replaces a service's data and all its scope entries.

Jump to

Keyboard shortcuts

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