backend

package
v0.0.0-...-d12011c Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 16 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

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

Returns [[removed], [added]]

func JSON

func JSON(data interface{}) string

JSON string from any value

func SplitParts

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

type Config interface {
	AddGroup(label string, users, hosts []string) error
	AddHost(host *Host, withUsers bool) error
	AddUser(newuser *User, host string) error
	AddUserByEmail(email string) bool
	AddUserToHosts(newuser *User)
	DeleteGroup(label string) error
	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) error
	UpdateHost(host *Host) error
	UpdateUser(newuser *User) error
	UserExists(lsum string) bool
	Users() map[string]*User
	Write()
}

type Data

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

func DefaultConfig() *Data

func NewData

func NewData(storage Storage) *Data

NewData creates a new storage with a logger

func ReadData

func ReadData(storage Storage) *Data

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

func (*Data) AddGroup

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

func (*Data) AddHost

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

AddHost adds a host to the storage

func (*Data) AddUser

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

AddUser adds a user to the config

func (*Data) AddUserByEmail

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

AddUserByEmail adds a user to the hosts indicated by config groups

func (*Data) AddUserToHosts

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

AddUserToHosts adds user to all allowed hosts' authorized_keys files

func (*Data) DeleteGroup

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

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

func (*Data) DeleteHost

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

DeleteHost removes a host from the config

func (*Data) DeleteUser

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

DeleteUser removes a user from the storage

func (*Data) DeleteUserByID

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

DeleteUserByID removes a user from the storage

func (*Data) FromGroup

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

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

func (*Data) GetGroups

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

GetGroups returns all groups for frontend (web and cli)

func (*Data) GetHost

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

GetHost is getting host by alias

func (*Data) GetHosts

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

func (*Data) GetUser

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

GetUser is getting user by id

func (*Data) GetUserByEmail

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

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

GetUserByKey returns user if exists by ssh key

func (*Data) GetUsers

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

GetUsers will return users that have the given group

func (*Data) Hosts

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

Hosts is a getter for hosts

func (*Data) Log

func (c *Data) Log() *ILog

func (*Data) PrepareHost

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

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

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

Regenerate updates group information for given hosts

func (*Data) RemoveUserFromHosts

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

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

func (*Data) Roles

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

Roles returns the roles map

func (*Data) SetHost

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

SetHost is a setter for hosts

func (*Data) SetLog

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

SetLog sets a logger for output, used for web

func (*Data) StopUpdate

func (c *Data) StopUpdate()

StopSync stops Update() loop

func (*Data) Update

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

Update updates all hosts by given aliases

func (*Data) UpdateGroup

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

UpdateGroup updates a group in the config, propagating host/user access changes.

func (*Data) UpdateHost

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

UpdateHost reads users from host and adds them to users list

func (*Data) UpdateUser

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

UpdateUser finds and replaces user

func (*Data) UserExists

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

UserExists checks if a user exists ignoring the result

func (*Data) Users

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

Users is a getter for users

func (*Data) Write

func (c *Data) Write()

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

type Errors

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

Errors handles errors list for hadnling arrays

func (*Errors) Add

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

Add adds an error to the list

func (*Errors) Error

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

func (g Group) HasHost(alias string) bool

type Host

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

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

AddUser adds a user to the host's authorized_keys file

func (*Host) DueGroup

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

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

func (*Host) GetGroups

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

GetGroups is a getter for host's Groups

func (*Host) GetUsers

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

GetUsers is a getter for host's Users

func (*Host) HasMatchingGroups

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

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

func (*Host) HasUser

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

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

func (*Host) ReadUsers

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

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

RemoveUser removes a user from the host's authorized_keys file

func (*Host) SetGroups

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

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

func (*Host) UpdateGroups

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

UpdateGroups updates the host's groups based on old groups

func (*Host) UpdateUsersList

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

UpdateUsersList sets the host's user list

func (*Host) Upload

func (h *Host) Upload() error

Upload new authorized_keys file content

type ILog

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

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

func NewLog

func NewLog(web bool) *ILog

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

func (*ILog) Close

func (l *ILog) Close(worker LogWorker)

Close removes a worker from the log

func (*ILog) Errorf

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

Errorf prints an error message to the log

func (*ILog) Infof

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

Infof prints an info message to the log

func (*ILog) Open

func (l *ILog) Open(worker LogWorker)

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

type ILogMode

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

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

func (*JsonStorage) Load

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

func (*JsonStorage) Save

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

func (*JsonStorage) Watch

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

type LabelGroup

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

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

LogWorker is log transfer agent for web

type MemoryStorage

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

func (*MemoryStorage) Load

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

func (*MemoryStorage) Save

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

func (*MemoryStorage) Watch

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

type Role

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

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

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

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

type SFTPMockHost struct {
	Host string
	User string
	File string
}

SFTPMockHost is a build-in mock for testing

type Storage

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

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

NewUser creates a new user

func (*User) GetGroups

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

GetGroups is a getter for Groups

func (*User) SetGroups

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
⌂ 换网址