publish / publish (push) Successful in 2m53s
The host connector answers filesystem_usage, directory_sizes and old_files on configured hosts over SSH with pinned host keys, dialling through the egress tunnel when confined. The sops engine decrypts with the sops library instead of the CLI, takes its file from settings and an age identity from its secrets.
167 lines
5.4 KiB
Go
167 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"github.com/samcharles93/archipelago/sdk/connector"
|
|
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
|
|
)
|
|
|
|
// Target is one host the connector may reach, from its hosts setting.
|
|
type Target struct {
|
|
Name string `json:"name"`
|
|
Address string `json:"address"`
|
|
// HostKey is the host's public key in authorized_keys form, as
|
|
// ssh-keyscan prints it. A host presenting any other key is refused.
|
|
HostKey string `json:"host_key"`
|
|
|
|
key ssh.PublicKey
|
|
}
|
|
|
|
type host struct {
|
|
targets map[string]Target
|
|
username string
|
|
signer ssh.Signer
|
|
}
|
|
|
|
func newHost(cfg connector.Config) (*host, error) {
|
|
var targets []Target
|
|
if err := json.Unmarshal([]byte(cfg.Settings["hosts"]), &targets); err != nil {
|
|
return nil, fmt.Errorf("hosts setting: %w", err)
|
|
}
|
|
h := &host{targets: map[string]Target{}, username: cfg.Secrets["ssh.username"]}
|
|
for _, t := range targets {
|
|
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(t.HostKey))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("host %q: host_key: %w", t.Name, err)
|
|
}
|
|
t.key = key
|
|
h.targets[t.Name] = t
|
|
}
|
|
pem, pass := cfg.Secrets["ssh.private_key"], cfg.Secrets["ssh.private_key_passphrase"]
|
|
if h.username == "" || pem == "" {
|
|
return nil, errNoCredential
|
|
}
|
|
var err error
|
|
if pass != "" {
|
|
h.signer, err = ssh.ParsePrivateKeyWithPassphrase([]byte(pem), []byte(pass))
|
|
} else {
|
|
h.signer, err = ssh.ParsePrivateKey([]byte(pem))
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ssh private key: %w", err)
|
|
}
|
|
return h, nil
|
|
}
|
|
|
|
func (h *host) Describe() connector.Description {
|
|
return connector.Description{Lookups: []*connectorv1.LookupType{
|
|
{
|
|
Name: "filesystem_usage",
|
|
Description: "Size, used and available bytes and inode use of the filesystem holding a path on a host.",
|
|
InputSchema: []byte(`{"type":"object","required":["host","path"],"additionalProperties":false,"properties":{"host":{"type":"string"},"path":{"type":"string","description":"An absolute path."}}}`),
|
|
},
|
|
{
|
|
Name: "directory_sizes",
|
|
Description: "The largest entries directly under a directory on a host, by total size, staying on its filesystem.",
|
|
InputSchema: []byte(`{"type":"object","required":["host","path"],"additionalProperties":false,"properties":{"host":{"type":"string"},"path":{"type":"string","description":"An absolute path."},"limit":{"type":"integer","minimum":1,"maximum":50}}}`),
|
|
},
|
|
{
|
|
Name: "old_files",
|
|
Description: "The largest files under a directory on a host not modified for at least a number of hours, staying on its filesystem.",
|
|
InputSchema: []byte(`{"type":"object","required":["host","path"],"additionalProperties":false,"properties":{"host":{"type":"string"},"path":{"type":"string","description":"An absolute path."},"older_than_hours":{"type":"integer","minimum":0},"limit":{"type":"integer","minimum":1,"maximum":50}}}`),
|
|
},
|
|
}}
|
|
}
|
|
|
|
type lookupInput struct {
|
|
Host string `json:"host"`
|
|
Path string `json:"path"`
|
|
Limit int `json:"limit"`
|
|
OlderThanHours *int `json:"older_than_hours"`
|
|
}
|
|
|
|
func (h *host) Lookup(ctx context.Context, name string, raw jsontext.Value) (jsontext.Value, bool, error) {
|
|
var in lookupInput
|
|
if err := json.Unmarshal(raw, &in); err != nil {
|
|
return nil, false, fmt.Errorf("input: %w", err)
|
|
}
|
|
target, ok := h.targets[in.Host]
|
|
if !ok {
|
|
return nil, false, fmt.Errorf("host %q isn't configured", in.Host)
|
|
}
|
|
p, err := checkPath(in.Path)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
limit := in.Limit
|
|
if limit <= 0 {
|
|
limit = 20
|
|
}
|
|
command, parse, err := lookupCommand(name, p, limit, in.OlderThanHours)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
out, err := h.run(ctx, target, command)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
result, err := parse(out)
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("%s on %s: %w", name, in.Host, err)
|
|
}
|
|
encoded, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return encoded, true, nil
|
|
}
|
|
|
|
// lookupCommand builds the fixed command for a lookup. Every argument from
|
|
// the input is shell-quoted, because ssh hands the command to the remote
|
|
// shell.
|
|
func lookupCommand(name, p string, limit int, olderThan *int) (string, func([]byte) (any, error), error) {
|
|
q := quote(p)
|
|
n := strconv.Itoa(limit)
|
|
switch name {
|
|
case "filesystem_usage":
|
|
return "LC_ALL=C df -P -B1 -- " + q + " && LC_ALL=C df -P -i -- " + q, parseDF, nil
|
|
case "directory_sizes":
|
|
return "LC_ALL=C du -x -B1 --max-depth=1 -- " + q + " 2>/dev/null | sort -rn | head -n " + n, parseDU, nil
|
|
case "old_files":
|
|
hours := 24
|
|
if olderThan != nil {
|
|
hours = *olderThan
|
|
}
|
|
minutes := strconv.Itoa(hours * 60)
|
|
return "LC_ALL=C find " + q + " -xdev -type f -mmin +" + minutes +
|
|
" -printf '%s\\t%T@\\t%u\\t%p\\n' 2>/dev/null | sort -rn | head -n " + n, parseFind, nil
|
|
}
|
|
return "", nil, fmt.Errorf("no lookup named %q", name)
|
|
}
|
|
|
|
// checkPath accepts an absolute path with no control characters, cleaned.
|
|
func checkPath(p string) (string, error) {
|
|
if !path.IsAbs(p) {
|
|
return "", fmt.Errorf("path %q must be absolute", p)
|
|
}
|
|
if strings.ContainsFunc(p, func(r rune) bool { return r < 0x20 || r == 0x7f }) {
|
|
return "", errors.New("path contains control characters")
|
|
}
|
|
return path.Clean(p), nil
|
|
}
|
|
|
|
// quote makes s one single-quoted shell word.
|
|
func quote(s string) string {
|
|
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
|
|
}
|