-
Notifications
You must be signed in to change notification settings - Fork 14
/
grants.go
150 lines (125 loc) · 3.35 KB
/
grants.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"context"
"fmt"
"os"
cli "github.com/jawher/mow.cli"
"github.com/monax/hoard/v6"
"github.com/monax/hoard/v6/api"
"github.com/monax/hoard/v6/grant"
)
// PutSeal encrypts and stores data then prints a grant
func (client *Client) PutSeal(cmd *cli.Cmd) {
salt := addStringOpt(cmd, "salt", saltOpt)
key := addStringOpt(cmd, "key", keyOpt)
chunk := addIntOpt(cmd, "chunk", chunkOpt, chunkSize)
cmd.Action = func() {
validateChunkSize(*chunk)
spec := grant.Spec{Plaintext: &grant.PlaintextSpec{}}
if *key != "" {
spec = grant.Spec{
Plaintext: nil,
Symmetric: &grant.SymmetricSpec{PublicID: *key},
}
}
data := readData(os.Stdin)
seal, err := client.grant.PutSeal(context.Background())
if err != nil {
fatalf("Error starting client: %v", err)
}
err = hoard.SendPlaintextAndGrantSpec(seal, &spec, data, parseSalt(salt), *chunk)
if err != nil {
fatalf("Error sending data: %v", err)
}
grant, err := seal.CloseAndRecv()
if err != nil {
fatalf("Error closing client: %v", err)
}
fmt.Printf("%s\n", jsonString(grant))
}
}
// Seal reads encrypted data then prints a grant
func (client *Client) Seal(cmd *cli.Cmd) {
address := addStringOpt(cmd, "address", addrOpt)
key := addStringOpt(cmd, "key", keyOpt)
cmd.Action = func() {
spec := grant.Spec{Plaintext: &grant.PlaintextSpec{}}
if *key != "" {
spec = grant.Spec{
Plaintext: nil,
Symmetric: &grant.SymmetricSpec{PublicID: *key},
}
}
ref := readReference(address)
seal, err := client.grant.Seal(context.Background(),
&api.ReferenceAndGrantSpec{
Reference: ref,
GrantSpec: &spec,
},
)
if err != nil {
fatalf("Error sealing data: %v", err)
}
fmt.Printf("%s\n", jsonString(seal))
}
}
// Reseal reads a grant then prints a new grant
func (client *Client) Reseal(cmd *cli.Cmd) {
key := addStringOpt(cmd, "key", keyOpt)
cmd.Action = func() {
prev := readGrant()
next := grant.Spec{Plaintext: &grant.PlaintextSpec{}}
if *key != "" {
next = grant.Spec{
Plaintext: nil,
Symmetric: &grant.SymmetricSpec{PublicID: *key},
}
}
ref, err := client.grant.Reseal(context.Background(),
&api.GrantAndGrantSpec{
Grant: prev,
GrantSpec: &next,
})
if err != nil {
fatalf("Error resealing data: %v", err)
}
fmt.Printf("%s\n", jsonString(ref))
}
}
// Unseal reads a grant then prints the original reference
func (client *Client) Unseal(cmd *cli.Cmd) {
cmd.Action = func() {
grt := readGrant()
ref, err := client.grant.Unseal(context.Background(), grt)
if err != nil {
fatalf("Error unsealing data: %v", err)
}
fmt.Printf("%s\n", jsonString(ref))
}
}
// UnsealGet reads a grant, decrypts and prints the stored data
func (client *Client) UnsealGet(cmd *cli.Cmd) {
cmd.Action = func() {
grt := readGrant()
unseal, err := client.grant.UnsealGet(context.Background(), grt)
if err != nil {
fatalf("Error starting client: %v", err)
}
data, _, err := hoard.ReceivePlaintext(unseal)
if err != nil {
fatalf("Error receiving data: %v", err)
}
os.Stdout.Write(data)
}
}
// UnsealDelete reads a grant and deletes the encrypted data
func (client *Client) UnsealDelete(cmd *cli.Cmd) {
cmd.Action = func() {
grt := readGrant()
_, err := client.grant.UnsealDelete(context.Background(), grt)
if err != nil {
fatalf("Error unsealing data: %v", err)
}
return
}
}