database

package
v0.0.0-...-c8edb77 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrForbidden = errors.New("forbidden")
	// ErrReservedGroupPrefix indicates a requested group name collides with the
	// reserved prefix used for automatically managed personal groups.
	ErrReservedGroupPrefix = errors.New("reserved group name prefix 'user-'")
)
View Source
var DirectorDB *gorm.DB
View Source
var ServerDatabase *gorm.DB

Functions

func AddCollectionMembers

func AddCollectionMembers(db *gorm.DB, id string, members []string, addedBy string, groups []string, isAdmin bool) error

func AddGroupMember

func AddGroupMember(db *gorm.DB, groupId, userId, addedByUserId string) error

func CreateApiKey

func CreateApiKey(db *gorm.DB, name, createdBy, scopes string, expiration time.Time) (string, error)

CreateApiKey creates a new API key with the given name, creator, scopes, and expiration time. It returns the API key in the format "$ID.$SECRET_IN_HEX" and an error if an error occurred. The scopes can are a comma-separated list of capabilities. i.e "monitoring.query,monitoring.scrape" The scopes are defined in the token_scopes package

func CreateCounter

func CreateCounter(key string, value int) error

func CreateDowntime

func CreateDowntime(downtime *server_structs.Downtime) error

CRUD operations for downtimes table Create a new downtime entry

func CreateOrUpdateCounter

func CreateOrUpdateCounter(key string, value int) error

func DeleteApiKey

func DeleteApiKey(db *gorm.DB, id string, verifiedKeysCache *ttlcache.Cache[string, server_structs.ApiKeyCached]) error

DeleteApiKey deletes the API key with the given ID. It returns an error if an error occurred. It also removes the API key from the verifiedKeysCache so that the deleted key is no longer valid.

func DeleteCollection

func DeleteCollection(db *gorm.DB, id string, owner string, groups []string, isAdmin bool) error

func DeleteCollectionMetadata

func DeleteCollectionMetadata(db *gorm.DB, id, user string, groups []string, key string, isAdmin bool) error

func DeleteDowntime

func DeleteDowntime(uuid string) error

Delete a downtime entry by UUID (hard delete)

func GetAllDowntimes

func GetAllDowntimes(source string) ([]server_structs.Downtime, error)

Retrieve all downtime entries

func GetDowntimeByUUID

func GetDowntimeByUUID(uuid string) (*server_structs.Downtime, error)

Retrieve a downtime entry by UUID

func GetIncompleteDowntimes

func GetIncompleteDowntimes(source string) ([]server_structs.Downtime, error)

Retrieve all downtime entries where EndTime is later than the current UTC time.

func GetServerLocalMetadata

func GetServerLocalMetadata() (server_structs.ServerLocalMetadata, error)

Retrieve the server local metadata in use - lookup the entry whose UpdatedAt is the most recent

func GetServerLocalMetadataHistory

func GetServerLocalMetadataHistory() ([]server_structs.ServerLocalMetadata, error)

Retrieve server local metadata history from most recent to oldest

func GrantCollectionAcl

func GrantCollectionAcl(db *gorm.DB, id, user string, groups []string, groupId string, role AclRole, expiresAt *time.Time, isAdmin bool) error

func InitServerDatabase

func InitServerDatabase(serverType server_structs.ServerType) error

Initialize a centralized server database and run universal and server-type-specific migrations

func InsertMockDowntime

func InsertMockDowntime(d server_structs.Downtime) error

func ListApiKeys

func ListApiKeys(db *gorm.DB) ([]server_structs.ApiKey, error)

func RemoveCollectionMembers

func RemoveCollectionMembers(db *gorm.DB, id string, members []string, user string, groups []string, isAdmin bool) error

func RemoveGroupMember

func RemoveGroupMember(db *gorm.DB, groupId, userId, removedByUserId string) error

func RevokeCollectionAcl

func RevokeCollectionAcl(db *gorm.DB, id, user string, groups []string, groupId string, role AclRole, isAdmin bool) error

