sshkeymanager

package module
v0.0.0-...-2ba466c Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2021 License: MIT Imports: 14 Imported by: 0

README

golang ssh key manager

Includes a golang library, API server and client library, and a CLI tool with support for working locally or via a remote server.

Project status:

Beta. Key management and most extra features (api, client-server mode) work.

Known issues:
  • no locking is present (when attempting to add multiple keys from multiple goroutines to one user at the same time, chances are only one will be added)

Security contact

Please be responsible and contact me directly at glebtv [at] gmail.com in case you find security issues.

Not intended for web-facing applications.
No safeguards or security measures included besides API Key.
You should implement your own.
$ go get github.com/rs-pro/sshkeymanager

Examples:

Get users:
package main

import (
	"fmt"
	"github.com/rs-pro/sshkeymanager"
	"log"
	)

func main() {
    	host := "host.name"
    	port := "22"

			client := sshkeymanager.NewClient(host, port, sshkeymanager.DefaultConfig)
			users, err := client.GetUsers()

    	users, err := c.GetUsers()
    	if err != nil {
    		log.Println(err)
    	}

    	for _, u := range users {
    		fmt.Printf("UID: %v\nUsername: %v\nHome dir: %v\nShell: %v\n\n", u.UID, u.Name, u.Home, u.Shell)
    	}

Get user keys:
    	uid := "3104"
    	keys, err := c.GetKeys(uid)
    	if err != nil {
    		log.Println(err)
    	}
    	for _, k := range keys {
    		fmt.Printf("String num: %d\nKey: %s\nEmail: %v\n\n", k.Num, k.Key, k.Email)
    	}
Add key
    	key1 := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCqF4hRYsFzO3ylja7wPxut+vu6y2VhYmfOz5wMHuP7XpUvoK/O6Red4bOUAPgexHzRw5kRAKYnaIoMPjNQYCSIhr5xNLwkZTWBxKQ48pLkuBC0yrm+ePXe8sjdFq/0ctPMYX2ZAKYUledoAeb/JbE+zPCEnzhUUqq9pkqGkJJ7I3Fp6uaRx+DELYggIHs6gqWgXLHGdaGkGPNs1xoG4EFwHOx51Jlp1IKAktRjooM9rqPV/TUkM02CoR0VncWbkgDja2lSywdFb8e8keFvbBSPYsB40VMSpXroRJjQ5eQyJlaVyuodXkKGuJmd/5lEZrtQQLISspAjYF2cFgJSsvzz mail1@example.com"
    	err = c.AddKey(key1, uid)
    	if err != nil {
    		fmt.Println(err)
    	}
Delete key
        err = c.DeleteKey(key1, uid)
           	if err != nil {
           		fmt.Println(err)
           	}
        // Closing connection
    	err = c.CloseConnection()
}

Client-server mode

Create config.yml (see example)

KEY_PASS=your-ssh-key-passphrase go run cmd/sshkeyserver/main.go
go run cmd/sshkeymanager/main.go --keyserver http://localhost:12010 --apikey your-key --host r8s.ru list-users

Running specs

env DEBUG=YES INSECURE_IGNORE_HOST_KEY=YES go test -v ./...

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultConfig

func DefaultConfig() *ssh.ClientConfig

func MakeConfig

func MakeConfig(keys []string) *ssh.ClientConfig

Types

type Client

type Client struct {
	SSHConfig   *ssh.ClientConfig
	SSHClient   *ssh.Client
	SSHSession  *ssh.Session
	GroupsCache *[]group.Group
	UsersCache  *[]passwd.User
	// contains filtered or unexported fields
}

func NewClient

func NewClient(host, port, user string, config *ssh.ClientConfig) (*Client, error)

func (*Client) AddGroup

func (c *Client) AddGroup(group group.Group) (*group.Group, error)

func (*Client) AddKey

func (c *Client) AddKey(user passwd.User, key authorized_keys.SSHKey) error

func (*Client) AddUser

func (c *Client) AddUser(user passwd.User, createHome bool) (*passwd.User, error)

AddUser adds a user

func (*Client) ClearGroupCache

