* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
365 lines
9.0 KiB
Go
365 lines
9.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package event
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
|
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/groups"
|
|
"github.com/hashicorp/eventlogger"
|
|
"github.com/hashicorp/eventlogger/formatter_filters/cloudevents"
|
|
"github.com/hashicorp/go-hclog"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
|
)
|
|
|
|
// CloudEventFromFile will marshal a single cloud event from the provided file
|
|
// name
|
|
func CloudEventFromFile(t testing.TB, fileName string) *cloudevents.Event {
|
|
t.Helper()
|
|
b, err := os.ReadFile(fileName)
|
|
assert.NoError(t, err)
|
|
got := &cloudevents.Event{}
|
|
err = json.Unmarshal(b, got)
|
|
require.NoErrorf(t, err, "json: %s", string(b))
|
|
return got
|
|
}
|
|
|
|
// CloudEventFromBuf will marshal a single cloud event from the provided buffer
|
|
func CloudEventFromBuf(t testing.TB, b []byte) *cloudevents.Event {
|
|
t.Helper()
|
|
got := &cloudevents.Event{}
|
|
err := json.Unmarshal(b, got)
|
|
require.NoErrorf(t, err, "json: %s", string(b))
|
|
return got
|
|
}
|
|
|
|
// TestWithoutEventing allows the caller to "disable" all eventing for a test.
|
|
// You must not run the test in parallel (no calls to t.Parallel) since the
|
|
// function relies on modifying the system wide default eventer.
|
|
func TestWithoutEventing(t testing.TB) *Eventer {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
testConfig := EventerConfig{
|
|
AuditEnabled: false,
|
|
ObservationsEnabled: false,
|
|
SysEventsEnabled: false,
|
|
}
|
|
testLock := &sync.Mutex{}
|
|
testLogger := hclog.New(&hclog.LoggerOptions{
|
|
Mutex: testLock,
|
|
Output: ioutil.Discard,
|
|
})
|
|
testEventer, err := NewEventer(testLogger, testLock, "TestWithoutEventing", testConfig, withNoDefaultSink(t))
|
|
require.NoError(err)
|
|
|
|
require.NoError(InitSysEventer(testLogger, testLock, "TestWithoutEventing", WithEventer(testEventer)))
|
|
return testEventer
|
|
}
|
|
|
|
// TestGetEventerConfig is a test accessor for the eventer's config
|
|
func TestGetEventerConfig(t testing.TB, e *Eventer) EventerConfig {
|
|
t.Helper()
|
|
return e.conf
|
|
}
|
|
|
|
// TestResetSysEventer will reset event.syseventer to an uninitialized state.
|
|
func TestResetSystEventer(t testing.TB) {
|
|
t.Helper()
|
|
sysEventerLock.Lock()
|
|
defer sysEventerLock.Unlock()
|
|
sysEventer = nil
|
|
}
|
|
|
|
type TestConfig struct {
|
|
EventerConfig EventerConfig
|
|
AllEvents *os.File
|
|
ErrorEvents *os.File
|
|
ObservationEvents *os.File
|
|
AuditEvents *os.File
|
|
}
|
|
|
|
// TestEventerConfig creates a test config and registers a cleanup func for its
|
|
// test tmp files.
|
|
func TestEventerConfig(t testing.TB, testName string, opt ...Option) TestConfig {
|
|
t.Helper()
|
|
require := require.New(t)
|
|
tmpAllFile, err := ioutil.TempFile("./", "tmp-all-events-"+testName)
|
|
require.NoError(err)
|
|
|
|
tmpErrFile, err := ioutil.TempFile("./", "tmp-errors-"+testName)
|
|
require.NoError(err)
|
|
|
|
t.Cleanup(func() {
|
|
os.Remove(tmpAllFile.Name())
|
|
os.Remove(tmpErrFile.Name())
|
|
})
|
|
|
|
opts := getOpts(opt...)
|
|
if opts.withSinkFormat == "" {
|
|
opts.withSinkFormat = JSONSinkFormat
|
|
}
|
|
|
|
c := TestConfig{
|
|
EventerConfig: EventerConfig{
|
|
ObservationsEnabled: true,
|
|
AuditEnabled: true,
|
|
SysEventsEnabled: true,
|
|
Sinks: []*SinkConfig{
|
|
{
|
|
Name: "every-type-file-sink",
|
|
Type: FileSink,
|
|
EventTypes: []Type{EveryType},
|
|
Format: opts.withSinkFormat,
|
|
FileConfig: &FileSinkTypeConfig{
|
|
Path: "./",
|
|
FileName: tmpAllFile.Name(),
|
|
},
|
|
AuditConfig: DefaultAuditConfig(),
|
|
},
|
|
{
|
|
Name: "err-file-sink",
|
|
Type: FileSink,
|
|
EventTypes: []Type{ErrorType},
|
|
Format: opts.withSinkFormat,
|
|
FileConfig: &FileSinkTypeConfig{
|
|
Path: "./",
|
|
FileName: tmpErrFile.Name(),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
AllEvents: tmpAllFile,
|
|
ErrorEvents: tmpErrFile,
|
|
}
|
|
if opts.withStderrSink {
|
|
c.EventerConfig.Sinks = append(c.EventerConfig.Sinks, &SinkConfig{
|
|
Name: "stderr",
|
|
Type: StderrSink,
|
|
EventTypes: []Type{EveryType},
|
|
Format: opts.withSinkFormat,
|
|
AuditConfig: DefaultAuditConfig(),
|
|
})
|
|
}
|
|
if opts.withAuditSink {
|
|
tmpFile, err := ioutil.TempFile("./", "tmp-audit-"+testName)
|
|
require.NoError(err)
|
|
t.Cleanup(func() {
|
|
os.Remove(tmpFile.Name())
|
|
})
|
|
c.EventerConfig.Sinks = append(c.EventerConfig.Sinks, &SinkConfig{
|
|
Name: "audit-file-sink",
|
|
Type: FileSink,
|
|
EventTypes: []Type{AuditType},
|
|
Format: opts.withSinkFormat,
|
|
FileConfig: &FileSinkTypeConfig{
|
|
Path: "./",
|
|
FileName: tmpFile.Name(),
|
|
},
|
|
AuditConfig: DefaultAuditConfig(),
|
|
})
|
|
c.AuditEvents = tmpFile
|
|
}
|
|
if opts.withObservationSink {
|
|
tmpFile, err := ioutil.TempFile("./", "tmp-observation-"+testName)
|
|
require.NoError(err)
|
|
t.Cleanup(func() {
|
|
os.Remove(tmpFile.Name())
|
|
})
|
|
c.EventerConfig.Sinks = append(c.EventerConfig.Sinks, &SinkConfig{
|
|
Name: "err-observation-sink",
|
|
Type: FileSink,
|
|
EventTypes: []Type{ObservationType},
|
|
Format: opts.withSinkFormat,
|
|
FileConfig: &FileSinkTypeConfig{
|
|
Path: "./",
|
|
FileName: tmpFile.Name(),
|
|
},
|
|
})
|
|
c.ObservationEvents = tmpFile
|
|
}
|
|
if opts.withSysSink {
|
|
tmpFile, err := ioutil.TempFile("./", "tmp-sysevents-"+testName)
|
|
require.NoError(err)
|
|
t.Cleanup(func() {
|
|
os.Remove(tmpFile.Name())
|
|
})
|
|
c.EventerConfig.Sinks = append(c.EventerConfig.Sinks, &SinkConfig{
|
|
Name: "err-sysevents-sink",
|
|
Type: FileSink,
|
|
EventTypes: []Type{SystemType},
|
|
Format: opts.withSinkFormat,
|
|
FileConfig: &FileSinkTypeConfig{
|
|
Path: "./",
|
|
FileName: tmpFile.Name(),
|
|
},
|
|
})
|
|
}
|
|
return c
|
|
}
|
|
|
|
// TestRequestInfo provides a test RequestInfo
|
|
func TestRequestInfo(t testing.TB) *RequestInfo {
|
|
t.Helper()
|
|
return &RequestInfo{
|
|
EventId: "test-event-id",
|
|
Id: "test-request-info",
|
|
Method: "POST",
|
|
Path: "/test/request/info",
|
|
PublicId: "public-id",
|
|
}
|
|
}
|
|
|
|
func testAuth(t testing.TB) *Auth {
|
|
t.Helper()
|
|
return &Auth{
|
|
UserEmail: "test-auth@example.com",
|
|
UserName: "test-auth-user-name",
|
|
}
|
|
}
|
|
|
|
func testRequest(t testing.TB) *Request {
|
|
t.Helper()
|
|
return &Request{
|
|
Operation: "op",
|
|
Endpoint: "/group/<id>",
|
|
Details: &pbs.GetGroupRequest{
|
|
Id: "group-id",
|
|
},
|
|
}
|
|
}
|
|
|
|
func testResponse(t testing.TB) *Response {
|
|
t.Helper()
|
|
return &Response{
|
|
StatusCode: 200,
|
|
Details: &pbs.GetGroupResponse{
|
|
Item: &groups.Group{
|
|
Id: "group-id",
|
|
ScopeId: "org-id",
|
|
Name: &wrapperspb.StringValue{
|
|
Value: "group-name",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// TestWithBroker is an unexported and a test option for passing in an optional broker
|
|
func TestWithBroker(t testing.TB, b broker) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withBroker = b
|
|
}
|
|
}
|
|
|
|
// TestWithObservationSink is a test option
|
|
func TestWithObservationSink(t testing.TB) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withObservationSink = true
|
|
}
|
|
}
|
|
|
|
// TestWithAuditSink is a test option
|
|
func TestWithAuditSink(t testing.TB) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withAuditSink = true
|
|
}
|
|
}
|
|
|
|
// TestWithSysSink is a test option
|
|
func TestWithSysSink(t testing.TB) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withSysSink = true
|
|
}
|
|
}
|
|
|
|
// TestWithStderrSink is a test option
|
|
func TestWithStderrSink(t testing.TB) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withStderrSink = true
|
|
}
|
|
}
|
|
|
|
// withNoDefaultSink is an unexported test option
|
|
func withNoDefaultSink(t testing.TB) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withNoDefaultSink = true
|
|
}
|
|
}
|
|
|
|
// testWithSinkFormat is an unexported and a test option
|
|
func testWithSinkFormat(t testing.TB, fmt SinkFormat) Option {
|
|
t.Helper()
|
|
return func(o *options) {
|
|
o.withSinkFormat = fmt
|
|
}
|
|
}
|
|
|
|
type testMockBroker struct {
|
|
reopened bool
|
|
stopTimeAt time.Time
|
|
registeredNodeIds []eventlogger.NodeID
|
|
successThresholds map[eventlogger.EventType]int
|
|
pipelines []eventlogger.Pipeline
|
|
|
|
errorOnSend error
|
|
}
|
|
|
|
func (b *testMockBroker) Reopen(ctx context.Context) error {
|
|
b.reopened = true
|
|
return nil
|
|
}
|
|
|
|
func (b *testMockBroker) RegisterPipeline(def eventlogger.Pipeline) error {
|
|
b.pipelines = append(b.pipelines, def)
|
|
return nil
|
|
}
|
|
|
|
func (b *testMockBroker) Send(ctx context.Context, t eventlogger.EventType, payload any) (eventlogger.Status, error) {
|
|
if b.errorOnSend != nil {
|
|
return eventlogger.Status{}, b.errorOnSend
|
|
}
|
|
return eventlogger.Status{}, nil
|
|
}
|
|
|
|
func (b *testMockBroker) StopTimeAt(t time.Time) {
|
|
b.stopTimeAt = t
|
|
}
|
|
|
|
func (b *testMockBroker) RegisterNode(id eventlogger.NodeID, node eventlogger.Node) error {
|
|
b.registeredNodeIds = append(b.registeredNodeIds, id)
|
|
return nil
|
|
}
|
|
|
|
func (b *testMockBroker) SetSuccessThreshold(t eventlogger.EventType, successThreshold int) error {
|
|
if b.successThresholds == nil {
|
|
b.successThresholds = map[eventlogger.EventType]int{}
|
|
}
|
|
b.successThresholds[t] = successThreshold
|
|
return nil
|
|
}
|
|
|
|
func testLogger(t *testing.T, testLock hclog.Locker) hclog.Logger {
|
|
t.Helper()
|
|
return hclog.New(&hclog.LoggerOptions{
|
|
Mutex: testLock,
|
|
Name: "test",
|
|
JSONFormat: true,
|
|
})
|
|
}
|