From 7aeed60fe13fb1aa4884e22145cb5e27e63f3267 Mon Sep 17 00:00:00 2001 From: Johan Siebens Date: Tue, 13 Feb 2024 11:09:26 +0100 Subject: [PATCH] chore: refactor domain repository interfaces --- internal/domain/account.go | 6 ++ internal/domain/api_key.go | 7 +++ internal/domain/auth_key.go | 11 ++++ internal/domain/authentication_request.go | 6 ++ internal/domain/machine.go | 17 ++++++ internal/domain/registration_request.go | 6 ++ internal/domain/repository.go | 73 ++++------------------- internal/domain/ssh_action_request.go | 6 ++ internal/domain/system_api_key.go | 5 ++ internal/domain/tailnet.go | 8 +++ internal/domain/user.go | 10 ++++ 11 files changed, 93 insertions(+), 62 deletions(-) diff --git a/internal/domain/account.go b/internal/domain/account.go index 5bf22f7..7f93859 100644 --- a/internal/domain/account.go +++ b/internal/domain/account.go @@ -8,6 +8,12 @@ import ( "time" ) +type AccountRepository interface { + GetAccount(ctx context.Context, accountID uint64) (*Account, error) + GetOrCreateAccount(ctx context.Context, externalID, loginName string) (*Account, bool, error) + SetAccountLastAuthenticated(ctx context.Context, accountID uint64) error +} + type Account struct { ID uint64 `gorm:"primary_key"` ExternalID string diff --git a/internal/domain/api_key.go b/internal/domain/api_key.go index 7c7ecb2..b57329c 100644 --- a/internal/domain/api_key.go +++ b/internal/domain/api_key.go @@ -33,6 +33,13 @@ func CreateApiKey(tailnet *Tailnet, user *User, expiresAt *time.Time) (string, * } } +type ApiKeyRepository interface { + SaveApiKey(ctx context.Context, key *ApiKey) error + LoadApiKey(ctx context.Context, key string) (*ApiKey, error) + DeleteApiKeysByTailnet(ctx context.Context, tailnetID uint64) error + DeleteApiKeysByUser(ctx context.Context, userID uint64) error +} + type ApiKey struct { ID uint64 `gorm:"primary_key"` Key string diff --git a/internal/domain/auth_key.go b/internal/domain/auth_key.go index 827527f..4fb546e 100644 --- a/internal/domain/auth_key.go +++ b/internal/domain/auth_key.go @@ -36,6 +36,17 @@ func CreateAuthKey(tailnet *Tailnet, user *User, ephemeral bool, preAuthorized b } } +type AuthKeyRepository interface { + GetAuthKey(ctx context.Context, id uint64) (*AuthKey, error) + SaveAuthKey(ctx context.Context, key *AuthKey) error + DeleteAuthKey(ctx context.Context, id uint64) (bool, error) + DeleteAuthKeysByTailnet(ctx context.Context, tailnetID uint64) error + DeleteAuthKeysByUser(ctx context.Context, userID uint64) error + ListAuthKeys(ctx context.Context, tailnetID uint64) ([]AuthKey, error) + ListAuthKeysByTailnetAndUser(ctx context.Context, tailnetID, userID uint64) ([]AuthKey, error) + LoadAuthKey(ctx context.Context, key string) (*AuthKey, error) +} + type AuthKey struct { ID uint64 `gorm:"primary_key"` Key string diff --git a/internal/domain/authentication_request.go b/internal/domain/authentication_request.go index 9c973f8..41e5725 100644 --- a/internal/domain/authentication_request.go +++ b/internal/domain/authentication_request.go @@ -7,6 +7,12 @@ import ( "time" ) +type AuthenticationRequestRepository interface { + SaveAuthenticationRequest(ctx context.Context, session *AuthenticationRequest) error + GetAuthenticationRequest(ctx context.Context, key string) (*AuthenticationRequest, error) + DeleteAuthenticationRequest(ctx context.Context, key string) error +} + type AuthenticationRequest struct { Key string `gorm:"primary_key"` Token string diff --git a/internal/domain/machine.go b/internal/domain/machine.go index 1cede34..09397a3 100644 --- a/internal/domain/machine.go +++ b/internal/domain/machine.go @@ -13,6 +13,23 @@ import ( "time" ) +type MachineRepository interface { + SaveMachine(ctx context.Context, m *Machine) error + DeleteMachine(ctx context.Context, id uint64) (bool, error) + GetMachine(ctx context.Context, id uint64) (*Machine, error) + GetMachineByKeyAndUser(ctx context.Context, key string, userID uint64) (*Machine, error) + GetMachineByKeys(ctx context.Context, machineKey string, nodeKey string) (*Machine, error) + CountMachinesWithIPv4(ctx context.Context, ip string) (int64, error) + GetNextMachineNameIndex(ctx context.Context, tailnetID uint64, name string) (uint64, error) + ListMachineByTailnet(ctx context.Context, tailnetID uint64) (Machines, error) + CountMachineByTailnet(ctx context.Context, tailnetID uint64) (int64, error) + DeleteMachineByTailnet(ctx context.Context, tailnetID uint64) error + DeleteMachineByUser(ctx context.Context, userID uint64) error + ListMachinePeers(ctx context.Context, tailnetID uint64, machineID uint64) (Machines, error) + ListInactiveEphemeralMachines(ctx context.Context, checkpoint time.Time) (Machines, error) + SetMachineLastSeen(ctx context.Context, machineID uint64) error +} + type Machine struct { ID uint64 `gorm:"primary_key"` Name string diff --git a/internal/domain/registration_request.go b/internal/domain/registration_request.go index b6c5262..d6b9159 100644 --- a/internal/domain/registration_request.go +++ b/internal/domain/registration_request.go @@ -12,6 +12,12 @@ import ( "time" ) +type RegistrationRequestRepository interface { + SaveRegistrationRequest(ctx context.Context, request *RegistrationRequest) error + GetRegistrationRequestByKey(ctx context.Context, key string) (*RegistrationRequest, error) + GetRegistrationRequestByMachineKey(ctx context.Context, key string) (*RegistrationRequest, error) +} + type RegistrationRequest struct { MachineKey string `gorm:"primary_key"` Key string diff --git a/internal/domain/repository.go b/internal/domain/repository.go index 6b95c69..f079664 100644 --- a/internal/domain/repository.go +++ b/internal/domain/repository.go @@ -13,6 +13,17 @@ import ( ) type Repository interface { + AccountRepository + ApiKeyRepository + SystemApiKeyRepository + AuthKeyRepository + MachineRepository + TailnetRepository + UserRepository + AuthenticationRequestRepository + RegistrationRequestRepository + SSHActionRequestRepository + GetControlKeys(ctx context.Context) (*ControlKeys, error) SetControlKeys(ctx context.Context, keys *ControlKeys) error @@ -22,68 +33,6 @@ type Repository interface { GetDERPMap(ctx context.Context) (*DERPMap, error) SetDERPMap(ctx context.Context, v *DERPMap) error - GetAccount(ctx context.Context, accountID uint64) (*Account, error) - GetOrCreateAccount(ctx context.Context, externalID, loginName string) (*Account, bool, error) - SetAccountLastAuthenticated(ctx context.Context, accountID uint64) error - - SaveTailnet(ctx context.Context, tailnet *Tailnet) error - GetTailnet(ctx context.Context, id uint64) (*Tailnet, error) - GetTailnetByName(ctx context.Context, name string) (*Tailnet, error) - ListTailnets(ctx context.Context) ([]Tailnet, error) - DeleteTailnet(ctx context.Context, id uint64) error - - SaveSystemApiKey(ctx context.Context, key *SystemApiKey) error - LoadSystemApiKey(ctx context.Context, key string) (*SystemApiKey, error) - - SaveApiKey(ctx context.Context, key *ApiKey) error - LoadApiKey(ctx context.Context, key string) (*ApiKey, error) - DeleteApiKeysByTailnet(ctx context.Context, tailnetID uint64) error - DeleteApiKeysByUser(ctx context.Context, userID uint64) error - - GetAuthKey(ctx context.Context, id uint64) (*AuthKey, error) - SaveAuthKey(ctx context.Context, key *AuthKey) error - DeleteAuthKey(ctx context.Context, id uint64) (bool, error) - DeleteAuthKeysByTailnet(ctx context.Context, tailnetID uint64) error - DeleteAuthKeysByUser(ctx context.Context, userID uint64) error - ListAuthKeys(ctx context.Context, tailnetID uint64) ([]AuthKey, error) - ListAuthKeysByTailnetAndUser(ctx context.Context, tailnetID, userID uint64) ([]AuthKey, error) - LoadAuthKey(ctx context.Context, key string) (*AuthKey, error) - - GetOrCreateServiceUser(ctx context.Context, tailnet *Tailnet) (*User, bool, error) - GetOrCreateUserWithAccount(ctx context.Context, tailnet *Tailnet, account *Account) (*User, bool, error) - GetUser(ctx context.Context, userID uint64) (*User, error) - DeleteUser(ctx context.Context, userID uint64) error - ListUsers(ctx context.Context, tailnetID uint64) (Users, error) - DeleteUsersByTailnet(ctx context.Context, tailnetID uint64) error - SetUserLastAuthenticated(ctx context.Context, userID uint64, timestamp time.Time) error - - SaveMachine(ctx context.Context, m *Machine) error - DeleteMachine(ctx context.Context, id uint64) (bool, error) - GetMachine(ctx context.Context, id uint64) (*Machine, error) - GetMachineByKeyAndUser(ctx context.Context, key string, userID uint64) (*Machine, error) - GetMachineByKeys(ctx context.Context, machineKey string, nodeKey string) (*Machine, error) - CountMachinesWithIPv4(ctx context.Context, ip string) (int64, error) - GetNextMachineNameIndex(ctx context.Context, tailnetID uint64, name string) (uint64, error) - ListMachineByTailnet(ctx context.Context, tailnetID uint64) (Machines, error) - CountMachineByTailnet(ctx context.Context, tailnetID uint64) (int64, error) - DeleteMachineByTailnet(ctx context.Context, tailnetID uint64) error - DeleteMachineByUser(ctx context.Context, userID uint64) error - ListMachinePeers(ctx context.Context, tailnetID uint64, machineID uint64) (Machines, error) - ListInactiveEphemeralMachines(ctx context.Context, checkpoint time.Time) (Machines, error) - SetMachineLastSeen(ctx context.Context, machineID uint64) error - - SaveRegistrationRequest(ctx context.Context, request *RegistrationRequest) error - GetRegistrationRequestByKey(ctx context.Context, key string) (*RegistrationRequest, error) - GetRegistrationRequestByMachineKey(ctx context.Context, key string) (*RegistrationRequest, error) - - SaveAuthenticationRequest(ctx context.Context, session *AuthenticationRequest) error - GetAuthenticationRequest(ctx context.Context, key string) (*AuthenticationRequest, error) - DeleteAuthenticationRequest(ctx context.Context, key string) error - - SaveSSHActionRequest(ctx context.Context, session *SSHActionRequest) error - GetSSHActionRequest(ctx context.Context, key string) (*SSHActionRequest, error) - DeleteSSHActionRequest(ctx context.Context, key string) error - Transaction(func(rp Repository) error) error } diff --git a/internal/domain/ssh_action_request.go b/internal/domain/ssh_action_request.go index 737831a..16d172d 100644 --- a/internal/domain/ssh_action_request.go +++ b/internal/domain/ssh_action_request.go @@ -7,6 +7,12 @@ import ( "time" ) +type SSHActionRequestRepository interface { + SaveSSHActionRequest(ctx context.Context, session *SSHActionRequest) error + GetSSHActionRequest(ctx context.Context, key string) (*SSHActionRequest, error) + DeleteSSHActionRequest(ctx context.Context, key string) error +} + type SSHActionRequest struct { Key string `gorm:"primary_key"` Action string diff --git a/internal/domain/system_api_key.go b/internal/domain/system_api_key.go index 7e216df..436291d 100644 --- a/internal/domain/system_api_key.go +++ b/internal/domain/system_api_key.go @@ -32,6 +32,11 @@ func CreateSystemApiKey(account *Account, expiresAt *time.Time) (string, *System } } +type SystemApiKeyRepository interface { + SaveSystemApiKey(ctx context.Context, key *SystemApiKey) error + LoadSystemApiKey(ctx context.Context, key string) (*SystemApiKey, error) +} + type SystemApiKey struct { ID uint64 `gorm:"primary_key"` Key string diff --git a/internal/domain/tailnet.go b/internal/domain/tailnet.go index 2fdcc11..80e9e6c 100644 --- a/internal/domain/tailnet.go +++ b/internal/domain/tailnet.go @@ -22,6 +22,14 @@ type Tailnet struct { MachineAuthorizationEnabled bool } +type TailnetRepository interface { + SaveTailnet(ctx context.Context, tailnet *Tailnet) error + GetTailnet(ctx context.Context, id uint64) (*Tailnet, error) + GetTailnetByName(ctx context.Context, name string) (*Tailnet, error) + ListTailnets(ctx context.Context) ([]Tailnet, error) + DeleteTailnet(ctx context.Context, id uint64) error +} + func (t Tailnet) GetDERPMap(ctx context.Context, fallack DefaultDERPMap) (*DERPMap, error) { if t.DERPMap.Checksum == "" { return fallack.GetDERPMap(ctx) diff --git a/internal/domain/user.go b/internal/domain/user.go index 70767f0..fc57f1b 100644 --- a/internal/domain/user.go +++ b/internal/domain/user.go @@ -38,6 +38,16 @@ func (s UserRole) IsAdmin() bool { return s == UserRoleAdmin } +type UserRepository interface { + GetOrCreateServiceUser(ctx context.Context, tailnet *Tailnet) (*User, bool, error) + GetOrCreateUserWithAccount(ctx context.Context, tailnet *Tailnet, account *Account) (*User, bool, error) + GetUser(ctx context.Context, userID uint64) (*User, error) + DeleteUser(ctx context.Context, userID uint64) error + ListUsers(ctx context.Context, tailnetID uint64) (Users, error) + DeleteUsersByTailnet(ctx context.Context, tailnetID uint64) error + SetUserLastAuthenticated(ctx context.Context, userID uint64, timestamp time.Time) error +} + type User struct { ID uint64 `gorm:"primary_key"` Name string