clients

package
v0.0.0-...-8fd811e Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: MIT Imports: 36 Imported by: 0

Documentation

Overview

Package contains everything related to Client Repository.

Generating data for tests is always cumbersome. To make it easier this package should be a single source of truth for generating Clients data.

This package provides a builder that can generate Clients with: - preset fields, - randomly generated fields, - fields set on demand.

It can be extended by needs.

Index

Constants

View Source
const DefaultInitialClientsArraySize = 64

Variables

View Source
var DataSourceOptions = sqlite.DataSourceOptions{WALEnabled: false}
View Source
var OptionsListDefaultFields = map[string][]string{
	"fields[clients]": {
		"id",
		"name",
		"hostname",
	},
}
View Source
var OptionsSupportedFields = map[string]map[string]bool{
	"clients": {
		"id":                       true,
		"name":                     true,
		"os":                       true,
		"os_arch":                  true,
		"os_family":                true,
		"os_kernel":                true,
		"hostname":                 true,
		"ipv4":                     true,
		"ipv6":                     true,
		"tags":                     true,
		"labels":                   true,
		"version":                  true,
		"address":                  true,
		"tunnels":                  true,
		"disconnected_at":          true,
		"last_heartbeat_at":        true,
		"connection_state":         true,
		"client_auth_id":           true,
		"os_full_name":             true,
		"os_version":               true,
		"os_virtualization_system": true,
		"os_virtualization_role":   true,
		"cpu_family":               true,
		"cpu_model":                true,
		"cpu_model_name":           true,
		"cpu_vendor":               true,
		"timezone":                 true,
		"num_cpus":                 true,
		"mem_total":                true,
		"allowed_user_groups":      true,
		"updates_status":           true,
		"ip_addresses":             true,
		"client_configuration":     true,
		"groups":                   true,
	},
}
View Source
var OptionsSupportedFilters = map[string]bool{
	"id":                       true,
	"name":                     true,
	"os":                       true,
	"os_arch":                  true,
	"os_family":                true,
	"os_kernel":                true,
	"os_full_name":             true,
	"os_version":               true,
	"os_virtualization_system": true,
	"os_virtualization_role":   true,
	"cpu_family":               true,
	"cpu_model":                true,
	"cpu_model_name":           true,
	"cpu_vendor":               true,
	"num_cpus":                 true,
	"timezone":                 true,
	"hostname":                 true,
	"ipv4":                     true,
	"ipv6":                     true,
	"tags":                     true,
	"labels":                   true,
	"version":                  true,
	"address":                  true,
	"client_auth_id":           true,
	"allowed_user_groups":      true,
	"groups":                   true,
	"connection_state":         true,
}
View Source
var OptionsSupportedSorts = map[string]bool{
	"id":       true,
	"name":     true,
	"os":       true,
	"hostname": true,
	"version":  true,
}

Functions

func FormatConnectionState

func FormatConnectionState(client *clientdata.Client) string

func LoadInitialClients

func LoadInitialClients(ctx context.Context, p ClientStore, logger *logger.Logger) ([]*clientdata.Client, error)

LoadInitialClients returns an initial Client Repository state populated with clients from the internal storage.

func SortByHostname

func SortByHostname(a []*clientdata.CalculatedClient, desc bool)

func SortByID

func SortByID(a []*clientdata.CalculatedClient, desc bool)

func SortByName

func SortByName(a []*clientdata.CalculatedClient, desc bool)

func SortByOS

func SortByOS(a []*clientdata.CalculatedClient, desc bool)

func SortByVersion

func SortByVersion(a []*clientdata.CalculatedClient, desc bool)

Types

type CleanupTask

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

func NewCleanupTask

func NewCleanupTask(log *logger.Logger, cr *ClientRepository) *CleanupTask

NewCleanupTask returns a task to cleanup Client Repository from obsolete clients.

func (*CleanupTask) Run

func (t *CleanupTask) Run(ctx context.Context) error

type ClientBuilder

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

func New

func New(t *testing.T) ClientBuilder

New returns a builder to generate a client that can be used in tests.

func (ClientBuilder) AllowedUserGroups

func (b ClientBuilder) AllowedUserGroups(allowedUserGroups []string) ClientBuilder

func (ClientBuilder) Build

func (b ClientBuilder) Build() *clientdata.Client

func (ClientBuilder) ClientAuthID

func (b ClientBuilder) ClientAuthID(clientAuthID string) ClientBuilder

func (ClientBuilder) Config

func (ClientBuilder) Connection

func (b ClientBuilder) Connection(conn ssh.Conn) ClientBuilder

