From 04b87a37467f5f872a8ebdfae9a7cf5f1c8e9e50 Mon Sep 17 00:00:00 2001 From: itayherzberg Date: Sun, 25 Oct 2026 19:57:55 +0200 Subject: [PATCH 1/2] fix(logger): do not log the image URL of a virtual media insert The image URL handed to InsertMedia carries the image service token in its query string, and it was written to the log at INFO on every insert, plus the DEBUG lines of the handler, the task manager and the worker. The parse error of an unparsable URL repeated the input as well. RedactURL keeps scheme, host and path and drops userinfo, query and fragment; an input that does not parse is replaced entirely. Every log line that carried the URL goes through it, and the parse error keeps only the reason. Signed-off-by: itayherzberg --- pkg/kubevirt/client.go | 21 +++++---- pkg/kubevirt/log_redaction_test.go | 68 ++++++++++++++++++++++++++++++ pkg/logger/redact.go | 16 +++++++ pkg/logger/redact_test.go | 27 ++++++++++++ pkg/server/log_redaction_test.go | 29 +++++++++++++ pkg/server/task_manager.go | 4 +- 6 files changed, 155 insertions(+), 10 deletions(-) create mode 100644 pkg/kubevirt/log_redaction_test.go create mode 100644 pkg/logger/redact.go create mode 100644 pkg/logger/redact_test.go create mode 100644 pkg/server/log_redaction_test.go diff --git a/pkg/kubevirt/client.go b/pkg/kubevirt/client.go index 26ad6c2..5c04928 100644 --- a/pkg/kubevirt/client.go +++ b/pkg/kubevirt/client.go @@ -1573,7 +1573,7 @@ func (c *Client) IsVirtualMediaInserted(namespace, name, mediaID string) (bool, // downloadISO downloads an ISO file to a temporary directory func (c *Client) downloadISO(imageURL string) (string, error) { - logger.Info("Downloading ISO from %s to temporary directory", imageURL) + logger.Info("Downloading ISO from %s to temporary directory", logger.RedactURL(imageURL)) // Create temp directory tempDir, err := os.MkdirTemp("/tmp", "kubevirt-redfish-iso-*") @@ -1681,8 +1681,8 @@ func (c *Client) CheckInsertedMedia(namespace, name, mediaID, imageURL string) ( // InsertVirtualMedia inserts virtual media into a VirtualMachine func (c *Client) InsertVirtualMedia(namespace, name, mediaID, imageURL string) error { - logger.Info("Inserting virtual media %s with image %s for VM %s/%s", mediaID, imageURL, namespace, name) - logger.Debug("InsertVirtualMedia called - namespace=%s, name=%s, mediaID=%s, imageURL=%s", namespace, name, mediaID, imageURL) + logger.Info("Inserting virtual media %s with image %s for VM %s/%s", mediaID, logger.RedactURL(imageURL), namespace, name) + logger.Debug("InsertVirtualMedia called - namespace=%s, name=%s, mediaID=%s, imageURL=%s", namespace, name, mediaID, logger.RedactURL(imageURL)) // Perform the actual insertion work logger.Debug("Calling insertVirtualMediaAsync for VM %s/%s", namespace, name) @@ -1698,7 +1698,7 @@ func (c *Client) InsertVirtualMedia(namespace, name, mediaID, imageURL string) e // insertVirtualMediaAsync performs the actual virtual media insertion work func (c *Client) insertVirtualMediaAsync(namespace, name, mediaID, imageURL string) error { - logger.Debug("insertVirtualMediaAsync called - namespace=%s, name=%s, mediaID=%s, imageURL=%s", namespace, name, mediaID, imageURL) + logger.Debug("insertVirtualMediaAsync called - namespace=%s, name=%s, mediaID=%s, imageURL=%s", namespace, name, mediaID, logger.RedactURL(imageURL)) // Get DataVolume configuration first to determine timeouts storageSize, allowInsecureTLS, storageClass, vmUpdateTimeout, isoDownloadTimeout, helperImage := c.getDataVolumeConfig() @@ -1717,14 +1717,19 @@ func (c *Client) insertVirtualMediaAsync(namespace, name, mediaID, imageURL stri ctx, cancel := context.WithTimeout(context.Background(), vmUpdateDuration) defer cancel() - logger.Info("Inserting virtual media %s with image %s for VM %s/%s", mediaID, imageURL, namespace, name) + logger.Info("Inserting virtual media %s with image %s for VM %s/%s", mediaID, logger.RedactURL(imageURL), namespace, name) logger.Debug("Starting virtual media insertion process for VM %s/%s", namespace, name) // Parse URL to determine scheme u, parseErr := url.Parse(imageURL) if parseErr != nil { - logger.Error("Failed to parse URL %s: %v", imageURL, parseErr) - return fmt.Errorf("failed to parse URL %s: %w", imageURL, parseErr) + // A *url.Error repeats the input, and the error ends up in the log and + // in the task message; keep only the reason. + if urlErr, ok := parseErr.(*url.Error); ok { + parseErr = urlErr.Err + } + logger.Error("Failed to parse image URL %s: %v", logger.RedactURL(imageURL), parseErr) + return fmt.Errorf("failed to parse image URL %s: %w", logger.RedactURL(imageURL), parseErr) } logger.Debug("Parsed URL - scheme=%s, host=%s", u.Scheme, u.Host) @@ -2019,7 +2024,7 @@ func (c *Client) insertVirtualMediaAsync(namespace, name, mediaID, imageURL stri // The pod spec is built based on the PVC's volume mode: Block uses dd to a raw device, // Filesystem uses a direct curl download to a mounted path. func (c *Client) copyISOToPVC(namespace, dataVolumeName, imageURL, isoDownloadTimeout, vmName, deviceName string) error { - logger.Info("Copying ISO from %s to PVC for DataVolume %s", imageURL, dataVolumeName) + logger.Info("Copying ISO from %s to PVC for DataVolume %s", logger.RedactURL(imageURL), dataVolumeName) _, _, _, _, configISODownloadTimeout, helperImage := c.getDataVolumeConfig() diff --git a/pkg/kubevirt/log_redaction_test.go b/pkg/kubevirt/log_redaction_test.go new file mode 100644 index 0000000..c033725 --- /dev/null +++ b/pkg/kubevirt/log_redaction_test.go @@ -0,0 +1,68 @@ +package kubevirt + +import ( + "bytes" + "log" + "strings" + "testing" + + "github.com/kubevirt/redfish-controller/pkg/logger" +) + +// captureLog routes the package logger to a buffer at DEBUG level for the +// duration of the test, so a log line that reintroduces the full URL fails it. +func captureLog(t *testing.T) *bytes.Buffer { + t.Helper() + var buf bytes.Buffer + logger.Init("DEBUG") + log.SetOutput(&buf) + t.Cleanup(func() { + log.SetOutput(log.Writer()) + logger.Init("INFO") + }) + return &buf +} + +const ( + redactedISOURL = "http://example.com/images/abc/full.iso" + secretISOURL = redactedISOURL + "?api_key=SECRET-TOKEN" +) + +func TestInsertVirtualMedia_DoesNotLogTheImageToken(t *testing.T) { + client, _, _ := setupInsertVirtualMediaTest(t, false) + buf := captureLog(t) + + if err := client.InsertVirtualMedia("test-ns", "test-vm", "cdrom0", secretISOURL); err != nil { + t.Fatalf("InsertVirtualMedia failed: %v", err) + } + + out := buf.String() + if strings.Contains(out, "SECRET-TOKEN") { + t.Errorf("the image token was logged:\n%s", linesContaining(out, "SECRET-TOKEN")) + } + if !strings.Contains(out, redactedISOURL) { + t.Errorf("the redacted image URL was not logged at all:\n%s", out) + } +} + +func TestInsertVirtualMedia_UnparsableURLIsNotLogged(t *testing.T) { + client, _, _ := setupInsertVirtualMediaTest(t, false) + buf := captureLog(t) + + if err := client.InsertVirtualMedia("test-ns", "test-vm", "cdrom0", "://SECRET-TOKEN"); err == nil { + t.Fatal("expected an error for an unparsable URL") + } + if out := buf.String(); strings.Contains(out, "SECRET-TOKEN") { + t.Errorf("the unparsable URL was logged:\n%s", linesContaining(out, "SECRET-TOKEN")) + } +} + +func linesContaining(s, needle string) string { + var hits []string + for _, line := range strings.Split(s, "\n") { + if strings.Contains(line, needle) { + hits = append(hits, line) + } + } + return strings.Join(hits, "\n") +} diff --git a/pkg/logger/redact.go b/pkg/logger/redact.go new file mode 100644 index 0000000..c4fbd4a --- /dev/null +++ b/pkg/logger/redact.go @@ -0,0 +1,16 @@ +package logger + +import "net/url" + +// RedactURL returns a form of a URL that is safe to log: scheme, host and +// path only. Image URLs handed to the bridge carry credentials in the query +// string (an image service token) or in the userinfo part, and the log files +// are read by people who must not get them. An input that does not parse is +// replaced entirely, since the reason it does not parse may be the secret. +func RedactURL(raw string) string { + u, err := url.Parse(raw) + if err != nil || u.Scheme == "" && u.Host == "" { + return "" + } + return (&url.URL{Scheme: u.Scheme, Host: u.Host, Path: u.Path}).String() +} diff --git a/pkg/logger/redact_test.go b/pkg/logger/redact_test.go new file mode 100644 index 0000000..3992fbf --- /dev/null +++ b/pkg/logger/redact_test.go @@ -0,0 +1,27 @@ +package logger + +import "testing" + +func TestRedactURL(t *testing.T) { + tests := []struct { + name string + in string + want string + }{ + {"query string dropped", "https://images.example.com/images/abc/full.iso?api_key=SECRET&arch=x86_64", "https://images.example.com/images/abc/full.iso"}, + {"userinfo dropped", "https://user:SECRET@host.example.com/boot.iso", "https://host.example.com/boot.iso"}, + {"fragment dropped", "http://host/boot.iso#SECRET", "http://host/boot.iso"}, + {"port kept", "http://slow-iso.ns.svc:8080/alpine.iso", "http://slow-iso.ns.svc:8080/alpine.iso"}, + {"plain url unchanged", "http://example.com/image.iso", "http://example.com/image.iso"}, + {"unparsable", "://not-a-url?token=SECRET", ""}, + {"empty", "", ""}, + {"no scheme or host", "SECRET", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := RedactURL(tt.in); got != tt.want { + t.Errorf("RedactURL(%q) = %q, want %q", tt.in, got, tt.want) + } + }) + } +} diff --git a/pkg/server/log_redaction_test.go b/pkg/server/log_redaction_test.go new file mode 100644 index 0000000..41ae5d4 --- /dev/null +++ b/pkg/server/log_redaction_test.go @@ -0,0 +1,29 @@ +package server + +import ( + "bytes" + "log" + "strings" + "testing" + + "github.com/kubevirt/redfish-controller/pkg/logger" +) + +func TestTaskManager_DoesNotLogTheImageToken(t *testing.T) { + var buf bytes.Buffer + logger.Init("DEBUG") + log.SetOutput(&buf) + t.Cleanup(func() { + log.SetOutput(log.Writer()) + logger.Init("INFO") + }) + + tm := NewTaskManager(0, nil) + t.Cleanup(tm.Stop) + + tm.CreateTask("insert", "ns", "vm", "cdrom0", "http://example.com/full.iso?api_key=SECRET-TOKEN") + + if out := buf.String(); strings.Contains(out, "SECRET-TOKEN") { + t.Errorf("the image token was logged:\n%s", out) + } +} diff --git a/pkg/server/task_manager.go b/pkg/server/task_manager.go index 026248c..7ae753b 100644 --- a/pkg/server/task_manager.go +++ b/pkg/server/task_manager.go @@ -303,7 +303,7 @@ func (w *Worker) processVirtualMediaInsert(job *Job) error { imageURL := payload["imageURL"] logger.Debug("Worker %d processing virtual media insert - namespace=%s, vmName=%s, mediaID=%s, imageURL=%s", - w.ID, namespace, vmName, mediaID, imageURL) + w.ID, namespace, vmName, mediaID, logger.RedactURL(imageURL)) // Update progress logger.Debug("Worker %d updating task progress to 'Starting virtual media insertion'", w.ID) @@ -515,7 +515,7 @@ func (tm *TaskManager) getAvailableWorker() *Worker { // CreateTask creates a new task and queues it for processing func (tm *TaskManager) CreateTask(name, namespace, vmName, mediaID, imageURL string) string { logger.Debug("Creating task for virtual media insertion - name=%s, namespace=%s, vmName=%s, mediaID=%s, imageURL=%s", - name, namespace, vmName, mediaID, imageURL) + name, namespace, vmName, mediaID, logger.RedactURL(imageURL)) tm.taskMutex.Lock() defer tm.taskMutex.Unlock() From 9f98767212f515370f816a56b88b1bb463d2debb Mon Sep 17 00:00:00 2001 From: itayherzberg Date: Thu, 12 Nov 2026 18:36:37 +0200 Subject: [PATCH 2/2] fix(logger): mark the redacted pieces of a logged image URL A URL that was silently shortened looks complete, and someone debugging from the log may try it as is. RedactURL now leaves a marker in place of each removed piece, only when that piece was present: https://@host/path?# The username is redacted together with the password, and the query string as a whole, without keeping the parameter names. A URL without those pieces is logged unchanged; an input that does not parse is still replaced entirely. Signed-off-by: itayherzberg --- pkg/kubevirt/log_redaction_test.go | 5 +++-- pkg/logger/redact.go | 29 ++++++++++++++++++++++++----- pkg/logger/redact_test.go | 8 +++++--- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/pkg/kubevirt/log_redaction_test.go b/pkg/kubevirt/log_redaction_test.go index c033725..69a0322 100644 --- a/pkg/kubevirt/log_redaction_test.go +++ b/pkg/kubevirt/log_redaction_test.go @@ -24,8 +24,9 @@ func captureLog(t *testing.T) *bytes.Buffer { } const ( - redactedISOURL = "http://example.com/images/abc/full.iso" - secretISOURL = redactedISOURL + "?api_key=SECRET-TOKEN" + plainISOURL = "http://example.com/images/abc/full.iso" + secretISOURL = plainISOURL + "?api_key=SECRET-TOKEN" + redactedISOURL = plainISOURL + "?" ) func TestInsertVirtualMedia_DoesNotLogTheImageToken(t *testing.T) { diff --git a/pkg/logger/redact.go b/pkg/logger/redact.go index c4fbd4a..e037b38 100644 --- a/pkg/logger/redact.go +++ b/pkg/logger/redact.go @@ -1,16 +1,35 @@ package logger -import "net/url" +import ( + "net/url" + "strings" +) + +const redactedMark = "" // RedactURL returns a form of a URL that is safe to log: scheme, host and -// path only. Image URLs handed to the bridge carry credentials in the query -// string (an image service token) or in the userinfo part, and the log files -// are read by people who must not get them. An input that does not parse is +// path, with a marker in place of each removed piece (userinfo, query string, +// fragment) so the reader sees the URL is incomplete and does not try it as +// is. Image URLs handed to the bridge carry credentials in the query string +// (an image service token) or in the userinfo part, and the log files are +// read by people who must not get them. An input that does not parse is // replaced entirely, since the reason it does not parse may be the secret. func RedactURL(raw string) string { u, err := url.Parse(raw) if err != nil || u.Scheme == "" && u.Host == "" { return "" } - return (&url.URL{Scheme: u.Scheme, Host: u.Host, Path: u.Path}).String() + + // The markers are added as text: url.URL would percent-encode them. + redacted := (&url.URL{Scheme: u.Scheme, Host: u.Host, Path: u.Path}).String() + if u.User != nil { + redacted = strings.Replace(redacted, "//", "//"+redactedMark+"@", 1) + } + if u.RawQuery != "" { + redacted += "?" + redactedMark + } + if u.Fragment != "" { + redacted += "#" + redactedMark + } + return redacted } diff --git a/pkg/logger/redact_test.go b/pkg/logger/redact_test.go index 3992fbf..67a4c61 100644 --- a/pkg/logger/redact_test.go +++ b/pkg/logger/redact_test.go @@ -8,9 +8,11 @@ func TestRedactURL(t *testing.T) { in string want string }{ - {"query string dropped", "https://images.example.com/images/abc/full.iso?api_key=SECRET&arch=x86_64", "https://images.example.com/images/abc/full.iso"}, - {"userinfo dropped", "https://user:SECRET@host.example.com/boot.iso", "https://host.example.com/boot.iso"}, - {"fragment dropped", "http://host/boot.iso#SECRET", "http://host/boot.iso"}, + {"query string marked", "https://images.example.com/images/abc/full.iso?api_key=SECRET&arch=x86_64", "https://images.example.com/images/abc/full.iso?"}, + {"username and password marked", "https://user:SECRET@host.example.com/boot.iso", "https://@host.example.com/boot.iso"}, + {"username alone marked", "https://SECRET@host.example.com/boot.iso", "https://@host.example.com/boot.iso"}, + {"fragment marked", "http://host/boot.iso#SECRET", "http://host/boot.iso#"}, + {"every removed piece marked", "https://user:SECRET@host:8443/boot.iso?token=SECRET#SECRET", "https://@host:8443/boot.iso?#"}, {"port kept", "http://slow-iso.ns.svc:8080/alpine.iso", "http://slow-iso.ns.svc:8080/alpine.iso"}, {"plain url unchanged", "http://example.com/image.iso", "http://example.com/image.iso"}, {"unparsable", "://not-a-url?token=SECRET", ""},