Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
solnicki committed Nov 26, 2024
1 parent 683f5ea commit be64e5b
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

.DS_Store
dist/
19 changes: 12 additions & 7 deletions cmd/logveil/logveil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/logmanager-oss/logveil/internal/anonymizer"
"github.com/logmanager-oss/logveil/internal/config"
"github.com/logmanager-oss/logveil/internal/files"
"github.com/logmanager-oss/logveil/internal/handlers"
"github.com/logmanager-oss/logveil/internal/proof"
"github.com/logmanager-oss/logveil/internal/reader"
"github.com/logmanager-oss/logveil/internal/writer"
Expand All @@ -25,23 +25,30 @@ func Start() {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

filesHandler := &files.FilesHandler{}
filesHandler := &handlers.Files{}
defer filesHandler.Close()

buffersHandler := &handlers.Buffers{}
defer buffersHandler.Flush()

inputReader, err := reader.CreateInputReader(config, filesHandler)
if err != nil {
slog.Error("initializing input reader", "error", err)
return
}
outputWriter, err := writer.CreateOutputWriter(config, filesHandler)
outputWriter, err := writer.CreateOutputWriter(config, filesHandler, buffersHandler)
if err != nil {
slog.Error("initializing output writer", "error", err)
return
}
proofWriter, err := proof.CreateProofWriter(config, filesHandler)
proofWriter, err := proof.CreateProofWriter(config, filesHandler, buffersHandler)
if err != nil {
slog.Error("initializing proof writer", "error", err)
return
}
anonymizerDoer, err := anonymizer.CreateAnonymizer(config, proofWriter)
if err != nil {
slog.Error("initializing anonymizer", "error", err)
return
}

Expand All @@ -55,8 +62,6 @@ func Start() {
}

func RunAnonymizationLoop(inputReader reader.InputReader, outputWriter *bufio.Writer, anonymizerDoer *anonymizer.Anonymizer) error {
defer outputWriter.Flush()

for {
logLine, err := inputReader.ReadLine()
if err != nil {
Expand All @@ -70,7 +75,7 @@ func RunAnonymizationLoop(inputReader reader.InputReader, outputWriter *bufio.Wr

_, err = fmt.Fprintln(outputWriter, anonymizedLogLine)
if err != nil {
return fmt.Errorf("writing log line to buffer: %v", err)
return fmt.Errorf("writing log line %s: %v", anonymizedLogLine, err)
}
}
}
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "github.com/logmanager-oss/logveil/cmd/logveil"
import (
"github.com/logmanager-oss/logveil/cmd/logveil"
)

func main() {
logveil.Start()
Expand Down
1 change: 1 addition & 0 deletions internal/anonymizer/anonymizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (an *Anonymizer) generateAndReplace(rawLog string, replacementMap map[strin
}

replacementMap[value] = generatedData
slog.Debug(fmt.Sprintf("Value matched via regexp. Reaplacing from %s to %s.\n", value, generatedData))
}

return replacementMap
Expand Down
8 changes: 4 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ type Config struct {

// LoadAndValidate loads values from user supplied input into Config struct and validates them
func (c *Config) LoadAndValidate() {
flag.Func("d", "Path to directory with anonymizing data", validateDir(c.AnonymizationDataPath))
flag.Func("d", "Path to directory with anonymizing data", c.validateDirPath())

flag.Func("i", "Path to input file containing logs to be anonymized", validateInput(c.InputPath))
flag.Func("i", "Path to input file containing logs to be anonymized", c.validateInputPath())

flag.Func("c", "Path to input file containing custom anonymization mappings", validateInput(c.CustomReplacementMapPath))
flag.Func("c", "Path to input file containing custom anonymization mappings", c.validateCustomMappingPath())

flag.Func("o", "Path to output file (default: Stdout)", validateOutput(c.OutputPath))
flag.Func("o", "Path to output file (default: Stdout)", c.validateOutput())

flag.BoolVar(&c.IsVerbose, "v", false, "Enable verbose logging (default: Disabled)")
flag.BoolVar(&c.IsLmExport, "e", false, "Change input file type to LM export (default: LM Backup)")
Expand Down
33 changes: 27 additions & 6 deletions internal/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
)

func validateInput(inputPath string) func(string) error {
func (c *Config) validateInputPath() func(string) error {
return func(flagValue string) error {
fileInfo, err := os.Stat(flagValue)
if err != nil {
Expand All @@ -17,33 +17,54 @@ func validateInput(inputPath string) func(string) error {
return fmt.Errorf("Input file %s cannot be a directory.\n", flagValue)
}

inputPath = flagValue
c.InputPath = flagValue

return nil
}
}

func validateOutput(outputPath string) func(string) error {
func (c *Config) validateCustomMappingPath() func(string) error {
return func(flagValue string) error {
fileInfo, err := os.Stat(flagValue)
if err != nil {
return err
}

if fileInfo.IsDir() {
return fmt.Errorf("Path to custom mapping file %s cannot be a directory.\n", flagValue)
}

c.CustomReplacementMapPath = flagValue

return nil
}
}

func (c *Config) validateOutput() func(string) error {
return func(flagValue string) error {
fileInfo, err := os.Stat(flagValue)
if err != nil {
// If output path does not exist it's ok - we will create it
if errors.Is(err, os.ErrNotExist) {
c.OutputPath = flagValue
return nil
}
return err
}

// If output path exists check if it's a directory - which would be wrong
if fileInfo.IsDir() {
return fmt.Errorf("Output file %s cannot be a directory.\n", flagValue)
}

outputPath = flagValue
// If output path exists and is not a dir it's ok - file will be truncated
c.OutputPath = flagValue

return nil
}
}

func validateDir(dir string) func(string) error {
func (c *Config) validateDirPath() func(string) error {
return func(flagValue string) error {
fileInfo, err := os.Stat(flagValue)
if err != nil {
Expand All @@ -54,7 +75,7 @@ func validateDir(dir string) func(string) error {
return fmt.Errorf("Path to anonymization data %s needs to be a directory.\n", flagValue)
}

dir = flagValue
c.AnonymizationDataPath = flagValue

return nil
}
Expand Down
17 changes: 0 additions & 17 deletions internal/files/handler.go

This file was deleted.

24 changes: 24 additions & 0 deletions internal/handlers/buffers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package handlers

import (
"bufio"
"log/slog"
)

type Buffers struct {
buffers []*bufio.Writer
}

func (b *Buffers) Add(buffer *bufio.Writer) {
b.buffers = append(b.buffers, buffer)
}

func (b *Buffers) Flush() {
slog.Debug("flush buffers on exit...")
for _, buffer := range b.buffers {
err := buffer.Flush()
if err != nil {
slog.Error("buffer flush failed", "error", err)
}
}
}
21 changes: 21 additions & 0 deletions internal/handlers/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handlers

import (
"log/slog"
"os"
)

type Files struct {
files []*os.File
}

func (f *Files) Add(file *os.File) {
f.files = append(f.files, file)
}

func (f *Files) Close() {
slog.Debug("closing files on exit...")
for _, file := range f.files {
file.Close()
}
}
38 changes: 20 additions & 18 deletions internal/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,30 @@ import (
func LoadCustomReplacementMap(path string) (map[string]string, error) {
customReplacementMap := make(map[string]string)

file, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
values := strings.Split(line, ":")
if len(values) == 1 {
slog.Error("wrong custom mapping: %s", "error", line)
if path != "" {
file, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm)
if err != nil {
return nil, err
}
defer file.Close()

originalValue := values[0]
newValue := values[1]
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
values := strings.Split(line, ":")
if len(values) == 1 {
slog.Error("wrong custom mapping: %s", "error", line)
}

customReplacementMap[originalValue] = newValue
}
originalValue := values[0]
newValue := values[1]

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading custom anonymization mapping: %w", err)
customReplacementMap[originalValue] = newValue
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading custom anonymization mapping: %w", err)
}
}

return customReplacementMap, nil
Expand Down
11 changes: 7 additions & 4 deletions internal/proof/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"

"github.com/logmanager-oss/logveil/internal/config"
"github.com/logmanager-oss/logveil/internal/files"
"github.com/logmanager-oss/logveil/internal/handlers"
)

type Proof struct {
Expand All @@ -22,17 +22,20 @@ type ProofWriter struct {
file *os.File
}

func CreateProofWriter(config *config.Config, openFiles *files.FilesHandler) (*ProofWriter, error) {
func CreateProofWriter(config *config.Config, filesHandler *handlers.Files, buffersHandler *handlers.Buffers) (*ProofWriter, error) {
if config.IsProofWriter {
file, err := os.Create(ProofFilename)
if err != nil {
return nil, fmt.Errorf("creating/opening proof file: %v", err)
}
openFiles.Add(file)
filesHandler.Add(file)

proofWriter := bufio.NewWriter(file)
buffersHandler.Add(proofWriter)

return &ProofWriter{
IsEnabled: true,
writer: bufio.NewWriter(file),
writer: proofWriter,
file: file,
}, nil
}
Expand Down
9 changes: 6 additions & 3 deletions internal/proof/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/logmanager-oss/logveil/internal/config"
"github.com/logmanager-oss/logveil/internal/files"
"github.com/logmanager-oss/logveil/internal/handlers"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -36,10 +36,13 @@ func TestProof_Write(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
filesHandler := &files.FilesHandler{}
filesHandler := &handlers.Files{}
defer filesHandler.Close()

p, err := CreateProofWriter(&config.Config{IsProofWriter: tt.isProofWriter}, filesHandler)
buffersHandler := &handlers.Buffers{}
defer buffersHandler.Flush()

p, err := CreateProofWriter(&config.Config{IsProofWriter: tt.isProofWriter}, filesHandler, buffersHandler)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import (
"os"

"github.com/logmanager-oss/logveil/internal/config"
"github.com/logmanager-oss/logveil/internal/files"
"github.com/logmanager-oss/logveil/internal/handlers"
)

type InputReader interface {
ReadLine() (map[string]string, error)
Close() error
}

func CreateInputReader(config *config.Config, openFiles *files.FilesHandler) (InputReader, error) {
func CreateInputReader(config *config.Config, filesHandler *handlers.Files) (InputReader, error) {
inputFile, err := os.Open(config.InputPath)
if err != nil {
return nil, fmt.Errorf("opening input file for reading: %v", err)
}
openFiles.Add(inputFile)
filesHandler.Add(inputFile)

if config.IsLmExport {
inputReader, err := NewLmExportReader(inputFile)
Expand Down
Loading

0 comments on commit be64e5b

Please sign in to comment.