Skip to content

Commit

Permalink
Fix up output registry and add spammy logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmith5 committed Jan 6, 2022
1 parent b5e60cd commit 1836494
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
19 changes: 18 additions & 1 deletion agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"errors"
"fmt"
"time"

"github.com/nsmith5/rekor-sidekick/outputs"
Expand All @@ -26,6 +27,8 @@ func newAgent(c config) (*agent, error) {

policies := c.Policies

fmt.Println("debug: outputs in config", c.Outputs)

var outs []outputs.Output
for name, conf := range c.Outputs {
output, err := outputs.LoadDriver(name, conf)
Expand All @@ -36,6 +39,8 @@ func newAgent(c config) (*agent, error) {
outs = append(outs, output)
}

fmt.Printf("debug: output drivers %#v\n", outs)

quit := make(chan struct{})

return &agent{rc, policies, outs, quit}, nil
Expand Down Expand Up @@ -66,28 +71,35 @@ func (a *agent) run() error {
if err != nil {
if err == rekor.ErrEntryDoesntExist {
// Log doesn't exist yet, lets just wait 10 seconds and try again
fmt.Println("debug: no entry available. time to snooze")
time.Sleep(10 * time.Second)

} else {
// Lets assume a temporary outage and retry with exponential backoff
fmt.Println("debug: outage! backoff started")
time.Sleep(currentBackoff * time.Second)
currentBackoff *= 2
}
break
}

fmt.Println("debug: got an entry!")

// Incase we just recovered from a temporary outage, lets reset the backoff
currentBackoff = initialBackoff

// Policy checks!
for _, p := range a.policies {
fmt.Printf("debug: iterating policies")

violation, err := p.allowed(entry)
if err != nil {
// huh... what to do here?
continue
}

if violation {
fmt.Println("debug: violation!")
for _, out := range a.outs {
// TODO: Populate the rekor URL!
e := outputs.Event{
Expand All @@ -97,7 +109,12 @@ func (a *agent) run() error {
}

// TODO: Do something on send failure
out.Send(e)
err = out.Send(e)
if err != nil {
fmt.Println("debug: error sending output")
} else {
fmt.Println("debug: successful sent output")
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions etc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ policies:
default allow = false
allow {
format := input.spec.signature.format
format != "x509"
format == "x509"
}
- name: allow-all
description: |-
Expand All @@ -19,4 +19,5 @@ policies:
default allow = true
outputs:
stdout: {}
stdout:
enabled: true
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
// Loading output drivers
_ "github.com/nsmith5/rekor-sidekick/outputs"
_ "github.com/nsmith5/rekor-sidekick/outputs/stdout"
)

Expand Down
8 changes: 7 additions & 1 deletion outputs/registry.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package outputs

import "errors"
import (
"errors"
"fmt"
)

type CreatorFunc func(map[string]interface{}) (Output, error)

Expand All @@ -11,14 +14,17 @@ func init() {
}

func RegisterDriver(name string, maker CreatorFunc) {
fmt.Println("debug: registering driver", name)
drivers[name] = maker
}

func LoadDriver(name string, conf map[string]interface{}) (Output, error) {
f, ok := drivers[name]
if !ok {
fmt.Println("debug: failed to load driver", name)
return nil, errors.New(`driver doesn't exist or wasn't loaded`)
}

fmt.Println("debug: loading driver", name)
return f(conf)
}

0 comments on commit 1836494

Please sign in to comment.