|
| 1 | +package elasticsearch |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/crazy-max/diun/v4/internal/model" |
| 13 | + "github.com/crazy-max/diun/v4/internal/msg" |
| 14 | + "github.com/crazy-max/diun/v4/internal/notif/notifier" |
| 15 | + "github.com/crazy-max/diun/v4/pkg/utl" |
| 16 | + "github.com/pkg/errors" |
| 17 | +) |
| 18 | + |
| 19 | +// Client represents an active elasticsearch notification object |
| 20 | +type Client struct { |
| 21 | + *notifier.Notifier |
| 22 | + cfg *model.NotifElasticsearch |
| 23 | + meta model.Meta |
| 24 | +} |
| 25 | + |
| 26 | +// New creates a new elasticsearch notification instance |
| 27 | +func New(config *model.NotifElasticsearch, meta model.Meta) notifier.Notifier { |
| 28 | + return notifier.Notifier{ |
| 29 | + Handler: &Client{ |
| 30 | + cfg: config, |
| 31 | + meta: meta, |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Name returns notifier's name |
| 37 | +func (c *Client) Name() string { |
| 38 | + return "elasticsearch" |
| 39 | +} |
| 40 | + |
| 41 | +// Send creates and sends an elasticsearch notification with an entry |
| 42 | +func (c *Client) Send(entry model.NotifEntry) error { |
| 43 | + username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile) |
| 44 | + if err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + // Use the same JSON structure as webhook notifier |
| 54 | + message, err := msg.New(msg.Options{ |
| 55 | + Meta: c.meta, |
| 56 | + Entry: entry, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + body, err := message.RenderJSON() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + // Parse the JSON to add the client field |
| 68 | + var doc map[string]any |
| 69 | + if err := json.Unmarshal(body, &doc); err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + // Add the current time |
| 74 | + doc["@timestamp"] = time.Now().Format(time.RFC3339Nano) |
| 75 | + |
| 76 | + // Add the client field from the configuration |
| 77 | + doc["client"] = c.cfg.Client |
| 78 | + |
| 79 | + // Re-marshal the JSON with the client field |
| 80 | + body, err = json.Marshal(doc) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + // Build the Elasticsearch indexing URL |
| 86 | + // This uses the Index API (POST /{index}/_doc) to create a document with an auto-generated _id: |
| 87 | + // https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-create |
| 88 | + url := fmt.Sprintf("%s://%s:%d/%s/_doc", c.cfg.Scheme, c.cfg.Host, c.cfg.Port, c.cfg.Index) |
| 89 | + |
| 90 | + cancelCtx, cancel := context.WithCancelCause(context.Background()) |
| 91 | + timeoutCtx, _ := context.WithTimeoutCause(cancelCtx, *c.cfg.Timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent |
| 92 | + defer func() { cancel(errors.WithStack(context.Canceled)) }() |
| 93 | + |
| 94 | + hc := http.Client{ |
| 95 | + Transport: &http.Transport{ |
| 96 | + TLSClientConfig: &tls.Config{ |
| 97 | + InsecureSkipVerify: c.cfg.InsecureSkipVerify, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + req, err := http.NewRequestWithContext(timeoutCtx, "POST", url, bytes.NewBuffer(body)) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + req.Header.Set("Content-Type", "application/json") |
| 108 | + req.Header.Set("User-Agent", c.meta.UserAgent) |
| 109 | + |
| 110 | + // Add authentication if provided |
| 111 | + if username != "" && password != "" { |
| 112 | + req.SetBasicAuth(username, password) |
| 113 | + } |
| 114 | + |
| 115 | + resp, err := hc.Do(req) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + defer resp.Body.Close() |
| 120 | + |
| 121 | + if resp.StatusCode != http.StatusCreated { |
| 122 | + var errBody struct { |
| 123 | + Status int `json:"status"` |
| 124 | + Error struct { |
| 125 | + Type string `json:"type"` |
| 126 | + Reason string `json:"reason"` |
| 127 | + } `json:"error"` |
| 128 | + } |
| 129 | + if err := json.NewDecoder(resp.Body).Decode(&errBody); err != nil { |
| 130 | + return errors.Wrapf(err, "cannot decode JSON error response for HTTP %d %s status", |
| 131 | + resp.StatusCode, http.StatusText(resp.StatusCode)) |
| 132 | + } |
| 133 | + return errors.Errorf("%d %s: %s", errBody.Status, errBody.Error.Type, errBody.Error.Reason) |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
0 commit comments