Skip to content

Commit 6289582

Browse files
committed
refactor: remove prefix from event function and replace it with a constant
1 parent 2e1c09c commit 6289582

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

internal/event/event.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"github.com/ilijamt/vault-plugin-secrets-gitlab/internal/errs"
1313
)
1414

15-
func Event(ctx context.Context, b *framework.Backend, prefix, eventType string, metadata map[string]string) error {
15+
const Prefix = "gitlab"
16+
17+
func Event(ctx context.Context, b *framework.Backend, eventType string, metadata map[string]string) error {
1618
var err error
1719
var ev *logical.EventData
1820
if b == nil {
@@ -23,7 +25,7 @@ func Event(ctx context.Context, b *framework.Backend, prefix, eventType string,
2325
metadataBytes, _ = json.Marshal(metadata)
2426
ev.Metadata = &structpb.Struct{}
2527
_ = ev.Metadata.UnmarshalJSON(metadataBytes)
26-
err = b.SendEvent(ctx, logical.EventType(fmt.Sprintf("%s/%s", prefix, eventType)), ev)
28+
err = b.SendEvent(ctx, logical.EventType(fmt.Sprintf("%s/%s", Prefix, eventType)), ev)
2729
}
2830
return err
2931
}

internal/event/event_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestEvent(t *testing.T) {
3939
event.Event(
4040
t.Context(),
4141
nil,
42-
"test", "test",
42+
"test",
4343
map[string]string{"test": "test"},
4444
),
4545
errs.ErrNilValue,
@@ -53,7 +53,7 @@ func TestEvent(t *testing.T) {
5353
event.Event(
5454
t.Context(),
5555
&framework.Backend{},
56-
"test", "test",
56+
"test",
5757
map[string]string{"test": "test"},
5858
),
5959
framework.ErrNoEvents,
@@ -67,7 +67,7 @@ func TestEvent(t *testing.T) {
6767
require.NoError(t,
6868
event.Event(
6969
t.Context(), b,
70-
"test", "test",
70+
"test",
7171
map[string]string{"test": "test"},
7272
),
7373
)

path_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (b *Backend) pathConfigDelete(ctx context.Context, req *logical.Request, da
9595
}
9696

9797
if err = req.Storage.Delete(ctx, fmt.Sprintf("%s/%s", PathConfigStorage, name)); err == nil {
98-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-delete", map[string]string{
98+
_ = event.Event(ctx, b.Backend, "config-delete", map[string]string{
9999
"path": fmt.Sprintf("%s/%s", PathConfigStorage, name),
100100
})
101101
b.SetClient(nil, name)
@@ -150,7 +150,7 @@ func (b *Backend) pathConfigPatch(ctx context.Context, req *logical.Request, dat
150150
defer b.lockClientMutex.Unlock()
151151
if err = saveConfig(ctx, config, req.Storage); err == nil {
152152
lrd := config.LogicalResponseData(b.flags.ShowConfigToken)
153-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-patch", changes)
153+
_ = event.Event(ctx, b.Backend, "config-patch", changes)
154154
b.SetClient(nil, name)
155155
b.Logger().Debug("Patched config", "lrd", lrd, "warnings", warnings)
156156
lResp = &logical.Response{Data: lrd, Warnings: warnings}
@@ -209,7 +209,7 @@ func (b *Backend) pathConfigWrite(ctx context.Context, req *logical.Request, dat
209209
var lResp *logical.Response
210210

211211
if err = saveConfig(ctx, config, req.Storage); err == nil {
212-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-write", map[string]string{
212+
_ = event.Event(ctx, b.Backend, "config-write", map[string]string{
213213
"path": fmt.Sprintf("%s/%s", PathConfigStorage, name),
214214
"auto_rotate_token": strconv.FormatBool(config.AutoRotateToken),
215215
"auto_rotate_before": config.AutoRotateBefore.String(),

path_config_rotate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (b *Backend) pathConfigTokenRotate(ctx context.Context, request *logical.Re
108108

109109
lResp = &logical.Response{Data: config.LogicalResponseData(b.flags.ShowConfigToken)}
110110
lResp.Data["token"] = config.Token
111-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "config-token-rotate", map[string]string{
111+
_ = event.Event(ctx, b.Backend, "config-token-rotate", map[string]string{
112112
"path": fmt.Sprintf("%s/%s", PathConfigStorage, name),
113113
"expires_at": entryToken.ExpiresAt.Format(time.RFC3339),
114114
"created_at": entryToken.CreatedAt.Format(time.RFC3339),

path_flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (b *Backend) pathFlagsUpdate(ctx context.Context, req *logical.Request, dat
4545
eventData["show_config_token"] = strconv.FormatBool(b.flags.ShowConfigToken)
4646
}
4747

48-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "flags-write", eventData)
48+
_ = event.Event(ctx, b.Backend, "flags-write", eventData)
4949

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

path_role.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (b *Backend) pathRolesDelete(ctx context.Context, req *logical.Request, dat
168168
return nil, fmt.Errorf("error deleting role: %w", err)
169169
}
170170

171-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "role-delete", map[string]string{
171+
_ = event.Event(ctx, b.Backend, "role-delete", map[string]string{
172172
"path": "roles",
173173
"role_name": roleName,
174174
})
@@ -372,7 +372,7 @@ func (b *Backend) pathRolesWrite(ctx context.Context, req *logical.Request, data
372372
return nil, err
373373
}
374374

375-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "role-write", map[string]string{
375+
_ = event.Event(ctx, b.Backend, "role-write", map[string]string{
376376
"path": "roles",
377377
"role_name": roleName,
378378
"config_name": role.ConfigName,

path_token_role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (b *Backend) pathTokenRoleCreate(ctx context.Context, req *logical.Request,
158158
}
159159

160160
_ = event.Event(
161-
ctx, b.Backend, operationPrefixGitlabAccessTokens, "token-write",
161+
ctx, b.Backend, "token-write",
162162
token.Event(map[string]string{"path": fmt.Sprintf("%s/%s", PathRoleStorage, roleName)}),
163163
)
164164
return resp, nil

secret_access_tokens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (b *Backend) secretAccessTokenRevoke(ctx context.Context, req *logical.Requ
128128
}
129129
}
130130

131-
_ = event.Event(ctx, b.Backend, operationPrefixGitlabAccessTokens, "token-revoke", map[string]string{
131+
_ = event.Event(ctx, b.Backend, "token-revoke", map[string]string{
132132
"lease_id": secret.LeaseID,
133133
"path": req.Secret.InternalData["path"].(string),
134134
"name": req.Secret.InternalData["name"].(string),

0 commit comments

Comments
 (0)