func SetupMockDowntimeDB

func SetupMockDowntimeDB(t *testing.T)

Test helper functions for Downtime

func ShutdownDB

func ShutdownDB() error

func SoftDeleteServerLocalMetadata

func SoftDeleteServerLocalMetadata(id string) error

Mark a server local metadata as deleted without actually removing it from the database

func TeardownMockDowntimeDB

func TeardownMockDowntimeDB(t *testing.T)

func UpdateCollection

func UpdateCollection(db *gorm.DB, id, user string, groups []string, name, description *string, visibility *Visibility, isAdmin bool) error

func UpdateDowntime

func UpdateDowntime(uuid string, updatedDowntime *server_structs.Downtime) error

Update an existing downtime entry by UUID

func UpsertCollectionMetadata

func UpsertCollectionMetadata(db *gorm.DB, id, user string, groups []string, key, value string, isAdmin bool) error

func UpsertServerLocalMetadata

func UpsertServerLocalMetadata(metadata server_structs.ServerRegistration) error

Create or update a record to sync local server metadata with the Registry Server id is an unique 7 characters string randomly generated by the server itself during initial registration, consisting of [0-9a-z], e.g. 18f1jk5 Server name is a human-friendly name set by the admin via SiteName field in webUI or Xrootd.Sitename in local config during initial registration, e.g. "UW_OSDF_CACHE" 1) If no such row exists, it inserts a new one. 2) If a row with that server ID exists, it updates the existing entry.

func VerifyApiKey

func VerifyApiKey(db *gorm.DB, apiKey string, verifiedKeysCache *ttlcache.Cache[string, server_structs.ApiKeyCached]) (bool, []string, error)

VerifyApiKey verifies the API key and returns the capabilities associated with the key. It assumes that the API key is in the format "$ID.$SECRET_IN_HEX". It returns true if the API key is valid, false if the API key is invalid, and an error if an error occurred. If the API key is valid, it also returns the capabilities associated with the key.

Types

type AclRole

type AclRole string
const (
	AclRoleRead  AclRole = "read"
	AclRoleWrite AclRole = "write"
	AclRoleOwner AclRole = "owner"
)

type Collection

type Collection struct {
	ID          string               `gorm:"primaryKey" json:"id"`
	Name        string               `gorm:"not null;uniqueIndex:idx_owner_name" json:"name"`
	Description string               `json:"description"`
	Owner       string               `gorm:"not null;uniqueIndex:idx_owner_name" json:"owner"`
	Namespace   string               `gorm:"not null" json:"namespace"`
	Visibility  Visibility           `gorm:"not null;default:private" json:"visibility"`
	CreatedAt   time.Time            `gorm:"not null;default:CURRENT_TIMESTAMP" json:"createdAt"`
	UpdatedAt   time.Time            `gorm:"not null;default:CURRENT_TIMESTAMP" json:"updatedAt"`
	Members     []CollectionMember   `gorm:"foreignKey:CollectionID" json:"members"`
	ACLs        []CollectionACL      `gorm:"foreignKey:CollectionID" json:"acls"`
	Metadata    []CollectionMetadata `gorm:"foreignKey:CollectionID" json:"metadata"`
}

func CreateCollection

func CreateCollection(db *gorm.DB, name, description, owner, namespace string, visibility Visibility) (*Collection, error)

func CreateCollectionWithMetadata

func CreateCollectionWithMetadata(db *gorm.DB, name, description, owner, namespace string, visibility Visibility, metadata map[string]string) (*Collection, error)

func GetCollection

func GetCollection(db *gorm.DB, id string, user string, groups []string) (*Collection, error)

func ListCollections

func ListCollections(db *gorm.DB, user string, groups []string) ([]Collection, error)

type CollectionACL

