-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
solnicki
committed
Nov 19, 2024
1 parent
0f9a78a
commit aed9920
Showing
9 changed files
with
252 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package proof | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/logmanager-oss/logveil/internal/config" | ||
"github.com/logmanager-oss/logveil/internal/files" | ||
) | ||
|
||
type ProofWriter struct { | ||
IsEnabled bool | ||
writer *bufio.Writer | ||
file *os.File | ||
} | ||
|
||
func CreateProofWriter(config *config.Config, openFiles *files.FilesHandler) (*ProofWriter, error) { | ||
if config.IsProofWriter { | ||
file, err := os.Create("proof.json") | ||
if err != nil { | ||
return nil, fmt.Errorf("creating/opening proof file: %v", err) | ||
} | ||
openFiles.Add(file) | ||
|
||
return &ProofWriter{ | ||
IsEnabled: true, | ||
writer: bufio.NewWriter(file), | ||
file: file, | ||
}, nil | ||
} | ||
|
||
return &ProofWriter{IsEnabled: false}, nil | ||
} | ||
|
||
func (p *ProofWriter) Write(originalValue string, maskedValue string) { | ||
if !p.IsEnabled { | ||
return | ||
} | ||
|
||
proof := struct { | ||
OriginalValue string `json:"original"` | ||
MaskedValue string `json:"new"` | ||
}{ | ||
OriginalValue: originalValue, | ||
MaskedValue: maskedValue, | ||
} | ||
|
||
bytes, err := json.Marshal(proof) | ||
if err != nil { | ||
slog.Error("marshalling anonymisation proof", "error", err) | ||
} | ||
|
||
_, err = fmt.Fprintf(p.writer, "%s\n", bytes) | ||
if err != nil { | ||
slog.Error("writing anonymisation proof", "error", err) | ||
} | ||
} | ||
|
||
func (p *ProofWriter) Flush() { | ||
if !p.IsEnabled { | ||
return | ||
} | ||
|
||
p.writer.Flush() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package proof | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/logmanager-oss/logveil/internal/config" | ||
"github.com/logmanager-oss/logveil/internal/files" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProof_Write(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
isProofWriter bool | ||
originalValue string | ||
maskedValue string | ||
expectedOutput string | ||
}{ | ||
{ | ||
name: "Test case 1: write proof", | ||
isProofWriter: true, | ||
originalValue: "test", | ||
maskedValue: "masked", | ||
expectedOutput: "{\"original\":\"test\",\"new\":\"masked\"}\n", | ||
}, | ||
{ | ||
name: "Test case 2: proof writer disabled", | ||
isProofWriter: false, | ||
originalValue: "test", | ||
maskedValue: "masked", | ||
expectedOutput: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
filesHandler := &files.FilesHandler{} | ||
defer filesHandler.Close() | ||
|
||
p, err := CreateProofWriter(&config.Config{IsProofWriter: tt.isProofWriter}, filesHandler) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
p.Write(tt.originalValue, tt.maskedValue) | ||
p.Flush() | ||
|
||
file, err := os.OpenFile("proof.json", os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
buf := bytes.NewBuffer(nil) | ||
_, err = io.Copy(buf, file) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
file.Close() | ||
|
||
assert.Equal(t, tt.expectedOutput, buf.String()) | ||
|
||
os.Remove("proof.json") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.