Skip to content

Commit

Permalink
WIP: stdout output
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmith5 committed Jan 5, 2022
1 parent 7cfb64f commit b5e60cd
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
29 changes: 24 additions & 5 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package main
import (
"context"
"errors"
"fmt"
"time"

"github.com/nsmith5/rekor-sidekick/outputs"
"github.com/nsmith5/rekor-sidekick/rekor"
)

type agent struct {
rc *rekor.Client
policies []policy
outs []outputs.Output

quit chan struct{}
}
Expand All @@ -25,9 +26,19 @@ func newAgent(c config) (*agent, error) {

policies := c.Policies

var outs []outputs.Output
for name, conf := range c.Outputs {
output, err := outputs.LoadDriver(name, conf)
if err != nil {
// Huh... log this issue I guess?
continue
}
outs = append(outs, output)
}

quit := make(chan struct{})

return &agent{rc, policies, quit}, nil
return &agent{rc, policies, outs, quit}, nil
}

// run starts off the agent. The call blocks or exits returning an error
Expand Down Expand Up @@ -77,9 +88,17 @@ func (a *agent) run() error {
}

if violation {
// TODO: Send to outputs!
fmt.Printf("Entry %#v violated policy %s\n", entry, p.Name)
time.Sleep(5 * time.Second)
for _, out := range a.outs {
// TODO: Populate the rekor URL!
e := outputs.Event{
Name: p.Name,
Description: p.Description,
RekorURL: `dunno...`,
}

// TODO: Do something on send failure
out.Send(e)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

type config struct {
RekorServerURL string `yaml:"rekorServerURL"`
Policies []policy `yaml:"policies"`
RekorServerURL string `yaml:"rekorServerURL"`
Policies []policy `yaml:"policies"`
Outputs map[string]map[string]interface{} `yaml:"outputs"`
}
3 changes: 3 additions & 0 deletions etc/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rekorServerURL: https://rekor.sigstore.dev

policies:
- name: x509-used
description: |-
Expand All @@ -17,3 +18,5 @@ policies:
package auth
default allow = true
outputs:
stdout: {}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package main

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

func main() {
cmd := newCLI()
cmd.Execute()
Expand Down
11 changes: 11 additions & 0 deletions outputs/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package outputs

type Event struct {
Name string
Description string
RekorURL string
}

type Output interface {
Send(Event) error
}
24 changes: 24 additions & 0 deletions outputs/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package outputs

import "errors"

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

var drivers map[string]CreatorFunc

func init() {
drivers = make(map[string]CreatorFunc)
}

func RegisterDriver(name string, maker CreatorFunc) {
drivers[name] = maker
}

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

return f(conf)
}
28 changes: 28 additions & 0 deletions outputs/stdout/stdout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package stdout

import (
"fmt"

"github.com/nsmith5/rekor-sidekick/outputs"
)

type impl struct{}

func (i impl) Send(e outputs.Event) error {
fmt.Printf(
`{"name": "%s", "description": "%s", rekorURL: "%s"}`,
e.Name,
e.Description,
e.RekorURL,
)
fmt.Println()
return nil
}

func New(map[string]interface{}) (outputs.Output, error) {
return &impl{}, nil
}

func init() {
outputs.RegisterDriver("stdout", outputs.CreatorFunc(New))
}

0 comments on commit b5e60cd

Please sign in to comment.