Skip to content

Commit

Permalink
Merge pull request #65 from ibm-hyper-protect/cleue-upgrade
Browse files Browse the repository at this point in the history
fix: adjust to latest updates of fp-go and update dependencies
  • Loading branch information
CarstenLeue authored Nov 28, 2023
2 parents 60e8dcd + 999f756 commit c47b9ad
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 85 deletions.
15 changes: 3 additions & 12 deletions archive/tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ func toReader[A io.Reader](a A) io.Reader {
}

func onClose[A io.Closer](a A) E.Either[error, any] {
err := a.Close()
return E.TryCatchError(func() (any, error) {
return nil, err
})
return E.TryCatchError[any](nil, a.Close())
}

func onOpenFile(file string) func() E.Either[error, *os.File] {
Expand Down Expand Up @@ -98,9 +95,7 @@ func writeHeader(src string) func(*tar.Writer) func(file string, fi os.FileInfo)

// callback to write the header
writeHeader := func(hdr *tar.Header) E.Either[error, *tar.Writer] {
return E.TryCatchError(func() (*tar.Writer, error) {
return w, w.WriteHeader(hdr)
})
return E.TryCatchError(w, w.WriteHeader(hdr))
}

return func(file string, fi os.FileInfo) E.Either[error, *tar.Writer] {
Expand Down Expand Up @@ -172,11 +167,7 @@ func TarFolder[W io.Writer](src string) func(W) E.Either[error, W] {
)
}

return E.TryCatchError(func() (W, error) {
// walk through every file in the folder
return buf, walk(walkFunc)

})
return E.TryCatchError(buf, walk(walkFunc))
})
}
}
21 changes: 10 additions & 11 deletions common/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ func ExecCommand(name string, arg ...string) func([]byte) E.Either[error, Comman
// command result
var stdOut bytes.Buffer
var stdErr bytes.Buffer
// command input
cmd := exec.Command(name, arg...)
cmd.Stdin = bytes.NewReader(dataIn)

cmd.Stdout = &stdOut
cmd.Stderr = &stdErr

err := cmd.Run()

// execute the command
return F.Pipe1(
// run the command
E.TryCatchError(func() (CommandOutput, error) {
// command input
cmd := exec.Command(name, arg...)
cmd.Stdin = bytes.NewReader(dataIn)

cmd.Stdout = &stdOut
cmd.Stderr = &stdErr

err := cmd.Run()
return T.MakeTuple2(RA.Copy(stdOut.Bytes()), RA.Copy(stdErr.Bytes())), err
}),
E.TryCatchError(T.MakeTuple2(RA.Copy(stdOut.Bytes()), RA.Copy(stdErr.Bytes())), err),
// enrich the error
E.MapLeft[error, CommandOutput](func(cause error) error {
return fmt.Errorf("command execution of [%s][%s] failed, stdout [%s], stderr [%s], cause [%w]", name, arg, stdOut.String(), stdErr.String(), cause)
Expand Down
2 changes: 1 addition & 1 deletion datasource/datasource_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func toCertificateMap(data map[string]any) E.Either[error, map[string]string] {
dst[k] = s
}
return dst, nil
})
}())
}

// parses a constraint string into a constraint object
Expand Down
11 changes: 5 additions & 6 deletions datasource/datasource_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,16 @@ func resolveUrl(tmp *template.Template) func(version *semver.Version) E.Either[e
KeyMinor: version.Minor(),
KeyPatch: version.Patch(),
}
return E.TryCatchError(func() (string, error) {
var buffer bytes.Buffer
err := tmp.Execute(&buffer, ctx)
return buffer.String(), err
})
var buffer bytes.Buffer
err := tmp.Execute(&buffer, ctx)
return E.TryCatchError(buffer.String(), err)
}
}

