Skip to content

Commit

Permalink
Fix http client, call url and sms message
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhny committed Oct 19, 2023
1 parent 31dcdbb commit 34ea876
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 41 deletions.
9 changes: 6 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ import (
"os"
)

var logLevel *slog.LevelVar

func init() {
logLevel = &slog.LevelVar{}
logLevel.Set(slog.LevelInfo)

logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
Level: logLevel,
}))
slog.SetDefault(logger)
}

func main() {
port := ":8080"
slog.Info("Starting smseagle-proxy", "port", port)

cfg := config.Read()
if cfg.Debug {
logLevel := &slog.LevelVar{}
logLevel.Set(slog.LevelDebug)
slog.Debug("Debug mode on")
}
Expand Down
31 changes: 9 additions & 22 deletions pkg/alerter/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,15 @@ import (
)

type OncallWebhook struct {
GrafanaWebhook GrafanaWebhook `json:"alert_payload"`
AlertGroup AlertGroup `json:"alert_group"`
}

type GrafanaWebhook struct {
Title string `json:"title"`
State string `json:"state"`
Message string `json:"message"`
Alerts []GrafanaAlert `json:"alerts"`
type AlertGroup struct {
Permalinks OncallPermalink `json:"permalinks"`
}

type GrafanaAlert struct {
Status string `json:"status"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
StartsAt string `json:"startsAt"`
EndsAt string `json:"endsAt"`
GeneratorURL string `json:"generatorURL"`
Fingerpriot string `json:"fingerprint"`
SilenceURL string `json:"silenceURL"`
DashboardURL string `json:"dashboardURL"`
PanelURL string `json:"panelURL"`
Values map[string]float32 `json:"values"`
type OncallPermalink struct {
Web string `json:"web"`
}

type Notifier interface {
Expand All @@ -50,15 +37,15 @@ func NewGrafana(notifier Notifier, cfg *config.ProxyConfig) *Grafana {
}
}

func parseGrafanaWebhook(r *http.Request) (*GrafanaWebhook, error) {
func parseOncallWebhook(r *http.Request) (*OncallWebhook, error) {
slog.Debug("Parsing", "request", r)
var webhook OncallWebhook
err := json.NewDecoder(r.Body).Decode(&webhook)
if err != nil {
return nil, err
}
slog.Debug("Parsed %w", "webhook", &webhook)
return &webhook.GrafanaWebhook, nil
return &webhook, nil
}

func (g *Grafana) HandleCall(w http.ResponseWriter, r *http.Request) {
Expand All @@ -76,7 +63,7 @@ func (g *Grafana) handleRequest(w http.ResponseWriter, r *http.Request, c Contac
return
}

webhook, err := parseGrafanaWebhook(r)
webhook, err := parseOncallWebhook(r)
if err != nil {
slog.Error("decoding webhook failed", "error", err)
w.WriteHeader(http.StatusBadRequest)
Expand All @@ -94,7 +81,7 @@ func (g *Grafana) handleRequest(w http.ResponseWriter, r *http.Request, c Contac

message := SMSEagleMessage{
Receiver: receiver,
Message: fmt.Sprintf("test: %s", webhook.Title),
Message: fmt.Sprintf("Ny alarm: %s", webhook.AlertGroup.Permalinks.Web),
ContactType: c,
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/alerter/grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var _ = Describe("Grafana", func() {
Expect(mock.message.Receiver).Should(Equal(smseagle.Infrastrukturdrift))
})
It("message should be correct", func() {
Expect(mock.message.Message).Should(Equal("Instance localhost:8082 down"))
Expect(mock.message.Message).Should(Equal("Ny alarm: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY"))
})
It("should have sms contact type", func() {
Expect(mock.message.ContactType).Should(Equal(smseagle.SMS))
Expand All @@ -110,7 +110,7 @@ var _ = Describe("Grafana", func() {
Expect(mock.message.Receiver).Should(Equal(smseagle.Appdrift))
})
It("message should be correct", func() {
Expect(mock.message.Message).Should(Equal("Instance localhost:8082 down"))
Expect(mock.message.Message).Should(Equal("Ny alarm: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY"))
})
It("should have sms contact type", func() {
Expect(mock.message.ContactType).Should(Equal(smseagle.SMS))
Expand Down Expand Up @@ -140,7 +140,7 @@ var _ = Describe("Grafana", func() {
Expect(mock.message.Receiver).Should(Equal(smseagle.Infrastrukturdrift))
})
It("message should be correct", func() {
Expect(mock.message.Message).Should(Equal("Instance localhost:8082 down"))
Expect(mock.message.Message).Should(Equal("Ny alarm: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY"))
})
It("should have call contact type", func() {
Expect(mock.message.ContactType).Should(Equal(smseagle.Call))
Expand All @@ -160,7 +160,7 @@ var _ = Describe("Grafana", func() {
Expect(mock.message.Receiver).Should(Equal(smseagle.Appdrift))
})
It("message should be correct", func() {
Expect(mock.message.Message).Should(Equal("Instance localhost:8082 down"))
Expect(mock.message.Message).Should(Equal("Ny alarm: http://grafana:3000/a/grafana-oncall-app/alert-groups/I57917WDFNGHY"))
})
It("should have call contact type", func() {
Expect(mock.message.ContactType).Should(Equal(smseagle.Call))
Expand Down
16 changes: 8 additions & 8 deletions pkg/smseagle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
"net/http"
)

func sendSMS(cfg *config.ProxyConfig, phoneNumber string, message string) error {
requestUrl := fmt.Sprintf("http://%s/http_api/send_sms?access_token=%s&to=%s&message=%s", cfg.SMS.Url, cfg.SMS.AccessToken, phoneNumber, message)
func sendSMS(cfg *config.ProxyConfig, phoneNumber string, message string, client *http.Client) error {
requestUrl := fmt.Sprintf("%s/http_api/send_sms?access_token=%s&to=%s&message=%s", cfg.SMS.Url, cfg.SMS.AccessToken, phoneNumber, message)
slog.Debug("Sending sms", "url", requestUrl)
res, err := http.Get(requestUrl)
res, err := client.Get(requestUrl)
if err != nil {
return err
}
slog.Debug("sms request succesful", "response code", res.StatusCode, "response text", res.Body)
slog.Debug("sms request successful", "response code", res.StatusCode, "response text", res.Status)
return nil
}

func call(cfg *config.ProxyConfig, phoneNumber string) error {
requestUrl := fmt.Sprintf("http://%s/http_api/call_with_termination?access_token=%s&to=%s", cfg.Call.Url, cfg.Call.AccessToken, phoneNumber)
func call(cfg *config.ProxyConfig, phoneNumber string, client *http.Client) error {
requestUrl := fmt.Sprintf("%s/http_api/call_with_termination?access_token=%s&to=%s&duration=30", cfg.Call.Url, cfg.Call.AccessToken, phoneNumber)
slog.Debug("Sending call request", "url", requestUrl)
res, err := http.Get(requestUrl)
res, err := client.Get(requestUrl)
if err != nil {
return err
}
slog.Debug("Call request successful", "response", res.Body)
slog.Debug("Call request successful", "response code", res.StatusCode, "response text", res.Status)
return nil
}
10 changes: 8 additions & 2 deletions pkg/smseagle/handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package smseagle

import (
"crypto/tls"
"kartverket.no/smseagle-proxy/pkg/config"
"log/slog"
"net/http"
"strings"
)

Expand Down Expand Up @@ -46,15 +48,19 @@ func (s *SMSEagle) Notify(message *SMSEagleMessage) error {
phoneNumber = s.cfg.InfraPhoneNumber
}

client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}

if message.ContactType == SMS {
msg := strings.ReplaceAll(message.Message, " ", "+")
err := sendSMS(s.cfg, phoneNumber, msg)
err := sendSMS(s.cfg, phoneNumber, msg, client)
if err != nil {
slog.Error("Error sending sms", "error", err)
return err
}
} else if message.ContactType == Call {
err := call(s.cfg, phoneNumber)
err := call(s.cfg, phoneNumber, client)
if err != nil {
slog.Error("Error sending call request", "error", err)
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/smseagle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ var _ = Describe("Handler", func() {
AppdriftPhoneNumber: "123",
InfraPhoneNumber: "456",
Call: config.SMSEagleConfig{
Url: server.Addr(),
Url: server.URL(),
AccessToken: "calltoken",
},
SMS: config.SMSEagleConfig{
Url: server.Addr(),
Url: server.URL(),
AccessToken: "smstoken",
},
}
Expand Down

0 comments on commit 34ea876

Please sign in to comment.