func (ClientBuilder) DisconnectedDuration

func (b ClientBuilder) DisconnectedDuration(disconnectedDuration time.Duration) ClientBuilder

func (ClientBuilder) ID

func (ClientBuilder) Logger

type ClientPayload

type ClientPayload struct {
	ID                     *string                 `json:"id,omitempty"`
	Name                   *string                 `json:"name,omitempty"`
	Address                *string                 `json:"address,omitempty"`
	Hostname               *string                 `json:"hostname,omitempty"`
	OS                     *string                 `json:"os,omitempty"`
	OSFullName             *string                 `json:"os_full_name,omitempty"`
	OSVersion              *string                 `json:"os_version,omitempty"`
	OSArch                 *string                 `json:"os_arch,omitempty"`
	OSFamily               *string                 `json:"os_family,omitempty"`
	OSKernel               *string                 `json:"os_kernel,omitempty"`
	OSVirtualizationSystem *string                 `json:"os_virtualization_system,omitempty"`
	OSVirtualizationRole   *string                 `json:"os_virtualization_role,omitempty"`
	NumCPUs                *int                    `json:"num_cpus,omitempty"`
	CPUFamily              *string                 `json:"cpu_family,omitempty"`
	CPUModel               *string                 `json:"cpu_model,omitempty"`
	CPUModelName           *string                 `json:"cpu_model_name,omitempty"`
	CPUVendor              *string                 `json:"cpu_vendor,omitempty"`
	MemoryTotal            *uint64                 `json:"mem_total,omitempty"`
	Timezone               *string                 `json:"timezone,omitempty"`
	ClientAuthID           *string                 `json:"client_auth_id,omitempty"`
	Version                *string                 `json:"version,omitempty"`
	DisconnectedAt         **time.Time             `json:"disconnected_at,omitempty"`
	LastHeartbeatAt        **time.Time             `json:"last_heartbeat_at,omitempty"`
	ConnectionState        *string                 `json:"connection_state,omitempty"`
	IPv4                   *[]string               `json:"ipv4,omitempty"`
	IPv6                   *[]string               `json:"ipv6,omitempty"`
	Tags                   *[]string               `json:"tags,omitempty"`
	AllowedUserGroups      *[]string               `json:"allowed_user_groups,omitempty"`
	Tunnels                *[]*clienttunnel.Tunnel `json:"tunnels,omitempty"`
	UpdatesStatus          **models.UpdatesStatus  `json:"updates_status,omitempty"`
	IPAddresses            **models.IPAddresses    `json:"ext_ip_addresses,omitempty"`
	ClientConfiguration    **clientconfig.Config   `json:"client_configuration,omitempty"`
	Groups                 *[]string               `json:"groups,omitempty"`
	Labels                 *map[string]string      `json:"labels,omitempty"`
}

func ConvertToClientPayload

func ConvertToClientPayload(client *clientdata.CalculatedClient, fields []query.FieldsOption) ClientPayload

func ConvertToClientsPayload

func ConvertToClientsPayload(clientsList []*clientdata.CalculatedClient, fields []query.FieldsOption) []ClientPayload

type ClientQueryFn

type ClientQueryFn func(client *clientdata.Client) (match bool)

type ClientRepository

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

func InitClientRepository

func InitClientRepository(
	ctx context.Context,
	db *sqlx.DB,
	keepDisconnectedClients *time.Duration,
	logger *logger.Logger,
) (*ClientRepository, error)

func NewClientRepository

func NewClientRepository(initClients []*clientdata.Client, keepDisconnectedClients *time.Duration, logger *logger.Logger) *ClientRepository

NewClientRepository returns a new thread-safe in-memory cache to store client connections populated with given clients if any. keepDisconnectedClients is a duration to keep disconnected clients. If a client was disconnected longer than a given duration it will be treated as obsolete.

func NewClientRepositoryWithDB

func NewClientRepositoryWithDB(initialClients []*clientdata.Client, keepDisconnectedClients *time.Duration, store ClientStore, logger *logger.Logger) *ClientRepository

NewClientRepositoryWithDB @todo: used for test setup in two separate packages. need to review use as part of the test code refactoring.

func (*ClientRepository) Count

func (r *ClientRepository) Count() int

Count returns a number of non-obsolete active and disconnected clients.

func (*ClientRepository) CountActive

func (r *ClientRepository) CountActive() (count int)

CountActive returns a number of active clients.

func (*ClientRepository) CountDisconnected

func (r *ClientRepository) CountDisconnected() (int, error)

CountDisconnected returns a number of disconnected clients.

