From 67958b7a03ce62f3c5dcd13179be2658b66148e9 Mon Sep 17 00:00:00 2001 From: Henning Surmeier Date: Fri, 11 Jun 2021 14:29:24 +0200 Subject: [PATCH 1/3] make optional fields optional with native go types --- Makefile | 5 --- client_test.go | 3 +- deployments/vault/docker-compose.yaml | 17 -------- test/testdata/container_vault.go | 2 +- transit.go | 57 +++++++++++++-------------- transit_test.go | 25 +++++++----- utils.go | 15 +++++++ 7 files changed, 61 insertions(+), 63 deletions(-) delete mode 100644 deployments/vault/docker-compose.yaml diff --git a/Makefile b/Makefile index 6cdc1a1..10a49ed 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,3 @@ - -.PHONY: enable_transit -enable_transit: - docker exec -ti vault vault secrets enable transit || echo "this is fine" - .PHONY: test test: go test -v -count=1 -failfast ./... diff --git a/client_test.go b/client_test.go index e3c65ac..fb97aea 100644 --- a/client_test.go +++ b/client_test.go @@ -2,7 +2,6 @@ package vault import ( "fmt" - "gopkg.in/guregu/null.v3" "log" ) @@ -58,7 +57,7 @@ func Example_encryptDecryptType() { key := "test123bacd" err = transit.Create(key, &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), Type: rsa4096, }) if err != nil { diff --git a/deployments/vault/docker-compose.yaml b/deployments/vault/docker-compose.yaml deleted file mode 100644 index fb02acd..0000000 --- a/deployments/vault/docker-compose.yaml +++ /dev/null @@ -1,17 +0,0 @@ -version: '3.6' -services: - vault: - image: vault:1.4.2 - container_name: vault - restart: unless-stopped - ports: - - "8200:8200" - environment: - VAULT_ADDR: 'http://0.0.0.0:8200' - VAULT_DEV_ROOT_TOKEN_ID: 'test' - VAULT_TOKEN: 'test' - cap_add: - - IPC_LOCK - healthcheck: - retries: 5 - command: server -dev \ No newline at end of file diff --git a/test/testdata/container_vault.go b/test/testdata/container_vault.go index dba5839..6df8293 100644 --- a/test/testdata/container_vault.go +++ b/test/testdata/container_vault.go @@ -36,7 +36,7 @@ func InitVaultContainer(ctx context.Context) (*VaultContainer, error) { token := "test" req := testcontainers.ContainerRequest{ - Image: "vault:1.4.2", + Image: "vault:1.6.2", ExposedPorts: []string{string(port)}, WaitingFor: wait.ForListeningPort(port), Env: map[string]string{ diff --git a/transit.go b/transit.go index 69549c7..bf6c2b0 100644 --- a/transit.go +++ b/transit.go @@ -7,7 +7,6 @@ import ( "net/url" "github.com/hashicorp/vault/api" - "gopkg.in/guregu/null.v3" ) type Transit struct { @@ -28,11 +27,11 @@ func (c *Client) TransitWithMountPoint(mountPoint string) *Transit { } type TransitCreateOptions struct { - ConvergentEncryption null.Bool `json:"convergent_encryption,omitempty"` - Derived null.Bool `json:"derived,omitempty"` - Exportable null.Bool `json:"exportable,omitempty"` - AllowPlaintextBackup null.Bool `json:"allow_plaintext_backup,omitempty"` - Type string `json:"type,omitempty"` + ConvergentEncryption *bool `json:"convergent_encryption,omitempty"` + Derived *bool `json:"derived,omitempty"` + Exportable *bool `json:"exportable,omitempty"` + AllowPlaintextBackup *bool `json:"allow_plaintext_backup,omitempty"` + Type string `json:"type,omitempty"` } func (t *Transit) Create(key string, opts *TransitCreateOptions) error { @@ -104,7 +103,7 @@ func (t *Transit) Delete(key string) error { func (t *Transit) ForceDelete(key string) error { err := t.Update(key, TransitUpdateOptions{ - DeletionAllowed: null.BoolFrom(true), + DeletionAllowed: BoolPtr(true), }) if err != nil { return err @@ -114,11 +113,11 @@ func (t *Transit) ForceDelete(key string) error { } type TransitUpdateOptions struct { - MinDecryptionVersion int `json:"min_decrytion_version"` - MinEncryptionVersion int `json:"min_encryption_version"` - DeletionAllowed null.Bool `json:"deletion_allowed"` - Exportable null.Bool `json:"exportable"` - AllowPlaintextBackup null.Bool `json:"allow_plaintext_backup"` + MinDecryptionVersion int `json:"min_decrytion_version,omitempty"` + MinEncryptionVersion int `json:"min_encryption_version,omitempty"` + DeletionAllowed *bool `json:"deletion_allowed,omitempty"` + Exportable *bool `json:"exportable,omitempty"` + AllowPlaintextBackup *bool `json:"allow_plaintext_backup,omitempty"` } func (t *Transit) Update(key string, opts TransitUpdateOptions) error { @@ -141,7 +140,7 @@ func (t *Transit) Rotate(key string) error { type TransitExportOptions struct { KeyType string `json:"key_type"` - Version string `json:"version"` + Version string `json:"version,omitempty"` } type TransitExportResponse struct { @@ -184,22 +183,22 @@ func (t *Transit) KeyExists(key string) (bool, error) { } type TransitBatchCiphertext struct { - Ciphertext string `json:"ciphertext"` - Context null.String `json:"context"` + Ciphertext string `json:"ciphertext"` + Context string `json:"context,omitempty"` } type TransitBatchPlaintext struct { - Plaintext string `json:"plaintext"` - Context null.String `json:"context"` + Plaintext string `json:"plaintext"` + Context string `json:"context,omitempty"` } type TransitEncryptOptions struct { - Plaintext string `json:"plaintext"` - Context null.String `json:"context"` - KeyVersion null.Int `json:"key_version"` - Nonce null.String `json:"nonce"` - Type null.String `json:"type"` - ConvergentEncryption null.String `json:"convergent_encryption"` + Plaintext string `json:"plaintext"` + Context string `json:"context,omitempty"` + KeyVersion *int `json:"key_version,omitempty"` + Nonce string `json:"nonce,omitempty"` + Type string `json:"type,omitempty"` + ConvergentEncryption string `json:"convergent_encryption,omitempty"` } type TransitEncryptResponse struct { @@ -223,9 +222,9 @@ func (t *Transit) Encrypt(key string, opts *TransitEncryptOptions) (*TransitEncr type TransitEncryptOptionsBatch struct { BatchInput []TransitBatchPlaintext `json:"batch_input"` - KeyVersion null.Int `json:"key_version"` - Type null.String `json:"type"` - ConvergentEncryption null.String `json:"convergent_encryption"` + KeyVersion *int `json:"key_version,omitempty"` + Type string `json:"type,omitempty"` + ConvergentEncryption string `json:"convergent_encryption,omitempty"` } type TransitEncryptResponseBatch struct { @@ -250,9 +249,9 @@ func (t *Transit) EncryptBatch(key string, opts *TransitEncryptOptionsBatch) (*T } type TransitDecryptOptions struct { - Ciphertext string `json:"ciphertext"` - Context null.String `json:"context"` - Nonce null.String `json:"nonce"` + Ciphertext string `json:"ciphertext"` + Context string `json:"context,omitempty"` + Nonce string `json:"nonce,omitempty"` } type TransitDecryptResponse struct { diff --git a/transit_test.go b/transit_test.go index 933a84f..3c9df77 100644 --- a/transit_test.go +++ b/transit_test.go @@ -10,7 +10,6 @@ import ( "github.com/hashicorp/vault/api" "github.com/stretchr/testify/suite" - "gopkg.in/guregu/null.v3" ) type TransitTestSuite struct { @@ -33,7 +32,7 @@ func TestTransitTestSuite(t *testing.T) { func (s *TransitTestSuite) TestCreateAndRead() { err := s.client.Create("testCreateAndRead", &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -41,11 +40,12 @@ func (s *TransitTestSuite) TestCreateAndRead() { require.NoError(s.T(), err) s.Equal(true, res.Data.Exportable) + s.T().Log(res.Data.Type) } func (s *TransitTestSuite) TestCreateAndList() { err := s.client.Create("testCreateAndList", &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -61,7 +61,7 @@ func (s *TransitTestSuite) TestCreateAndList() { func (s *TransitTestSuite) TestCreateListAllowDelete() { key := "testCreateListAllowDelete" err := s.client.Create(key, &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -70,7 +70,7 @@ func (s *TransitTestSuite) TestCreateListAllowDelete() { s.Contains(res.Data.Keys, key) err = s.client.Update(key, TransitUpdateOptions{ - DeletionAllowed: null.BoolFrom(true), + DeletionAllowed: BoolPtr(true), }) require.NoError(s.T(), err) @@ -85,7 +85,7 @@ func (s *TransitTestSuite) TestCreateListAllowDelete() { func (s *TransitTestSuite) TestCreateListForceDelete() { key := "testCreateListForceDelete" err := s.client.Create(key, &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -104,7 +104,7 @@ func (s *TransitTestSuite) TestCreateListForceDelete() { func (s *TransitTestSuite) TestRotate() { key := "testRotate" err := s.client.Create(key, &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -122,7 +122,7 @@ func (s *TransitTestSuite) TestRotate() { func (s *TransitTestSuite) TestExport() { key := "testExport" err := s.client.Create(key, &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -138,7 +138,7 @@ func (s *TransitTestSuite) TestExport() { func (s *TransitTestSuite) TestKeyExists() { err := s.client.Create("testExists", &TransitCreateOptions{ - Exportable: null.BoolFrom(true), + Exportable: BoolPtr(true), }) require.NoError(s.T(), err) @@ -198,6 +198,13 @@ func (s *TransitTestSuite) TestEncryptDecryptBatch() { s.Equal(text2, dec.Data.BatchResults[1].Plaintext) } +func (s *TransitTestSuite) TestImplicitEncryptCreate() { + _, err := s.client.Encrypt("test404", &TransitEncryptOptions{ + Plaintext: "asdf", + }) + require.NoError(s.T(), err) +} + func (s *TransitTestSuite) TestDecryptWithoutKey() { _, err := s.client.Decrypt("test404", &TransitDecryptOptions{ Ciphertext: "asdf", diff --git a/utils.go b/utils.go index 03766b4..294d63d 100644 --- a/utils.go +++ b/utils.go @@ -11,3 +11,18 @@ func resolvePath(parts []string) string { return "/" + strings.Join(trimmedParts, "/") } + +func BoolPtr(input bool) *bool { + b := input + return &b +} + +func IntPtr(input int) *int { + i := input + return &i +} + +func StringPtr(input string) *string { + s := input + return &s +} From e8074b066f189580eb2d7f5429388e218d07b2ba Mon Sep 17 00:00:00 2001 From: Henning Surmeier Date: Fri, 11 Jun 2021 14:34:07 +0200 Subject: [PATCH 2/3] bump golangci-lint --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 90c037e..f02e464 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -33,7 +33,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: golangci-lint - uses: golangci/golangci-lint-action@v1 + uses: golangci/golangci-lint-action@v2 with: - version: v1.27 + version: v1.40.1 args: --config=build/ci/.golangci.yml From ec1a55b1c14b6a9f822c87e327caafbd457690d2 Mon Sep 17 00:00:00 2001 From: Henning Surmeier Date: Fri, 11 Jun 2021 14:37:07 +0200 Subject: [PATCH 3/3] remove outdated config --- .github/workflows/build.yml | 1 - build/ci/.golangci.yml | 59 ------------------------------------- 2 files changed, 60 deletions(-) delete mode 100644 build/ci/.golangci.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f02e464..06c2008 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -36,4 +36,3 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: v1.40.1 - args: --config=build/ci/.golangci.yml diff --git a/build/ci/.golangci.yml b/build/ci/.golangci.yml deleted file mode 100644 index e3e4e0c..0000000 --- a/build/ci/.golangci.yml +++ /dev/null @@ -1,59 +0,0 @@ -linters-settings: - govet: - check-shadowing: true - golint: - min-confidence: 0 - gocyclo: - min-complexity: 20 - maligned: - suggest-new: true - dupl: - threshold: 100 - goconst: - min-len: 2 - min-occurrences: 2 - depguard: - list-type: blacklist - packages: - # logging is allowed only by logutils.Log, logrus - # is allowed to use only in logutils package - #- github.com/sirupsen/logrus - misspell: - locale: US - lll: - line-length: 180 - funlen: - lines: 100 - statements: 60 - gocritic: - enabled-tags: - - performance - - style - #- experimental - disabled-checks: - - wrapperFunc - - commentFormatting # https://github.com/go-critic/go-critic/issues/755 - - unnamedResult - - ifElseChain - -linters: - enable-all: true - -issues: - # Excluding configuration per-path, per-linter, per-text and per-source - exclude-rules: - - path: _test\.go - linters: - - gomnd - -run: - skip-dirs: - - test/ - - examples/ - - deployments/ - skip-files: - - .*_test.go - deadline: 60m - -service: - golangci-lint-version: 1.27.x \ No newline at end of file