Skip to content

Commit 7a0efe0

Browse files
committed
moved events to its own pkg
1 parent 59b3f51 commit 7a0efe0

File tree

8 files changed

+84
-13
lines changed

8 files changed

+84
-13
lines changed

events.go renamed to internal/event/event.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package gitlab
1+
package event
22

33
import (
44
"context"
@@ -10,14 +10,15 @@ import (
1010
"google.golang.org/protobuf/types/known/structpb"
1111
)
1212

13-
func Event(ctx context.Context, b *framework.Backend, eventType string, metadata map[string]string) {
13+
func Event(ctx context.Context, b *framework.Backend, prefix, eventType string, metadata map[string]string) error {
1414
var err error
1515
var ev *logical.EventData
1616
if ev, err = logical.NewEvent(); err == nil {
1717
var metadataBytes []byte
1818
metadataBytes, _ = json.Marshal(metadata)
1919
ev.Metadata = &structpb.Struct{}
2020
_ = ev.Metadata.UnmarshalJSON(metadataBytes)
21-
_ = b.SendEvent(ctx, logical.EventType(fmt.Sprintf("%s/%s", operationPrefixGitlabAccessTokens, eventType)), ev)
21+
err = b.SendEvent(ctx, logical.EventType(fmt.Sprintf("%s/%s", prefix, eventType)), ev)
2222
}
23+
return err
2324
}

internal/event/event_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package event_test
2+
3+
import (
4+
"context"
5+
"sync"
6+
"testing"
7+
8+
"github.com/hashicorp/vault/sdk/framework"
9+
"github.com/hashicorp/vault/sdk/logical"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
13+
)
14+
15+
type mockEventsSender struct {
16+
events []*logical.EventReceived
17+
mu sync.Mutex
18+
}
19+
20+
var _ logical.EventSender = (*mockEventsSender)(nil)
21+
22+
func (m *mockEventsSender) SendEvent(ctx context.Context, eventType logical.EventType, event *logical.EventData) error {
23+
if m == nil {
24+
return nil
25+
}
26+
m.mu.Lock()
27+
defer m.mu.Unlock()
28+
m.events = append(m.events, &logical.EventReceived{
29+
EventType: string(eventType),
30+
Event: event,
31+
})
32+
return nil
33+
}
34+
35+
func TestEvent(t *testing.T) {
36+
t.Run("no sender specified", func(t *testing.T) {
37+
b := &framework.Backend{}
38+
require.NoError(t, b.Setup(t.Context(), &logical.BackendConfig{}))
39+
require.ErrorIs(t,
40+
event.Event(
41+
t.Context(),
42+
&framework.Backend{},
43+
"test", "test",
44+
map[string]string{"test": "test"},
45+
),
46+
framework.ErrNoEvents,
47+
)
48+
})
49+
50+
t.Run("with event sender", func(t *testing.T) {
51+
b := &framework.Backend{}
52+
evt := &mockEventsSender{}
53+
require.NoError(t, b.Setup(t.Context(), &logical.BackendConfig{EventsSender: evt}))
54+
require.NoError(t,
55+
event.Event(
56+
t.Context(), b,
57+
"test", "test",
58+
map[string]string{"test": "test"},
59+
),
60+
)
61+
require.Len(t, evt.events, 1)
62+
})
63+
}

path_config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
g "gitlab.com/gitlab-org/api/client-go"
1414

1515
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs"
16+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
1617
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/gitlab"
1718
)
1819

@@ -91,7 +92,7 @@ func (b *Backend) pathConfigDelete(ctx context.Context, req *logical.Request, da
9192
}
9293

9394
if err = req.Storage.Delete(ctx, fmt.Sprintf("%s/%s", PathConfigStorage, name)); err == nil {
94-
Event(ctx, b.Backend, "config-delete", map[string]string{
95+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-delete", map[string]string{
9596
"path": fmt.Sprintf("%s/%s", PathConfigStorage, name),
9697
})
9798
b.SetClient(nil, name)
@@ -146,7 +147,7 @@ func (b *Backend) pathConfigPatch(ctx context.Context, req *logical.Request, dat
146147
defer b.lockClientMutex.Unlock()
147148
if err = saveConfig(ctx, *config, req.Storage); err == nil {
148149
lrd := config.LogicalResponseData(b.flags.ShowConfigToken)
149-
Event(ctx, b.Backend, "config-patch", changes)
150+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-patch", changes)
150151
b.SetClient(nil, name)
151152
b.Logger().Debug("Patched config", "lrd", lrd, "warnings", warnings)
152153
lResp = &logical.Response{Data: lrd, Warnings: warnings}
@@ -205,7 +206,7 @@ func (b *Backend) pathConfigWrite(ctx context.Context, req *logical.Request, dat
205206
var lResp *logical.Response
206207

207208
if err = saveConfig(ctx, *config, req.Storage); err == nil {
208-
Event(ctx, b.Backend, "config-write", map[string]string{
209+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-write", map[string]string{
209210
"path": fmt.Sprintf("%s/%s", PathConfigStorage, name),
210211
"auto_rotate_token": strconv.FormatBool(config.AutoRotateToken),
211212
"auto_rotate_before": config.AutoRotateBefore.String(),

path_config_rotate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/vault/sdk/logical"
1313

1414
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs"
15+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
1516
)
1617

1718
const pathConfigRotateHelpSynopsis = `Rotate the gitlab token for this configuration.`
@@ -105,7 +106,7 @@ func (b *Backend) pathConfigTokenRotate(ctx context.Context, request *logical.Re
105106

106107
lResp = &logical.Response{Data: config.LogicalResponseData(b.flags.ShowConfigToken)}
107108
lResp.Data["token"] = config.Token
108-
Event(ctx, b.Backend, "config-token-rotate", map[string]string{
109+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-token-rotate", map[string]string{
109110
"path": fmt.Sprintf("%s/%s", PathConfigStorage, name),
110111
"expires_at": entryToken.ExpiresAt.Format(time.RFC3339),
111112
"created_at": entryToken.CreatedAt.Format(time.RFC3339),

path_flags.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/hashicorp/vault/sdk/framework"
1010
"github.com/hashicorp/vault/sdk/logical"
1111
"github.com/mitchellh/mapstructure"
12+
13+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
1214
)
1315

1416
const (
@@ -43,7 +45,7 @@ func (b *Backend) pathFlagsUpdate(ctx context.Context, req *logical.Request, dat
4345
eventData["show_config_token"] = strconv.FormatBool(b.flags.ShowConfigToken)
4446
}
4547

46-
Event(ctx, b.Backend, "flags-write", eventData)
48+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "flags-write", eventData)
4749

4850
var flagData map[string]any
4951
err = mapstructure.Decode(b.flags, &flagData)

path_role.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hashicorp/vault/sdk/logical"
1717

1818
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs"
19+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
1920
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/gitlab"
2021
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token"
2122
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/utils"
@@ -166,7 +167,7 @@ func (b *Backend) pathRolesDelete(ctx context.Context, req *logical.Request, dat
166167
return nil, fmt.Errorf("error deleting role: %w", err)
167168
}
168169

169-
Event(ctx, b.Backend, "role-delete", map[string]string{
170+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "role-delete", map[string]string{
170171
"path": "roles",
171172
"role_name": roleName,
172173
})
@@ -370,7 +371,7 @@ func (b *Backend) pathRolesWrite(ctx context.Context, req *logical.Request, data
370371
return nil, err
371372
}
372373

373-
Event(ctx, b.Backend, "role-write", map[string]string{
374+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "role-write", map[string]string{
374375
"path": "roles",
375376
"role_name": roleName,
376377
"config_name": role.ConfigName,

path_token_role.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/hashicorp/vault/sdk/logical"
1414

1515
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs"
16+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
1617
token2 "github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token"
1718
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/utils"
1819
)
@@ -155,8 +156,8 @@ func (b *Backend) pathTokenRoleCreate(ctx context.Context, req *logical.Request,
155156
resp.Secret.TTL = token.TTL()
156157
}
157158

158-
Event(
159-
ctx, b.Backend, "token-write",
159+
event.Event(
160+
ctx, b.Backend, operationPrefixGitlabAccessTokens, "token-write",
160161
token.Event(map[string]string{"path": fmt.Sprintf("%s/%s", PathRoleStorage, roleName)}),
161162
)
162163
return resp, nil

secret_access_tokens.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/vault/sdk/logical"
1111

1212
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs"
13+
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/event"
1314
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/token"
1415
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/utils"
1516
)
@@ -127,7 +128,7 @@ func (b *Backend) secretAccessTokenRevoke(ctx context.Context, req *logical.Requ
127128
}
128129
}
129130

130-
Event(ctx, b.Backend, "token-revoke", map[string]string{
131+
event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "token-revoke", map[string]string{
131132
"lease_id": secret.LeaseID,
132133
"path": req.Secret.InternalData["path"].(string),
133134
"name": req.Secret.InternalData["name"].(string),

0 commit comments

Comments
 (0)