func (*ClientRepository) Delete

func (r *ClientRepository) Delete(client *clientdata.Client) error

func (*ClientRepository) DeleteObsolete

func (r *ClientRepository) DeleteObsolete() ([]*clientdata.Client, error)

DeleteObsolete deletes obsolete disconnected clients and returns them.

func (*ClientRepository) GetActiveByID

func (r *ClientRepository) GetActiveByID(id string) (*clientdata.Client, error)

GetActiveByID returns an active client by a given id.

func (*ClientRepository) GetAllActiveClients

func (r *ClientRepository) GetAllActiveClients() (matchingClients []*clientdata.Client)

GetAllActiveClients returns a new client array that can be used without locks (assuming not shared)

func (*ClientRepository) GetAllByClientAuthID

func (r *ClientRepository) GetAllByClientAuthID(clientAuthID string) (matchingClients []*clientdata.Client)

GetAllByClientAuthID @todo: make it consistent with others whether to return an error. In general it's just a cache, so should not return an err.

func (*ClientRepository) GetAllClients

func (r *ClientRepository) GetAllClients() []*clientdata.Client

GetAll returns all non-obsolete active and disconnected client clients.

func (*ClientRepository) GetByID

func (r *ClientRepository) GetByID(id string) (*clientdata.Client, error)

GetByID returns non-obsolete active or disconnected client by a given id.

func (*ClientRepository) GetClientsByTag

func (r *ClientRepository) GetClientsByTag(tags []string, operator string, allowDisconnected bool) (matchingClients []*clientdata.Client, err error)

func (*ClientRepository) GetFilteredUserClients

func (r *ClientRepository) GetFilteredUserClients(user User, filterOptions []query.FilterOption, groups []*cgroups.ClientGroup) (matchingClients []*clientdata.CalculatedClient, err error)

GetFilteredUserClients returns all non-obsolete active and disconnected clients that current user has access to, filtered by parameters

func (*ClientRepository) GetKeepDisconnectedClients

func (r *ClientRepository) GetKeepDisconnectedClients() (keep *time.Duration)

func (*ClientRepository) GetPostSaveHandlerFn

func (r *ClientRepository) GetPostSaveHandlerFn() (handlerFn func(cl *clientdata.Client))

func (*ClientRepository) GetUserClients

func (r *ClientRepository) GetUserClients(user User, groups []*cgroups.ClientGroup) []*clientdata.Client

GetUserClients returns all non-obsolete active and disconnected clients that current user has access to

func (*ClientRepository) Save

func (r *ClientRepository) Save(cl *clientdata.Client) error

func (*ClientRepository) SetPostSaveHandlerFn

func (r *ClientRepository) SetPostSaveHandlerFn(handlerFn func(cl *clientdata.Client))

type ClientService

type ClientService interface {
	SetPlusLicenseInfoCap(licensecap licensecap.CapabilityEx)
	SetPlusAlertingServiceCap(as alertingcap.Service)

	Count() int
	CountActive() int
	CountDisconnected() (int, error)
	GetByID(id string) (*clientdata.Client, error)
	GetActiveByID(id string) (*clientdata.Client, error)
	GetByGroups(groups []*cgroups.ClientGroup) ([]*clientdata.Client, error)
	GetClientsByTag(tags []string, operator string, allowDisconnected bool) (clients []*clientdata.Client, err error)
	GetAllByClientID(clientID string) []*clientdata.Client
	GetAll() []*clientdata.Client
	GetUserClients(groups []*cgroups.ClientGroup, user User) []*clientdata.Client
	GetFilteredUserClients(user User, filterOptions []query.FilterOption, groups []*cgroups.ClientGroup) ([]*clientdata.CalculatedClient, error)

	PopulateGroupsWithUserClients(groups []*cgroups.ClientGroup, user User)
	UpdateClientStatus()

	StartClient(
		ctx context.Context, clientAuthID, clientID string, sshConn ssh.Conn, authMultiuseCreds bool,
		req *chshare.ConnectionRequest, clog *logger.Logger,
	) (*clientdata.Client, error)
	Terminate(client *clientdata.Client) error
	ForceDelete(client *clientdata.Client) error
	DeleteOffline(clientID string) error

	SetACL(clientID string, allowedUserGroups []string) error
	CheckClientAccess(clientID string, user User, groups []*cgroups.ClientGroup) error
	CheckClientsAccess(clients []*clientdata.Client, user User, groups []*cgroups.ClientGroup) error

	SetUpdatesStatus(clientID string, updatesStatus *models.UpdatesStatus) error
	SetLastHeartbeat(clientID string, heartbeat time.Time) error
	SetIPAddresses(clientID string, IPAddresses *models.IPAddresses) error

	GetRepo() *ClientRepository

	SetCaddyAPI(capi caddy.API)
	StartClientTunnels(client *clientdata.Client, remotes []*models.Remote) ([]*clienttunnel.Tunnel, error)
	StartTunnel(c *clientdata.Client, r *models.Remote, acl *clienttunnel.TunnelACL) (*clienttunnel.Tunnel, error)
	FindTunnel(c *clientdata.Client, id string) *clienttunnel.Tunnel
	FindTunnelByRemote(c *clientdata.Client, r *models.Remote) *clienttunnel.Tunnel
	TerminateTunnel(c *clientdata.Client, t *clienttunnel.Tunnel, force bool) error
	SetTunnelACL(c *clientdata.Client, t *clienttunnel.Tunnel, aclStr *string) error
}

