backend

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// InfoPrefix is the prefix for info messages
	InfoPrefix = "\033[1;34mINF\033[0m "
	// ErrorPrefix is the prefix for error messages
	ErrorPrefix = "\033[1;31mERR\033[0m "
)
View Source
const AUTHFILE = ".ssh/authorized_keys"

Variables

This section is empty.

Functions

func Difference added in v1.3.0

func Difference(slice1 []string, slice2 []string) [2][]string

Returns [[removed], [added]]

func JSON added in v1.3.0

func JSON(data interface{}) string

JSON string from any value

func SplitParts added in v1.3.0

func SplitParts(content string) ([]string, error)

SplitParts splits a public key into []string parts order will be key type, key, comment we assume comment is a user name or email

Types

type Config added in v1.3.0

type Config interface {
	AddGroup(label string, users, hosts []string)
	AddHost(host *Host, withUsers bool) error
	AddUser(newuser *User, host string) error
	AddUserByEmail(email string) bool
	AddUserToHosts(newuser *User)
	DeleteGroup(label string) bool
	DeleteHost(alias string) bool
	DeleteUser(email string) bool
	DeleteUserByID(id string) bool
	FromGroup(host *Host, email string) bool
	GetGroup(id string) *Group
	GetGroups() map[string]LabelGroup
	GetHost(alias string) *Host
	GetHosts(group string) []*Host
	GetUser(lsum string) *User
	GetUserByEmail(email string) (string, *User)
	GetUserByKey(lsum string) *User
	GetUsers(group string) []*User
	Hosts() map[string]*Host
	Log() *ILog
	PrepareHost(args ...string) (*Host, error)
	PrepareUser(email, filename string, groups ...string) (*User, error)
	Regenerate(aliases ...string)
	RemoveUserFromHosts(deluser *User) error
	SetLog(log *ILog) *Data
	SetHost(alias string, host *Host)
	StopUpdate()
	Update(aliases ...string)
	UpdateGroup(groupLabel string, users, hosts []string)
	UpdateHost(host *Host) error
	UpdateUser(newuser *User) error
	UserExists(lsum string) bool
	Users() map[string]*User
	Write()
}

type Data added in v1.3.0

type Data struct {
	Conn SFTP

	Stop    bool
	Storage Storage
	// contains filtered or unexported fields
}

Data is the main storage (data storage) for the sshman backend

func DefaultConfig added in v1.3.0

func DefaultConfig() *Data

func NewData added in v1.3.0

func NewData(storage Storage) *Data

NewData creates a new storage with a logger

func ReadData added in v1.3.0

func ReadData(storage Storage) *Data

ReadData reads the storage file ~/.ssh/.sshman and returns a new Data

func (*Data) AddGroup added in v1.3.0

func (c *Data) AddGroup(label string, hosts, users []string)

func (*Data) AddHost added in v1.3.0

func (c *Data) AddHost(host *Host, withUsers bool) error

AddHost adds a host to the storage

func (*Data) AddUser added in v1.3.0

func (c *Data) AddUser(newuser *User, host string) error

AddUser adds a user to the config

func (*Data) AddUserByEmail added in v1.3.0

func (c *Data) AddUserByEmail(email string) bool

AddUserByEmail adds a user to the hosts indicated by config groups

func (*Data) AddUserToHosts added in v1.3.0

func (c *Data) AddUserToHosts(newuser *User)

AddUserToHosts adds user to all allowed hosts' authorized_keys files

func (*Data) DeleteGroup added in v1.3.0

func (c *Data) DeleteGroup(label string) bool

DeleteGroup removes a group from the config, removing all users and hosts group labels

func (*Data) DeleteHost added in v1.3.0

func (c *Data) DeleteHost(alias string) bool

DeleteHost removes a host from the config

func (*Data) DeleteUser added in v1.3.0

func (c *Data) DeleteUser(email string) bool

DeleteUser removes a user from the storage

func (*Data) DeleteUserByID added in v1.3.0

func (c *Data) DeleteUserByID(id string) bool

