feat: l for details; details for vaults, scopes and files; getting started; the age of cached lists (#17)
- `l` goes to the details of what is selected; `enter` still drills in - vaults, scopes and files show their details (vault settings from the listing, no extra requests) - with nothing to show, the right pane says how to get started - a list from the cache says how old it is while it reloads - what is expiring, in the status pane and `lazysecrets expiring` - when access is refused, what to check, for Azure and Databricks - no group list in an account's details, and no Microsoft Graph call for it Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit was merged in pull request #17.
This commit is contained in:
1 parent
9b0329e9da
commit
09bacd8438
32 files changed
+1321
-254
No files matched your search
@@ -0,0 +1,62 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.kebcloud.no/keb/lazysecrets/internal/provider"
|
||||
)
|
||||
|
||||
// Details are how a file is encrypted and for whom, and what git knows of
|
||||
// it: a plain .env file that git tracks, or could, is marked.
|
||||
func (p *Provider) Details(ctx context.Context, c provider.Container) ([]provider.Attr, error) {
|
||||
r, ok := p.repo(c.Source.ID)
|
||||
if !ok {
|
||||
return nil, provider.ErrUnsupported
|
||||
}
|
||||
rel, err := filepath.Rel(r.root, c.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []provider.Attr
|
||||
meta, isSops := readMeta(c.ID)
|
||||
if isSops {
|
||||
out = append(out, provider.Attr{Label: "encryption", Value: "sops · " + strings.Join(meta.methods, ", ")})
|
||||
for i, k := range meta.recipients {
|
||||
label := ""
|
||||
if i == 0 {
|
||||
label = "keys"
|
||||
}
|
||||
out = append(out, provider.Attr{Label: label, Value: k})
|
||||
}
|
||||
if !meta.modified.IsZero() {
|
||||
out = append(out, provider.Attr{Label: "encrypted", Value: meta.modified.Local().Format("2006-01-02 15:04")})
|
||||
}
|
||||
} else {
|
||||
out = append(out, provider.Attr{Label: "encryption", Value: "none: plain text"})
|
||||
}
|
||||
|
||||
git := func(args ...string) (string, bool) {
|
||||
b, err := exec.CommandContext(ctx, "git", append([]string{"-C", r.root}, args...)...).Output()
|
||||
return strings.TrimSpace(string(b)), err == nil
|
||||
}
|
||||
switch _, tracked := git("ls-files", "--error-unmatch", "--", rel); {
|
||||
case tracked && !isSops:
|
||||
out = append(out, provider.Attr{Label: "git", Value: "tracked: its values are in the repo's history", Warn: true})
|
||||
case tracked:
|
||||
out = append(out, provider.Attr{Label: "git", Value: "tracked"})
|
||||
default:
|
||||
if _, ignored := git("check-ignore", "-q", "--", rel); ignored {
|
||||
out = append(out, provider.Attr{Label: "git", Value: "ignored"})
|
||||
} else {
|
||||
out = append(out, provider.Attr{Label: "git", Value: "untracked and not ignored: easy to commit by mistake", Warn: !isSops})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
if last, ok := git("log", "-1", "--format=%h · %ar · %an: %s", "--", rel); ok && last != "" {
|
||||
out = append(out, provider.Attr{Label: "last commit", Value: last})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -815,8 +815,30 @@ func anyMatch(res []*regexp.Regexp, f string) bool {
|
||||
// --- sops files ---
|
||||
|
||||
type sopsMeta struct {
|
||||
methods []string // age, azure_kv, …
|
||||
modified time.Time
|
||||
methods []string // age, azure_kv, …
|
||||
recipients []string // "age age1…", "azure_kv https://kv…/keys/sops"
|
||||
modified time.Time
|
||||
}
|
||||
|
||||
// recipient names a key a file is encrypted for, from its sops metadata:
|
||||
// public, never a secret.
|
||||
func recipient(method string, f map[string]string) string {
|
||||
var v string
|
||||
switch method {
|
||||
case "age":
|
||||
v = f["recipient"]
|
||||
case "pgp":
|
||||
v = f["fp"]
|
||||
case "kms":
|
||||
v = f["arn"]
|
||||
case "gcp_kms":
|
||||
v = f["resource_id"]
|
||||
case "azure_kv":
|
||||
v = strings.TrimSuffix(f["vault_url"], "/") + "/keys/" + f["name"]
|
||||
case "hc_vault":
|
||||
v = strings.TrimSuffix(f["vault_address"], "/") + " " + f["engine_path"] + "/" + f["key_name"]
|
||||
}
|
||||
return method + " " + v
|
||||
}
|
||||
|
||||
const maxFileSize = 4 << 20
|
||||
@@ -848,6 +870,15 @@ func readMeta(file string) (sopsMeta, bool) {
|
||||
for _, k := range known {
|
||||
if v, ok := doc.Sops[k].([]any); ok && len(v) > 0 {
|
||||
m.methods = append(m.methods, k)
|
||||
for _, e := range v {
|
||||
fields := map[string]string{}
|
||||
if em, ok := e.(map[string]any); ok {
|
||||
for fk, fv := range em {
|
||||
fields[fk] = fmt.Sprint(fv)
|
||||
}
|
||||
}
|
||||
m.recipients = append(m.recipients, recipient(k, fields))
|
||||
}
|
||||
}
|
||||
}
|
||||
if s, ok := doc.Sops["lastmodified"].(string); ok {
|
||||
@@ -864,6 +895,7 @@ func readMeta(file string) (sopsMeta, bool) {
|
||||
for _, k := range known {
|
||||
if strings.Contains(text, "sops_"+k+"__") {
|
||||
m.methods = append(m.methods, k)
|
||||
m.recipients = append(m.recipients, envRecipients(text, k)...)
|
||||
}
|
||||
}
|
||||
if i := strings.Index(text, "sops_lastmodified"); i >= 0 {
|
||||
@@ -881,6 +913,35 @@ func readMeta(file string) (sopsMeta, bool) {
|
||||
return m, true
|
||||
}
|
||||
|
||||
// envRecipients are method's keys in a dotenv or ini sops file, whose
|
||||
// metadata is flattened: sops_age__list_0__map_recipient=age1….
|
||||
func envRecipients(text, method string) []string {
|
||||
prefix := "sops_" + method + "__list_"
|
||||
var order []string
|
||||
keys := map[string]map[string]string{}
|
||||
for line := range strings.Lines(text) {
|
||||
k, v, ok := strings.Cut(strings.TrimSpace(line), "=")
|
||||
rest, found := strings.CutPrefix(strings.TrimSpace(k), prefix)
|
||||
if !ok || !found {
|
||||
continue
|
||||
}
|
||||
n, field, ok := strings.Cut(rest, "__map_")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if keys[n] == nil {
|
||||
keys[n] = map[string]string{}
|
||||
order = append(order, n)
|
||||
}
|
||||
keys[n][field] = strings.TrimSpace(v)
|
||||
}
|
||||
out := make([]string, len(order))
|
||||
for i, n := range order {
|
||||
out[i] = recipient(method, keys[n])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func formatOf(file string) string {
|
||||
switch strings.ToLower(filepath.Ext(file)) {
|
||||
case ".yaml", ".yml":
|
||||
|
||||
@@ -477,3 +477,47 @@ func TestFile(t *testing.T) {
|
||||
t.Errorf("lines %v\nwant %v", lines, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileDetails(t *testing.T) {
|
||||
root := gitRepo(t, map[string]string{
|
||||
".gitignore": ".env\n",
|
||||
"secrets/app.yaml": sopsYAML,
|
||||
"prod.env": sopsEnv,
|
||||
"tracked.env": "TOKEN=x\n", // a plain .env in git: worth a look
|
||||
})
|
||||
os.WriteFile(filepath.Join(root, ".env"), []byte("TOKEN=x\n"), 0o600)
|
||||
os.WriteFile(filepath.Join(root, "local.env"), []byte("TOKEN=x\n"), 0o600)
|
||||
p := New(Settings{Dir: root})
|
||||
ctx := context.Background()
|
||||
srcs, _ := p.Sources(ctx)
|
||||
details := func(name string) map[string]provider.Attr {
|
||||
t.Helper()
|
||||
attrs, err := p.Details(ctx, provider.Container{Source: srcs[0], ID: filepath.Join(root, name), Name: name})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := map[string]provider.Attr{}
|
||||
for _, a := range attrs {
|
||||
out[a.Label] = a
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
d := details("secrets/app.yaml")
|
||||
if d["encryption"].Value != "sops · age" || d["keys"].Value != "age age1xxxx" || d["git"].Value != "tracked" ||
|
||||
!strings.Contains(d["last commit"].Value, "t: files") || d["encrypted"].Value == "" {
|
||||
t.Errorf("sops yaml: %+v", d)
|
||||
}
|
||||
if d := details("prod.env"); d["keys"].Value != "age age1xxxx" {
|
||||
t.Errorf("sops env: %+v", d)
|
||||
}
|
||||
if d := details("tracked.env"); d["encryption"].Value != "none: plain text" || !d["git"].Warn {
|
||||
t.Errorf("tracked plain env: %+v", d)
|
||||
}
|
||||
if d := details(".env"); d["git"].Value != "ignored" || d["git"].Warn {
|
||||
t.Errorf("ignored env: %+v", d)
|
||||
}
|
||||
if d := details("local.env"); !d["git"].Warn || !strings.Contains(d["git"].Value, "not ignored") {
|
||||
t.Errorf("untracked env: %+v", d)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user