type ClientServiceProvider

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

func InitClientService

func InitClientService(
	ctx context.Context,
	tunnelProxyConfig *clienttunnel.InternalTunnelProxyConfig,
	portDistributor *ports.PortDistributor,
	db *sqlx.DB,
	keepDisconnectedClients *time.Duration,
	logger *logger.Logger,
	acme *acme.Acme,
) (*ClientServiceProvider, error)

func NewClientService

func NewClientService(
	tunnelProxyConfig *clienttunnel.InternalTunnelProxyConfig,
	portDistributor *ports.PortDistributor,
	repo *ClientRepository,
	logger *logger.Logger,
	acme *acme.Acme,
) *ClientServiceProvider

NewClientService returns a new instance of client service.

func (*ClientServiceProvider) CheckClientAccess

func (s *ClientServiceProvider) CheckClientAccess(clientID string, user User, groups []*cgroups.ClientGroup) error

CheckClientAccess returns nil if a given user has an access to a given client. Otherwise, APIError with 403 is returned.

func (*ClientServiceProvider) CheckClientsAccess

func (s *ClientServiceProvider) CheckClientsAccess(clients []*clientdata.Client, user User, clientGroups []*cgroups.ClientGroup) error

CheckClientsAccess returns nil if a given user has an access to all of the given Otherwise, APIError with 403 is returned.

func (*ClientServiceProvider) Count

func (s *ClientServiceProvider) Count() int

func (*ClientServiceProvider) CountActive

func (s *ClientServiceProvider) CountActive() int

func (*ClientServiceProvider) CountDisconnected

func (s *ClientServiceProvider) CountDisconnected() (int, error)

func (*ClientServiceProvider) DeleteOffline

func (s *ClientServiceProvider) DeleteOffline(clientID string) error

func (*ClientServiceProvider) FindTunnel

TODO: (rs): can this move to the tunnel package?

func (*ClientServiceProvider) FindTunnelByRemote

func (s *ClientServiceProvider) FindTunnelByRemote(c *clientdata.Client, r *models.Remote) *clienttunnel.Tunnel

TODO: (rs): can this move to the tunnel package?

func (*ClientServiceProvider) ForceDelete

func (s *ClientServiceProvider) ForceDelete(client *clientdata.Client) error

ForceDelete deletes client from repo regardless off KeepDisconnectedClients setting, if client is active it will be closed

func (*ClientServiceProvider) GetActiveByID

func (s *ClientServiceProvider) GetActiveByID(id string) (*clientdata.Client, error)

func (*ClientServiceProvider) GetAll

func (s *ClientServiceProvider) GetAll() []*clientdata.Client

func (*ClientServiceProvider) GetAllByClientID

func (s *ClientServiceProvider) GetAllByClientID(clientID string) []*clientdata.Client

func (*ClientServiceProvider) GetByGroups

func (s *ClientServiceProvider) GetByGroups(groups []*cgroups.ClientGroup) ([]*clientdata.Client, error)

func (*ClientServiceProvider) GetByID

func (s *ClientServiceProvider) GetByID(id string) (*clientdata.Client, error)

func (*ClientServiceProvider) GetClientsByTag

func (s *ClientServiceProvider) GetClientsByTag(tags []string, operator string, allowDisconnected bool) (clients []*clientdata.Client, err error)

func (*ClientServiceProvider) GetFilteredUserClients

func (s *ClientServiceProvider) GetFilteredUserClients(user User, filterOptions []query.FilterOption, groups []*cgroups.ClientGroup) ([]*clientdata.CalculatedClient, error)

func (*ClientServiceProvider) GetMaxClients

