Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf4094b9e4 | ||
|
|
e067eee330 | ||
|
|
9e0cf6aa33 | ||
|
|
68ba03c1af | ||
|
|
907ddb6000 | ||
|
|
0dcf41a839 | ||
|
|
ee52682499 | ||
|
|
4728b3cff0 |
@@ -0,0 +1,21 @@
|
||||
# CI [#832] — pure-Go CLI: fmt, vet, build, secret scan. No Rust in the chain.
|
||||
name: ci
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
jobs:
|
||||
vet:
|
||||
runs-on: ultix
|
||||
container:
|
||||
image: golang:1.23-alpine
|
||||
steps:
|
||||
- run: apk add --no-cache nodejs git
|
||||
- uses: actions/checkout@v4
|
||||
- run: gofmt -l cli/ | tee /tmp/fmt.out && test ! -s /tmp/fmt.out
|
||||
- run: cd cli && go vet ./... && go build ./...
|
||||
- name: secret scan
|
||||
run: |
|
||||
if grep -rInE "BEGIN (RSA |OPENSSH |EC )?PRIVATE KEY|BW_PASSWORD='|SM_PASSWORD=" --exclude-dir=.git --exclude-dir=.smstate .; then
|
||||
echo "::error::secret material committed"; exit 1
|
||||
fi
|
||||
@@ -1,5 +1,43 @@
|
||||
# KNELSecretsManager (ARCHIVED — moved to KNEL/secrets)
|
||||
# KNELSecretsManager
|
||||
|
||||
This body of work moved to **[KNEL/secrets](https://git.knownelement.com/KNEL/secrets)** per the 2026-09-03 repo split ([#769](https://projects.knownelement.com/issues/769)); content was ported as `legacy-knelsecretsmanager/` (secret-scanned clean — placeholders only).
|
||||
Fleet secrets management: a **pure-Go Bitwarden/Vaultwarden CLI** (`smcli`)
|
||||
in a house container, backed by the self-hosted TSGCOO vault. No upstream
|
||||
Rust `bw` binary, no Node runtime, no `.creds` text files — those patterns
|
||||
are retired (ADR-003; founder rulings #829/#832).
|
||||
|
||||
This repo is historical. New secrets-management work happens in KNEL/secrets (#770).
|
||||
- Docs: [docs/architecture.md](docs/architecture.md) (diagrams, crypto, rotation program)
|
||||
- KNELBMS integration: [docs/integration-knelbms.md](docs/integration-knelbms.md)
|
||||
- Redmine: https://projects.knownelement.com/issues/832 (build) / #829 (migration+rotation)
|
||||
|
||||
## Quick start (lane)
|
||||
|
||||
```bash
|
||||
# TSGCOO account (COO-area chats; docker group, no sudo)
|
||||
/data2/TSGCOO/.local/bin/sm status
|
||||
|
||||
# reachableceo crossover
|
||||
~/projects/KNEL/OAM/.tools/sm env creds/cloudron # export URI/USERNAME/PASSWORD + keys
|
||||
~/projects/KNEL/OAM/.tools/sm get creds/librenms --field password
|
||||
~/projects/KNEL/OAM/.tools/sm setfield creds/<item> <KEY> <newvalue> # rotation updates
|
||||
```
|
||||
|
||||
## Layout
|
||||
|
||||
| Path | What |
|
||||
|---|---|
|
||||
| `cli/cmd/smcli/` | the Go CLI (crypto, API, commands) |
|
||||
| `docker/Dockerfile.cli` | golang build → alpine runtime (CA certs, non-root) |
|
||||
| `docker/compose.yaml` | always-hot service `ukrrs-secretsmgr-cli` (digest-pinned) |
|
||||
| `archive/rust-bw-era/` | retired upstream-binary wrapper scripts |
|
||||
| `docs/ADR-003-GoCLI.md` | decision record |
|
||||
|
||||
## Rules (binding)
|
||||
|
||||
- Secrets live ONLY in the TSGCOO Bitwarden vault, accessed ONLY via this
|
||||
CLI (container `ukrrs-secretsmgr-cli`, shims above). No textfile creds,
|
||||
no upstream bw CLI — anywhere.
|
||||
- All work product is authored by Cloudron account identities
|
||||
(ic-builder / ic-reviewer / manager-tsg / vptechops); the founder
|
||||
account (ReachableCEO) reviews and approves.
|
||||
- Production-affecting rotations follow the CR gating + cross-linking
|
||||
house rules (GLPI CR deep link in the PR/ticket; evidence on solve).
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
.smstate/
|
||||
@@ -0,0 +1,359 @@
|
||||
package main
|
||||
|
||||
// Vaultwarden/Bitwarden API client: prelogin, login, sync, cipher create/edit.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Server string // e.g. https://pwvault.turnsys.com
|
||||
HTTP *http.Client
|
||||
Email string
|
||||
Password string
|
||||
TOTPSecret string
|
||||
reloginDone bool
|
||||
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
KDFType int
|
||||
KDFIter uint32
|
||||
KDFMemory uint32
|
||||
KDFParallel uint32
|
||||
MasterKey []byte // 32B
|
||||
StretchedKey []byte // 64B
|
||||
UserSymKey []byte // 64B (decrypted from profile.Key)
|
||||
}
|
||||
|
||||
func (c *Client) api(method, path string, body any, auth bool) ([]byte, error) {
|
||||
var rd io.Reader
|
||||
if body != nil {
|
||||
switch b := body.(type) {
|
||||
case url.Values:
|
||||
rd = strings.NewReader(b.Encode())
|
||||
default:
|
||||
j, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rd = bytes.NewReader(j)
|
||||
}
|
||||
} else {
|
||||
rd = strings.NewReader("")
|
||||
}
|
||||
req, err := http.NewRequest(method, c.Server+path, rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body != nil {
|
||||
if _, ok := body.(url.Values); ok {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
} else {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
}
|
||||
if auth {
|
||||
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
|
||||
}
|
||||
resp, err := c.HTTP.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
out, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode >= 300 {
|
||||
// access token expired: refresh once and retry (never for the
|
||||
// identity endpoints themselves, which manage their own tokens)
|
||||
if resp.StatusCode == 401 && auth && c.RefreshToken != "" && !strings.HasPrefix(path, "/identity/") {
|
||||
if rerr := c.refresh(); rerr == nil {
|
||||
return c.api(method, path, body, auth)
|
||||
}
|
||||
}
|
||||
return out, fmt.Errorf("%s %s: HTTP %d: %s", method, path, resp.StatusCode, truncate(string(out), 200))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// refresh exchanges the persisted refresh_token for a fresh access token
|
||||
// (Vaultwarden rotates the refresh token on every use). Scope must match
|
||||
// the original grant (api offline_access).
|
||||
func (c *Client) refresh() error {
|
||||
if c.RefreshToken == "" {
|
||||
return errors.New("no refresh token in state; re-login required")
|
||||
}
|
||||
form := url.Values{}
|
||||
form.Set("grant_type", "refresh_token")
|
||||
form.Set("refresh_token", c.RefreshToken)
|
||||
form.Set("client_id", "cli")
|
||||
form.Set("scope", "api offline_access")
|
||||
out, err := c.apiRaw("POST", "/identity/connect/token", form, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("refresh: %w", err)
|
||||
}
|
||||
var t tokenResp
|
||||
if err := json.Unmarshal(out, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
if t.AccessToken == "" {
|
||||
return fmt.Errorf("refresh failed: %s", truncate(string(out), 200))
|
||||
}
|
||||
c.AccessToken = t.AccessToken
|
||||
if t.RefreshTok != "" {
|
||||
c.RefreshToken = t.RefreshTok
|
||||
}
|
||||
persistTokens(c)
|
||||
return nil
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "..."
|
||||
}
|
||||
|
||||
type preloginResp struct {
|
||||
KDF int `json:"kdf"`
|
||||
KDFIterations uint32 `json:"kdfIterations"`
|
||||
KDFMemory uint32 `json:"kdfMemory"`
|
||||
KDFParallelism uint32 `json:"kdfParallelism"`
|
||||
}
|
||||
|
||||
func (c *Client) Prelogin() error {
|
||||
out, err := c.api("POST", "/api/accounts/prelogin", map[string]string{"email": c.Email}, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var p preloginResp
|
||||
if err := json.Unmarshal(out, &p); err != nil {
|
||||
return err
|
||||
}
|
||||
c.KDFType, c.KDFIter, c.KDFMemory, c.KDFParallel = p.KDF, p.KDFIterations, p.KDFMemory, p.KDFParallelism
|
||||
return nil
|
||||
}
|
||||
|
||||
type tokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshTok string `json:"refresh_token"`
|
||||
Key string `json:"Key"`
|
||||
PrivateKey string `json:"PrivateKey"`
|
||||
ErrorDesc string `json:"ErrorDescription"`
|
||||
}
|
||||
|
||||
// Login performs prelogin + key derivation + password grant.
|
||||
func (c *Client) Login() error {
|
||||
if err := c.Prelogin(); err != nil {
|
||||
return fmt.Errorf("prelogin: %w", err)
|
||||
}
|
||||
c.MasterKey = deriveMasterKey(c.Password, c.Email, c.KDFType, c.KDFIter, c.KDFMemory, c.KDFParallel)
|
||||
c.StretchedKey = stretchKey(c.MasterKey)
|
||||
|
||||
form := url.Values{}
|
||||
form.Set("grant_type", "password")
|
||||
form.Set("username", c.Email)
|
||||
form.Set("password", masterPasswordHash(c.MasterKey, c.Password))
|
||||
form.Set("scope", "api offline_access")
|
||||
form.Set("client_id", "cli")
|
||||
form.Set("deviceIdentifier", deviceID())
|
||||
form.Set("deviceName", "smcli")
|
||||
form.Set("deviceType", "9")
|
||||
var lastOut []byte
|
||||
out, err := c.apiRaw("POST", "/identity/connect/token", form, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("token: %w", err)
|
||||
}
|
||||
var t tokenResp
|
||||
if err := json.Unmarshal(out, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
if t.AccessToken == "" {
|
||||
// 2FA retry path (provider 0 = authenticator TOTP)
|
||||
if strings.Contains(string(out), "Two factor required") {
|
||||
secret := c.TOTPSecret
|
||||
if secret != "" {
|
||||
code, terr := totpNow(secret, time.Now())
|
||||
if terr != nil {
|
||||
return fmt.Errorf("totp: %w", terr)
|
||||
}
|
||||
form.Set("twoFactor", "0")
|
||||
form.Set("twoFactorProvider", "0")
|
||||
form.Set("twoFactorToken", code)
|
||||
form.Set("twoFactorRemember", "1")
|
||||
out2, err2 := c.api("POST", "/identity/connect/token", form, false)
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("token(2fa): %w", err2)
|
||||
}
|
||||
if err := json.Unmarshal(out2, &t); err != nil {
|
||||
return err
|
||||
}
|
||||
lastOut = out2
|
||||
}
|
||||
}
|
||||
}
|
||||
if t.AccessToken == "" {
|
||||
payload := string(out)
|
||||
if lastOut != nil {
|
||||
payload = string(lastOut)
|
||||
}
|
||||
return fmt.Errorf("login failed: %s", truncate(payload, 300))
|
||||
}
|
||||
c.AccessToken = t.AccessToken
|
||||
c.RefreshToken = t.RefreshTok
|
||||
return nil
|
||||
}
|
||||
|
||||
type syncResp struct {
|
||||
Profile struct {
|
||||
Key string `json:"key"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Email string `json:"email"`
|
||||
} `json:"profile"`
|
||||
Folders []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"folders"`
|
||||
Ciphers []json.RawMessage `json:"ciphers"`
|
||||
}
|
||||
|
||||
// Unlock performs sync-light: fetches profile key, decrypts the user sym key.
|
||||
func (c *Client) Unlock() error {
|
||||
out, err := c.api("GET", "/api/sync?excludeDomains=true", nil, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var s syncResp
|
||||
if err := json.Unmarshal(out, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.Profile.Key == "" {
|
||||
return errors.New("sync: empty profile key")
|
||||
}
|
||||
es, err := ParseEncString(s.Profile.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.UserSymKey, err = symDecrypt(c.StretchedKey, es)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt user key: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sync returns the raw sync payload (cached by caller as needed).
|
||||
func (c *Client) Sync() ([]byte, error) {
|
||||
return c.api("GET", "/api/sync?excludeDomains=true", nil, true)
|
||||
}
|
||||
|
||||
// CreateCipher posts an encrypted cipher.
|
||||
func (c *Client) CreateCipher(cipherJSON any) ([]byte, error) {
|
||||
return c.api("POST", "/api/ciphers", cipherJSON, true)
|
||||
}
|
||||
|
||||
// EditCipher updates an encrypted cipher.
|
||||
func (c *Client) EditCipher(id string, cipherJSON any) ([]byte, error) {
|
||||
return c.api("PUT", "/api/ciphers/"+id, cipherJSON, true)
|
||||
}
|
||||
|
||||
func deviceID() string {
|
||||
// stable per machine: hash of hostname (no secrets involved)
|
||||
hn := hostnameSafe()
|
||||
sum := sha256Sum([]byte("knel-secretsmgr:" + hn))
|
||||
var b [16]byte
|
||||
copy(b[:], sum[:16])
|
||||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
|
||||
binary.BigEndian.Uint32(b[0:4]),
|
||||
binary.BigEndian.Uint16(b[4:6]),
|
||||
binary.BigEndian.Uint16(b[6:8]),
|
||||
binary.BigEndian.Uint16(b[8:10]),
|
||||
b[10:16])
|
||||
}
|
||||
|
||||
// apiRaw performs the request and returns the body regardless of status.
|
||||
func (c *Client) apiRaw(method, path string, body any, auth bool) ([]byte, error) {
|
||||
var rd io.Reader
|
||||
if body != nil {
|
||||
switch b := body.(type) {
|
||||
case url.Values:
|
||||
rd = strings.NewReader(b.Encode())
|
||||
default:
|
||||
j, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rd = bytes.NewReader(j)
|
||||
}
|
||||
} else {
|
||||
rd = strings.NewReader("")
|
||||
}
|
||||
req, err := http.NewRequest(method, c.Server+path, rd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if body != nil {
|
||||
if _, ok := body.(url.Values); ok {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
} else {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
}
|
||||
if auth {
|
||||
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
|
||||
}
|
||||
resp, err := c.HTTP.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
// DeleteCipher moves a cipher to trash (soft delete). Best-effort permanent
|
||||
// purge afterwards; Vaultwarden tolerates trash-only items.
|
||||
func (c *Client) DeleteCipher(id string) error {
|
||||
if _, err := c.api("DELETE", "/api/ciphers/"+id, nil, true); err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = c.api("PUT", "/api/ciphers/"+id+"/purge", map[string]any{}, true)
|
||||
return nil
|
||||
}
|
||||
|
||||
// selfRelogin performs the full login+unlock using SM_* env credentials
|
||||
// (injected by the sm shims from the TSGCOO vault-account env). Saves state.
|
||||
func (c *Client) selfRelogin() error {
|
||||
if c.Password == "" {
|
||||
c.Password = os.Getenv("SM_PASSWORD")
|
||||
}
|
||||
if c.TOTPSecret == "" {
|
||||
c.TOTPSecret = os.Getenv("SM_TOTP_SECRET")
|
||||
}
|
||||
if c.Password == "" {
|
||||
return errors.New("relogin unavailable: SM_PASSWORD not set")
|
||||
}
|
||||
if err := c.Login(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.Unlock(); err != nil {
|
||||
return err
|
||||
}
|
||||
if s, err := loadState(); err == nil {
|
||||
s.AccessToken = c.AccessToken
|
||||
s.UserSymKey = toHex(c.UserSymKey)
|
||||
s.StretchedKey = toHex(c.StretchedKey)
|
||||
s.MasterKey = toHex(c.MasterKey)
|
||||
_ = saveState(s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package main
|
||||
|
||||
// Bitwarden-compatible crypto for the KNELSecretsManager CLI (Vaultwarden API).
|
||||
// - master key: PBKDF2-SHA256(password, email, iterations, 32B) or Argon2id
|
||||
// - auth: masterPasswordHash = base64(PBKDF2-SHA256(masterKey, password, 1, 32B))
|
||||
// - stretched master key: HKDF-SHA256 expand, info "enc"/"mac" (32B each)
|
||||
// - encString "2.iv|ct|mac": AES-256-CBC(encKey32) + HMAC-SHA256(macKey32, iv||ct)
|
||||
// ("AesCbc128_HmacSha256_B64" is Bitwarden's legacy misnomer; keys are 32+32 bytes)
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
)
|
||||
|
||||
type EncString struct {
|
||||
Type byte
|
||||
IV []byte
|
||||
CT []byte
|
||||
MAC []byte
|
||||
}
|
||||
|
||||
func ParseEncString(s string) (*EncString, error) {
|
||||
if s == "" {
|
||||
return nil, errors.New("empty encstring")
|
||||
}
|
||||
parts := strings.SplitN(s, ".", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, errors.New("encstring missing type header")
|
||||
}
|
||||
t, err := strconv.Atoi(parts[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bad encstring type: %w", err)
|
||||
}
|
||||
es := &EncString{Type: byte(t)}
|
||||
switch t {
|
||||
case 0: // AesCbc256_B64 (legacy, no mac)
|
||||
b, err := base64.StdEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
es.IV, es.CT = b[:16], b[16:]
|
||||
case 1, 2: // both handled identically on the wire (enc32+mac32 keys)
|
||||
seg := strings.Split(parts[1], "|")
|
||||
if len(seg) != 3 {
|
||||
return nil, errors.New("encstring needs iv|ct|mac")
|
||||
}
|
||||
if es.IV, err = base64.StdEncoding.DecodeString(seg[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if es.CT, err = base64.StdEncoding.DecodeString(seg[1]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if es.MAC, err = base64.StdEncoding.DecodeString(seg[2]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported encstring type %d", t)
|
||||
}
|
||||
return es, nil
|
||||
}
|
||||
|
||||
func (e *EncString) String() string {
|
||||
if e.Type == 0 {
|
||||
return "0." + base64.StdEncoding.EncodeToString(append(append([]byte{}, e.IV...), e.CT...))
|
||||
}
|
||||
return fmt.Sprintf("%d.%s|%s|%s", e.Type,
|
||||
base64.StdEncoding.EncodeToString(e.IV),
|
||||
base64.StdEncoding.EncodeToString(e.CT),
|
||||
base64.StdEncoding.EncodeToString(e.MAC))
|
||||
}
|
||||
|
||||
func deriveMasterKey(password, email string, kdfType int, iterations, memory, parallelism uint32) []byte {
|
||||
salt := []byte(strings.ToLower(strings.TrimSpace(email)))
|
||||
switch kdfType {
|
||||
case 1:
|
||||
return argon2.IDKey([]byte(password), salt, iterations, memory, uint8(parallelism), 32)
|
||||
default:
|
||||
return pbkdf2.Key([]byte(password), salt, int(iterations), 32, sha256.New)
|
||||
}
|
||||
}
|
||||
|
||||
func masterPasswordHash(masterKey []byte, password string) string {
|
||||
return base64.StdEncoding.EncodeToString(pbkdf2.Key(masterKey, []byte(password), 1, 32, sha256.New))
|
||||
}
|
||||
|
||||
// stretchKey expands the 32B master key to a 64B symmetric key (enc 32 | mac 32).
|
||||
func stretchKey(masterKey []byte) []byte {
|
||||
out := make([]byte, 64)
|
||||
copy(out[:32], hkdfExpand(masterKey, []byte("enc"), 32))
|
||||
copy(out[32:], hkdfExpand(masterKey, []byte("mac"), 32))
|
||||
return out
|
||||
}
|
||||
|
||||
func hkdfExpand(key, info []byte, length int) []byte {
|
||||
out := make([]byte, 0, length)
|
||||
t := []byte{}
|
||||
var i byte
|
||||
for len(out) < length {
|
||||
i++
|
||||
h := hmac.New(sha256.New, key)
|
||||
h.Write(t)
|
||||
h.Write(info)
|
||||
h.Write([]byte{i})
|
||||
t = h.Sum(nil)
|
||||
out = append(out, t...)
|
||||
}
|
||||
return out[:length]
|
||||
}
|
||||
|
||||
// symDecrypt decrypts a type-1/2 encstring with a 64-byte key (enc32|mac32).
|
||||
func symDecrypt(key64 []byte, es *EncString) ([]byte, error) {
|
||||
if len(key64) != 64 {
|
||||
return nil, errors.New("symmetric key must be 64 bytes")
|
||||
}
|
||||
if es.Type == 0 {
|
||||
return aesCBCDecrypt(key64[:32], es.IV, es.CT)
|
||||
}
|
||||
if es.Type != 1 && es.Type != 2 {
|
||||
return nil, fmt.Errorf("unsupported encstring type %d", es.Type)
|
||||
}
|
||||
mac := hmac.New(sha256.New, key64[32:])
|
||||
mac.Write(es.IV)
|
||||
mac.Write(es.CT)
|
||||
if subtle.ConstantTimeCompare(mac.Sum(nil), es.MAC) != 1 {
|
||||
return nil, errors.New("mac mismatch")
|
||||
}
|
||||
return aesCBCDecrypt(key64[:32], es.IV, es.CT)
|
||||
}
|
||||
|
||||
// symEncrypt encrypts plaintext into a type-2 encstring with a 64-byte key.
|
||||
func symEncrypt(key64 []byte, plaintext []byte) (string, error) {
|
||||
if len(key64) != 64 {
|
||||
return "", errors.New("symmetric key must be 64 bytes")
|
||||
}
|
||||
iv := make([]byte, 16)
|
||||
if _, err := rand.Read(iv); err != nil {
|
||||
return "", err
|
||||
}
|
||||
ct, err := aesCBCEncrypt(key64[:32], iv, plaintext)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
mac := hmac.New(sha256.New, key64[32:])
|
||||
mac.Write(iv)
|
||||
mac.Write(ct)
|
||||
es := &EncString{Type: 2, IV: iv, CT: ct, MAC: mac.Sum(nil)}
|
||||
return es.String(), nil
|
||||
}
|
||||
|
||||
func aesCBCDecrypt(key, iv, ct []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(ct) == 0 || len(ct)%aes.BlockSize != 0 {
|
||||
return nil, errors.New("ciphertext not block aligned")
|
||||
}
|
||||
pt := make([]byte, len(ct))
|
||||
cipher.NewCBCDecrypter(block, iv).CryptBlocks(pt, ct)
|
||||
return unpad(pt)
|
||||
}
|
||||
|
||||
func aesCBCEncrypt(key, iv, pt []byte) ([]byte, error) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ct := make([]byte, len(pad(pt)))
|
||||
cipher.NewCBCEncrypter(block, iv).CryptBlocks(ct, pad(pt))
|
||||
return ct, nil
|
||||
}
|
||||
|
||||
func unpad(b []byte) ([]byte, error) {
|
||||
if len(b) == 0 {
|
||||
return nil, errors.New("empty plaintext")
|
||||
}
|
||||
n := int(b[len(b)-1])
|
||||
if n == 0 || n > aes.BlockSize || n > len(b) {
|
||||
return nil, errors.New("bad padding")
|
||||
}
|
||||
return b[:len(b)-n], nil
|
||||
}
|
||||
|
||||
func pad(b []byte) []byte {
|
||||
n := aes.BlockSize - len(b)%aes.BlockSize
|
||||
out := make([]byte, len(b)+n)
|
||||
copy(out, b)
|
||||
for i := len(b); i < len(out); i++ {
|
||||
out[i] = byte(n)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,665 @@
|
||||
package main
|
||||
|
||||
// smcli — KNELSecretsManager Go CLI (pure Go; replaces the upstream Rust bw binary).
|
||||
// Speaks the Bitwarden/Vaultwarden API against the self-hosted vault.
|
||||
//
|
||||
// Commands:
|
||||
// login authenticate (SM_EMAIL/SM_PASSWORD/SM_SERVER env or flags)
|
||||
// status show auth/key state
|
||||
// list [pattern] list item names
|
||||
// get <name> [--field KEY] print decrypted item (or one field / KEY=VALUE block)
|
||||
// env <name> print KEY=VALUE lines for `eval`/sourcing
|
||||
// set <name> [k=v ...] create/update a secure-note item from --file or inline k=v
|
||||
// rm <name> delete item
|
||||
// folders list folders
|
||||
//
|
||||
// State: SM_STATE_DIR (default ~/.config/smcli), files 0600.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const stateVersion = 1
|
||||
|
||||
type State struct {
|
||||
Version int `json:"version"`
|
||||
Server string `json:"server"`
|
||||
Email string `json:"email"`
|
||||
AccessToken string `json:"access_token"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
KDFType int `json:"kdf_type"`
|
||||
KDFIter uint32 `json:"kdf_iter"`
|
||||
KDFMemory uint32 `json:"kdf_memory"`
|
||||
KDFParallel uint32 `json:"kdf_parallel"`
|
||||
// MasterKey/StretchedKey/UserSymKey stored raw (hex) — file must be 0600.
|
||||
MasterKey string `json:"master_key"`
|
||||
StretchedKey string `json:"stretched_key"`
|
||||
UserSymKey string `json:"user_sym_key"`
|
||||
}
|
||||
|
||||
func stateDir() string {
|
||||
if v := os.Getenv("SM_STATE_DIR"); v != "" {
|
||||
return v
|
||||
}
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, ".config", "smcli")
|
||||
}
|
||||
|
||||
func statePath() string { return filepath.Join(stateDir(), "state.json") }
|
||||
|
||||
// persistTokens updates just the token pair in the existing state file
|
||||
// after a successful refresh (called from api.go refresh()).
|
||||
func persistTokens(c *Client) {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return // no readable state; tokens stay in-memory for this run
|
||||
}
|
||||
s.AccessToken = c.AccessToken
|
||||
s.RefreshToken = c.RefreshToken
|
||||
_ = saveState(s)
|
||||
}
|
||||
|
||||
func saveState(s *State) error {
|
||||
if err := os.MkdirAll(stateDir(), 0o700); err != nil {
|
||||
return err
|
||||
}
|
||||
b, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(statePath(), b, 0o600)
|
||||
}
|
||||
|
||||
func loadState() (*State, error) {
|
||||
b, err := os.ReadFile(statePath())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var s State
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.Version != stateVersion {
|
||||
return nil, errors.New("state version mismatch; re-login")
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func newClientFromState(s *State) (*Client, error) {
|
||||
c := &Client{
|
||||
Server: s.Server, Email: s.Email,
|
||||
Password: os.Getenv("SM_PASSWORD"), TOTPSecret: os.Getenv("SM_TOTP_SECRET"),
|
||||
AccessToken: s.AccessToken, RefreshToken: s.RefreshToken,
|
||||
KDFType: s.KDFType, KDFIter: s.KDFIter, KDFMemory: s.KDFMemory, KDFParallel: s.KDFParallel,
|
||||
HTTP: &http.Client{Timeout: 30 * time.Second},
|
||||
}
|
||||
var err error
|
||||
if c.MasterKey, err = fromHex(s.MasterKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.StretchedKey, err = fromHex(s.StretchedKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.UserSymKey, err = fromHex(s.UserSymKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func cmdLogin(server, email, password string) error {
|
||||
c := &Client{Server: server, Email: email, Password: password, TOTPSecret: os.Getenv("SM_TOTP_SECRET"),
|
||||
HTTP: &http.Client{Timeout: 30 * time.Second}}
|
||||
if err := c.Login(); err != nil {
|
||||
return err
|
||||
}
|
||||
s := &State{
|
||||
Version: stateVersion, Server: server, Email: email,
|
||||
AccessToken: c.AccessToken,
|
||||
RefreshToken: c.RefreshToken,
|
||||
KDFType: c.KDFType, KDFIter: c.KDFIter, KDFMemory: c.KDFMemory, KDFParallel: c.KDFParallel,
|
||||
MasterKey: toHex(c.MasterKey),
|
||||
StretchedKey: toHex(c.StretchedKey),
|
||||
}
|
||||
if err := saveState(s); err != nil {
|
||||
return err
|
||||
}
|
||||
// immediately decrypt the user sym key
|
||||
if err := c.Unlock(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.UserSymKey = toHex(c.UserSymKey)
|
||||
return saveState(s)
|
||||
}
|
||||
|
||||
func cmdList(pattern string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, err := c.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var sync struct {
|
||||
Ciphers []json.RawMessage `json:"ciphers"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &sync); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, r := range sync.Ciphers {
|
||||
pc, err := DecryptCipher(c.UserSymKey, r)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if pattern == "" || strings.Contains(strings.ToLower(pc.Name), strings.ToLower(pattern)) {
|
||||
fmt.Println(pc.Name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findCipher(c *Client, userKey []byte, name string) (*PlainCipher, json.RawMessage, error) {
|
||||
raw, err := c.Sync()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var sync struct {
|
||||
Ciphers []json.RawMessage `json:"ciphers"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &sync); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, r := range sync.Ciphers {
|
||||
pc, err := DecryptCipher(userKey, r)
|
||||
if err != nil || pc.Name != name {
|
||||
continue
|
||||
}
|
||||
return pc, r, nil
|
||||
}
|
||||
return nil, nil, errors.New("item not found: " + name)
|
||||
}
|
||||
|
||||
func rawCipherByID(raw []byte, id string) (*cipherRaw, error) {
|
||||
var sync struct {
|
||||
Ciphers []json.RawMessage `json:"ciphers"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &sync); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range sync.Ciphers {
|
||||
var cr cipherRaw
|
||||
if err := json.Unmarshal(r, &cr); err != nil {
|
||||
continue
|
||||
}
|
||||
if cr.ID == id {
|
||||
return &cr, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("cipher id not found: " + id)
|
||||
}
|
||||
|
||||
func cmdGet(name, field string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pc, _, err := findCipher(c, c.UserSymKey, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if field != "" {
|
||||
for _, f := range pc.Fields {
|
||||
if f.Name == field {
|
||||
fmt.Println(f.Value)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if field == "password" {
|
||||
fmt.Println(pc.Login.Password)
|
||||
return nil
|
||||
}
|
||||
if field == "username" {
|
||||
fmt.Println(pc.Login.Username)
|
||||
return nil
|
||||
}
|
||||
if field == "notes" {
|
||||
fmt.Print(pc.Notes)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("field not found: %s", field)
|
||||
}
|
||||
out, _ := json.MarshalIndent(pc, "", " ")
|
||||
fmt.Println(string(out))
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdEnv(name string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pc, _, err := findCipher(c, c.UserSymKey, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range pc.Fields {
|
||||
v := strings.ReplaceAll(f.Value, "'", "'\\''")
|
||||
fmt.Printf("export %s='%s'\n", f.Name, v)
|
||||
}
|
||||
if len(pc.URIs) > 0 {
|
||||
fmt.Printf("export URI='%s'\n", pc.URIs[0])
|
||||
}
|
||||
if pc.Login.Username != "" {
|
||||
fmt.Printf("export USERNAME='%s'\n", pc.Login.Username)
|
||||
}
|
||||
if pc.Login.Password != "" {
|
||||
fmt.Printf("export PASSWORD='%s'\n", pc.Login.Password)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdSet(name, file string, kv []string, folderID string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var fields []PlainField
|
||||
if file != "" {
|
||||
b, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fields = parseEnvFields(string(b))
|
||||
}
|
||||
for _, kv := range kv {
|
||||
eq := strings.Index(kv, "=")
|
||||
if eq <= 0 {
|
||||
return fmt.Errorf("bad k=v: %s", kv)
|
||||
}
|
||||
fields = append(fields, PlainField{Type: 1, Name: kv[:eq], Value: kv[eq+1:]})
|
||||
}
|
||||
raw, err := c.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pc, _, err := findCipher(c, c.UserSymKey, name)
|
||||
exists := err == nil
|
||||
var payload map[string]any
|
||||
if exists {
|
||||
var cr *cipherRaw
|
||||
cr, err = rawCipherByID(raw, pc.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err = BuildSecureNoteJSON(c.UserSymKey, name, folderID, pc.Notes, fields, cr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.EditCipher(pc.ID, payload)
|
||||
} else {
|
||||
payload, err = BuildSecureNoteJSON(c.UserSymKey, name, folderID, "", fields, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.CreateCipher(payload)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("ok:", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdRm(name string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pc, raw, err := findCipher(c, c.UserSymKey, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var parsed map[string]any
|
||||
_ = json.Unmarshal(raw, &parsed)
|
||||
id, _ := parsed["id"].(string)
|
||||
_ = pc
|
||||
fmt.Println("deleted:", name)
|
||||
return c.DeleteCipher(id)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
var err error
|
||||
switch os.Args[1] {
|
||||
case "login":
|
||||
fs := flag.NewFlagSet("login", flag.ExitOnError)
|
||||
server := fs.String("server", envOr("SM_SERVER", "https://pwvault.turnsys.com"), "vault server")
|
||||
email := fs.String("email", os.Getenv("SM_EMAIL"), "account email")
|
||||
password := fs.String("password", os.Getenv("SM_PASSWORD"), "master password (prefer env)")
|
||||
_ = fs.Parse(os.Args[2:])
|
||||
if *email == "" || *password == "" {
|
||||
fatal("login needs SM_EMAIL and SM_PASSWORD (or -email/-password)")
|
||||
}
|
||||
err = cmdLogin(*server, *email, *password)
|
||||
case "status":
|
||||
s, e := loadState()
|
||||
if e != nil {
|
||||
fmt.Println("locked/absent:", e)
|
||||
return
|
||||
}
|
||||
fmt.Printf("server=%s email=%s unlocked=%v\n", s.Server, s.Email, s.UserSymKey != "")
|
||||
case "list":
|
||||
pat := ""
|
||||
if len(os.Args) > 2 {
|
||||
pat = os.Args[2]
|
||||
}
|
||||
err = cmdList(pat)
|
||||
case "get":
|
||||
if len(os.Args) < 3 {
|
||||
fatal("get <name> [--field KEY]")
|
||||
}
|
||||
field := ""
|
||||
for i, a := range os.Args {
|
||||
if a == "--field" && i+1 < len(os.Args) {
|
||||
field = os.Args[i+1]
|
||||
}
|
||||
}
|
||||
err = cmdGet(os.Args[2], field)
|
||||
case "env":
|
||||
if len(os.Args) < 3 {
|
||||
fatal("env <name>")
|
||||
}
|
||||
err = cmdEnv(os.Args[2])
|
||||
case "set":
|
||||
fs := flag.NewFlagSet("set", flag.ExitOnError)
|
||||
file := fs.String("file", "", "env file with KEY=VALUE lines")
|
||||
folder := fs.String("folder", "", "folder id")
|
||||
_ = fs.Parse(os.Args[2:])
|
||||
rest := fs.Args()
|
||||
if len(rest) < 1 {
|
||||
fatal("set <name> [--file F] [k=v ...]")
|
||||
}
|
||||
err = cmdSet(rest[0], *file, rest[1:], *folder)
|
||||
case "rm":
|
||||
if len(os.Args) < 3 {
|
||||
fatal("rm <name>")
|
||||
}
|
||||
err = cmdRm(os.Args[2])
|
||||
case "folders":
|
||||
s, e := loadState()
|
||||
if e != nil {
|
||||
fatal(e)
|
||||
}
|
||||
c, e := newClientFromState(s)
|
||||
if e != nil {
|
||||
fatal(e)
|
||||
}
|
||||
raw, e := c.Sync()
|
||||
if e != nil {
|
||||
fatal(e)
|
||||
}
|
||||
var sync struct {
|
||||
Folders []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"folders"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &sync)
|
||||
for _, f := range sync.Folders {
|
||||
fmt.Println(f.ID, f.Name)
|
||||
}
|
||||
case "setfield":
|
||||
if len(os.Args) < 5 {
|
||||
fatal("setfield <name> <key> <value>")
|
||||
}
|
||||
err = cmdSetField(os.Args[2], os.Args[3], os.Args[4])
|
||||
case "convert":
|
||||
if len(os.Args) < 3 {
|
||||
fatal("convert <name>")
|
||||
}
|
||||
err = cmdConvert(os.Args[2])
|
||||
default:
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func envOr(k, def string) string {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func fatal(v any) {
|
||||
fmt.Fprintln(os.Stderr, "smcli:", v)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprint(os.Stderr, `smcli — KNELSecretsManager Go CLI (Bitwarden/Vaultwarden)
|
||||
login authenticate (SM_EMAIL/SM_PASSWORD/SM_SERVER)
|
||||
status auth/key state
|
||||
list [pattern] list item names
|
||||
get <name> [--field KEY] decrypt item / field
|
||||
env <name> print export KEY='VALUE' lines (sourcable)
|
||||
set <name> [--file F] k=v create/update secure note with hidden fields
|
||||
rm <name> delete item
|
||||
folders list folders
|
||||
`)
|
||||
}
|
||||
|
||||
// classifyCreds maps lifted env fields to login-item structure.
|
||||
func classifyCreds(fields []PlainField) (username, password string, uris []string, rest []PlainField) {
|
||||
used := map[int]bool{}
|
||||
pick := func(res []string) (PlainField, bool) {
|
||||
for i, f := range fields {
|
||||
if used[i] {
|
||||
continue
|
||||
}
|
||||
up := strings.ToUpper(f.Name)
|
||||
for _, re := range res {
|
||||
if strings.Contains(up, re) && f.Value != "" {
|
||||
used[i] = true
|
||||
return f, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return PlainField{}, false
|
||||
}
|
||||
if f, ok := pick([]string{"USERNAME", "USER", "EMAIL", "LOGIN", "AUTH_ID"}); ok {
|
||||
username = f.Value
|
||||
}
|
||||
if f, ok := pick([]string{"PASSWORD", "PASS", "TOKEN", "SECRET", "APIKEY", "API_KEY", "KEY", "HASH", "REFRESH"}); ok {
|
||||
password = f.Value
|
||||
}
|
||||
for i, f := range fields {
|
||||
up := strings.ToUpper(f.Name)
|
||||
if !used[i] && (strings.HasSuffix(up, "URL") || strings.HasSuffix(up, "URI") || strings.HasSuffix(up, "DASH") || strings.Contains(up, "ENDPOINT")) && f.Value != "" {
|
||||
used[i] = true
|
||||
uris = append(uris, f.Value)
|
||||
}
|
||||
}
|
||||
for i, f := range fields {
|
||||
if used[i] || f.Value == "" {
|
||||
continue
|
||||
}
|
||||
up := strings.ToUpper(f.Name)
|
||||
hidden := strings.Contains(up, "PASS") || strings.Contains(up, "TOKEN") || strings.Contains(up, "SECRET") || strings.Contains(up, "KEY") || strings.Contains(up, "HASH")
|
||||
t := 0
|
||||
if hidden {
|
||||
t = 1
|
||||
}
|
||||
rest = append(rest, PlainField{Type: t, Name: f.Name, Value: f.Value})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cmdConvert(name string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pc, raw, err := findCipher(c, c.UserSymKey, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
all := append([]PlainField{}, pc.Fields...)
|
||||
var folderID string
|
||||
var parsed map[string]any
|
||||
_ = json.Unmarshal(raw, &parsed)
|
||||
if v, ok := parsed["folderId"].(string); ok {
|
||||
folderID = v
|
||||
}
|
||||
username, password, uris, rest := classifyCreds(all)
|
||||
payload, err := BuildLoginJSON(c.UserSymKey, name, folderID, username, password, uris, rest, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := c.CreateCipher(payload); err != nil {
|
||||
return err
|
||||
}
|
||||
// remove the old note
|
||||
var parsedOld map[string]any
|
||||
_ = json.Unmarshal(raw, &parsedOld)
|
||||
if id, ok := parsedOld["id"].(string); ok {
|
||||
_ = c.DeleteCipher(id)
|
||||
}
|
||||
fmt.Printf("converted: %s (user=%v pass=%v uris=%d fields=%d)\n", name, username != "", password != "", len(uris), len(rest))
|
||||
return nil
|
||||
}
|
||||
|
||||
func urisOf(pc *PlainCipher) []string { return pc.URIs }
|
||||
|
||||
// cmdSetField surgically updates one key on an existing item: login.password,
|
||||
// login.username, an existing custom field, or appends a new hidden field.
|
||||
func cmdSetField(name, key, value string) error {
|
||||
s, err := loadState()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c, err := newClientFromState(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, err := c.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var sync struct {
|
||||
Ciphers []json.RawMessage `json:"ciphers"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &sync); err != nil {
|
||||
return err
|
||||
}
|
||||
var cr *cipherRaw
|
||||
for _, r := range sync.Ciphers {
|
||||
var x cipherRaw
|
||||
if json.Unmarshal(r, &x) == nil && x.ID != "" {
|
||||
pc2, derr := DecryptCipher(c.UserSymKey, r)
|
||||
if derr == nil && pc2.Name == name {
|
||||
cr = &x
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if cr == nil {
|
||||
return fmt.Errorf("item not found: %s", name)
|
||||
}
|
||||
ck, err := cipherKeyFor(c.UserSymKey, cr.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch strings.ToLower(key) {
|
||||
case "password":
|
||||
p, err := encStr(c.UserSymKey, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cr.Login == nil {
|
||||
cr.Login = &CipherLogin{}
|
||||
}
|
||||
cr.Login.Password = &p
|
||||
case "username":
|
||||
p, err := encStr(c.UserSymKey, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cr.Login == nil {
|
||||
cr.Login = &CipherLogin{}
|
||||
}
|
||||
cr.Login.Username = &p
|
||||
default:
|
||||
found := false
|
||||
for i := range cr.Fields {
|
||||
fn, derr := decStr(ck, ck, cr.Fields[i].Name)
|
||||
if derr == nil && fn == key {
|
||||
fv, err := encStr(c.UserSymKey, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cr.Fields[i].Value = &fv
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
fn, _ := encStr(c.UserSymKey, key)
|
||||
fv, _ := encStr(c.UserSymKey, value)
|
||||
cr.Fields = append(cr.Fields, CipherField{Type: 1, Name: &fn, Value: &fv})
|
||||
}
|
||||
}
|
||||
payload := map[string]any{"type": cr.Type, "name": cr.Name}
|
||||
if cr.Login != nil {
|
||||
payload["login"] = cr.Login
|
||||
}
|
||||
if cr.Fields != nil {
|
||||
payload["fields"] = cr.Fields
|
||||
}
|
||||
if cr.Notes != nil {
|
||||
payload["notes"] = *cr.Notes
|
||||
}
|
||||
if _, err := c.EditCipher(cr.ID, payload); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("updated:", name, key)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
package main
|
||||
|
||||
// Cipher model: decrypt/encrypt for the operations the lane uses
|
||||
// (secure notes with hidden fields; login items for password fields).
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// encStr handles empty/unset gracefully.
|
||||
type encFunc func(string) (string, error)
|
||||
|
||||
type CipherField struct {
|
||||
Type int `json:"type"` // 0=text, 1=hidden, 2=boolean (Bitwarden field type)
|
||||
Name *string `json:"name,omitempty"`
|
||||
Value *string `json:"value,omitempty"`
|
||||
}
|
||||
|
||||
type CipherLogin struct {
|
||||
Username *string `json:"username,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
URIs []any `json:"uris,omitempty"`
|
||||
}
|
||||
|
||||
type cipherRaw struct {
|
||||
ID string `json:"id"`
|
||||
OrganizationID *string `json:"organizationId"`
|
||||
Type int `json:"type"` // 1=login, 2=secureNote
|
||||
Name string `json:"name"`
|
||||
Notes *string `json:"notes"`
|
||||
Fields []CipherField `json:"fields,omitempty"`
|
||||
Key *string `json:"key,omitempty"`
|
||||
Login *CipherLogin `json:"login,omitempty"`
|
||||
SecureNote map[string]any `json:"secureNote,omitempty"`
|
||||
DeletedDate *string `json:"deletedDate,omitempty"`
|
||||
Extra map[string]interface{} `json:"-"`
|
||||
}
|
||||
|
||||
// cipherKeyFor returns the 64B key to use for a cipher's data.
|
||||
func cipherKeyFor(userKey []byte, encKey *string) ([]byte, error) {
|
||||
if encKey == nil || *encKey == "" {
|
||||
return userKey, nil
|
||||
}
|
||||
es, err := ParseEncString(*encKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return symDecrypt(userKey, es)
|
||||
}
|
||||
|
||||
func decStr(userKey []byte, cipherKey []byte, s *string) (string, error) {
|
||||
if s == nil || *s == "" {
|
||||
return "", nil
|
||||
}
|
||||
es, err := ParseEncString(*s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pt, err := symDecrypt(cipherKey, es)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(pt), nil
|
||||
}
|
||||
|
||||
func encStr(userKey []byte, s string) (string, error) {
|
||||
if s == "" {
|
||||
return "", nil
|
||||
}
|
||||
return symEncrypt(userKey, []byte(s))
|
||||
}
|
||||
|
||||
// PlainField is a decrypted custom field.
|
||||
type PlainField struct {
|
||||
Type int `json:"type"`
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// PlainCipher is a decrypted item view.
|
||||
type PlainCipher struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Notes string `json:"notes"`
|
||||
Type int `json:"type"`
|
||||
Fields []PlainField `json:"fields"`
|
||||
Login struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
} `json:"login"`
|
||||
FolderID string `json:"folderId"`
|
||||
URIs []string `json:"uris"`
|
||||
}
|
||||
|
||||
// DecryptCipher converts a raw sync cipher into PlainCipher.
|
||||
func DecryptCipher(userKey []byte, raw []byte) (*PlainCipher, error) {
|
||||
var cr cipherRaw
|
||||
if err := json.Unmarshal(raw, &cr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cr.DeletedDate != nil {
|
||||
return nil, fmt.Errorf("deleted")
|
||||
}
|
||||
ck, err := cipherKeyFor(userKey, cr.Key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pc := &PlainCipher{ID: cr.ID, Type: cr.Type}
|
||||
if pc.Name, err = decStr(userKey, ck, &cr.Name); err != nil {
|
||||
return nil, fmt.Errorf("name: %w", err)
|
||||
}
|
||||
if cr.Notes != nil {
|
||||
if pc.Notes, err = decStr(userKey, ck, cr.Notes); err != nil {
|
||||
return nil, fmt.Errorf("notes: %w", err)
|
||||
}
|
||||
}
|
||||
for _, f := range cr.Fields {
|
||||
pf := PlainField{Type: f.Type}
|
||||
if pf.Name, err = decStr(userKey, ck, f.Name); err != nil {
|
||||
continue
|
||||
}
|
||||
if pf.Value, err = decStr(userKey, ck, f.Value); err != nil {
|
||||
continue
|
||||
}
|
||||
pc.Fields = append(pc.Fields, pf)
|
||||
}
|
||||
if cr.Login != nil {
|
||||
if cr.Login.Username != nil {
|
||||
pc.Login.Username, _ = decStr(userKey, ck, cr.Login.Username)
|
||||
}
|
||||
if cr.Login.Password != nil {
|
||||
pc.Login.Password, _ = decStr(userKey, ck, cr.Login.Password)
|
||||
}
|
||||
for _, u := range cr.Login.URIs {
|
||||
var um map[string]any
|
||||
um, _ = u.(map[string]any)
|
||||
if um != nil {
|
||||
if us, ok := um["uri"].(string); ok {
|
||||
v, err := decStr(userKey, ck, &us)
|
||||
if err == nil {
|
||||
pc.URIs = append(pc.URIs, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return pc, nil
|
||||
}
|
||||
|
||||
// BuildSecureNoteJSON produces an encrypted create/update payload for a
|
||||
// secure-note item with hidden fields (type 1).
|
||||
func BuildSecureNoteJSON(userKey []byte, name, folderID, notes string, fields []PlainField, existing *cipherRaw) (map[string]any, error) {
|
||||
nameEnc, err := encStr(userKey, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
notesEnc, err := encStr(userKey, notes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var encFields []map[string]any
|
||||
for _, f := range fields {
|
||||
fn, err := encStr(userKey, f.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fv, err := encStr(userKey, f.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encFields = append(encFields, map[string]any{
|
||||
"type": f.Type,
|
||||
"name": fn,
|
||||
"value": fv,
|
||||
})
|
||||
}
|
||||
payload := map[string]any{
|
||||
"type": 2,
|
||||
"name": nameEnc,
|
||||
"notes": notesEnc,
|
||||
"fields": encFields,
|
||||
"secureNote": map[string]any{"type": 0},
|
||||
}
|
||||
if folderID != "" {
|
||||
payload["folderId"] = folderID
|
||||
}
|
||||
if existing != nil {
|
||||
payload["id"] = existing.ID
|
||||
if existing.OrganizationID != nil {
|
||||
payload["organizationId"] = existing.OrganizationID
|
||||
}
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
// parseEnvFields parses KEY=VALUE lines (quoted or bare) into hidden fields.
|
||||
func parseEnvFields(text string) []PlainField {
|
||||
var out []PlainField
|
||||
for _, line := range strings.Split(text, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
eq := strings.Index(line, "=")
|
||||
if eq <= 0 {
|
||||
continue
|
||||
}
|
||||
k := line[:eq]
|
||||
v := line[eq+1:]
|
||||
v = strings.Trim(v, "'\"")
|
||||
out = append(out, PlainField{Type: 1, Name: k, Value: v})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// BuildLoginJSON produces an encrypted login-item payload: username/password
|
||||
// as first-class login fields, URIs, and the remaining keys as custom fields
|
||||
// (hidden for secrets, text for URLs/IDs).
|
||||
func BuildLoginJSON(userKey []byte, name, folderID, username, password string, uris []string, fields []PlainField, existing *cipherRaw) (map[string]any, error) {
|
||||
nameEnc, err := encStr(userKey, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
login := map[string]any{}
|
||||
if username != "" {
|
||||
u, err := encStr(userKey, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
login["username"] = u
|
||||
}
|
||||
if password != "" {
|
||||
p, err := encStr(userKey, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
login["password"] = p
|
||||
}
|
||||
if len(uris) > 0 {
|
||||
var list []map[string]any
|
||||
for _, u := range uris {
|
||||
ue, err := encStr(userKey, u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, map[string]any{"uri": ue})
|
||||
}
|
||||
login["uris"] = list
|
||||
}
|
||||
var encFields []map[string]any
|
||||
for _, f := range fields {
|
||||
fn, err := encStr(userKey, f.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fv, err := encStr(userKey, f.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encFields = append(encFields, map[string]any{"type": f.Type, "name": fn, "value": fv})
|
||||
}
|
||||
payload := map[string]any{"type": 1, "name": nameEnc, "login": login}
|
||||
if len(encFields) > 0 {
|
||||
payload["fields"] = encFields
|
||||
}
|
||||
if folderID != "" {
|
||||
payload["folderId"] = folderID
|
||||
}
|
||||
if existing != nil {
|
||||
payload["id"] = existing.ID
|
||||
if existing.OrganizationID != nil {
|
||||
payload["organizationId"] = existing.OrganizationID
|
||||
}
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
// RFC 6238 TOTP (SHA1, 30s, 6 digits) for Bitwarden 2FA provider 0.
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/base32"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func totpNow(secretB32 string, at time.Time) (string, error) {
|
||||
secret := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(secretB32, " ", ""), "-", ""))
|
||||
pad := len(secret) % 8
|
||||
if pad != 0 {
|
||||
secret += strings.Repeat("=", 8-pad)
|
||||
}
|
||||
key, err := base32.StdEncoding.DecodeString(secret)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
counter := uint64(at.Unix()) / 30
|
||||
var ctr [8]byte
|
||||
binary.BigEndian.PutUint64(ctr[:], counter)
|
||||
h := hmac.New(sha1.New, key)
|
||||
h.Write(ctr[:])
|
||||
sum := h.Sum(nil)
|
||||
off := sum[len(sum)-1] & 0x0f
|
||||
code := (binary.BigEndian.Uint32(sum[off:off+4]) & 0x7fffffff) % 1000000
|
||||
return fmt.Sprintf("%06d", code), nil
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
)
|
||||
|
||||
func toHex(b []byte) string { return hex.EncodeToString(b) }
|
||||
|
||||
func fromHex(s string) ([]byte, error) {
|
||||
return hex.DecodeString(s)
|
||||
}
|
||||
|
||||
func hostnameSafe() string {
|
||||
h, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "unknown"
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func sha256Sum(b []byte) []byte {
|
||||
s := sha256.Sum256(b)
|
||||
return s[:]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
module git.knownelement.com/KNEL/KNELSecretsManager/cli
|
||||
|
||||
go 1.23
|
||||
|
||||
require golang.org/x/crypto v0.31.0
|
||||
|
||||
require golang.org/x/sys v0.28.0 // indirect
|
||||
@@ -0,0 +1,4 @@
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
@@ -0,0 +1,12 @@
|
||||
# KNELSecretsManager Go CLI — pure Go (no upstream Rust binary, no Node).
|
||||
FROM golang:1.23-alpine AS build
|
||||
WORKDIR /src
|
||||
COPY cli/go.mod cli/go.sum ./
|
||||
COPY cli/cmd/ ./cmd/
|
||||
RUN go build -ldflags="-s -w" -o /out/smcli ./cmd/smcli
|
||||
|
||||
FROM alpine:3.20
|
||||
RUN apk add --no-cache ca-certificates && adduser -D -u 65532 app
|
||||
COPY --from=build /out/smcli /usr/local/bin/smcli
|
||||
USER 65532:65532
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# KNELSecretsManager Go CLI (ukrrs-secretsmgr-cli) — always-hot container per
|
||||
# house convention (Charles 2026-08-31): docker exec per invocation, never
|
||||
# docker run per call. Scoped ops only; NEVER bare compose down.
|
||||
#
|
||||
# docker compose -f /path/to/this up -d smcli
|
||||
# docker exec -i ukrrs-secretsmgr-cli smcli env creds/cloudron
|
||||
#
|
||||
# Credentials for the vault account come from a 0600 env file (SM_EMAIL,
|
||||
# SM_PASSWORD, SM_TOTP_SECRET, SM_SERVER) — never committed.
|
||||
name: knel-secretsmanager
|
||||
services:
|
||||
smcli:
|
||||
image: git.knownelement.com/knel/knel-secretsmanager-cli@sha256:8abfc55dfa7ca9e70b286a249b7ca823531bd55da29bed70d3354a58ec4d8fec
|
||||
container_name: ukrrs-secretsmgr-cli
|
||||
restart: unless-stopped
|
||||
entrypoint: ["sleep", "infinity"]
|
||||
init: true
|
||||
env_file:
|
||||
- path: ./smcli.env
|
||||
required: false
|
||||
environment:
|
||||
SM_STATE_DIR: /data/state
|
||||
volumes:
|
||||
- smcli-state:/data/state
|
||||
volumes:
|
||||
smcli-state:
|
||||
@@ -0,0 +1,3 @@
|
||||
SM_SERVER=https://pwvault.turnsys.com
|
||||
SM_EMAIL=coo@turnsys.com
|
||||
SM_STATE_DIR=/data/state
|
||||
@@ -0,0 +1,25 @@
|
||||
# ADR-003: Custom Go CLI replaces the upstream Rust bw binary
|
||||
|
||||
Date: 2026-09-06. Decided by founder directive (finish the project with our
|
||||
custom Go cli, not the upstream rust one; no Rust supply-chain risk).
|
||||
|
||||
## Decision
|
||||
|
||||
KNELSecretsManager ships a pure-Go CLI (`cli/cmd/smcli`) implementing the
|
||||
Bitwarden/Vaultwarden API client: prelogin (PBKDF2/Argon2id), password grant
|
||||
with 2FA (TOTP), key derivation + decryption (stretched master key, user
|
||||
sym key), sync, item create/edit/delete with hidden fields.
|
||||
|
||||
The upstream Rust `bw` binary is RETIRED: bin/ scripts moved to
|
||||
archive/rust-bw-era/. The CLI ships in our own container
|
||||
(golang build -> alpine runtime, CA certs, non-root), delivered as the
|
||||
always-hot compose service `ukrrs-secretsmgr-cli` and the lane shim
|
||||
`.tools/sm`.
|
||||
|
||||
## Consequences
|
||||
|
||||
- No Rust/Node supply chain in the secrets tooling; Go module set is
|
||||
stdlib + golang.org/x/crypto.
|
||||
- Vault account bootstrapping (password + TOTP) happens via docker exec
|
||||
from the TSGCOO env file; the container env_file holds non-secret config.
|
||||
- Rotation waves (#829) rewire consumers from `bwlane.sh`/`.creds` to `sm`.
|
||||
@@ -0,0 +1,99 @@
|
||||
# KNELSecretsManager — Architecture
|
||||
|
||||
Status: production. Ruling chain: ADR-002 (containerized CLI) → ADR-003
|
||||
(pure-Go `smcli`, Rust `bw` retired). Founder directives: #829/#832.
|
||||
|
||||
## Components
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
subgraph vault["TSGCOO Bitwarden vault (self-hosted)"]
|
||||
V["pwvault.turnsys.com\n(Vaultwarden API)"]
|
||||
end
|
||||
subgraph workstation["Workstation (dev-only host)"]
|
||||
C["ukrrs-secretsmgr-cli\n(compose, always-hot)\npure-Go smcli v9+"]
|
||||
S1["/data2/TSGCOO/.local/bin/sm\n(TSGCOO account entry)"]
|
||||
S2[".tools/sm\n(reachableceo crossover)"]
|
||||
W1["mred-vp → Redmine identity"]
|
||||
W2["ci-green.sh → Gitea admin"]
|
||||
W3["redmine-sweep.sh"]
|
||||
end
|
||||
subgraph fleet["Fleet consumers (rotation waves)"]
|
||||
B["KNELBMS: on-box secrets.yaml\n(deploy webhook, gitea_auth_header,\nkuma_push_url, pve_*_api_token)"]
|
||||
K["pfv-k8s secrets\n(glpi-creds for kuma-glpi-bridge,\nflux PAT — wave 4)"]
|
||||
R["Nightly timers\n(pve-config-backup, glpi-reconcile)"]
|
||||
end
|
||||
S1 --> C
|
||||
S2 --> C
|
||||
W1 --> C
|
||||
W2 --> C
|
||||
W3 --> C
|
||||
C -->|HTTPS: prelogin/login(TOTP)/sync/CRUD| V
|
||||
C -.->|reads after rotation| B
|
||||
C -.-> K
|
||||
C -.-> R
|
||||
```
|
||||
|
||||
## Auth + unlock sequence
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant U as Operator/Consumer
|
||||
participant S as smcli (container)
|
||||
participant V as pwvault.turnsys.com
|
||||
U->>S: smcli login (SM_EMAIL, SM_PASSWORD, SM_TOTP_SECRET)
|
||||
S->>V: POST /api/accounts/prelogin {email}
|
||||
V-->>S: kdf type + iterations (PBKDF2 600k / Argon2id)
|
||||
S->>S: masterKey = KDF(password, email); authHash = PBKDF2(masterKey, password, 1)
|
||||
S->>V: POST /identity/connect/token (password grant, device fields)
|
||||
V-->>S: 400 Two factor required (provider 0)
|
||||
S->>S: TOTP code from SM_TOTP_SECRET (RFC 6238)
|
||||
S->>V: token request + twoFactorToken
|
||||
V-->>S: access_token (+Key, PrivateKey)
|
||||
S->>V: GET /api/sync
|
||||
V-->>S: profile.key (enc, type 2)
|
||||
S->>S: stretchedKey = HKDF(masterKey,"enc"/"mac"); userSymKey = decrypt(profile.key)
|
||||
S->>S: state.json 0600 (master/stretched/user keys + token)
|
||||
```
|
||||
|
||||
Item payloads are type-2 encStrings (AES-256-CBC + HMAC-SHA256, 32B enc +
|
||||
32B mac keys) encrypted/decrypted locally; the vault never sees plaintext.
|
||||
|
||||
## Consumer pattern
|
||||
|
||||
```bash
|
||||
# env-style consumers (export lines; URI/USERNAME/PASSWORD + original keys)
|
||||
eval "$(docker exec -i ukrrs-secretsmgr-cli smcli env creds/<name>)"
|
||||
# surgical single-field rotation update
|
||||
docker exec -i ukrrs-secretsmgr-cli smcli setfield creds/<name> <KEY> <newvalue>
|
||||
```
|
||||
|
||||
Items are LOGIN type: username + password/API-key as first-class fields,
|
||||
service URLs as URIs, remaining keys as named custom fields (hidden where
|
||||
secret). Original env key names are preserved as the custom-field names.
|
||||
|
||||
## Rotation program (#829)
|
||||
|
||||
All pre-migration material is presumed BURNED (plaintext on disk + LLM
|
||||
exposure). Waves, each item = rotate at source → `setfield` in vault →
|
||||
rewire consumers to `sm env` → validate (guard rule: never write the vault
|
||||
from a failed rotation):
|
||||
|
||||
1. Tooling tokens (librenms*, wazuh ✓, grafana*, gvm, technitium, phpipam,
|
||||
rancher-sectest, beszel, pihole, PMG pair, PBS tokens)
|
||||
2. Agent identities (gitea agent-stack, vptechops gitea/redmine)
|
||||
3. Platform (cloudron API token, kuma, discourse)
|
||||
4. Deep-wired (flux PAT + gitea runner + HA on-box secrets.yaml + k8s
|
||||
secrets + PVE upsagent tokens)
|
||||
|
||||
\* item-specific notes: librenms — token-mint API route absent on this
|
||||
install (UI/DB path); grafana — SSO-managed, local admin parked (instance
|
||||
password policy blocks CLI reset).
|
||||
|
||||
## KNELBMS integration
|
||||
|
||||
See [integration-knelbms.md](integration-knelbms.md). Short form: the BMS
|
||||
deploy pipeline (KNELBMS repo, packages/deploy_pipeline.yaml) consumes
|
||||
`gitea_auth_header` + `kuma_push_url` + `pve_*_api_token` from on-box
|
||||
`/config/secrets.yaml`; the vault is the source of record and rotations
|
||||
push to the box through the CR-gated provisioning path.
|
||||
@@ -0,0 +1,50 @@
|
||||
# KNELBMS integration — secrets flow
|
||||
|
||||
Repo: https://git.knownelement.com/KNEL/KNELBMS (PhysicalPlant lane;
|
||||
Home Assistant BMS on VM 100 @ pfv-tsys1, dev→release deploy by git).
|
||||
|
||||
## What the BMS consumes
|
||||
|
||||
| On-box secret (HA `secrets.yaml`) | Vault item + field | Provenance |
|
||||
|---|---|---|
|
||||
| `gitea_auth_header` | `creds/pfv-bms-deploy` → GITEA_DEPLOY_WATCH_TOKEN | release-branch sha-watch REST sensor |
|
||||
| `deploy_webhook_id` / `pfv_relay_webhook_id` | `creds/pfv-bms-deploy` | fast-path deploy webhook |
|
||||
| `kuma_push_url` | `creds/pfv-bms-beta` sibling — dead-man monitor `pfv-bms-ha-heartbeat-2026-09` (push token) | rotated 2026-09-06 under CR 21 |
|
||||
| `pve_tsys{1,3,4,5,6,7}_api_token` | `creds/pve-upsagent` (per-node fields) | `upsagent@pam!ups`, PVEAdmin-on-/vms, privsep=0 |
|
||||
| `doorman_*`, `pfvbms_smb_*`, beta HA creds | respective `creds/*` items | as rotated |
|
||||
|
||||
## Provisioning + rotation flow
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant R as Rotation run (#829 wave)
|
||||
participant V as TSGCOO vault
|
||||
participant B as pfv-bms on-box secrets.yaml
|
||||
participant H as Home Assistant
|
||||
R->>V: new value (sm setfield creds/<item> <KEY>)
|
||||
R->>B: CR-gated provisioning (ssh -p 22222, CR + ha core check)
|
||||
R->>H: ha core restart (Kuma window; dead-man covers the gap)
|
||||
H-->>R: post-deploy validation (entity/heartbeat checks)
|
||||
R->>V: rotation evidence on #829
|
||||
```
|
||||
|
||||
Rules that bind this flow (house rules + #811):
|
||||
|
||||
- pfv-bms prod changes need a GLPI CR **and** a Kuma maintenance window
|
||||
when a restart is involved; the deploy path itself stays
|
||||
dev → CI → release PR (founder merges).
|
||||
- HA runtime template contexts cannot read secrets — the on-box
|
||||
`shell_command` entries reference `!secret` names only (see KNELBMS
|
||||
PR #6 / CR 21 for the dead-man fix that taught us this).
|
||||
- The dead-man heartbeat (`pfv-bms-ha-heartbeat-2026-09`, Kuma id 291)
|
||||
is the canary for provisioning mistakes: if the on-box secret and the
|
||||
vault disagree, the push fails and the monitor pages.
|
||||
|
||||
## Current integration state (2026-09-06)
|
||||
|
||||
- On-box `secrets.yaml` provisioned manually under CR 21 (PVE tokens +
|
||||
rotated Kuma push URL); git-side KNELBMS matches for the `!secret`
|
||||
keys it owns (PR #6 on dev, awaiting founder release merge).
|
||||
- Automated push-from-vault (rotation waves writing the box directly via
|
||||
the AWX ssh path) is **planned, not built** — wave 4. Until then the
|
||||
table above is the manual runbook, executed under CR.
|
||||
Reference in New Issue
Block a user