DeleteUserByID removes a user from the storage

func (*Data) FromGroup added in v1.3.0

func (c *Data) FromGroup(host *Host, email string) bool

FromGroup returns true if user (by email) is in same group as host

func (*Data) GetGroup added in v1.3.0

func (c *Data) GetGroup(id string) *Group

func (*Data) GetGroups added in v1.3.0

func (c *Data) GetGroups() map[string]LabelGroup

GetGroups returns all groups for frontend (web and cli)

func (*Data) GetHost added in v1.3.0

func (c *Data) GetHost(alias string) *Host

GetHost is getting host by alias

func (*Data) GetHosts added in v1.3.0

func (c *Data) GetHosts(group string) []*Host

func (*Data) GetUser added in v1.3.0

func (c *Data) GetUser(lsum string) *User

GetUser is getting user by id

func (*Data) GetUserByEmail added in v1.3.0

func (c *Data) GetUserByEmail(email string) (string, *User)

GetUserByEmail get a user from config by email as we store them by key checksum

func (*Data) GetUserByKey added in v1.3.0

func (c *Data) GetUserByKey(lsum string) *User

GetUserByKey returns user if exists by ssh key

func (*Data) GetUsers added in v1.3.0

func (c *Data) GetUsers(group string) []*User

GetUsers will return users that have the given group

func (*Data) Hosts added in v1.3.0

func (c *Data) Hosts() map[string]*Host

Hosts is a getter for hosts

func (*Data) Log added in v1.3.0

func (c *Data) Log() *ILog

func (*Data) PrepareHost added in v1.3.0

func (c *Data) PrepareHost(args ...string) (*Host, error)

PrepareHost will prepare a host entry from array of strings Args: alias, hostname, user, keyfile, groups

func (*Data) PrepareUser added in v1.3.0

func (c *Data) PrepareUser(email, filename string, groups ...string) (*User, error)

PrepareUser will prepare a user entry from array of strings New user: old groups, email, key file, new groups

func (*Data) Regenerate added in v1.3.0

func (c *Data) Regenerate(aliases ...string)

Regenerate updates group information for given hosts

func (*Data) RemoveUserFromHosts added in v1.3.0

func (c *Data) RemoveUserFromHosts(deluser *User) error

RemoveUserFromHosts removes user's key from all hosts' authorized_keys files

func (*Data) Roles added in v1.3.0

func (c *Data) Roles() map[string]*Role

Roles returns the roles map

func (*Data) SetHost added in v1.3.0

func (c *Data) SetHost(alias string, host *Host)

SetHost is a setter for hosts

func (*Data) SetLog added in v1.3.0

func (c *Data) SetLog(log *ILog) *Data

SetLog sets a logger for output, used for web

func (*Data) StopUpdate added in v1.3.0

func (c *Data) StopUpdate()

StopSync stops Update() loop

func (*Data) Update added in v1.3.0

func (c *Data) Update(aliases ...string)

Update updates all hosts by given aliases

func (*Data) UpdateGroup added in v1.3.0

func (c *Data) UpdateGroup(groupLabel string, hosts, users []string)

UpdateGroup updates a group in the config removing all users and hosts group labels then adding them again

func (*Data) UpdateHost added in v1.3.0

func (c *Data) UpdateHost(host *Host) error

UpdateHost reads users from host and adds them to users list

func (*Data) UpdateUser added in v1.3.0

func (c *Data) UpdateUser(newuser *User) error

UpdateUser finds and replaces user

func (*Data) UserExists added in v1.3.0

func (c *Data) UserExists(lsum string) bool

UserExists checks if a user exists ignoring the result

func (*Data) Users added in v1.3.0

func (c *Data) Users() map[string]*User

Users is a getter for users

func (*Data) Write added in v1.3.0

func (c *Data) Write()

Write storage file into ~/.ssh/.sshman (if not testing)

type Errors added in v1.3.0

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

Errors handles errors list for hadnling arrays

func (*Errors) Add added in v1.3.0

func (e *Errors) Add(format string, args ...interface{})

