Skip to content

Commit 9de830f

Browse files
authored
Merge pull request #26 from TimothyYe/cache
Cache
2 parents 8e29431 + 3af09e6 commit 9de830f

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Download it from [releases](https://github.com/TimothyYe/skm/releases) and extac
5151
```bash
5252
% skm
5353

54-
SKM V0.6
54+
SKM V0.7
5555
https://github.com/TimothyYe/skm
5656

5757
NAME:
@@ -61,7 +61,7 @@ USAGE:
6161
skm [global options] command [command options] [arguments...]
6262

6363
VERSION:
64-
0.6
64+
0.7
6565

6666
COMMANDS:
6767
init, i Initialize SSH keys store for the first time usage.
@@ -74,11 +74,15 @@ COMMANDS:
7474
display, dp Display the current SSH public key or specific SSH public key by alias name.
7575
backup, b Backup all SSH keys to an archive file.
7676
restore, r Restore SSH keys from an existing archive file.
77+
cache Add your SSH to SSH agent cache via alias name.
7778
help, h Shows a list of commands or help for one command.
7879

7980
GLOBAL OPTIONS:
80-
--help, -h show help
81-
--version, -v print the version
81+
--store-path value Path where SKM should store its profiles (default: "/Users/timothy/.skm")
82+
--ssh-path value Path to a .ssh folder (default: "/Users/timothy/.ssh")
83+
--restic-path value Path to the restic binary
84+
--help, -h show help
85+
--version, -v print the version
8286
```
8387

8488
### For the first time use
@@ -250,6 +254,37 @@ restoring <Snapshot $SNAPSHOT of [/Users/$USER/.skm] at 2018-10-03 19:40:33.3331
250254
✔ Backup restored to /Users/$USER/.skm
251255
```
252256
257+
### Integrate with SSH agent
258+
259+
You can use `cache` command to cache your SSH key into SSH agent's cache via SSH alias name.
260+
261+
__Cache your SSH key__
262+
263+
```bash
264+
λ tim [~/]
265+
→ skm cache --add my
266+
Enter passphrase for /Users/timothy/.skm/my/id_rsa:
267+
Identity added: /Users/timothy/.skm/my/id_rsa (/Users/timothy/.skm/my/id_rsa)
268+
✔ SSH key [my] already added into cache
269+
```
270+
271+
__Remove your SSH key from cache__
272+
273+
```bash
274+
λ tim [~/]
275+
→ ./skm cache --del my
276+
Identity removed: /Users/timothy/.skm/my/id_rsa (MyKEY)
277+
✔ SSH key [my] removed from cache
278+
```
279+
280+
__List your cached SSH keys from SSH agent__
281+
282+
```bash
283+
λ tim [skm/cmd/skm] at  cache !
284+
→ ./skm cache --list
285+
2048 SHA256:qAVcwc0tdUOCjH3sTskwxAmfMQiL2sKtfPBXFnUoZHQ /Users/timothy/.skm/my/id_rsa (RSA)
286+
```
287+
253288
### Hook mechanism
254289

255290
Edit and place a executable file named ```hook``` at the specified key directory, for example:

cmd/skm/actions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,37 @@ func restore(c *cli.Context) error {
410410
return nil
411411
}
412412

413+
func cache(c *cli.Context) error {
414+
alias := c.Args().Get(0)
415+
env := mustGetEnvironment(c)
416+
keyMap := skm.LoadSSHKeys(env)
417+
418+
if c.Bool("add") {
419+
// add SSH key into SSH agent cache
420+
if err := skm.AddCache(alias, keyMap, env); err != nil {
421+
color.Red("%s"+err.Error(), skm.CrossSymbol)
422+
return nil
423+
}
424+
color.Green("%s SSH key [%s] already added into cache", skm.CheckSymbol, alias)
425+
} else if c.Bool("del") {
426+
// delete SSH key from SSH agent cache
427+
if err := skm.DeleteCache(alias, keyMap, env); err != nil {
428+
color.Red("%s"+err.Error(), skm.CrossSymbol)
429+
return nil
430+
}
431+
color.Green("%s SSH key [%s] removed from cache", skm.CheckSymbol, alias)
432+
} else if c.Bool("list") {
433+
// list all cached SSH keys from SSH agent cache
434+
if err := skm.ListCache(); err != nil {
435+
return nil
436+
}
437+
} else {
438+
color.Red("%s Invalid parameter!", skm.CrossSymbol)
439+
return errors.New("invalid parameter")
440+
}
441+
return nil
442+
}
443+
413444
func getKeyPayload(keyPath string) string {
414445
key, err := ioutil.ReadFile(keyPath)
415446
if err != nil {

cmd/skm/commands.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,15 @@ func initCommands() []cli.Command {
8181
cli.StringFlag{Name: "restic-snapshot", Usage: "The snapshot to be restored"},
8282
},
8383
},
84+
{
85+
Name: "cache",
86+
Usage: "Add your SSH to SSH agent cache via alias name",
87+
Action: cache,
88+
Flags: []cli.Flag{
89+
cli.BoolFlag{Name: "add", Usage: "Add SSH key to SSH agent cache"},
90+
cli.BoolFlag{Name: "del", Usage: "Remove SSH key from SSH agent cache"},
91+
cli.BoolFlag{Name: "list", Usage: "List all SSH keys from SSH agent cache"},
92+
},
93+
},
8494
}
8595
}

utils.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package skm
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -114,6 +115,58 @@ func RunHook(alias string, env *Environment) {
114115
}
115116
}
116117

118+
// AddCache adds SSH to ssh agent cache via key alias
119+
func AddCache(alias string, keyMap map[string]*SSHKey, env *Environment) error {
120+
key, found := keyMap[alias]
121+
122+
if !found {
123+
return fmt.Errorf("SSH key [%s] not found", alias)
124+
}
125+
126+
// Add key to SSH agent cache
127+
privateKeyPath := filepath.Join(env.StorePath, alias, key.Type.PrivateKey())
128+
args := []string{privateKeyPath}
129+
result := Execute("", "ssh-add", args...)
130+
131+
if !result {
132+
return errors.New("Failed to remove SSH key from cache")
133+
}
134+
135+
return nil
136+
}
137+
138+
// DeleteCache removes SSH key from SSH agent cache via key alias
139+
func DeleteCache(alias string, keyMap map[string]*SSHKey, env *Environment) error {
140+
key, found := keyMap[alias]
141+
142+
if !found {
143+
return fmt.Errorf("SSH key [%s] not found", alias)
144+
}
145+
146+
// Remove key from SSH agent cache
147+
privateKeyPath := filepath.Join(env.StorePath, alias, key.Type.PrivateKey())
148+
args := []string{"-d", privateKeyPath}
149+
result := Execute("", "ssh-add", args...)
150+
151+
if !result {
152+
return errors.New("Failed to remove SSH key from cache")
153+
}
154+
155+
return nil
156+
}
157+
158+
// ListCache lists cached SSH key from SSH agent cache
159+
func ListCache() error {
160+
args := []string{"-l"}
161+
result := Execute("", "ssh-add", args...)
162+
163+
if !result {
164+
return errors.New("Failed to list SSH keys from cache")
165+
}
166+
167+
return nil
168+
}
169+
117170
// CreateLink creates symbol link for specified SSH key
118171
func CreateLink(alias string, keyMap map[string]*SSHKey, env *Environment) {
119172
ClearKey(env)

0 commit comments

Comments
 (0)