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..69a0322 --- /dev/null +++ b/pkg/kubevirt/log_redaction_test.go @@ -0,0 +1,69 @@ +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 ( + plainISOURL = "http://example.com/images/abc/full.iso" + secretISOURL = plainISOURL + "?api_key=SECRET-TOKEN" + redactedISOURL = plainISOURL + "?" +) + +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..e037b38 --- /dev/null +++ b/pkg/logger/redact.go @@ -0,0 +1,35 @@ +package logger + +import ( + "net/url" + "strings" +) + +const redactedMark = "" + +// RedactURL returns a form of a URL that is safe to log: scheme, host and +// 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 "" + } + + // 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 new file mode 100644 index 0000000..67a4c61 --- /dev/null +++ b/pkg/logger/redact_test.go @@ -0,0 +1,29 @@ +package logger + +import "testing" + +func TestRedactURL(t *testing.T) { + tests := []struct { + name string + in string + want string + }{ + {"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", ""}, + {"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()