func (s *ClientServiceProvider) GetMaxClients() (maxClients int)

func (*ClientServiceProvider) GetRepo

func (*ClientServiceProvider) GetUserClients

func (s *ClientServiceProvider) GetUserClients(groups []*cgroups.ClientGroup, user User) []*clientdata.Client

func (*ClientServiceProvider) PopulateGroupsWithUserClients

func (s *ClientServiceProvider) PopulateGroupsWithUserClients(groups []*cgroups.ClientGroup, user User)

func (*ClientServiceProvider) SendClientUpdateToAlerting

func (s *ClientServiceProvider) SendClientUpdateToAlerting(cl *clientdata.Client)

func (*ClientServiceProvider) SetACL

func (s *ClientServiceProvider) SetACL(clientID string, allowedUserGroups []string) error

func (*ClientServiceProvider) SetCaddyAPI

func (s *ClientServiceProvider) SetCaddyAPI(capi caddy.API)

func (*ClientServiceProvider) SetIPAddresses

func (s *ClientServiceProvider) SetIPAddresses(clientID string, IPAddresses *models.IPAddresses) error

func (*ClientServiceProvider) SetLastHeartbeat

func (s *ClientServiceProvider) SetLastHeartbeat(clientID string, heartbeat time.Time) error

func (*ClientServiceProvider) SetPlusAlertingServiceCap

func (s *ClientServiceProvider) SetPlusAlertingServiceCap(as alertingcap.Service)

func (*ClientServiceProvider) SetPlusLicenseInfoCap

func (s *ClientServiceProvider) SetPlusLicenseInfoCap(licensecap licensecap.CapabilityEx)

func (*ClientServiceProvider) SetTunnelACL

func (s *ClientServiceProvider) SetTunnelACL(c *clientdata.Client, t *clienttunnel.Tunnel, aclStr *string) error

func (*ClientServiceProvider) SetUpdatesStatus

func (s *ClientServiceProvider) SetUpdatesStatus(clientID string, updatesStatus *models.UpdatesStatus) error

func (*ClientServiceProvider) StartClient

func (s *ClientServiceProvider) StartClient(
	ctx context.Context, clientAuthID, clientID string, sshConn ssh.Conn, authMultiuseCreds bool,
	req *chshare.ConnectionRequest, clog *logger.Logger,
) (*clientdata.Client, error)

func (*ClientServiceProvider) StartClientTunnels

func (s *ClientServiceProvider) StartClientTunnels(client *clientdata.Client, remotes []*models.Remote) ([]*clienttunnel.Tunnel, error)

StartClientTunnels returns a new tunnel for each requested remote or nil if error occurred

func (*ClientServiceProvider) StartTunnel

func (s *ClientServiceProvider) StartTunnel(
	client *clientdata.Client,
	remote *models.Remote,
	acl *clienttunnel.TunnelACL) (tunnel *clienttunnel.Tunnel, err error)

func (*ClientServiceProvider) Terminate

func (s *ClientServiceProvider) Terminate(client *clientdata.Client) error

func (*ClientServiceProvider) TerminateTunnel

func (s *ClientServiceProvider) TerminateTunnel(c *clientdata.Client, t *clienttunnel.Tunnel, force bool) error

func (*ClientServiceProvider) UpdateClientStatus

func (s *ClientServiceProvider) UpdateClientStatus()

type ClientStore

type ClientStore interface {
	GetAll(ctx context.Context, l *logger.Logger) ([]*clientdata.Client, error)
	Save(ctx context.Context, client *clientdata.Client) error
	DeleteObsolete(ctx context.Context, l *logger.Logger) error
	Delete(ctx context.Context, id string, l *logger.Logger) error
	Close() error
}

type SqliteProvider

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

func NewFakeClientProvider

func NewFakeClientProvider(t *testing.T, exp *time.Duration, cs ...*clientdata.Client) *SqliteProvider

func (*SqliteProvider) Close

func (p *SqliteProvider) Close() error

func (*SqliteProvider) Delete

func (p *SqliteProvider) Delete(ctx context.Context, id string, l *logger.Logger) error

func (*SqliteProvider) DeleteObsolete

func (p *SqliteProvider) DeleteObsolete(ctx context.Context, l *logger.Logger) error

func (*SqliteProvider) GetAll

func (p *SqliteProvider) GetAll(ctx context.Context, l *logger.Logger) ([]*clientdata.Client, error)

func (*SqliteProvider) Save

func (p *SqliteProvider) Save(ctx context.Context, client *clientdata.Client) error

type User

type User interface {
	IsAdmin() bool
	GetGroups() []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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