-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nathan Smith <[email protected]>
- Loading branch information
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cloudevents | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
ce "github.com/cloudevents/sdk-go/v2" | ||
"github.com/cloudevents/sdk-go/v2/client" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/nsmith5/rekor-sidekick/outputs" | ||
) | ||
|
||
const ( | ||
driverName = `cloudevents` | ||
eventSourcePrefix = `github.com/nsmith5/rekor-sidekick` | ||
eventType = `rekor-sidekick.policy.violation.v1` | ||
) | ||
|
||
var ( | ||
ErrConfigMissingURL = errors.New(`cloudevents: driver requires "http.url" in configuration`) | ||
ErrConfigMissingSourceID = errors.New(`cloudevents: driver requires "sourceID" in configuration`) | ||
) | ||
|
||
type config struct { | ||
SourceID string | ||
HTTP struct { | ||
URL string | ||
} | ||
} | ||
|
||
type driver struct { | ||
sourceID string | ||
http struct { | ||
url string | ||
} | ||
client client.Client | ||
} | ||
|
||
func (d *driver) Send(e outputs.Event) error { | ||
event := ce.NewEvent() | ||
event.SetSource(fmt.Sprintf("%s:%s", eventSourcePrefix, d.sourceID)) | ||
event.SetType(eventType) | ||
event.SetData(ce.ApplicationJSON, e) | ||
|
||
ctx := ce.ContextWithTarget(context.Background(), d.http.url) | ||
|
||
if result := d.client.Send(ctx, event); !ce.IsACK(result) { | ||
return result | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *driver) Name() string { | ||
return driverName | ||
} | ||
|
||
func createDriver(conf map[string]interface{}) (outputs.Output, error) { | ||
var c config | ||
err := mapstructure.Decode(conf, &c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if c.SourceID == "" { | ||
return nil, ErrConfigMissingSourceID | ||
} | ||
if c.HTTP.URL == "" { | ||
return nil, ErrConfigMissingURL | ||
} | ||
|
||
client, err := ce.NewDefaultClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &driver{ | ||
sourceID: c.SourceID, | ||
http: struct{ url string }{ | ||
url: c.HTTP.URL, | ||
}, | ||
client: client, | ||
}, nil | ||
} | ||
|
||
func init() { | ||
outputs.RegisterDriver(driverName, outputs.CreatorFunc(createDriver)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cloudevents | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/nsmith5/rekor-sidekick/outputs" | ||
) | ||
|
||
func TestCreateDriver(t *testing.T) { | ||
tests := map[string]struct { | ||
Conf map[string]interface{} | ||
Error error | ||
}{ | ||
"valid configuration": { | ||
Conf: map[string]interface{}{ | ||
`sourceID`: `id`, | ||
`http`: map[string]interface{}{ | ||
`url`: `https://localhost:8080`, | ||
}, | ||
}, | ||
Error: nil, | ||
}, | ||
"missing url in config": { | ||
Conf: map[string]interface{}{ | ||
`sourceID`: `abc123`, | ||
}, | ||
Error: ErrConfigMissingURL, | ||
}, | ||
"missing sourceID in config": { | ||
Conf: map[string]interface{}{ | ||
`http`: map[string]interface{}{ | ||
`url`: `http://derp:8080`, | ||
}, | ||
}, | ||
Error: ErrConfigMissingSourceID, | ||
}, | ||
} | ||
|
||
for name, data := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
_, err := createDriver(data.Conf) | ||
if err != data.Error { | ||
t.Errorf("Expected err %q, but recieved %q", data.Error, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSend(t *testing.T) { | ||
sourceID := `23124131` | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
expectedSource := fmt.Sprintf("%s:%s", eventSourcePrefix, sourceID) | ||
if r.Header.Get("Ce-Source") != expectedSource { | ||
t.Errorf("expected event source to be %s but got %s", expectedSource, r.Header.Get("Ce-Source")) | ||
} | ||
if r.Header.Get("Ce-Type") != eventType { | ||
t.Errorf("expected event type to be %s but got %s", eventType, r.Header.Get("Ce-Type")) | ||
} | ||
})) | ||
|
||
conf := map[string]interface{}{ | ||
`sourceID`: sourceID, | ||
`http`: map[string]interface{}{ | ||
`url`: ts.URL, | ||
}, | ||
} | ||
|
||
driver, err := createDriver(conf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = driver.Send(outputs.Event{}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |