Skip to content

Commit

Permalink
(notifer) Add fallback on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
agrim123 committed Jan 22, 2021
1 parent 64143ad commit 537089d
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 22 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ Gatekeeper provides basic authentication, authorization, and notifier (default i
gatekeeper.WithNotifier(MyCustomNotifier)
```

#### Notifier Module

Default notifier module logs to stdout. However, it can entirely be customized by creating you own module and injecting it to gattekeeper on initialization.

`SlackNotifier` is also present but disabled by default. It can be enabled by using:
```golang
gatekeeper := NewGatekeeper(context.Background())
gatekeeper.WithNotifier(notifier.NewSlackNotifier("<SLACK_WEBHOOK_URL>"))
```

If any notifer fails, the default behaviour is to dump logs to stdout, so that you don't miss out any logs.
```bash
[SUCCESS] | Authenticated as agrim
[SUCCESS] | Authorized `agrim` to perform `service1 shell`
[INFO] | Executing plan: service1 shell
[INFO] | Spawning shell for <user>@<host>
[INFO] 🔐 | Reading private key
[ERROR] | Notifier: slack failed. Fallback to default notifier
[NOTIFIER] | Plan `service1 shell` executed by `agrim` failed. Error: Failed to connect to <user>@<host>. Error: dial tcp 3.84.241.53:22: i/o timeout
```

## Setup

Four configs drive gatekeeper:
Expand Down
2 changes: 1 addition & 1 deletion examples/configs/plan.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"/usr/bin/whoami",
"echo \"Hello from remote server\""
]
},
}
}
}
]
Expand Down
20 changes: 10 additions & 10 deletions gatekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type GateKeeper struct {
runtime *runtime.Runtime
guard *guard.Guard

notifier notifier.Notifier
notifyRequester func(string)
}

// NewGatekeeper returns new instance of gatekeeper with default modules
Expand All @@ -34,19 +34,19 @@ func NewGatekeeper(ctx context.Context) *GateKeeper {
ctx = utils.AttachExecutingUserToCtx(ctx)

g := &GateKeeper{
ctx: ctx,
runtime: runtime.NewRuntime(ctx),
notifier: notifier.NewDefaultNotifier(),
guard: guard.NewGuard(ctx),
store: store.Store,
ctx: ctx,
runtime: runtime.NewRuntime(ctx),
notifyRequester: notifier.AttachFallbackNotifier(notifier.NewDefaultNotifier()),
guard: guard.NewGuard(ctx),
store: store.Store,
}

return g
}

// WithNotifier updates the notifier module
func (g *GateKeeper) WithNotifier(notifier notifier.Notifier) *GateKeeper {
g.notifier = notifier
func (g *GateKeeper) WithNotifier(customNotifier notifier.Notifier) *GateKeeper {
g.notifyRequester = notifier.AttachFallbackNotifier(customNotifier)
return g
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func (g *GateKeeper) Run(plan, option string) {

err := g.runtime.Execute(plan, option)
if err != nil {
g.notifier.Notify(
g.notifyRequester(
fmt.Sprintf(
"Plan `%s %s` executed by `%s` failed. Error: %s",
plan,
Expand All @@ -91,7 +91,7 @@ func (g *GateKeeper) Run(plan, option string) {
),
)
} else {
g.notifier.Notify(
g.notifyRequester(
fmt.Sprintf(
"Plan `%s %s` executed by `%s` successfully!",
plan,
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/services/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ func (r *Remote) RunCommand(cmd string) {
}

// MakeNewConnection initiates new ssh connection to given host
func (r *Remote) MakeNewConnection() {
func (r *Remote) MakeNewConnection() error {
connection, err := ssh.Dial("tcp", r.address, &r.Config)
if err != nil {
logger.Fatal("Failed to connect to %s. Error: %s", r.address, err.Error())
return fmt.Errorf("Failed to connect to %s. Error: %s", r.address, err.Error())
}

r.Client = connection
return nil
}

// SpawnShell spwans shell on remote machine
Expand Down
8 changes: 6 additions & 2 deletions internal/store/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ func (s Shell) Run() error {
logger.Info("Spawning shell for %s", logger.Bold(instance.String()))

r := remote.NewRemoteConnection(instance.User, instance.IP, instance.Port, instance.PrivateKey)
r.MakeNewConnection()
err := r.MakeNewConnection()
if err != nil {
return err
}

r.SpawnShell()
r.Close()

return nil
return err
}

type Remote struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/notifier/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ func NewDefaultNotifier() Default {
}

func (d Default) Notify(message string) error {
d.FallbackNotify(message)
logger.Notifier(message)
return nil
}

func (d Default) FallbackNotify(message string) {
logger.Notifier(message)
func (d Default) Name() string {
return "default"
}
16 changes: 15 additions & 1 deletion pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package notifier

import (
"github.com/agrim123/gatekeeper/pkg/logger"
)

type Notifier interface {
Name() string
Notify(message string) error
FallbackNotify(message string)
}

func AttachFallbackNotifier(notifer Notifier) func(string) {
return func(message string) {
err := notifer.Notify(message)
if err != nil {
logger.Error("Notifier: %s failed. Fallback to default notifier", logger.Underline(notifer.Name()))
logger.Notifier(message)
}
}
}
7 changes: 4 additions & 3 deletions pkg/notifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func (s Slack) Notify(message string) error {
slackBody, _ := json.Marshal(slackRequestBody{Text: message})
req, err := http.NewRequest(http.MethodPost, s.Hook, bytes.NewBuffer(slackBody))
if err != nil {
s.FallbackNotify(message)
return err
}

Expand All @@ -31,20 +30,22 @@ func (s Slack) Notify(message string) error {
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
s.FallbackNotify(message)
return err
}

buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
if buf.String() != "ok" {
s.FallbackNotify(message)
return errors.New("Unable to send notification to slack")
}

return nil
}

func (s Slack) Name() string {
return "slack"
}

func NewSlackNotifier(hook string) *Slack {
return &Slack{
Hook: hook,
Expand Down

0 comments on commit 537089d

Please sign in to comment.