-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
293 lines (266 loc) · 8.47 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
// Copyright (C) 2021 Nicola Murino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"gocloud.dev/pubsub"
_ "gocloud.dev/pubsub/awssnssqs"
_ "gocloud.dev/pubsub/azuresb"
_ "gocloud.dev/pubsub/gcppubsub"
_ "gocloud.dev/pubsub/kafkapubsub"
_ "gocloud.dev/pubsub/natspubsub"
_ "gocloud.dev/pubsub/rabbitpubsub"
"github.com/sftpgo/sdk/plugin/notifier"
)
const version = "1.0.13"
var (
commitHash = ""
date = ""
)
var appLogger = hclog.New(&hclog.LoggerOptions{
DisableTime: true,
Level: hclog.Debug,
})
type fsEvent struct {
Timestamp string `json:"timestamp"`
Action string `json:"action"`
Username string `json:"username"`
FsPath string `json:"fs_path"`
FsTargetPath string `json:"fs_target_path,omitempty"`
VirtualPath string `json:"virtual_path"`
VirtualTargetPath string `json:"virtual_target_path,omitempty"`
SSHCmd string `json:"ssh_cmd,omitempty"`
FileSize int64 `json:"file_size,omitempty"`
Elapsed int64 `json:"elapsed,omitempty"`
Status int `json:"status"`
Protocol string `json:"protocol"`
IP string `json:"ip"`
SessionID string `json:"session_id"`
FsProvider int `json:"fs_provider"`
Bucket string `json:"bucket,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
OpenFlags int `json:"open_flags,omitempty"`
Role string `json:"role,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
}
type providerEvent struct {
Timestamp string `json:"timestamp"`
Action string `json:"action"`
Username string `json:"username"`
IP string `json:"ip"`
ObjectType string `json:"object_type"`
ObjectName string `json:"object_name"`
ObjectData []byte `json:"object_data"`
Role string `json:"role,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
}
type LogEvent struct {
Timestamp string `json:"timestamp"`
Event int `json:"event"`
Protocol string `json:"protocol,omitempty"`
Username string `json:"username,omitempty"`
IP string `json:"ip,omitempty"`
Message string `json:"message,omitempty"`
Role string `json:"role,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
}
type pubSubNotifier struct {
topic *pubsub.Topic
timeout time.Duration
instanceID string
}
func (n *pubSubNotifier) NotifyFsEvent(event *notifier.FsEvent) error {
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(n.timeout))
defer cancelFn()
ev := fsEvent{
Timestamp: getTimeFromNsecSinceEpoch(event.Timestamp).UTC().Format(time.RFC3339Nano),
Action: event.Action,
Username: event.Username,
FsPath: event.Path,
FsTargetPath: event.TargetPath,
VirtualPath: event.VirtualPath,
VirtualTargetPath: event.VirtualTargetPath,
Protocol: event.Protocol,
IP: event.IP,
SessionID: event.SessionID,
FileSize: event.FileSize,
Elapsed: event.Elapsed,
Status: event.Status,
FsProvider: event.FsProvider,
Bucket: event.Bucket,
Endpoint: event.Endpoint,
OpenFlags: event.OpenFlags,
Role: event.Role,
Metadata: event.Metadata,
InstanceID: n.instanceID,
}
msg, err := json.Marshal(ev)
if err != nil {
appLogger.Warn("unable to marshal fs event", "error", err)
return err
}
err = n.topic.Send(ctx, &pubsub.Message{
Body: msg,
Metadata: map[string]string{
"action": event.Action,
},
})
if err != nil {
appLogger.Warn("unable to publish fs event to topic", "action", event.Action, "username",
event.Username, "virtual path", event.VirtualPath, "error", err)
panic(err)
}
return nil
}
func (n *pubSubNotifier) NotifyProviderEvent(event *notifier.ProviderEvent) error {
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(n.timeout))
defer cancelFn()
ev := providerEvent{
Timestamp: getTimeFromNsecSinceEpoch(event.Timestamp).UTC().Format(time.RFC3339Nano),
Action: event.Action,
Username: event.Username,
IP: event.IP,
ObjectType: event.ObjectType,
ObjectName: event.ObjectName,
ObjectData: event.ObjectData,
Role: event.Role,
InstanceID: n.instanceID,
}
msg, err := json.Marshal(ev)
if err != nil {
appLogger.Warn("unable to marshal provider event", "error", err)
return err
}
err = n.topic.Send(ctx, &pubsub.Message{
Body: msg,
Metadata: map[string]string{
"action": event.Action,
"object_type": event.ObjectType,
},
})
if err != nil {
appLogger.Warn("unable to publish provider event to topic", "action", event.Action, "error", err)
panic(err)
}
return nil
}
func (n *pubSubNotifier) NotifyLogEvent(event *notifier.LogEvent) error {
ctx, cancelFn := context.WithDeadline(context.Background(), time.Now().Add(n.timeout))
defer cancelFn()
ev := LogEvent{
Timestamp: getTimeFromNsecSinceEpoch(event.Timestamp).UTC().Format(time.RFC3339Nano),
Event: int(event.Event),
Protocol: event.Protocol,
Username: event.Username,
IP: event.IP,
Message: event.Message,
Role: event.Role,
InstanceID: n.instanceID,
}
msg, err := json.Marshal(ev)
if err != nil {
appLogger.Warn("unable to marshal log event", "error", err)
return err
}
err = n.topic.Send(ctx, &pubsub.Message{
Body: msg,
Metadata: map[string]string{
"action": "log",
"event": strconv.Itoa(int(event.Event)),
},
})
if err != nil {
appLogger.Warn("unable to publish log event to topic", "event", getLogEventString(event.Event), "error", err)
panic(err)
}
return nil
}
func getTimeFromNsecSinceEpoch(nsec int64) time.Time {
return time.Unix(0, nsec)
}
func getLogEventString(event notifier.LogEventType) string {
switch event {
case notifier.LogEventTypeLoginFailed:
return "Login failed"
case notifier.LogEventTypeLoginNoUser:
return "Login with non-existent user"
case notifier.LogEventTypeNoLoginTried:
return "No login tried"
case notifier.LogEventTypeNotNegotiated:
return "Algorithm negotiation failed"
default:
return fmt.Sprintf("unknown type: %d", event)
}
}
func getVersionString() string {
var sb strings.Builder
sb.WriteString(version)
if commitHash != "" {
sb.WriteString("-")
sb.WriteString(commitHash)
}
if date != "" {
sb.WriteString("-")
sb.WriteString(date)
}
return sb.String()
}
func main() {
if len(os.Args) < 2 {
appLogger.Error("please specify the topic url as command line argument")
os.Exit(1)
}
var instanceID string
topicUrl := os.Args[1]
if len(os.Args) > 2 {
instanceID = os.Args[2]
}
appLogger.Info("starting sftpgo-plugin-pubsub", "version", getVersionString(), "topic", topicUrl,
"instance id", instanceID)
ctx, cancelFn := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFn()
topic, err := pubsub.OpenTopic(ctx, topicUrl)
if err != nil {
appLogger.Error("unable to open topic", "error", err)
os.Exit(1)
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
err := topic.Shutdown(shutdownCtx)
if err != nil {
appLogger.Error("unable to close topic", "error", err)
}
cancel()
}()
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: notifier.Handshake,
Plugins: map[string]plugin.Plugin{
notifier.PluginName: ¬ifier.Plugin{Impl: &pubSubNotifier{
topic: topic,
timeout: 30 * time.Second,
instanceID: instanceID,
}},
},
GRPCServer: plugin.DefaultGRPCServer,
})
}