func (c *Client) ClearGroupCache() error

func (*Client) ClearUserCache

func (c *Client) ClearUserCache() error

ClearUserCache clears user cache for a client

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) CreateHome

func (c *Client) CreateHome(u passwd.User) (*passwd.User, error)

CreateHome creates user's home directory

func (*Client) DeleteGroup

func (c *Client) DeleteGroup(group group.Group) (*group.Group, error)

func (*Client) DeleteKey

func (c *Client) DeleteKey(user passwd.User, key authorized_keys.SSHKey) error

func (*Client) DeleteUser

func (c *Client) DeleteUser(user passwd.User, removeHome bool) (*passwd.User, error)

DeleteUser deletes a user

func (*Client) Execute

func (c *Client) Execute(command string) (string, string, error)

func (*Client) FindGroup

func (c *Client) FindGroup(group group.Group) (*group.Group, error)

func (*Client) FindKey

func (c *Client) FindKey(user passwd.User, key authorized_keys.SSHKey) (*authorized_keys.SSHKey, error)

func (*Client) FindUser

func (c *Client) FindUser(user passwd.User) (*passwd.User, error)

GetUserByName finds user in /etc/passwd by passwd.User object (uid and name are supported)

func (*Client) GetGroups

func (c *Client) GetGroups() ([]group.Group, error)

func (*Client) GetHost

func (c *Client) GetHost() string

GetPort returns client's ssh host

func (*Client) GetKeys

func (c *Client) GetKeys(user passwd.User) ([]authorized_keys.SSHKey, error)

func (*Client) GetPort

func (c *Client) GetPort() string

GetPort returns client's ssh port

func (*Client) GetUser

func (c *Client) GetUser() string

GetPort returns client's ssh user

func (*Client) GetUserByName

func (c *Client) GetUserByName(name string) (*passwd.User, error)

GetUserByName finds user in /etc/passwd by name

func (*Client) GetUserByUid

func (c *Client) GetUserByUid(uid string) (*passwd.User, error)

GetUserByUid finds user in /etc/passwd by uid

func (*Client) GetUsers

func (c *Client) GetUsers() ([]passwd.User, error)

GetUsers lists all users from /etc/passwd

func (*Client) Prefix

func (c *Client) Prefix() string

func (*Client) StartSCP

func (c *Client) StartSCP(session *ssh.Session, path string) error

func (*Client) WriteFile

func (c *Client) WriteFile(path string, content []byte) error

func (*Client) WriteKeys

func (c *Client) WriteKeys(user passwd.User, keys []authorized_keys.SSHKey) error

type ClientInterface

type ClientInterface interface {
	GetGroups() ([]group.Group, error)
	ClearGroupCache() error
	FindGroup(group group.Group) (*group.Group, error)
	AddGroup(group group.Group) (*group.Group, error)
	DeleteGroup(group group.Group) (*group.Group, error)

	GetUsers() ([]passwd.User, error)
	ClearUserCache() error
	GetUserByUid(uid string) (*passwd.User, error)
	GetUserByName(name string) (*passwd.User, error)
	FindUser(user passwd.User) (*passwd.User, error)
	CreateHome(u passwd.User) (*passwd.User, error)
	AddUser(user passwd.User, createHome bool) (*passwd.User, error)
	DeleteUser(user passwd.User, removeHome bool) (*passwd.User, error)

	GetKeys(user passwd.User) ([]authorized_keys.SSHKey, error)
	FindKey(user passwd.User, key authorized_keys.SSHKey) (*authorized_keys.SSHKey, error)
	DeleteKey(user passwd.User, key authorized_keys.SSHKey) error
	AddKey(user passwd.User, key authorized_keys.SSHKey) error
	WriteKeys(user passwd.User, keys []authorized_keys.SSHKey) error
}

type KeyDoesNotExistError

type KeyDoesNotExistError struct{}

func (*KeyDoesNotExistError) Error

func (e *KeyDoesNotExistError) Error() string

type KeyExistsError

type KeyExistsError struct{}

func (*KeyExistsError) Error

func (e *KeyExistsError) Error() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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