From 8b6c6350e04f807a3cb55a0c36edda85f1edc4a2 Mon Sep 17 00:00:00 2001 From: Ross Nelson Date: Fri, 3 Jan 2025 23:29:38 -0600 Subject: [PATCH 1/2] Implement PGP Encrypt --- .gitignore | 1 + cryptkeeper/keepers/pgp/pgp.go | 18 +- cryptkeeper/keepers/pgp/pgp_test.go | 23 +- junit.xml | 378 ++++++++++++++++++++++++++++ 4 files changed, 412 insertions(+), 8 deletions(-) create mode 100644 junit.xml diff --git a/.gitignore b/.gitignore index 00f6582..7e8f9df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ coverage.out coverage-report.txt report.xml +junit.xml /tmp/* /examples/tmp/* /bin/serve/docker/prometheus/data diff --git a/cryptkeeper/keepers/pgp/pgp.go b/cryptkeeper/keepers/pgp/pgp.go index a95616d..9d15713 100644 --- a/cryptkeeper/keepers/pgp/pgp.go +++ b/cryptkeeper/keepers/pgp/pgp.go @@ -2,7 +2,6 @@ package pgp import ( "encoding/base64" - "errors" "io" "os" "strings" @@ -56,7 +55,22 @@ func (p Keeper) Decrypt(config keepers.Config, file io.Reader) (io.Reader, error } func (p Keeper) Encrypt(config keepers.Config, reader io.Reader) (io.Reader, error) { - return nil, errors.New("not implemented") + content, err := io.ReadAll(reader) + if err != nil { + return reader, err + } + + publicKey, _ := base64.StdEncoding.DecodeString(config.Get("publicKey")) + privateKey, _ := base64.StdEncoding.DecodeString(config.Get("privateKey")) + + encrypted, err := helper.EncryptSignMessageArmored( + string(publicKey), + string(privateKey), + []byte(config.Get("passphrase")), + string(content), + ) + + return strings.NewReader(encrypted), err } func New() *keepers.Keeper { diff --git a/cryptkeeper/keepers/pgp/pgp_test.go b/cryptkeeper/keepers/pgp/pgp_test.go index 59711f2..a8192b2 100644 --- a/cryptkeeper/keepers/pgp/pgp_test.go +++ b/cryptkeeper/keepers/pgp/pgp_test.go @@ -12,25 +12,35 @@ import ( "github.com/stretchr/testify/assert" ) -//go:embed support/Reguard_mock_contracts.csv.pgp -var encStr string +//go:embed support/Reguard_mock_contracts.csv +var str string //go:embed support/Reguard_mock_private.asc var secretKeyring string +//go:embed support/Reguard_mock_public.asc +var publicKeyring string + var passphrase = "reguard-mock-key" func TestPGP(t *testing.T) { + os.Setenv("PREFIX_PUBLIC_KEY", publicKeyring) os.Setenv("PREFIX_PRIVATE_KEY", secretKeyring) os.Setenv("PREFIX_PASSPHRASE", passphrase) - keeper := pgp.Keeper{} + keeper := pgp.New() - config := keeper.Setup("prefix") + keeper.Setup("prefix") + + encrypted, err := keeper.Encrypt( + strings.NewReader(str), + ) + + assert.NotEqual(t, encrypted, strings.NewReader(str)) + assert.NoError(t, err) content, err := keeper.Decrypt( - config, - strings.NewReader(encStr), + encrypted, ) assert.NoError(t, err) @@ -39,6 +49,7 @@ func TestPGP(t *testing.T) { assert.NoError(t, err) assert.Equal(t, len(readContent), 1860) + assert.Equal(t, string(readContent), str) } func TestPGPErr(t *testing.T) { diff --git a/junit.xml b/junit.xml new file mode 100644 index 0000000..23ca6c6 --- /dev/null +++ b/junit.xml @@ -0,0 +1,378 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + github.com/simiancreative/simiango/meta.TestGinRecovery.func2 (4 handlers) +[GIN] 2025/01/03 - 23:28:15 | 200 | 26.875µs | | GET "/test"]]> + + + github.com/simiancreative/simiango/meta.TestRecoverGinPanic.func2 (4 handlers) +{"level":"error","msg":"Test error","request":["GET /test HTTP/1.1","",""],"request_id":null,"stack":["/Users/ross/code/simian/simiango/meta/rescue_test.go:49 (0x102c373db)","TestRecoverGinPanic.func1: panic(errors.New(\"Test error\"))","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102c008cb)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x102c0085c)","CustomRecoveryWithWriter.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bff823)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0x102bff7b0)","LoggerWithConfig.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bfe13f)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x102bfde54)","(*Engine).handleHTTPRequest: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0x102bfd5b7)","(*Engine).ServeHTTP: engine.handleHTTPRequest(c)","/Users/ross/code/simian/simiango/meta/rescue_test.go:57 (0x102c3671f)","TestRecoverGinPanic: r.ServeHTTP(w, req)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/testing/testing.go:1595 (0x1025b3fd3)","tRunner: fn(t)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/runtime/asm_arm64.s:1197 (0x1024adc33)","goexit: MOVD\tR0, R0\t// NOP"],"time":"2025-01-03T23:28:15-06:00"} +[GIN] 2025/01/03 - 23:28:15 | 200 | 1.281041ms | | GET "/test"]]> + + + github.com/simiancreative/simiango/meta.TestRecoverGinPanic_BrokenPipe.func2 (4 handlers) +{"level":"error","msg":": : broken pipe","request":["GET /test HTTP/1.1","",""],"request_id":null,"stack":["/Users/ross/code/simian/simiango/meta/rescue_test.go:72 (0x102c370ab)","TestRecoverGinPanic_BrokenPipe.func1: panic(\u0026net.OpError{Err: \u0026os.SyscallError{Err: errors.New(\"broken pipe\")}})","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102c008cb)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x102c0085c)","CustomRecoveryWithWriter.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bff823)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0x102bff7b0)","LoggerWithConfig.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bfe13f)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x102bfde54)","(*Engine).handleHTTPRequest: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0x102bfd5b7)","(*Engine).ServeHTTP: engine.handleHTTPRequest(c)","/Users/ross/code/simian/simiango/meta/rescue_test.go:80 (0x102c369cf)","TestRecoverGinPanic_BrokenPipe: r.ServeHTTP(w, req)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/testing/testing.go:1595 (0x1025b3fd3)","tRunner: fn(t)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/runtime/asm_arm64.s:1197 (0x1024adc33)","goexit: MOVD\tR0, R0\t// NOP"],"time":"2025-01-03T23:28:15-06:00"} +[GIN] 2025/01/03 - 23:28:15 | 500 | 961.709µs | | GET "/test" +Error #01: : : broken pipe]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From bff9d017d8a03bf67c43fc7bc38874b074388e9f Mon Sep 17 00:00:00 2001 From: Ross Nelson Date: Sun, 5 Jan 2025 10:34:17 -0600 Subject: [PATCH 2/2] remove junit --- junit.xml | 378 ------------------------------------------------------ 1 file changed, 378 deletions(-) delete mode 100644 junit.xml diff --git a/junit.xml b/junit.xml deleted file mode 100644 index 23ca6c6..0000000 --- a/junit.xml +++ /dev/null @@ -1,378 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - github.com/simiancreative/simiango/meta.TestGinRecovery.func2 (4 handlers) -[GIN] 2025/01/03 - 23:28:15 | 200 | 26.875µs | | GET "/test"]]> - - - github.com/simiancreative/simiango/meta.TestRecoverGinPanic.func2 (4 handlers) -{"level":"error","msg":"Test error","request":["GET /test HTTP/1.1","",""],"request_id":null,"stack":["/Users/ross/code/simian/simiango/meta/rescue_test.go:49 (0x102c373db)","TestRecoverGinPanic.func1: panic(errors.New(\"Test error\"))","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102c008cb)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x102c0085c)","CustomRecoveryWithWriter.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bff823)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0x102bff7b0)","LoggerWithConfig.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bfe13f)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x102bfde54)","(*Engine).handleHTTPRequest: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0x102bfd5b7)","(*Engine).ServeHTTP: engine.handleHTTPRequest(c)","/Users/ross/code/simian/simiango/meta/rescue_test.go:57 (0x102c3671f)","TestRecoverGinPanic: r.ServeHTTP(w, req)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/testing/testing.go:1595 (0x1025b3fd3)","tRunner: fn(t)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/runtime/asm_arm64.s:1197 (0x1024adc33)","goexit: MOVD\tR0, R0\t// NOP"],"time":"2025-01-03T23:28:15-06:00"} -[GIN] 2025/01/03 - 23:28:15 | 200 | 1.281041ms | | GET "/test"]]> - - - github.com/simiancreative/simiango/meta.TestRecoverGinPanic_BrokenPipe.func2 (4 handlers) -{"level":"error","msg":": : broken pipe","request":["GET /test HTTP/1.1","",""],"request_id":null,"stack":["/Users/ross/code/simian/simiango/meta/rescue_test.go:72 (0x102c370ab)","TestRecoverGinPanic_BrokenPipe.func1: panic(\u0026net.OpError{Err: \u0026os.SyscallError{Err: errors.New(\"broken pipe\")}})","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102c008cb)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/recovery.go:102 (0x102c0085c)","CustomRecoveryWithWriter.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bff823)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/logger.go:240 (0x102bff7b0)","LoggerWithConfig.func1: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174 (0x102bfe13f)","(*Context).Next: c.handlers[c.index](c)","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 (0x102bfde54)","(*Engine).handleHTTPRequest: c.Next()","/Users/ross/.asdf/installs/golang/1.21.7/packages/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 (0x102bfd5b7)","(*Engine).ServeHTTP: engine.handleHTTPRequest(c)","/Users/ross/code/simian/simiango/meta/rescue_test.go:80 (0x102c369cf)","TestRecoverGinPanic_BrokenPipe: r.ServeHTTP(w, req)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/testing/testing.go:1595 (0x1025b3fd3)","tRunner: fn(t)","/Users/ross/.asdf/installs/golang/1.21.7/go/src/runtime/asm_arm64.s:1197 (0x1024adc33)","goexit: MOVD\tR0, R0\t// NOP"],"time":"2025-01-03T23:28:15-06:00"} -[GIN] 2025/01/03 - 23:28:15 | 500 | 961.709µs | | GET "/test" -Error #01: : : broken pipe]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -