Skip to content

Commit

Permalink
Merge pull request #10 from mittwald/fix-optionals
Browse files Browse the repository at this point in the history
make optional fields optional with native go types
  • Loading branch information
Lucaber authored Jun 11, 2021
2 parents 02b38e2 + ec1a55b commit f63470f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 125 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ 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
args: --config=build/ci/.golangci.yml
version: v1.40.1
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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 ./...
59 changes: 0 additions & 59 deletions build/ci/.golangci.yml

This file was deleted.

3 changes: 1 addition & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vault

import (
"fmt"
"gopkg.in/guregu/null.v3"
"log"
)

Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 0 additions & 17 deletions deployments/vault/docker-compose.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion test/testdata/container_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
57 changes: 28 additions & 29 deletions transit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"

"github.com/hashicorp/vault/api"
"gopkg.in/guregu/null.v3"
)

type Transit struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
25 changes: 16 additions & 9 deletions transit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/hashicorp/vault/api"
"github.com/stretchr/testify/suite"
"gopkg.in/guregu/null.v3"
)

type TransitTestSuite struct {
Expand All @@ -33,19 +32,20 @@ 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)

res, err := s.client.Read("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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit f63470f

Please sign in to comment.