Code Cleanup Around Audit Backends (#20933)

* clean up go compiler warnings

* remove unused field from backendEntry struct
remove function argument no longer needed

* add changelog record

* use context.Background instead of context.TODO
This commit is contained in:
Marc Boudreau
2023-06-05 16:53:29 -04:00
committed by GitHub
parent 8e576cf226
commit 4374d7633b
4 changed files with 21 additions and 20 deletions

3
changelog/20933.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:improvement
core: remove unnecessary *BarrierView field from backendEntry struct
```

View File

@@ -155,7 +155,7 @@ func (c *Core) enableAudit(ctx context.Context, entry *MountEntry, updateStorage
c.audit = newTable c.audit = newTable
// Register the backend // Register the backend
c.auditBroker.Register(entry.Path, backend, view, entry.Local) c.auditBroker.Register(entry.Path, backend, entry.Local)
if c.logger.IsInfo() { if c.logger.IsInfo() {
c.logger.Info("enabled audit backend", "path", entry.Path, "type", entry.Type) c.logger.Info("enabled audit backend", "path", entry.Path, "type", entry.Type)
} }
@@ -416,7 +416,7 @@ func (c *Core) setupAudits(ctx context.Context) error {
} }
// Mount the backend // Mount the backend
broker.Register(entry.Path, backend, view, entry.Local) broker.Register(entry.Path, backend, entry.Local)
successCount++ successCount++
} }

View File

@@ -19,7 +19,6 @@ import (
type backendEntry struct { type backendEntry struct {
backend audit.Backend backend audit.Backend
view *BarrierView
local bool local bool
} }
@@ -41,12 +40,11 @@ func NewAuditBroker(log log.Logger) *AuditBroker {
} }
// Register is used to add new audit backend to the broker // Register is used to add new audit backend to the broker
func (a *AuditBroker) Register(name string, b audit.Backend, v *BarrierView, local bool) { func (a *AuditBroker) Register(name string, b audit.Backend, local bool) {
a.Lock() a.Lock()
defer a.Unlock() defer a.Unlock()
a.backends[name] = backendEntry{ a.backends[name] = backendEntry{
backend: b, backend: b,
view: v,
local: local, local: local,
} }
} }

View File

@@ -44,7 +44,7 @@ func TestAudit_ReadOnlyViewDuringMount(t *testing.T) {
Path: "foo", Path: "foo",
Type: "noop", Type: "noop",
} }
err := c.enableAudit(namespace.RootContext(nil), me, true) err := c.enableAudit(namespace.RootContext(context.Background()), me, true)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@@ -59,7 +59,7 @@ func TestCore_EnableAudit(t *testing.T) {
Path: "foo", Path: "foo",
Type: "noop", Type: "noop",
} }
err := c.enableAudit(namespace.RootContext(nil), me, true) err := c.enableAudit(namespace.RootContext(context.Background()), me, true)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@@ -239,7 +239,7 @@ func TestCore_DisableAudit(t *testing.T) {
c, keys, _ := TestCoreUnsealed(t) c, keys, _ := TestCoreUnsealed(t)
c.auditBackends["noop"] = corehelpers.NoopAuditFactory(nil) c.auditBackends["noop"] = corehelpers.NoopAuditFactory(nil)
existed, err := c.disableAudit(namespace.RootContext(nil), "foo", true) existed, err := c.disableAudit(namespace.RootContext(context.Background()), "foo", true)
if existed && err != nil { if existed && err != nil {
t.Fatalf("existed: %v; err: %v", existed, err) t.Fatalf("existed: %v; err: %v", existed, err)
} }
@@ -249,12 +249,12 @@ func TestCore_DisableAudit(t *testing.T) {
Path: "foo", Path: "foo",
Type: "noop", Type: "noop",
} }
err = c.enableAudit(namespace.RootContext(nil), me, true) err = c.enableAudit(namespace.RootContext(context.Background()), me, true)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
existed, err = c.disableAudit(namespace.RootContext(nil), "foo", true) existed, err = c.disableAudit(namespace.RootContext(context.Background()), "foo", true)
if !existed || err != nil { if !existed || err != nil {
t.Fatalf("existed: %v; err: %v", existed, err) t.Fatalf("existed: %v; err: %v", existed, err)
} }
@@ -343,8 +343,8 @@ func TestAuditBroker_LogRequest(t *testing.T) {
b := NewAuditBroker(l) b := NewAuditBroker(l)
a1 := corehelpers.TestNoopAudit(t, nil) a1 := corehelpers.TestNoopAudit(t, nil)
a2 := corehelpers.TestNoopAudit(t, nil) a2 := corehelpers.TestNoopAudit(t, nil)
b.Register("foo", a1, nil, false) b.Register("foo", a1, false)
b.Register("bar", a2, nil, false) b.Register("bar", a2, false)
auth := &logical.Auth{ auth := &logical.Auth{
ClientToken: "foo", ClientToken: "foo",
@@ -403,8 +403,8 @@ func TestAuditBroker_LogRequest(t *testing.T) {
if !reflect.DeepEqual(a.Req[0], req) { if !reflect.DeepEqual(a.Req[0], req) {
t.Fatalf("Bad: %#v\n wanted %#v", a.Req[0], req) t.Fatalf("Bad: %#v\n wanted %#v", a.Req[0], req)
} }
if !reflect.DeepEqual(a.ReqErrs[0], reqErrs) { if a.ReqErrs[0].Error() != reqErrs.Error() {
t.Fatalf("Bad: %#v", a.ReqErrs[0]) t.Fatalf("expected %v, got %v", a.ReqErrs[0].Error(), reqErrs.Error())
} }
} }
@@ -430,8 +430,8 @@ func TestAuditBroker_LogResponse(t *testing.T) {
b := NewAuditBroker(l) b := NewAuditBroker(l)
a1 := corehelpers.TestNoopAudit(t, nil) a1 := corehelpers.TestNoopAudit(t, nil)
a2 := corehelpers.TestNoopAudit(t, nil) a2 := corehelpers.TestNoopAudit(t, nil)
b.Register("foo", a1, nil, false) b.Register("foo", a1, false)
b.Register("bar", a2, nil, false) b.Register("bar", a2, false)
auth := &logical.Auth{ auth := &logical.Auth{
NumUses: 10, NumUses: 10,
@@ -504,8 +504,8 @@ func TestAuditBroker_LogResponse(t *testing.T) {
if !reflect.DeepEqual(a.Resp[0], resp) { if !reflect.DeepEqual(a.Resp[0], resp) {
t.Fatalf("Bad: %#v", a.Resp[0]) t.Fatalf("Bad: %#v", a.Resp[0])
} }
if !reflect.DeepEqual(a.RespErrs[0], respErr) { if a.RespErrs[0].Error() != respErr.Error() {
t.Fatalf("Expected\n%v\nGot\n%#v", respErr, a.RespErrs[0]) t.Fatalf("expected %v, got %v", respErr, a.RespErrs[0])
} }
} }
@@ -537,8 +537,8 @@ func TestAuditBroker_AuditHeaders(t *testing.T) {
view := NewBarrierView(barrier, "headers/") view := NewBarrierView(barrier, "headers/")
a1 := corehelpers.TestNoopAudit(t, nil) a1 := corehelpers.TestNoopAudit(t, nil)
a2 := corehelpers.TestNoopAudit(t, nil) a2 := corehelpers.TestNoopAudit(t, nil)
b.Register("foo", a1, nil, false) b.Register("foo", a1, false)
b.Register("bar", a2, nil, false) b.Register("bar", a2, false)
auth := &logical.Auth{ auth := &logical.Auth{
ClientToken: "foo", ClientToken: "foo",