/
protocol.go
326 lines (267 loc) · 11.3 KB
/
protocol.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package choria
import (
"fmt"
"github.com/choria-io/go-choria/protocol"
v1 "github.com/choria-io/go-choria/protocol/v1"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
// NewMessage creates a new Message associated with this Choria instance
func (fw *Framework) NewMessage(payload string, agent string, collective string, msgType string, request *Message) (msg *Message, err error) {
return NewMessage(payload, agent, collective, msgType, request, fw)
}
// NewRequestMessageFromTransportJSON creates a Message from a Transport JSON that holds a Request
func (fw *Framework) NewRequestMessageFromTransportJSON(payload []byte) (msg *Message, err error) {
transport, err := fw.NewTransportFromJSON(string(payload))
if err != nil {
return nil, err
}
srequest, err := fw.NewSecureRequestFromTransport(transport, false)
if err != nil {
return nil, err
}
request, err := fw.NewRequestFromSecureRequest(srequest)
if err != nil {
return nil, err
}
protocol.CopyFederationData(transport, request)
msg, err = NewMessageFromRequest(request, transport.ReplyTo(), fw)
if err != nil {
return nil, err
}
return msg, nil
}
// NewReplyFromTransportJSON creates a new Reply from a transport JSON
func (fw *Framework) NewReplyFromTransportJSON(payload []byte, skipvalidate bool) (msg protocol.Reply, err error) {
transport, err := fw.NewTransportFromJSON(string(payload))
if err != nil {
return nil, err
}
sreply, err := fw.NewSecureReplyFromTransport(transport, skipvalidate)
if err != nil {
return nil, err
}
reply, err := fw.NewReplyFromSecureReply(sreply)
if err != nil {
return nil, err
}
protocol.CopyFederationData(transport, reply)
return reply, nil
}
// NewRequestFromTransportJSON creates a new Request from transport JSON
func (fw *Framework) NewRequestFromTransportJSON(payload []byte, skipvalidate bool) (msg protocol.Request, err error) {
transport, err := fw.NewTransportFromJSON(string(payload))
if err != nil {
return nil, err
}
sreq, err := fw.NewSecureRequestFromTransport(transport, skipvalidate)
if err != nil {
return nil, err
}
req, err := fw.NewRequestFromSecureRequest(sreq)
if err != nil {
return nil, err
}
protocol.CopyFederationData(transport, req)
return req, nil
}
// NewRequest creates a new Request complying with a specific protocol version like protocol.RequestV1
func (fw *Framework) NewRequest(version string, agent string, senderid string, callerid string, ttl int, requestid string, collective string) (request protocol.Request, err error) {
switch version {
case protocol.RequestV1:
request, err = v1.NewRequest(agent, senderid, callerid, ttl, requestid, collective)
default:
err = fmt.Errorf("do not know how to create a Request version %s", version)
}
return request, err
}
// NewRequestFromMessage creates a new Request with the Message settings preloaded complying with a specific protocol version like protocol.RequestV1
func (fw *Framework) NewRequestFromMessage(version string, msg *Message) (req protocol.Request, err error) {
if !(msg.Type() == "request" || msg.Type() == "direct_request") {
err = fmt.Errorf("cannot use `%s` message to construct a Request", msg.Type())
return nil, err
}
req, err = fw.NewRequest(version, msg.Agent, msg.SenderID, msg.CallerID, msg.TTL, msg.RequestID, msg.Collective())
if err != nil {
return nil, fmt.Errorf("could not create a Request from a Message: %s", err)
}
req.SetMessage(msg.Payload)
if msg.Filter == nil || msg.Filter.Empty() {
req.NewFilter()
} else {
req.SetFilter(msg.Filter)
}
return req, nil
}
// NewReply creates a new Reply, the version will match that of the given request
func (fw *Framework) NewReply(request protocol.Request) (reply protocol.Reply, err error) {
switch request.Version() {
case protocol.RequestV1:
return v1.NewReply(request, fw.Config.Identity)
default:
return nil, fmt.Errorf("do not know how to create a Reply version %s", request.Version())
}
}
// NewReplyFromMessage creates a new Reply with the Message settings preloaded complying with a specific protocol version like protocol.ReplyV1
func (fw *Framework) NewReplyFromMessage(version string, msg *Message) (rep protocol.Reply, err error) {
if msg.Type() != "reply" {
return nil, fmt.Errorf("cannot use '%s' message to construct a Reply", msg.Type())
}
if msg.Request == nil {
return nil, fmt.Errorf("cannot create a Reply from Messages without Requests")
}
req, err := fw.NewRequestFromMessage(version, msg.Request)
if err != nil {
return nil, err
}
rep, err = fw.NewReply(req)
rep.SetMessage(msg.Payload)
return rep, err
}
// NewReplyFromSecureReply creates a new Reply from the JSON payload of SecureReply, the version will match what is in the JSON payload
func (fw *Framework) NewReplyFromSecureReply(sr protocol.SecureReply) (reply protocol.Reply, err error) {
switch sr.Version() {
case protocol.SecureReplyV1:
return v1.NewReplyFromSecureReply(sr)
default:
return nil, fmt.Errorf("do not know how to create a Reply version %s", sr.Version())
}
}
// NewRequestFromSecureRequest creates a new Request from a SecureRequest, the version will match what is in the JSON payload
func (fw *Framework) NewRequestFromSecureRequest(sr protocol.SecureRequest) (request protocol.Request, err error) {
switch sr.Version() {
case protocol.SecureRequestV1:
return v1.NewRequestFromSecureRequest(sr)
default:
return nil, fmt.Errorf("do not know how to create a Reply version %s", sr.Version())
}
}
// NewSecureReply creates a new SecureReply with the given Reply message as payload
func (fw *Framework) NewSecureReply(reply protocol.Reply) (secure protocol.SecureReply, err error) {
switch reply.Version() {
case protocol.ReplyV1:
return v1.NewSecureReply(reply, fw.security)
default:
return nil, fmt.Errorf("do not know how to create a SecureReply based on a Reply version %s", reply.Version())
}
}
// NewSecureReplyFromTransport creates a new SecureReply from the JSON payload of TransportMessage, the version SecureReply will be the same as the TransportMessage
func (fw *Framework) NewSecureReplyFromTransport(message protocol.TransportMessage, skipvalidate bool) (secure protocol.SecureReply, err error) {
switch message.Version() {
case protocol.TransportV1:
return v1.NewSecureReplyFromTransport(message, fw.security, skipvalidate)
default:
return nil, fmt.Errorf("do not know how to create a SecureReply version %s", message.Version())
}
}
// NewSecureRequest creates a new SecureRequest with the given Request message as payload
func (fw *Framework) NewSecureRequest(request protocol.Request) (secure protocol.SecureRequest, err error) {
switch request.Version() {
case protocol.RequestV1:
if fw.Config.Choria.RemoteSignerURL == "" {
return v1.NewSecureRequest(request, fw.security)
} else {
fw.log.Info("Signing request using remote signer")
return v1.NewRemoteSignedSecureRequest(request, fw.security)
}
default:
return nil, fmt.Errorf("do not know how to create a SecureReply from a Request with version %s", request.Version())
}
}
// NewSecureRequestFromTransport creates a new SecureRequest from the JSON payload of TransportMessage, the version SecureRequest will be the same as the TransportMessage
func (fw *Framework) NewSecureRequestFromTransport(message protocol.TransportMessage, skipvalidate bool) (secure protocol.SecureRequest, err error) {
switch message.Version() {
case protocol.TransportV1:
return v1.NewSecureRequestFromTransport(message, fw.security, skipvalidate)
default:
return nil, fmt.Errorf("do not know how to create a SecureReply from a TransportMessage version %s", message.Version())
}
}
// NewTransportForSecureRequest creates a new TransportMessage with a SecureRequest as payload. The Transport will be the same version as the SecureRequest
func (fw *Framework) NewTransportForSecureRequest(request protocol.SecureRequest) (message protocol.TransportMessage, err error) {
switch request.Version() {
case protocol.SecureRequestV1:
message, err = v1.NewTransportMessage(fw.Config.Identity)
if err != nil {
logrus.Errorf("Failed to create transport from secure request: %s", err)
return nil, err
}
err = message.SetRequestData(request)
if err != nil {
logrus.Errorf("Failed to create transport from secure request: %s", err)
return nil, err
}
return message, nil
default:
return nil, fmt.Errorf("co not know how to create a Transport message for SecureRequest version %s", request.Version())
}
}
// NewTransportForSecureReply creates a new TransportMessage with a SecureReply as payload. The Transport will be the same version as the SecureRequest
func (fw *Framework) NewTransportForSecureReply(reply protocol.SecureReply) (message protocol.TransportMessage, err error) {
switch reply.Version() {
case protocol.SecureReplyV1:
message, err = v1.NewTransportMessage(fw.Config.Identity)
if err != nil {
return nil, err
}
message.SetReplyData(reply)
return message, nil
default:
return nil, fmt.Errorf("do not know how to create a Transport message for SecureRequest version %s", reply.Version())
}
}
// NewReplyTransportForMessage creates a new Transport message based on a Message and the request its a reply to
//
// The new transport message will have the same version as the request its based on
func (fw *Framework) NewReplyTransportForMessage(msg *Message, request protocol.Request) (protocol.TransportMessage, error) {
reply, err := fw.NewReply(request)
if err != nil {
return nil, fmt.Errorf("could not create Reply: %s", err)
}
reply.SetMessage(msg.Payload)
sreply, err := fw.NewSecureReply(reply)
if err != nil {
return nil, fmt.Errorf("could not create Secure Reply: %s", err)
}
transport, err := fw.NewTransportForSecureReply(sreply)
if err != nil {
return nil, fmt.Errorf("could not create Transport: %s", err)
}
protocol.CopyFederationData(request, transport)
return transport, nil
}
// NewRequestTransportForMessage creates a new versioned Transport message based on a Message
func (fw *Framework) NewRequestTransportForMessage(msg *Message, version string) (protocol.TransportMessage, error) {
req, err := fw.NewRequestFromMessage(version, msg)
if err != nil {
return nil, fmt.Errorf("could not create Request: %s", err)
}
sr, err := fw.NewSecureRequest(req)
if err != nil {
return nil, err
}
transport, err := fw.NewTransportForSecureRequest(sr)
if err != nil {
return nil, fmt.Errorf("could not create Transport: %s", err)
}
return transport, nil
}
// NewTransportMessage creates a new TransportMessage complying with a specific protocol version like protocol.TransportV1
func (fw *Framework) NewTransportMessage(version string) (message protocol.TransportMessage, err error) {
switch version {
case protocol.TransportV1:
return v1.NewTransportMessage(fw.Config.Identity)
default:
return nil, fmt.Errorf("so not know how to create a Transport version '%s'", version)
}
}
// NewTransportFromJSON creates a new TransportMessage from a JSON payload. The version will match what is in the payload
func (fw *Framework) NewTransportFromJSON(data string) (message protocol.TransportMessage, err error) {
version := gjson.Get(data, "protocol").String()
switch version {
case protocol.TransportV1:
return v1.NewTransportFromJSON(data)
default:
return nil, fmt.Errorf("do not know how to create a TransportMessage from an expected JSON format message with content: %s", data)
}
}