Skip to content

Commit

Permalink
implement suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethhealy committed Dec 10, 2024
1 parent b77b3b8 commit 73baae7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
14 changes: 0 additions & 14 deletions cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,6 @@ func readPipedStdin() []byte {
return nil
}

func readBytesFromFile(filePath string) []byte {
fileToEncrypt, err := os.Open(filePath)
if err != nil {
cli.ExitWithError(fmt.Sprintf("Failed to open file at path: %s", filePath), err)
}
defer fileToEncrypt.Close()

bytes, err := io.ReadAll(fileToEncrypt)
if err != nil {
cli.ExitWithError(fmt.Sprintf("Failed to read bytes from file at path: %s", filePath), err)
}
return bytes
}

func init() {
designCmd := man.Docs.GetCommand("dev/design-system",
man.WithRun(dev_designSystem),
Expand Down
5 changes: 4 additions & 1 deletion cmd/tdf-decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
"github.com/opentdf/otdfctl/pkg/utils"
"github.com/spf13/cobra"
)

Expand All @@ -28,9 +29,11 @@ func dev_tdfDecryptCmd(cmd *cobra.Command, args []string) {
// Prefer file argument over piped input over default filename
bytesToDecrypt := piped
var tdfFile string
var err error
if len(args) > 0 {
tdfFile = args[0]
bytesToDecrypt = readBytesFromFile(tdfFile)
bytesToDecrypt, err = utils.ReadBytesFromFile(tdfFile)

Check failure on line 35 in cmd/tdf-decrypt.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `bytesToDecrypt` is never used (staticcheck)
cli.ExitWithError("Failed to read file:", err)
}

if len(bytesToDecrypt) == 0 {
Expand Down
5 changes: 4 additions & 1 deletion cmd/tdf-encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gabriel-vasile/mimetype"
"github.com/opentdf/otdfctl/pkg/cli"
"github.com/opentdf/otdfctl/pkg/man"
"github.com/opentdf/otdfctl/pkg/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -63,8 +64,10 @@ func dev_tdfEncryptCmd(cmd *cobra.Command, args []string) {

// prefer filepath argument over stdin input
bytesSlice := piped
var err error
if filePath != "" {
bytesSlice = readBytesFromFile(filePath)
bytesSlice, err = utils.ReadBytesFromFile(filePath)

Check failure on line 69 in cmd/tdf-encrypt.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `bytesSlice` is never used (staticcheck)
cli.ExitWithError("Failed to read file:", err)
}

// auto-detect mime type if not provided
Expand Down
24 changes: 5 additions & 19 deletions pkg/handlers/tdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"

"github.com/opentdf/otdfctl/pkg/utils"
"github.com/opentdf/platform/sdk"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func (h Handler) EncryptBytes(tdfType string, unencrypted []byte, attrValues []s
err := json.Unmarshal([]byte(assertions), &assertionConfigs)
if err != nil {
// if unable to marshal to json, interpret as file string and try to read from file
assertionBytes, err := readBytesFromFile(assertions)
assertionBytes, err := utils.ReadBytesFromFile(assertions)
if err != nil {
return nil, errors.Join(ErrTDFUnableToReadAssertions, err)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (h Handler) EncryptBytes(tdfType string, unencrypted []byte, attrValues []s
}
}

func (h Handler) DecryptBytes(toDecrypt []byte, assertionVerification string, disableAssertionCheck bool) (*bytes.Buffer, error) {
func (h Handler) DecryptBytes(toDecrypt []byte, assertionVerificationKeysFile string, disableAssertionCheck bool) (*bytes.Buffer, error) {
out := &bytes.Buffer{}
pt := io.Writer(out)
ec := bytes.NewReader(toDecrypt)
Expand All @@ -126,9 +126,9 @@ func (h Handler) DecryptBytes(toDecrypt []byte, assertionVerification string, di
case sdk.Standard:
opts := []sdk.TDFReaderOption{sdk.WithDisableAssertionVerification(disableAssertionCheck)}
var assertionVerificationKeys sdk.AssertionVerificationKeys
if assertionVerification != "" {
if assertionVerificationKeysFile != "" {
// read the file
assertionVerificationBytes, err := readBytesFromFile(assertionVerification)
assertionVerificationBytes, err := utils.ReadBytesFromFile(assertionVerificationKeysFile)
if err != nil {
return nil, errors.Join(ErrTDFUnableToReadAssertionVerificationKeys, err)
}
Expand Down Expand Up @@ -215,20 +215,6 @@ func (h Handler) InspectTDF(toInspect []byte) (TDFInspect, []error) {
}
}

func readBytesFromFile(filePath string) ([]byte, error) {
fileToEncrypt, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file at path %s: %w", filePath, err)
}
defer fileToEncrypt.Close()

bytes, err := io.ReadAll(fileToEncrypt)
if err != nil {
return nil, fmt.Errorf("failed to read bytes from file at path %s: %w", filePath, err)
}
return bytes, nil
}

func correctKeyType(assertionKey sdk.AssertionKey, public bool) (interface{}, error) {
strKey, ok := assertionKey.Key.(string)
if !ok {
Expand Down
21 changes: 21 additions & 0 deletions pkg/utils/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import (
"fmt"
"io"
"os"
)

func ReadBytesFromFile(filePath string) ([]byte, error) {
fileToEncrypt, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open file at path %s: %w", filePath, err)
}
defer fileToEncrypt.Close()

bytes, err := io.ReadAll(fileToEncrypt)
if err != nil {
return nil, fmt.Errorf("failed to read bytes from file at path %s: %w", filePath, err)
}
return bytes, nil
}

0 comments on commit 73baae7

Please sign in to comment.