Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSS/instant-updates: Improve filtering of kv-v2 events #926

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions controllers/vaultstaticsecret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -436,6 +437,16 @@ func (r *VaultStaticSecretReconciler) streamStaticSecretEvents(ctx context.Conte
// status
r.Recorder.Eventf(o, corev1.EventTypeNormal, consts.ReasonEventWatcherStarted, "Started watching events")

specPathPattern := strings.Join([]string{o.Spec.Mount, o.Spec.Path}, "/")
if o.Spec.Type == consts.KVSecretTypeV2 {
specPathPattern = strings.Join([]string{o.Spec.Mount, "*", o.Spec.Path}, "/")
}

specNamespace := strings.Trim(o.Spec.Namespace, "/")
if o.Spec.Namespace == "" {
specNamespace = strings.Trim(wsClient.Namespace(), "/")
}

for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -464,15 +475,16 @@ func (r *VaultStaticSecretReconciler) streamStaticSecretEvents(ctx context.Conte
if modified {
namespace := strings.Trim(messageMap.Data.Namespace, "/")
path := messageMap.Data.Event.Metadata.Path
specPath := strings.Join([]string{o.Spec.Mount, o.Spec.Path}, "/")

if o.Spec.Type == consts.KVSecretTypeV2 {
specPath = strings.Join([]string{o.Spec.Mount, "data", o.Spec.Path}, "/")
}
logger.V(consts.LogLevelTrace).Info("modified Event received from Vault",
"namespace", namespace, "path", path, "spec.namespace", o.Spec.Namespace,
"spec path", specPath)
if namespace == o.Spec.Namespace && path == specPath {
"namespace", namespace, "path", path, "spec.namespace", specNamespace,
"spec.path", specPathPattern)

pathMatched, err := filepath.Match(specPathPattern, path)
if err != nil {
return fmt.Errorf("failed to match secret paht: %w", err)
}
if namespace == specNamespace && pathMatched {
logger.V(consts.LogLevelDebug).Info("Event matches, sending requeue",
"namespace", namespace, "path", path)
r.SourceCh <- event.GenericEvent{
Expand Down
6 changes: 6 additions & 0 deletions vault/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,9 @@ func (w *WebsocketClient) Connect(ctx context.Context) (*websocket.Conn, error)

return conn, nil
}

// Namespace returns the namespace associated with the client.
// If no namespace is set, an empty string is returned.
func (w *WebsocketClient) Namespace() string {
return w.Headers.Get(api.NamespaceHeaderName)
}
Loading