func textFromResponse(url string) func(resp *http.Response) E.Either[error, string] {
return func(resp *http.Response) E.Either[error, string] {
defer resp.Body.Close()

return F.Pipe1(
E.TryCatchError(func() ([]byte, error) {
data, err := io.ReadAll(resp.Body)
Expand All @@ -124,7 +123,7 @@ func textFromResponse(url string) func(resp *http.Response) E.Either[error, stri
return data, fmt.Errorf("url: %s, status %d: cause: [%s]", url, resp.StatusCode, data)
}
return data, err
}),
}()),
E.Map[error](B.ToString),
)
}
Expand Down
24 changes: 7 additions & 17 deletions encrypt/encrypt_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ func parsePrivateKeyE(priv []byte) E.Either[error, *rsa.PrivateKey] {
// cryptoRandomE returns a random sequence of bytes with the given length
func cryptoRandomE(n int) func() E.Either[error, []byte] {
return func() E.Either[error, []byte] {
return E.TryCatchError(func() ([]byte, error) {
buf := make([]byte, n)
_, err := rand.Read(buf)
return buf, err
})
buf := make([]byte, n)
_, err := rand.Read(buf)
return E.TryCatchError(buf, err)
}
}

Expand Down Expand Up @@ -163,18 +161,14 @@ func pemDecodeE(data []byte) E.Either[error, []byte] {
// encryptPKCS1v15 creates a function that encrypts a piece of text using a public key
func encryptPKCS1v15(pub *rsa.PublicKey) func([]byte) E.Either[error, []byte] {
return func(origData []byte) E.Either[error, []byte] {
return E.TryCatchError(func() ([]byte, error) {
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
})
return E.TryCatchError(rsa.EncryptPKCS1v15(rand.Reader, pub, origData))
}
}

// decryptPKCS1v15 creates a function that decrypts a piece of text using a private key
func decryptPKCS1v15(pub *rsa.PrivateKey) func([]byte) E.Either[error, []byte] {
return func(ciphertext []byte) E.Either[error, []byte] {
return E.TryCatchError(func() ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, pub, ciphertext)
})
return E.TryCatchError(rsa.DecryptPKCS1v15(rand.Reader, pub, ciphertext))
}
}

Expand Down Expand Up @@ -322,19 +316,15 @@ func privKeyToPem(privKey *rsa.PrivateKey) []byte {
// CryptoPrivateKey generates a private key
func CryptoPrivateKey() E.Either[error, []byte] {
return F.Pipe1(
E.TryCatchError(func() (*rsa.PrivateKey, error) {
return rsa.GenerateKey(rand.Reader, 4096)
}),
E.TryCatchError(rsa.GenerateKey(rand.Reader, 4096)),
E.Map[error](privKeyToPem),
)
}

// implements the signing operation in a functional way
func signPKCS1v15(privateKey *rsa.PrivateKey) func([]byte) E.Either[error, []byte] {
return func(digest []byte) E.Either[error, []byte] {
return E.TryCatchError(func() ([]byte, error) {
return rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, digest)
})
return E.TryCatchError(rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, digest))
}
}

Expand Down
4 changes: 1 addition & 3 deletions encrypt/openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ func validOpenSSL() E.Either[error, string] {
// helper to safely write data into a file
func writeData[W io.Writer](data []byte) func(w W) E.Either[error, int] {
return func(w W) E.Either[error, int] {
return E.TryCatchError(func() (int, error) {
return w.Write(data)
})
return E.TryCatchError(w.Write(data))
}
}

Expand Down
4 changes: 1 addition & 3 deletions fp/file/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ var (
}
onDelete = func(f *os.File) E.Either[error, any] {
f.Close() // #nosec
return E.TryCatchError(func() (any, error) {
return nil, os.Remove(f.Name())
})
return E.TryCatchError[any](nil, os.Remove(f.Name()))
}
)

Expand Down
14 changes: 4 additions & 10 deletions fp/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ import (
)

func Parse[A any](data []byte) E.Either[error, A] {
return E.TryCatchError(func() (A, error) {
var result A
err := yaml.Unmarshal(data, &result)
return result, err
})
var result A
err := yaml.Unmarshal(data, &result)
return E.TryCatchError(result, err)
}

func Stringify[A any](a A) E.Either[error, []byte] {
return E.TryCatchError(func() ([]byte, error) {
b, err := yaml.Marshal(a)
return b, err
})

return E.TryCatchError(yaml.Marshal(a))
}
44 changes: 22 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module github.com/ibm-hyper-protect/terraform-provider-hpcr
go 1.20

require (
github.com/IBM/fp-go v1.0.50
github.com/IBM/fp-go v1.0.62
github.com/Masterminds/semver v1.5.0
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.29.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.30.0
github.com/qri-io/jsonschema v0.2.1
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.16.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -24,33 +24,33 @@ require (
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/cloudflare/circl v1.3.6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.5.2 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.18.0 // indirect
github.com/hashicorp/hc-install v0.6.1 // indirect
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.17.1 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.0 // indirect
github.com/hashicorp/terraform-json v0.18.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.19.1 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/cli v1.1.5 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
Expand All @@ -65,16 +65,16 @@ require (
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.14.0 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect
google.golang.org/grpc v1.58.2 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading

0 comments on commit c47b9ad

Please sign in to comment.