type CollectionACL struct {
	CollectionID string     `gorm:"primaryKey" json:"collectionId"`
	GroupID      string     `gorm:"primaryKey" json:"groupId"`
	Role         AclRole    `gorm:"primaryKey;not null" json:"role"`
	GrantedBy    string     `gorm:"not null" json:"createdBy"`
	GrantedAt    time.Time  `gorm:"not null;default:CURRENT_TIMESTAMP" json:"createdAt"`
	ExpiresAt    *time.Time `json:"expiresAt"`
}

func GetCollectionAcls

func GetCollectionAcls(db *gorm.DB, id, user string, groups []string) ([]CollectionACL, error)

type CollectionMember

type CollectionMember struct {
	CollectionID string    `gorm:"primaryKey" json:"collectionId"`
	ObjectURL    string    `gorm:"primaryKey" json:"objectUrl"` // full pelican:// URL
	AddedBy      string    `gorm:"not null" json:"createdBy"`
	AddedAt      time.Time `gorm:"not null;default:CURRENT_TIMESTAMP" json:"createdAt"`
}

func GetCollectionMembers

func GetCollectionMembers(db *gorm.DB, id, user string, groups []string, since *time.Time, limit int) ([]CollectionMember, error)

type CollectionMetadata

type CollectionMetadata struct {
	CollectionID string `gorm:"primaryKey" json:"collectionId"`
	Key          string `gorm:"primaryKey;not null" json:"key"`
	Value        string `gorm:"not null" json:"value"`
}

func GetCollectionMetadata

func GetCollectionMetadata(db *gorm.DB, id, user string, groups []string) ([]CollectionMetadata, error)

type Counter

type Counter struct {
	Key   string `gorm:"primaryKey"`
	Value int    `gorm:"not null;default:0"`
}

type Group

type Group struct {
	ID          string        `gorm:"primaryKey" json:"id"`
	Name        string        `gorm:"not null;unique" json:"name"`
	Description string        `json:"description"`
	CreatedBy   string        `gorm:"not null" json:"createdBy"`
	CreatedAt   time.Time     `gorm:"not null;default:CURRENT_TIMESTAMP" json:"createdAt"`
	Members     []GroupMember `gorm:"foreignKey:GroupID" json:"members"`
}

func CreateGroup

func CreateGroup(db *gorm.DB, name, description, createdByUserID string, groups []string) (*Group, error)

func GetGroupWithMembers

func GetGroupWithMembers(db *gorm.DB, groupId string) (*Group, error)

func GetMemberGroups

func GetMemberGroups(db *gorm.DB, userId string) ([]Group, error)

func ListGroups

func ListGroups(db *gorm.DB) ([]Group, error)

type GroupMember

type GroupMember struct {
	GroupID string    `gorm:"primaryKey" json:"groupId"`
	UserID  string    `gorm:"primaryKey" json:"userId"`
	User    User      `gorm:"foreignKey:UserID" json:"user"`
	AddedBy string    `gorm:"not null" json:"createdBy"`
	AddedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP" json:"createdAt"`
}

type User

type User struct {
	ID        string    `gorm:"primaryKey" json:"id"`
	Username  string    `gorm:"not null;uniqueIndex:idx_user_issuer" json:"username"`
	Sub       string    `gorm:"not null;uniqueIndex:idx_user_sub_issuer" json:"sub"`
	Issuer    string    `gorm:"not null;uniqueIndex:idx_user_issuer;uniqueIndex:idx_user_sub_issuer" json:"issuer"`
	CreatedAt time.Time `gorm:"not null;default:CURRENT_TIMESTAMP" json:"createdAt"`
}

func CreateUser

func CreateUser(db *gorm.DB, username string, sub string, issuer string) (*User, error)

func GetOrCreateUser

func GetOrCreateUser(db *gorm.DB, username string, sub string, issuer string) (*User, error)

func GetUserByUsername

func GetUserByUsername(db *gorm.DB, username string) (*User, error)

func ListUsers

func ListUsers(db *gorm.DB) ([]User, error)

type Visibility

type Visibility string
const (
	VisibilityPrivate Visibility = "private"
	VisibilityPublic  Visibility = "public"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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