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

Greenmask v0.1.2 release #3

Merged
merged 4 commits into from
Jan 30, 2024
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CMD_FILES = $(wildcard *.go)
TEST_FILES = $(wildcard *.go)
COVERAGE_FILE := coverage.out
VERSION ?= $(shell git tag --points-at HEAD)
LDFLAGS ?= -X main.version=$(VERSION)
LDFLAGS ?= -X github.com/greenmaskio/greenmask/cmd/greenmask/cmd.Version=$(VERSION)

.PHONY: build

Expand Down
3 changes: 0 additions & 3 deletions cmd/greenmask/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import (
"github.com/greenmaskio/greenmask/cmd/greenmask/cmd"
)

var version string

func main() {
cmd.Version = version
if err := cmd.Execute(); err != nil {
log.Fatal().Err(err).Msg("")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/db/postgres/pgcopy/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *Row) Decode(raw []byte) error {

// Building column position slice
idx := 0
for colStartPos < len(raw) {
for colStartPos <= len(raw) {

colEndPos = slices.Index(raw[colStartPos:], DefaultCopyDelimiter)
if colEndPos == -1 {
Expand Down
74 changes: 74 additions & 0 deletions internal/db/postgres/pgcopy/row_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,80 @@ import (
"github.com/greenmaskio/greenmask/pkg/toolkit"
)

func TestDecode_positions_panic_error_regression(t *testing.T) {
// This case was found in https://github.com/GreenmaskIO/greenmask/issues/2
// Fixed bug:
// The position array Row.columnPos contained wrong values for the last column if the value is empty string
// This caused panic or corrupt dump due to the wrong positions that were not overridden after previous COPY line
// parsing

type params struct {
record []byte
}

type expected struct {
positions []*columnPos
values []string
}

type test struct {
name string
params params
expected expected
}

length := 4

tests := []test{
{
name: "not empty",
params: params{
record: []byte("27\t1213\t\tasda"),
},
expected: expected{
positions: []*columnPos{
{0, 2}, {3, 7}, {8, 8}, {9, 13},
},
values: []string{"27", "1213", "", "asda"},
},
},
{
name: "empty last two col",
params: params{
record: []byte("27\t12\t\t"),
},
expected: expected{
positions: []*columnPos{
{0, 2}, {3, 5}, {6, 6}, {7, 7},
},
values: []string{"27", "12", "", ""},
},
},
}

row := NewRow(length)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := row.Decode(tt.params.record)
require.NoError(t, err)
require.Equal(t, len(tt.expected.positions), len(row.columnPos))

for idx, _ := range row.columnPos {
assert.Equalf(t, tt.expected.positions[idx].start, row.columnPos[idx].start, "wrong start value for idx %d", idx)
assert.Equalf(t, tt.expected.positions[idx].end, row.columnPos[idx].end, "wrong end value for idx %d", idx)
}

for idx, _ := range tt.expected.values {
v, err := row.GetColumn(idx)
require.NoErrorf(t, err, "unexpected error for idx %d", idx)
require.Falsef(t, v.IsNull, "unexpected NULL value for idx %d", idx)
require.Equalf(t, tt.expected.values[idx], string(v.Data), "unexpected value for idx %d", idx)
}
})
}
}

func TestDecode(t *testing.T) {

type result struct {
Expand Down
5 changes: 3 additions & 2 deletions internal/db/postgres/pgdump/pgdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ type Options struct {
}

func (o *Options) GetPgDSN() (string, error) {
//return "host=localhost port=5432 user=postgres dbname=postgres", nil
if strings.Contains(o.DbName, "=") {
// URI or Standard format
if strings.HasPrefix(o.DbName, "postgresql://") || strings.Contains(o.DbName, "=") {
return o.DbName, nil
}

return fmt.Sprintf("host=%s port=%d user=%s dbname=%s", o.Host, o.Port, o.UserName, o.DbName), nil
}

Expand Down
Loading