- commit
- d3c5dba
- parent
- 375ace8
- author
- Eric Bower
- date
- 2024-04-30 14:46:04 +0000 UTC
feat: update pubkey comments w authorized_keys chore: better output when uploading pubkeys
1 files changed,
+42,
-8
+42,
-8
1@@ -11,6 +11,7 @@ import (
2 "time"
3
4 "github.com/charmbracelet/ssh"
5+ "github.com/charmbracelet/wish"
6 "github.com/picosh/pico/db"
7 "github.com/picosh/pico/filehandlers/util"
8 "github.com/picosh/pico/shared"
9@@ -140,17 +141,25 @@ type KeyWithId struct {
10 }
11
12 type KeyDiffResult struct {
13- Add []KeyWithId
14- Rm []string
15+ Add []KeyWithId
16+ Rm []string
17+ Update []KeyWithId
18 }
19
20 func authorizedKeysDiff(keyInUse ssh.PublicKey, curKeys []KeyWithId, nextKeys []KeyWithId) KeyDiffResult {
21+ update := []KeyWithId{}
22 add := []KeyWithId{}
23 for _, nk := range nextKeys {
24 found := false
25 for _, ck := range curKeys {
26 if ssh.KeysEqual(nk.Pk, ck.Pk) {
27 found = true
28+
29+ // update the comment field
30+ if nk.Comment != ck.Comment {
31+ ck.Comment = nk.Comment
32+ update = append(update, ck)
33+ }
34 break
35 }
36 }
37@@ -180,12 +189,14 @@ func authorizedKeysDiff(keyInUse ssh.PublicKey, curKeys []KeyWithId, nextKeys []
38 }
39
40 return KeyDiffResult{
41- Add: add,
42- Rm: rm,
43+ Add: add,
44+ Rm: rm,
45+ Update: update,
46 }
47 }
48
49 func (h *UploadHandler) ProcessAuthorizedKeys(text []byte, logger *slog.Logger, user *db.User, s ssh.Session) error {
50+ logger.Info("processing new authorized_keys")
51 dbpool := h.DBPool
52
53 curKeysStr, err := dbpool.FindKeysForUser(user)
54@@ -209,7 +220,7 @@ func (h *UploadHandler) ProcessAuthorizedKeys(text []byte, logger *slog.Logger,
55 if err != nil {
56 continue
57 }
58- curKeys = append(curKeys, KeyWithId{Pk: key, ID: pk.ID})
59+ curKeys = append(curKeys, KeyWithId{Pk: key, ID: pk.ID, Comment: pk.Name})
60 }
61
62 diff := authorizedKeysDiff(s.PublicKey(), curKeys, nextKeys)
63@@ -220,19 +231,43 @@ func (h *UploadHandler) ProcessAuthorizedKeys(text []byte, logger *slog.Logger,
64 continue
65 }
66
67- logger.Info("adding pubkey for user", "pubkey", key)
68+ wish.Errorf(s, "adding pubkey (%s)\n", key)
69+ logger.Info("adding pubkey", "pubkey", key)
70
71 err = dbpool.InsertPublicKey(user.ID, key, pk.Comment, nil)
72 if err != nil {
73+ wish.Errorf(s, "error: could not insert pubkey: %s (%s)\n", err.Error(), key)
74 logger.Error("could not insert pubkey", "err", err.Error())
75 }
76 }
77
78+ for _, pk := range diff.Update {
79+ key, err := shared.KeyForKeyText(pk.Pk)
80+ if err != nil {
81+ continue
82+ }
83+
84+ wish.Errorf(s, "updating pubkey with comment: %s (%s)\n", pk.Comment, key)
85+ logger.Info(
86+ "updating pubkey with comment",
87+ "pubkey", key,
88+ "comment", pk.Comment,
89+ )
90+
91+ _, err = dbpool.UpdatePublicKey(pk.ID, pk.Comment)
92+ if err != nil {
93+ wish.Errorf(s, "error: could not update pubkey: %s (%s)\n", err.Error(), key)
94+ logger.Error("could not update pubkey", "err", err.Error(), "key", key)
95+ }
96+ }
97+
98 if len(diff.Rm) > 0 {
99- logger.Info("removing pubkeys for user", "pubkeys", diff.Rm)
100+ wish.Errorf(s, "removing pubkeys: %s\n", diff.Rm)
101+ logger.Info("removing pubkeys", "pubkeys", diff.Rm)
102
103 err = dbpool.RemoveKeys(diff.Rm)
104 if err != nil {
105+ wish.Errorf(s, "error: could not rm pubkeys: %s\n", err.Error())
106 logger.Error("could not remove pubkey", "err", err.Error())
107 }
108 }
109@@ -252,7 +287,6 @@ func (h *UploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string, er
110 logger = logger.With(
111 "user", user.Name,
112 "filename", filename,
113- "space", h.Cfg.Space,
114 )
115
116 var text []byte