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

fixes: panic in case of zap logger init on windows os #1283

Merged
merged 2 commits into from
Jun 6, 2022
Merged
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
4 changes: 4 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ archives:
windows: Windows
386: i386
amd64: x86_64
# for windows it is good to have zip rather than tar.gz
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
52 changes: 48 additions & 4 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@
package logging

import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"

"github.com/tenable/terrascan/pkg/utils"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

const logFileName = "terrascan.log"
const (
logFileName = "terrascan.log"
terrascanZapSink = "terrascan-winfile-sink"
terrascanZapSinkWithSeparator = terrascanZapSink + ":///"
)

var globalLogger *zap.SugaredLogger

Expand Down Expand Up @@ -86,11 +95,20 @@ func GetLogger(logLevel, encoding, logDir string, encodingLevel func(zapcore.Lev
}

if logDir != "" {
zapConfig.OutputPaths = append(zapConfig.OutputPaths, filepath.Join(logDir, logFileName))
logDirPath := filepath.Join(logDir, logFileName)
// currently zap's default file sink do not support handling of windows files
// so if we are on windows os register an sink which will handle the opening of file.
if utils.IsWindowsPlatform() {
zap.RegisterSink(terrascanZapSink, newTerrascanWinFileSink)
logDirPath = terrascanZapSinkWithSeparator + logDirPath
}
zapConfig.OutputPaths = append(zapConfig.OutputPaths, logDirPath)
}

// create zap logger
logger, _ := zapConfig.Build()
logger, err := zapConfig.Build()
if err != nil {
log.Fatal("failed to initialize logger %w", err)
}

return logger
}
Expand All @@ -99,3 +117,29 @@ func GetLogger(logLevel, encoding, logDir string, encodingLevel func(zapcore.Lev
func GetDefaultLogger() *zap.SugaredLogger {
return globalLogger
}

// newTerrascanWinFileSink custom sink to open file on windows
// https://github.com/uber-go/zap/issues/621#issue-350197709 - referred from this issue comment
func newTerrascanWinFileSink(u *url.URL) (zap.Sink, error) {
// copy pasting this error from default FileSink of zap to keep the standard behaviour across all platforms
// newFileSink()
if u.User != nil {
return nil, fmt.Errorf("user and password not allowed with file URLs: got %v", u)
}
if u.Fragment != "" {
return nil, fmt.Errorf("fragments not allowed with file URLs: got %v", u)
}
if u.RawQuery != "" {
return nil, fmt.Errorf("query parameters not allowed with file URLs: got %v", u)
}
// Error messages are better if we check hostname and port separately.
if u.Port() != "" {
return nil, fmt.Errorf("ports not allowed with file URLs: got %v", u)
}
if hn := u.Hostname(); hn != "" && hn != "localhost" {
return nil, fmt.Errorf("file URLs must leave host empty or use localhost: got %v", u)
}
// after url.Parse() u.Path contains and extra leading slash
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
}