| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package auditorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/ |
| 18 | |
| 19 | import ( |
| 20 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 21 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/" |
| 22 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/" |
| 24 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 25 | "timescip-go gomod github.com/golang/go/src go1.18 time/" |
| 26 | |
| 27 | "github.com/sassoftware/relic/v7/lib/auditscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/audit`/" |
| 28 | ) |
| 29 | |
| 30 | func logGraylogscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/logGraylog().(info *auditscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/audit`/.Infoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/audit`/Info#, rowid int64) error { |
| 31 | if auditConfigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/auditConfig..GraylogURLscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/AuditConfig#GraylogURL. == "" { |
| 32 | return nil |
| 33 | } |
| 34 | msg := map[string]interface{}{ |
| 35 | "version": "1.1", |
| 36 | "host": info.Attributesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/audit`/Info#Attributes.["sig.hostname"], |
| 37 | "short_message": fmtRowscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/fmtRow().(info, rowid), |
| 38 | "level": 6, // INFO |
| 39 | } |
| 40 | if timestamp, err := timescip-go gomod github.com/golang/go/src go1.18 time/.Parsescip-go gomod github.com/golang/go/src go1.18 time/Parse().(timescip-go gomod github.com/golang/go/src go1.18 time/.RFC3339Nanoscip-go gomod github.com/golang/go/src go1.18 time/RFC3339Nano., info.Attributesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/audit`/Info#Attributes.["sig.timestamp"].(string)); err == nil { |
| 41 | msg["timestamp"] = timestamp.Unixscip-go gomod github.com/golang/go/src go1.18 time/Time#Unix().() |
| 42 | } |
| 43 | for k, v := range info.Attributesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/audit`/Info#Attributes. { |
| 44 | if v == nil { |
| 45 | continue |
| 46 | } |
| 47 | // graylog quietly changes dots to underscores, but only after running |
| 48 | // stream filters. that gets confusing real quickly so change it to |
| 49 | // underscore now. |
| 50 | k = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.18 strings/ReplaceAll().(k, ".", "_") |
| 51 | msg["_"+k] = v |
| 52 | } |
| 53 | blob, err := jsonscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.18 `encoding/json`/Marshal().(msg) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | resp, err := httpscip-go gomod github.com/golang/go/src go1.18 `net/http`/.Postscip-go gomod github.com/golang/go/src go1.18 `net/http`/Post().(auditConfigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/auditConfig..GraylogURLscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/cmdline/auditor`/AuditConfig#GraylogURL., "application/json", bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.18 bytes/NewReader().(blob)) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } else if resp.StatusCodescip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#StatusCode. >= 300 { |
| 61 | return fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("posting to graylog: %s", resp.Statusscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Status.) |
| 62 | } |
| 63 | resp.Bodyscip-go gomod github.com/golang/go/src go1.18 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.18 io/Closer#Close().() |
| 64 | return nil |
| 65 | } |