diff --git a/configurations/beelzebub.yaml b/configurations/beelzebub.yaml index 9bfe9eb..8c18987 100644 --- a/configurations/beelzebub.yaml +++ b/configurations/beelzebub.yaml @@ -3,4 +3,7 @@ core: debug: false debugReportCaller: false logDisableTimestamp: true - logsPath: ./logs \ No newline at end of file + logsPath: ./logs + tracing: + rabbitMQEnabled: true + rabbitMQURI: "amqp://guest:guest@localhost:5672/" diff --git a/docker-compose.yml b/docker-compose.yml index 85ea998..2ab1a7b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,9 +3,9 @@ version: "3.9" services: beelzebub: build: . - #network_mode: host #Not work on Mac OS + #network_mode: host # Not work on Mac OS container_name: beelzebub ports: # Remove me, if use configuration network_mode: host - "22:22" - "8080:8080" - - "80:80" + - "80:80" \ No newline at end of file diff --git a/go.mod b/go.mod index b9bdd6b..eca494b 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,9 @@ go 1.16 require ( github.com/gliderlabs/ssh v0.3.3 // indirect github.com/google/uuid v1.3.0 // indirect + github.com/rabbitmq/amqp091-go v1.3.4 // indirect github.com/sirupsen/logrus v1.8.1 - github.com/stretchr/testify v1.2.2 // indirect + github.com/stretchr/testify v1.6.1 // indirect golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 8473d44..0fb22b1 100644 --- a/go.sum +++ b/go.sum @@ -1,30 +1,89 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gliderlabs/ssh v0.3.3 h1:mBQ8NiOgDkINJrZtoizkC3nDNYgSaWtxyem6S2XHBtA= github.com/gliderlabs/ssh v0.3.3/go.mod h1:ZSS+CUoKHDrqVakTfTWUlKSr9MtMFkC4UvtQKD7O914= +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rabbitmq/amqp091-go v1.3.4 h1:tXuIslN1nhDqs2t6Jrz3BAoqvt4qIZzxvdbdcxWtHYU= +github.com/rabbitmq/amqp091-go v1.3.4/go.mod h1:ogQDLSOACsLPsIq0NpbtiifNZi2YOz0VTJ0kHRghqbM= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo= +github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.0.2 h1:akYIkZ28e6A96dkWNJQu3nmCzH3YfwMPQExUYDaRv7w= +github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= +github.com/xdg-go/stringprep v1.0.2 h1:6iq84/ryjjeRmMJwxutI51F2GIPlP5BfTvXHeYjyhBc= +github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c= +go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 h1:RqytpXGR1iVNX7psjB3ff8y7sNFinVFvkx1c8SjBkio= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d h1:bt+R27hbE7uVf7PY9S6wpNg9Xo2WRe/XQT0uGq9RQQw= +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 9b3e09d..0760488 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,9 @@ import ( "beelzebub/parser" "beelzebub/protocols" "beelzebub/tracer" + "encoding/json" + "fmt" + amqp "github.com/rabbitmq/amqp091-go" log "github.com/sirupsen/logrus" "io" "os" @@ -11,20 +14,28 @@ import ( var quit = make(chan struct{}) +var channel *amqp.Channel + func main() { parser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/") coreConfigurations, err := parser.ReadConfigurationsCore() - if err != nil { - log.Fatal(err) - } + failOnError(err, fmt.Sprintf("Error during coreConfigurations: ")) fileLogs := configureLoggingByConfigurations(coreConfigurations.Core.Logging) defer fileLogs.Close() beelzebubServicesConfiguration, err := parser.ReadConfigurationsServices() - if err != nil { - log.Fatal(err) + failOnError(err, fmt.Sprintf("Error during ReadConfigurationsServices: ")) + + if coreConfigurations.Core.Tracing.RabbitMQEnabled { + conn, err := amqp.Dial(coreConfigurations.Core.Tracing.RabbitMQURI) + failOnError(err, "Failed to connect to RabbitMQ") + defer conn.Close() + + channel, err = conn.Channel() + failOnError(err, "Failed to open a channel") + defer channel.Close() } // Init Protocol strategies @@ -48,17 +59,48 @@ func main() { } err := protocolManager.InitService(beelzebubServiceConfiguration) - if err != nil { - log.Errorf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error()) - } + failOnError(err, fmt.Sprintf("Error during init protocol: %s, ", beelzebubServiceConfiguration.Protocol)) } <-quit } + +func failOnError(err error, msg string) { + if err != nil { + log.Fatalf("%s: %s", msg, err) + } +} + func traceStrategyStdout(event tracer.Event) { log.WithFields(log.Fields{ - "status": event.Status.String(), + "status": event.Status, "event": event, }).Info("New Event") + + if channel != nil { + eventJSON, err := json.Marshal(event) + failOnError(err, "Failed to Marshal Event") + + queue, err := channel.QueueDeclare( + "event", + false, + false, + false, + false, + nil, + ) + failOnError(err, "Failed to declare a queue") + + err = channel.Publish( + "", + queue.Name, + false, + false, + amqp.Publishing{ + ContentType: "application/json", + Body: eventJSON, + }) + failOnError(err, "Failed to publish a message") + } } func configureLoggingByConfigurations(configurations parser.Logging) *os.File { diff --git a/parser/configurationsParser.go b/parser/configurationsParser.go index ff59d1a..55024b7 100644 --- a/parser/configurationsParser.go +++ b/parser/configurationsParser.go @@ -12,6 +12,7 @@ import ( type BeelzebubCoreConfigurations struct { Core struct { Logging Logging `yaml:"logging"` + Tracing Tracing `yaml:"tracing"` } } @@ -22,6 +23,11 @@ type Logging struct { LogsPath string `yaml:"logsPath,omitempty"` } +type Tracing struct { + RabbitMQEnabled bool `yaml:"rabbitMQEnabled,omitempty"` + RabbitMQURI string `yaml:"rabbitMQURI,omitempty"` +} + type BeelzebubServiceConfiguration struct { ApiVersion string `yaml:"apiVersion"` Protocol string `yaml:"protocol"` diff --git a/parser/configurationsParser_test.go b/parser/configurationsParser_test.go index 9557a27..25d4d82 100644 --- a/parser/configurationsParser_test.go +++ b/parser/configurationsParser_test.go @@ -13,7 +13,10 @@ core: debug: false debugReportCaller: false logDisableTimestamp: true - logsPath: ./logs`) + logsPath: ./logs + tracing: + rabbitMQEnabled: true + rabbitMQURI: provaMock`) return configurationsCoreBytes, nil } @@ -77,6 +80,8 @@ func TestReadConfigurationsCoreValid(t *testing.T) { assert.Equal(t, coreConfigurations.Core.Logging.LogDisableTimestamp, true) assert.Equal(t, coreConfigurations.Core.Logging.DebugReportCaller, false) assert.Equal(t, coreConfigurations.Core.Logging.LogsPath, "./logs") + assert.Equal(t, coreConfigurations.Core.Tracing.RabbitMQEnabled, true) + assert.Equal(t, coreConfigurations.Core.Tracing.RabbitMQURI, "provaMock") } func TestReadConfigurationsServicesFail(t *testing.T) { diff --git a/protocols/hypertextTransferProtocolStrategy.go b/protocols/hypertextTransferProtocolStrategy.go index 12aa2f3..2faa787 100644 --- a/protocols/hypertextTransferProtocolStrategy.go +++ b/protocols/hypertextTransferProtocolStrategy.go @@ -60,14 +60,14 @@ func traceRequest(request *http.Request, tr tracer.Tracer) { tr.TraceEvent(tracer.Event{ Msg: "HTTP New request", RequestURI: request.RequestURI, - Protocol: tracer.HTTP, + Protocol: tracer.HTTP.String(), HTTPMethod: request.Method, Body: body, HostHTTPRequest: request.Host, UserAgent: request.UserAgent(), Cookies: request.Cookies(), Headers: request.Header, - Status: tracer.Stateless, + Status: tracer.Stateless.String(), RemoteAddr: request.RemoteAddr, ID: uuid.New().String(), }) diff --git a/protocols/secureShellStrategy.go b/protocols/secureShellStrategy.go index d7117ab..21d50ef 100644 --- a/protocols/secureShellStrategy.go +++ b/protocols/secureShellStrategy.go @@ -28,9 +28,9 @@ func (SSHStrategy *SecureShellStrategy) Init(beelzebubServiceConfiguration parse tr.TraceEvent(tracer.Event{ Msg: "New SSH Session", - Protocol: tracer.SSH, + Protocol: tracer.SSH.String(), RemoteAddr: sess.RemoteAddr().String(), - Status: tracer.Start, + Status: tracer.Start.String(), ID: uuidSession.String(), Environ: strings.Join(sess.Environ(), ","), User: sess.User(), @@ -45,9 +45,10 @@ func (SSHStrategy *SecureShellStrategy) Init(beelzebubServiceConfiguration parse tr.TraceEvent(tracer.Event{ Msg: "New SSH Command", RemoteAddr: sess.RemoteAddr().String(), - Status: tracer.Interaction, + Status: tracer.Interaction.String(), Command: commandInput, ID: uuidSession.String(), + Protocol: tracer.SSH.String(), }) if commandInput == "exit" { break @@ -67,14 +68,15 @@ func (SSHStrategy *SecureShellStrategy) Init(beelzebubServiceConfiguration parse } tr.TraceEvent(tracer.Event{ Msg: "End SSH Session", - Status: tracer.End, + Status: tracer.End.String(), ID: uuidSession.String(), }) }, PasswordHandler: func(ctx ssh.Context, password string) bool { tr.TraceEvent(tracer.Event{ Msg: "New SSH attempt", - Status: tracer.Stateless, + Protocol: tracer.SSH.String(), + Status: tracer.Stateless.String(), User: ctx.User(), Password: password, Client: ctx.ClientVersion(), diff --git a/tracer/tracer.go b/tracer/tracer.go index 9978c55..88c5f26 100644 --- a/tracer/tracer.go +++ b/tracer/tracer.go @@ -2,6 +2,7 @@ package tracer import ( "net/http" + "time" ) type Strategy func(event Event) @@ -17,14 +18,16 @@ func Init(strategy Strategy) *Tracer { } func (tracer *Tracer) TraceEvent(event Event) { + event.DateTime = time.Now().UTC().String() tracer.strategy(event) } type Event struct { + DateTime string RemoteAddr string - Protocol Protocol + Protocol string Command string - Status Status + Status string Msg string ID string Environ string @@ -47,6 +50,10 @@ const ( SSH ) +func (status Protocol) String() string { + return [...]string{"HTTP", "SSH"}[status] +} + type Status int const ( diff --git a/tracer/tracer_test.go b/tracer/tracer_test.go index 1309c1a..7ab498a 100644 --- a/tracer/tracer_test.go +++ b/tracer/tracer_test.go @@ -24,14 +24,14 @@ func TestTraceEvent(t *testing.T) { tracer.TraceEvent(Event{ ID: "mockID", - Protocol: HTTP, - Status: Stateless, + Protocol: HTTP.String(), + Status: Stateless.String(), }) assert.NotNil(t, eventCalled.ID) assert.Equal(t, eventCalled.ID, "mockID") - assert.Equal(t, eventCalled.Protocol, HTTP) - assert.Equal(t, eventCalled.Status, Stateless) + assert.Equal(t, eventCalled.Protocol, HTTP.String()) + assert.Equal(t, eventCalled.Status, Stateless.String()) } func TestStringStatus(t *testing.T) {