Add adds an error to the list

func (*Errors) Error added in v1.3.0

func (e *Errors) Error() string

Error returns the error string

type Group

type Group struct {
	Name  string
	Size  int
	Hosts []*Host
	Users []*User
}

Group is used to store group information (connecting hosts and users)

func (Group) HasHost added in v1.3.0

func (g Group) HasHost(alias string) bool

type Host added in v1.3.0

type Host struct {
	Host        string              `json:"host"`
	User        string              `json:"user"`
	Key         string              `json:"key"`
	Protected   map[string]struct{} `json:"protected"`
	Alias       string              `json:"alias"`
	Config      *Data               `json:"-"`
	Users       []*User             `json:"userlist"`
	Groups      []string            `json:"groups"`
	LastUpdated time.Time           `json:"last_updated"`
	Checksum    string              `json:"checksum"`
	Modified    bool                `json:"modified"`
}

Host holds and manages a host entry in the config

func (*Host) AddUser added in v1.3.0

func (h *Host) AddUser(u *User) error

AddUser adds a user to the host's authorized_keys file

func (*Host) DueGroup added in v1.3.0

func (h *Host) DueGroup(u *User) bool

DueGroup answers the question if user is on host due to group membership

func (*Host) GetGroups added in v1.3.0

func (h *Host) GetGroups() []string

GetGroups is a getter for host's Groups

func (*Host) GetUsers added in v1.3.0

func (h *Host) GetUsers() []*User

GetUsers is a getter for host's Users

func (*Host) HasMatchingGroups added in v1.3.0

func (h *Host) HasMatchingGroups(user *User) bool

HasMatchingGroups checks if the host has any matching groups with the user

func (*Host) HasUser added in v1.3.0

func (h *Host) HasUser(email string) bool

HasUser checks if the host has a user with the given email

func (*Host) ReadUsers added in v1.3.0

func (h *Host) ReadUsers() ([]*User, string, error)

ReadUsers reads ssh entries from the host's authorized_keys file, returning user list, checksum and error

func (*Host) RemoveUser added in v1.3.0

func (h *Host) RemoveUser(u *User) error

RemoveUser removes a user from the host's authorized_keys file

func (*Host) SetGroups added in v1.3.0

func (h *Host) SetGroups(groups []string)

SetGroups is a setter for host's Groups (overwrite all groups at once)

func (*Host) UpdateGroups added in v1.3.0

func (h *Host) UpdateGroups(cfg Config, oldgroups []string) bool

UpdateGroups updates the host's groups based on old groups

func (*Host) UpdateUsersList added in v1.3.0

func (h *Host) UpdateUsersList(userlist []*User) error

UpdateUsersList sets the host's user list

func (*Host) Upload added in v1.3.0

func (h *Host) Upload() error

Upload new authorized_keys file content

type ILog added in v1.3.0

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

ILog is a logging engine with ability to send logs to multiple targets

func NewLog added in v1.3.0

func NewLog(web bool) *ILog

NewLog creates a new ILog log engine, if web is false it sets StdOut mode

func (*ILog) Close added in v1.3.0

func (l *ILog) Close(worker LogWorker)

Close removes a worker from the log

func (*ILog) Errorf added in v1.3.0

func (l *ILog) Errorf(msg string, args ...interface{})

Errorf prints an error message to the log

func (*ILog) Infof added in v1.3.0

func (l *ILog) Infof(msg string, args ...interface{})

Infof prints an info message to the log

func (*ILog) Open added in v1.3.0

func (l *ILog) Open(worker LogWorker)

Open adds a web worker to the log handling a keep-alive connection

type ILogMode added in v1.3.0

type ILogMode string

ILogMode is the mode for the log

var ILogModeClosed ILogMode = ""

ILogModeClosed is the closed mode

var ILogModeStdout ILogMode = "stdout"

ILogModeStdout is the stdout mode for CLI

var ILogModeWeb ILogMode = "web"

ILogModeWeb is the web mode for the log

type JsonStorage added in v1.3.0

