From 5330a6c50d5e9aa7b966286549c3a55a8aaec73b Mon Sep 17 00:00:00 2001 From: Nelson Parente Date: Wed, 18 Mar 2026 17:14:06 +0000 Subject: [PATCH 1/2] fix: support RSA and fix ed25519 key encoding in EncodePrivateKey (#148) * fix: support RSA key encoding in EncodePrivateKey EncodePrivateKey only handled *ecdsa.PrivateKey and ed25519.PrivateKey, rejecting RSA keys with "unsupported key type". Since RSA keys also use x509.MarshalPKCS8PrivateKey with the same PKCS#8 block type, add *rsa.PrivateKey to the existing case list. Adds table-driven tests covering RSA-2048, RSA-4096, ECDSA P-256, Ed25519, and unsupported type with roundtrip verification through DecodePEMPrivateKey. Signed-off-by: Nelson Parente * fix: address review feedback - Drop RSA-4096 test case to avoid slow CI (2048 is sufficient coverage) - Replace custom containsSubstr/searchSubstr with strings.Contains - Remove unused helper functions Signed-off-by: Nelson Parente --------- Signed-off-by: Nelson Parente --- crypto/pem/pem.go | 2 +- crypto/pem/pem_test.go | 133 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 crypto/pem/pem_test.go diff --git a/crypto/pem/pem.go b/crypto/pem/pem.go index 0b82f7f..5be6f56 100644 --- a/crypto/pem/pem.go +++ b/crypto/pem/pem.go @@ -115,7 +115,7 @@ func EncodePrivateKey(key any) ([]byte, error) { ) switch key := key.(type) { - case *ecdsa.PrivateKey, *ed25519.PrivateKey: + case *ecdsa.PrivateKey, ed25519.PrivateKey, *rsa.PrivateKey: keyBytes, err = x509.MarshalPKCS8PrivateKey(key) if err != nil { return nil, err diff --git a/crypto/pem/pem_test.go b/crypto/pem/pem_test.go new file mode 100644 index 0000000..49102c4 --- /dev/null +++ b/crypto/pem/pem_test.go @@ -0,0 +1,133 @@ +/* +Copyright 2023 The Dapr Authors +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pem + +import ( + "crypto" + "crypto/ecdsa" + "crypto/ed25519" + "crypto/elliptic" + "crypto/rand" + "crypto/rsa" + "strings" + "testing" +) + +func TestEncodePrivateKey(t *testing.T) { + ecKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatalf("failed to generate ECDSA key: %v", err) + } + + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatalf("failed to generate RSA key: %v", err) + } + + _, ed25519Key, err := ed25519.GenerateKey(rand.Reader) + if err != nil { + t.Fatalf("failed to generate Ed25519 key: %v", err) + } + + tests := []struct { + name string + key any + wantErr bool + errSubstr string + }{ + { + name: "ECDSA P-256", + key: ecKey, + }, + { + name: "RSA 2048", + key: rsaKey, + }, + { + name: "Ed25519", + key: ed25519Key, + }, + { + name: "unsupported type", + key: "not a key", + wantErr: true, + errSubstr: "unsupported key type", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + encoded, err := EncodePrivateKey(tt.key) + if tt.wantErr { + if err == nil { + t.Fatal("expected error, got nil") + } + if tt.errSubstr != "" && !strings.Contains(err.Error(), tt.errSubstr) { + t.Fatalf("expected error containing %q, got %q", tt.errSubstr, err.Error()) + } + return + } + + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(encoded) == 0 { + t.Fatal("encoded output is empty") + } + + decoded, err := DecodePEMPrivateKey(encoded) + if err != nil { + t.Fatalf("roundtrip decode failed: %v", err) + } + + if !keysEqual(t, tt.key, decoded) { + t.Fatal("roundtrip key does not match original") + } + }) + } +} + +// keysEqual compares the original key with the decoded signer using +// each key type's Equal method. +func keysEqual(t *testing.T, original any, decoded crypto.Signer) bool { + t.Helper() + + switch orig := original.(type) { + case *ecdsa.PrivateKey: + d, ok := decoded.(*ecdsa.PrivateKey) + if !ok { + t.Errorf("decoded key type %T, want *ecdsa.PrivateKey", decoded) + return false + } + return orig.Equal(d) + case *rsa.PrivateKey: + d, ok := decoded.(*rsa.PrivateKey) + if !ok { + t.Errorf("decoded key type %T, want *rsa.PrivateKey", decoded) + return false + } + return orig.Equal(d) + case ed25519.PrivateKey: + d, ok := decoded.(ed25519.PrivateKey) + if !ok { + t.Errorf("decoded key type %T, want ed25519.PrivateKey", decoded) + return false + } + return orig.Equal(d) + default: + t.Errorf("unknown key type %T", original) + return false + } +} From 5657cd4afdc9464bb06b8d6c0e117e314a6e169b Mon Sep 17 00:00:00 2001 From: Mirel <15373565+MyMirelHub@users.noreply.github.com> Date: Thu, 9 Apr 2026 13:09:05 +0200 Subject: [PATCH 2/2] feat: add OutputFile option for log destination in logger (#152) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add OutputFile option for log destination in logger Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com> * feat: add file output support for logging Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com> * lint Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com> * add cleanup for file output logger in options tests Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com> * fix: address review feedback for log file output - Relax file permissions from 0600 to 0644 to allow reads (per Albert's review) - Track open log file handle in package-level state protected by mutex to prevent FD leaks when ApplyOptionsToLoggers is called multiple times - Close previous log file before opening a new one on re-apply - Revert output to stdout when OutputFile is empty (handles config reload) - Extract setLogOutput helper for clarity - Add TestApplyOptionsToLoggersFileOutputReapply test verifying re-apply closes the previous file and switches to the new one Signed-off-by: Nelson Parente * fix: improve error handling and output file management in logging Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com> * fix: address Copilot review — atomic file swap and early test cleanup - Open new log file before closing the old one so loggers are never left pointing at a closed FD if OpenFile fails - Register t.Cleanup immediately after ApplyOptionsToLoggers to prevent FD leaks on assertion failures Signed-off-by: Nelson Parente --------- Signed-off-by: MyMirelHub <15373565+MyMirelHub@users.noreply.github.com> Signed-off-by: Nelson Parente Co-authored-by: Nelson Parente --- logger/options.go | 62 +++++++++++++++++++++++++++++++++ logger/options_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/logger/options.go b/logger/options.go index 62e576e..663731a 100644 --- a/logger/options.go +++ b/logger/options.go @@ -15,6 +15,9 @@ package logger import ( "fmt" + "io" + "os" + "sync" ) const ( @@ -23,6 +26,12 @@ const ( undefinedAppID = "" ) +var ( + // logOutputMu protects logOutputFile from concurrent access. + logOutputMu sync.Mutex + logOutputFile *os.File +) + // Options defines the sets of options for Dapr logging. type Options struct { // appID is the unique id of Dapr Application @@ -33,6 +42,9 @@ type Options struct { // OutputLevel is the level of logging OutputLevel string + + // OutputFile is the destination file path for logs. + OutputFile string } // SetOutputLevel sets the log output level. @@ -60,6 +72,11 @@ func (o *Options) AttachCmdFlags( "log-level", defaultOutputLevel, "Options are debug, info, warn, error, or fatal (default info)") + stringVar( + &o.OutputFile, + "log-file", + "", + "Path to a file where logs will be written") } if boolVar != nil { boolVar( @@ -76,6 +93,7 @@ func DefaultOptions() Options { JSONFormatEnabled: defaultJSONOutput, appID: undefinedAppID, OutputLevel: defaultOutputLevel, + OutputFile: "", } } @@ -100,5 +118,49 @@ func ApplyOptionsToLoggers(options *Options) error { for _, v := range internalLoggers { v.SetOutputLevel(daprLogLevel) } + + if err := setLogOutput(options.OutputFile, internalLoggers); err != nil { + return err + } + + return nil +} + +// setLogOutput configures log output destination. If path is non-empty, logs +// are written to the file at that path. If empty, output reverts to stdout. +// The new file is opened before closing the previous one so that loggers are +// never left pointing at a closed file descriptor. +func setLogOutput(path string, loggers map[string]Logger) error { + logOutputMu.Lock() + defer logOutputMu.Unlock() + + var ( + out io.Writer = os.Stdout + newFile *os.File + ) + + if path != "" { + var err error + + newFile, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) + if err != nil { + return fmt.Errorf("failed to open log file %q: %w", path, err) + } + + out = newFile + } + + // Switch all loggers to the new output before closing the old file. + for _, v := range loggers { + v.SetOutput(out) + } + + // Close the previous log file after loggers have been redirected. + if logOutputFile != nil { + logOutputFile.Close() + } + + logOutputFile = newFile + return nil } diff --git a/logger/options_test.go b/logger/options_test.go index e16ff89..1d4810f 100644 --- a/logger/options_test.go +++ b/logger/options_test.go @@ -14,6 +14,8 @@ limitations under the License. package logger import ( + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -26,6 +28,7 @@ func TestOptions(t *testing.T) { assert.Equal(t, defaultJSONOutput, o.JSONFormatEnabled) assert.Equal(t, undefinedAppID, o.appID) assert.Equal(t, defaultOutputLevel, o.OutputLevel) + assert.Empty(t, o.OutputFile) }) t.Run("set dapr ID", func(t *testing.T) { @@ -40,10 +43,15 @@ func TestOptions(t *testing.T) { o := DefaultOptions() logLevelAsserted := false + logFileAsserted := false testStringVarFn := func(p *string, name string, value string, usage string) { if name == "log-level" && value == defaultOutputLevel { logLevelAsserted = true } + + if name == "log-file" && value == "" { + logFileAsserted = true + } } logAsJSONAsserted := false @@ -57,6 +65,7 @@ func TestOptions(t *testing.T) { // assert assert.True(t, logLevelAsserted) + assert.True(t, logFileAsserted) assert.True(t, logAsJSONAsserted) }) } @@ -92,3 +101,72 @@ func TestApplyOptionsToLoggers(t *testing.T) { (l.(*daprLogger)).logger.Logger.GetLevel()) } } + +func TestApplyOptionsToLoggersFileOutput(t *testing.T) { + logPath := filepath.Join(t.TempDir(), "dapr.log") + + testOptions := Options{ + OutputLevel: "debug", + OutputFile: logPath, + } + + l := NewLogger("testLoggerFileOutput") + + require.NoError(t, ApplyOptionsToLoggers(&testOptions)) + t.Cleanup(func() { + // Revert to stdout, which also closes the log file. + require.NoError(t, ApplyOptionsToLoggers(&Options{ + OutputLevel: "info", + })) + }) + + dl, ok := l.(*daprLogger) + require.True(t, ok) + fileOut, ok := dl.logger.Logger.Out.(*os.File) + require.True(t, ok) + assert.Equal(t, logPath, fileOut.Name()) + + msg := "log-file-test-message" + l.Info(msg) + + b, err := os.ReadFile(logPath) + require.NoError(t, err) + assert.Contains(t, string(b), msg) +} + +func TestApplyOptionsToLoggersFileOutputReapply(t *testing.T) { + dir := t.TempDir() + logPath1 := filepath.Join(dir, "dapr1.log") + logPath2 := filepath.Join(dir, "dapr2.log") + + l := NewLogger("testLoggerReapply") + + t.Cleanup(func() { + require.NoError(t, ApplyOptionsToLoggers(&Options{ + OutputLevel: "info", + })) + }) + + // Apply first file output. + require.NoError(t, ApplyOptionsToLoggers(&Options{ + OutputLevel: "debug", + OutputFile: logPath1, + })) + l.Info("message-one") + + // Re-apply with a different file — should close the first. + require.NoError(t, ApplyOptionsToLoggers(&Options{ + OutputLevel: "debug", + OutputFile: logPath2, + })) + l.Info("message-two") + + b1, err := os.ReadFile(logPath1) + require.NoError(t, err) + assert.Contains(t, string(b1), "message-one") + assert.NotContains(t, string(b1), "message-two") + + b2, err := os.ReadFile(logPath2) + require.NoError(t, err) + assert.Contains(t, string(b2), "message-two") +}