Skip to content
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
21 changes: 13 additions & 8 deletions pkg/kubevirt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-*")
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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()

Expand Down
69 changes: 69 additions & 0 deletions pkg/kubevirt/log_redaction_test.go
Original file line number Diff line number Diff line change
@@ -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 + "?<redacted>"
)

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")
}
35 changes: 35 additions & 0 deletions pkg/logger/redact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package logger

import (
"net/url"
"strings"
)

const redactedMark = "<redacted>"

// 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 "<unparsable url>"
}

// 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
}
29 changes: 29 additions & 0 deletions pkg/logger/redact_test.go
Original file line number Diff line number Diff line change
@@ -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?<redacted>"},
{"username and password marked", "https://user:SECRET@host.example.com/boot.iso", "https://<redacted>@host.example.com/boot.iso"},
{"username alone marked", "https://SECRET@host.example.com/boot.iso", "https://<redacted>@host.example.com/boot.iso"},
{"fragment marked", "http://host/boot.iso#SECRET", "http://host/boot.iso#<redacted>"},
{"every removed piece marked", "https://user:SECRET@host:8443/boot.iso?token=SECRET#SECRET", "https://<redacted>@host:8443/boot.iso?<redacted>#<redacted>"},
{"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", "<unparsable url>"},
{"empty", "", "<unparsable url>"},
{"no scheme or host", "SECRET", "<unparsable url>"},
}
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)
}
})
}
}
29 changes: 29 additions & 0 deletions pkg/server/log_redaction_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions pkg/server/task_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down