type JsonStorage struct {
	Path string
	// contains filtered or unexported fields
}

func (*JsonStorage) Load added in v1.3.0

func (m *JsonStorage) Load(data *Data) error

func (*JsonStorage) Save added in v1.3.0

func (m *JsonStorage) Save(data *Data) error

func (*JsonStorage) Watch added in v1.3.0

func (m *JsonStorage) Watch(notify func())

type LabelGroup added in v1.3.0

type LabelGroup struct {
	Label string   `json:"label"`
	Hosts []string `json:"hosts"`
	Users []string `json:"users"`
}

LabelGroup is used for returning group information to frontend

type LogWorker added in v1.3.0

type LogWorker struct {
	Source chan interface{}
	Quit   chan struct{}
}

LogWorker is log transfer agent for web

type MemoryStorage added in v1.3.0

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

func (*MemoryStorage) Load added in v1.3.0

func (m *MemoryStorage) Load(data *Data) error

func (*MemoryStorage) Save added in v1.3.0

func (m *MemoryStorage) Save(data *Data) error

func (*MemoryStorage) Watch added in v1.3.0

func (m *MemoryStorage) Watch(notify func())

type Role added in v1.3.0

type Role struct {
	Name        string   `json:"name"`
	Permissions []string `json:"permissions"`
}

Role represents a role in the RBAC system

type SFTP

type SFTP interface {
	Connect(keyfile, host, user string) error
	Write(filename, data string) error
	Read(filename string) ([]byte, error)
	Close()
}

SFTP is interface for handling authorized_keys files

type SFTPConn

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

SFTPConn is a wrapper around sftp.Client, implements SFTP interface

func MockConn added in v1.3.0

func MockConn(testHosts map[string]SFTPMockHost) *SFTPConn

func (*SFTPConn) Close

func (s *SFTPConn) Close()

Close closes the connection to the remote host

func (*SFTPConn) Connect

func (s *SFTPConn) Connect(keyfile, host, user string) error

Connect connects to the host using the given keyfile and user

func (*SFTPConn) GetHosts added in v1.3.0

func (s *SFTPConn) GetHosts() map[string]SFTPMockHost

GetHosts is used for testing, returns the list of hosts

func (*SFTPConn) Read

func (s *SFTPConn) Read(filename string) ([]byte, error)

Read reads the authorized_keys file from the remote host when running from tests, returns the mocked data

func (*SFTPConn) SetError added in v1.3.0

func (s *SFTPConn) SetError(willError bool)

SetError is used for testing, sets the error flag

func (*SFTPConn) Write

func (s *SFTPConn) Write(filename, data string) error

Write writes the given data to the authorized_keys file on the remote host when data is empty, or if it's running from tests, simply returns

type SFTPMockHost added in v1.3.0

type SFTPMockHost struct {
	Host string
	User string
	File string
}

SFTPMockHost is a build-in mock for testing

type Storage added in v1.3.0

type Storage interface {
	Load(data *Data) error
	Save(data *Data) error
	Watch(notify func())
}

type User

type User struct {
	KeyType   string    `json:"type"`
	Name      string    `json:"name"`
	Email     string    `json:"email"`
	Key       string    `json:"key"`
	Groups    []string  `json:"groups"`
	Hosts     []string  `json:"hosts"`
	File      string    `json:"keyfile,omitempty"`
	Roles     []string  `json:"roles"`
	UpdatedAt time.Time `json:"updatedAt"`
	Config    Config    `json:"-"`
}

User holds the user information in the config

func NewUser added in v1.3.0

func NewUser(email, keytype, key, name string) *User

NewUser creates a new user

func (*User) GetGroups added in v1.3.0

func (u *User) GetGroups() []string

GetGroups is a getter for Groups

func (*User) SetGroups added in v1.3.0

func (u *User) SetGroups(groups []string)

SetGroups is a setter for Groups

func (*User) UpdateGroups

func (u *User) UpdateGroups(C Config, oldgroups []string) error

UpdateGroups updates the user's groups based on old groups

Jump to

Keyboard shortcuts

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