-
Notifications
You must be signed in to change notification settings - Fork 14
/
actions.go
165 lines (130 loc) · 4.15 KB
/
actions.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
// This file contains the implementation of the controller actions.
//
// Documentation Last Review: 07.10.2020
//
package controller
import (
"crypto/x509"
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"strings"
"go.dedis.ch/dela/cli/node"
"go.dedis.ch/dela/mino"
"go.dedis.ch/dela/mino/minogrpc"
"go.dedis.ch/dela/mino/minogrpc/certs"
"golang.org/x/xerrors"
)
// CertAction is an action to list the certificates known by the server.
//
// - implements node.ActionTemplate
type certAction struct{}
// Execute implements node.ActionTemplate. It prints the list of certificates
// known by the server with the address associated and the expiration date.
func (a certAction) Execute(req node.Context) error {
var m minogrpc.Joinable
err := req.Injector.Resolve(&m)
if err != nil {
return xerrors.Errorf("couldn't resolve: %v", err)
}
m.GetCertificateStore().Range(func(addr mino.Address, chain certs.CertChain) bool {
buff, _ := addr.MarshalText()
addrB64 := base64.StdEncoding.EncodeToString(buff)
certs, err := x509.ParseCertificates(chain)
if err != nil {
return false
}
certStr := make([]string, len(certs))
for i, c := range certs {
certStr[i] = hex.EncodeToString(c.Raw[:8]) + "..."
}
fmt.Fprintf(req.Out, "Address: %v (%s) Certificate: %s\n", addr, addrB64, strings.Join(certStr, "<-"))
return true
})
return nil
}
// RemoveAction is an action to remove certificates associated to an address
// from the server.
//
// - implements node.ActionTemplate
type removeAction struct{}
// Execute implements node.ActionTemplate. It removes a certificate based on an
// address.
func (a removeAction) Execute(req node.Context) error {
var m minogrpc.Joinable
err := req.Injector.Resolve(&m)
if err != nil {
return xerrors.Errorf("couldn't resolve: %v", err)
}
addrBuf, err := base64.StdEncoding.DecodeString(req.Flags.String("address"))
if err != nil {
return xerrors.Errorf("failed to decode base64 address: %v", err)
}
addr := m.GetAddressFactory().FromText(addrBuf)
err = m.GetCertificateStore().Delete(addr)
if err != nil {
return xerrors.Errorf("failed to delete: %v", err)
}
fmt.Fprintf(req.Out, "certificate(s) with address %q removed", addrBuf)
return nil
}
// TokenAction is an action to generate a token that will be valid for another
// server to join the network of participants.
//
// - implements node.ActionTemplate
type tokenAction struct{}
// Execute implements node.ActionTemplate. It generates a token that will be
// valid for the amount of time given in the request.
// If this node serves TLS itself, a hash of the certificate will be
// printed, too.
func (a tokenAction) Execute(req node.Context) error {
exp := req.Flags.Duration("expiration")
var m minogrpc.Joinable
err := req.Injector.Resolve(&m)
if err != nil {
return xerrors.Errorf("couldn't resolve: %v", err)
}
token := m.GenerateToken(exp)
var certHash string
if m.ServeTLS() {
chain := m.GetCertificateChain()
digest, err := m.GetCertificateStore().Hash(chain)
if err != nil {
return xerrors.Errorf("couldn't hash certificate: %v", err)
}
certHash = fmt.Sprintf(" --cert-hash %s", base64.StdEncoding.EncodeToString(digest))
}
fmt.Fprintf(req.Out, "--token %s%s\n",
token, certHash)
return nil
}
// JoinAction is an action to join a network of participants by providing a
// valid token and the certificate hash.
//
// - implements node.ActionTemplate
type joinAction struct{}
// Execute implements node.ActionTemplate. It parses the request and send the
// join request to the distant node.
func (a joinAction) Execute(req node.Context) error {
token := req.Flags.String("token")
certHash := req.Flags.String("cert-hash")
addrURL, err := url.Parse(req.Flags.String("address"))
if err != nil {
return xerrors.Errorf("failed to parse addr: %v", err)
}
var m minogrpc.Joinable
err = req.Injector.Resolve(&m)
if err != nil {
return xerrors.Errorf("couldn't resolve: %v", err)
}
cert, err := base64.StdEncoding.DecodeString(certHash)
if err != nil {
return xerrors.Errorf("couldn't decode digest: %v", err)
}
err = m.Join(addrURL, token, cert)
if err != nil {
return xerrors.Errorf("couldn't join: %v", err)
}
return nil
}