This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
74 lines (66 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/suside/ssh-crypt/lib"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
idRsaDefault = os.ExpandEnv(
strings.Join([]string{"$HOME", ".ssh", "id_rsa"}, string(os.PathSeparator)))
authKeysDefault = os.ExpandEnv(
strings.Join([]string{"$HOME", ".ssh", "authorized_keys"}, string(os.PathSeparator)))
version = "master"
app = kingpin.New(
"ssh-crypt",
fmt.Sprintf("Encrypt file with your ssh keys. (%s)", version),
)
idRsaPath = app.Flag(
"identity-file",
"File from which the identity (private key) is read.",
).Short('i').Default(idRsaDefault).ExistingFile()
edit = app.Command("edit", "Edit vault content")
vaultFileE = edit.Arg(
"vault-file",
"File where encrypted content is stored.",
).Required().String()
authKeysPath = edit.Flag(
"authorized-keys",
"File from which the public keys are read.",
).Short('a').Default(authKeysDefault).ExistingFile()
fromStdin = edit.Flag(
"stdin",
"Read from standard input instead from $EDITOR",
).Short('s').Bool()
view = app.Command("view", "Print vault content")
vaultFileV = view.Arg(
"vault-file",
"File where encrypted content is stored.",
).Required().String()
)
func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case edit.FullCommand():
vault := lib.Vault{}
vault.ReadAuthorizedKeys(*authKeysPath)
if *fromStdin {
vault.ReadStdIn()
} else {
err := vault.DecryptVaultWithKey(*vaultFileE, *idRsaPath)
if err != nil {
log.Fatal(err)
}
vault.EditVaultFile()
}
vault.StoreSecuredVault(*vaultFileE)
case view.FullCommand():
vault := lib.Vault{}
err := vault.DecryptVaultWithKey(*vaultFileV, *idRsaPath)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(vault.Plaintext))
}
}