/
main.go
295 lines (272 loc) · 8.72 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
bufio "bufio"
context "context"
fmt "fmt"
log "log"
os "os"
strings "strings"
rand "crypto/rand"
rsa "crypto/rsa"
ssh "golang.org/x/crypto/ssh"
grpc "google.golang.org/grpc"
grpc_example "github.com/jeroenvanmaanen/archetype-go-axon/src/pkg/grpc/example"
trusted "github.com/jeroenvanmaanen/archetype-go-axon/src/pkg/trusted"
utils "github.com/jeroenvanmaanen/archetype-go-axon/src/pkg/utils"
)
func main() {
log.Printf("Key Manager -- reading from standard input")
conn, e := grpc.Dial("localhost:8181", grpc.WithInsecure())
if e != nil {
log.Printf("Error when dialing application: %v", e)
panic("Could not connect to application")
}
defer utils.ReportError("Close connection to application", conn.Close)
client := grpc_example.NewGreeterServiceClient(conn)
log.Printf("Greeter service client: %v", client)
stream, e := client.ChangeTrustedKeys(context.Background())
if e != nil {
log.Printf("Error when opening stream for ChangeTrustedKeys: %v", e)
panic("Could not call ChangeTrustedKeys")
}
log.Printf("ChangeTrustedKeys stream: %v", stream)
request := grpc_example.TrustedKeyRequest{}
e = stream.Send(&request)
if e != nil {
log.Printf("Error when sending first (empty) request for ChangeTrustedKeys: %v", e)
panic("Could not send first request")
}
response, e := stream.Recv()
if e != nil {
log.Printf("Error when receiving first response for ChangeTrustedKeys: %v", e)
panic("Could not receive first response]")
}
log.Printf("First response: %v", response)
nonce := response.Nonce
reader := bufio.NewReader(os.Stdin)
line := readLine(reader)
var name string
var pem string
var managerPrivateKey *rsa.PrivateKey
var signer ssh.Signer
var signatureName string
for true {
if strings.HasPrefix(line, ">>> Manager: ") {
signatureName = getName(line)
pem, line = readPem(reader)
log.Printf("Manager name: %v: %d", signatureName, len(pem))
managerPrivateKey, e = trusted.ParsePrivateKey(pem)
if e != nil {
log.Printf("Error when parsing manager private key: %v", e)
panic("Could not parse manager private key")
}
signer, e = ssh.NewSignerFromKey(managerPrivateKey)
if e != nil {
log.Printf("Unable to create signer from private key: %v", e)
panic("Could not create signer")
}
} else if strings.HasPrefix(line, ">>> Management") {
line, nonce = addPublicKeys(true, reader, &signer, signatureName, nonce, stream)
} else if strings.HasPrefix(line, ">>> Trusted") {
line, nonce = addPublicKeys(false, reader, &signer, signatureName, nonce, stream)
} else if strings.HasPrefix(line, ">>> Identity Provider: ") {
name = getName(line)
pem, line = readPem(reader)
log.Printf("Identity Provider name: %v: %d", name, len(pem))
grpcPrivateKey := grpc_example.PrivateKey{
Name: name,
PrivateKey: pem,
}
_, e = client.SetPrivateKey(context.Background(), &grpcPrivateKey)
if e != nil {
log.Printf("Error when setting private key for identity provider: %v", e)
panic("Could not set private key")
}
} else if strings.HasPrefix(line, ">>> Secrets") {
line = addSecrets(client, reader, signatureName, &signer)
} else if strings.HasPrefix(line, ">>> Properties") {
line = addProperties(client, reader)
} else if strings.HasPrefix(line, ">>> End") {
log.Printf("End")
request.PublicKey = nil
request.Nonce = nonce
request.Signature = nil
request.IsKeyManager = false
e = stream.Send(&request)
if e != nil {
log.Printf("Error when sending final request: %v", e)
panic("Could not send final request")
}
e = stream.CloseSend()
if e != nil {
log.Printf("Error when receiving closing send directions: %v", e)
panic("Could not close send direction")
}
response, e = stream.Recv()
if e != nil {
log.Printf("Error when receiving final response: %v", e)
panic("Could not receive final response")
}
log.Printf("Final response: %v: %v: %v", response.Status.Code, response.Status.Message, response.Nonce)
break
} else {
panic("Expected: >>> { Manager: <name> | Management | Trusted | Identity Provider: <name> | End }: got: [" + line + "]")
}
}
}
func readLine(reader *bufio.Reader) string {
text, e := reader.ReadString('\n')
if e != nil {
m, _ := fmt.Printf("Error reading string from stdin: %v", e)
panic(m)
}
return text
}
func getName(line string) string {
parts := strings.SplitN(line, ":", 2)
if len(parts) < 2 {
return "???"
} else {
return strings.Trim(parts[1], " \t\r\n")
}
}
func readPem(reader *bufio.Reader) (pem string, nextLine string) {
pem = ""
var builder strings.Builder
for true {
nextLine = readLine(reader)
if strings.HasPrefix(nextLine, ">>> ") {
break
}
builder.WriteString(nextLine)
}
pem = builder.String()
return
}
func addPublicKeys(isKeyManager bool, reader *bufio.Reader, signer *ssh.Signer, signatureName string, startNonce []byte, stream grpc_example.GreeterService_ChangeTrustedKeysClient) (line string, nonce []byte) {
nonce = startNonce
for true {
line = readLine(reader)
if strings.HasPrefix(line, ">>> ") {
return
}
parts := strings.Split(strings.Trim(line, "\n"), " ")
if len(parts) < 3 {
log.Printf("Not enough parts: %v", line)
continue
}
log.Printf("Trusted public key: %v: %v", parts[2], parts[1])
publicKey := grpc_example.PublicKey{
Name: parts[2],
PublicKey: parts[1],
}
log.Printf("Public key: %v", publicKey)
signature, e := (*signer).Sign(rand.Reader, nonce)
if e != nil {
log.Printf("Unable to sign nonce: %v", e)
panic("Could not sign nonce")
}
log.Printf("Signature: %v", signature)
grpcSignature := grpc_example.Signature{
Format: signature.Format,
Blob: signature.Blob,
Rest: signature.Rest,
SignatureName: signatureName,
}
request := grpc_example.TrustedKeyRequest{
PublicKey: &publicKey,
Nonce: nonce,
Signature: &grpcSignature,
IsKeyManager: isKeyManager,
}
e = stream.Send(&request)
if e != nil {
log.Printf("Error when sending add key request for ChangeTrustedKeys: %v", e)
panic("Could not send first request")
}
response, e := stream.Recv()
if e != nil {
log.Printf("Error when receiving response for ChangeTrustedKeys: %v", e)
panic("Could not receive first response")
}
log.Printf("Response: %v", response)
nonce = response.Nonce
}
return "", nonce
}
func addSecrets(client grpc_example.GreeterServiceClient, reader *bufio.Reader, signatureName string, signer *ssh.Signer) (line string) {
log.Printf("Add secrets: client: %v", client)
stream, e := client.ChangeCredentials(context.Background())
if e != nil {
log.Printf("Add secrets: Error when opening stream: %v", e)
}
log.Printf("Add secrets: stream: %v", stream)
defer utils.ReportError("Close connection to application", func() error { _, e := stream.CloseAndRecv(); return e })
for true {
line = readLine(reader)
if strings.HasPrefix(line, ">>>") {
break
}
log.Printf("Add secret: %v", line)
parts := strings.SplitN(line, "=", 2)
log.Printf("Number of parts: %v", len(parts))
signature, e := (*signer).Sign(rand.Reader, []byte(line))
if e != nil {
log.Printf("Add secrets: Unable to sign nonce: %v", e)
panic("Could not sign nonce")
}
log.Printf("Add secrets: Signature: %v", signature)
grpcSignature := grpc_example.Signature{
Format: signature.Format,
Blob: signature.Blob,
Rest: signature.Rest,
SignatureName: signatureName,
}
credentials := grpc_example.Credentials{
Identifier: parts[0],
Secret: parts[1],
Signature: &grpcSignature,
}
log.Printf("Add secret: Credentials: %v", credentials)
e = stream.Send(&credentials)
if e != nil {
log.Printf("Add secrets: Unable to send credentials: %v", e)
panic("Could not send credentials")
}
}
emptyCredentials := grpc_example.Credentials{
Identifier: "",
Secret: "",
Signature: nil,
}
e = stream.Send(&emptyCredentials)
if e != nil {
log.Printf("Error while sending empty credentials: %v", e)
}
return
}
func addProperties(client grpc_example.GreeterServiceClient, reader *bufio.Reader) (line string) {
log.Printf("Add properties: client: %v", client)
for true {
line = readLine(reader)
if strings.HasPrefix(line, ">>>") {
break
}
log.Printf("Add property: %v", line)
parts := strings.SplitN(line, "=", 2)
log.Printf("Number of parts: %v", len(parts))
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
keyValue := grpc_example.KeyValue{
Key: key,
Value: value,
}
log.Printf("Add property: KeyValue: %v", keyValue)
_, e := client.SetProperty(context.Background(), &keyValue)
if e != nil {
log.Printf("Add properties: Unable to set property: %v: %v", key, e)
panic("Could not set property")
}
}
return
}