Skip to content

Commit

Permalink
added wrapcheck linter
Browse files Browse the repository at this point in the history
Signed-off-by: Enrico Candino <enrico.candino@gmail.com>
  • Loading branch information
enrichman committed Oct 29, 2022
1 parent 059c378 commit 3e9d3b4
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 73 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ linters:
- forbidigo

# to evaluate
- wrapcheck
- varnamelen
- noctx
- gomnd
Expand Down
19 changes: 15 additions & 4 deletions internal/cli/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"github.com/enrichman/stegosecrets/internal/decrypt"
"github.com/enrichman/stegosecrets/pkg/file"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -32,15 +33,20 @@ func newDecryptCmd() *cobra.Command {
func runDecryptCmd(cmd *cobra.Command, args []string) error {
decrypter, err := buildDecrypter()
if err != nil {
return err
return errors.Wrap(err, "failed building decrypter")
}

encryptedBytes, err := file.ReadFile(encryptedFile)
if err != nil {
return err
return errors.Wrapf(err, "failed reading file '%s'", encryptedFile)
}

return decrypter.Decrypt(encryptedBytes, encryptedFile)
err = decrypter.Decrypt(encryptedBytes, encryptedFile)
if err != nil {
return errors.Wrapf(err, "failed decrypting file '%s'", encryptedFile)
}

return nil
}

func buildDecrypter() (*decrypt.Decrypter, error) {
Expand All @@ -58,5 +64,10 @@ func buildDecrypter() (*decrypt.Decrypter, error) {
decrypterOpts = append(decrypterOpts, decrypt.WithPartialKeyImageFile(filename))
}

return decrypt.NewDecrypter(decrypterOpts...)
decrypter, err := decrypt.NewDecrypter(decrypterOpts...)
if err != nil {
return nil, errors.Wrap(err, "failed creating decrypter")
}

return decrypter, nil
}
12 changes: 9 additions & 3 deletions internal/cli/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/enrichman/stegosecrets/internal/encrypt"
"github.com/enrichman/stegosecrets/internal/log"
"github.com/enrichman/stegosecrets/pkg/file"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -38,7 +39,7 @@ func runEncryptCmd(cmd *cobra.Command, args []string) error {
encrypt.WithThreshold(keyThreshold),
)
if err != nil {
return err
return errors.Wrap(err, "failed creating encrypter")
}

if silent {
Expand All @@ -56,8 +57,13 @@ func runEncryptCmd(cmd *cobra.Command, args []string) error {
}

if err != nil {
return err
return errors.Wrapf(err, "failed getting input to encrypt '%s'", cleartextFile)
}

return encrypter.Encrypt(bytes.NewReader(toEncrypt), cleartextFile)
err = encrypter.Encrypt(bytes.NewReader(toEncrypt), cleartextFile)
if err != nil {
return errors.Wrapf(err, "failed encrypting file '%s'", cleartextFile)
}

return nil
}
18 changes: 11 additions & 7 deletions internal/cli/images.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cli

import (
"errors"
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/enrichman/stegosecrets/pkg/file"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -47,24 +47,28 @@ func runImagesCmd(cmd *cobra.Command, args []string) error {
// creates the output folder if it doesn't exists
err := os.MkdirAll(output, 0o755)
if err != nil {
return err
return errors.Wrapf(err, "failed creating output images folder '%s'", output)
}

for i := 1; i <= int(imagesNum); i++ {
resp, err := client.Get(fmt.Sprintf("https://picsum.photos/%d/%d", width, height))
url := fmt.Sprintf("https://picsum.photos/%d/%d", width, height)

resp, err := client.Get(url)
if err != nil {
return err
return errors.Wrapf(err, "failed http get request to Picsum [%s]", url)
}
defer resp.Body.Close()

bb, err := io.ReadAll(resp.Body)
if err != nil {
return err
return errors.Wrap(err, "failed reading response from Picsum")
}

err = file.WriteFile(bb, fmt.Sprintf("%s/%03d.jpg", output, i))
imageFilename := fmt.Sprintf("%s/%03d.jpg", output, i)

err = file.WriteFile(bb, imageFilename)
if err != nil {
return err
return errors.Wrapf(err, "failed writing file '%s'", imageFilename)
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -43,7 +44,7 @@ func getInputFromStdin() ([]byte, error) {

text, err := reader.ReadBytes('\n')
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed reading bytes from stdin")
}

return text, nil
Expand Down
22 changes: 14 additions & 8 deletions internal/decrypt/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/enrichman/stegosecrets/pkg/file"
sss "github.com/enrichman/stegosecrets/pkg/stego"
"github.com/pkg/errors"
)

type Decrypter struct {
Expand All @@ -22,7 +23,7 @@ func NewDecrypter(opts ...OptFunc) (*Decrypter, error) {
for _, opt := range opts {
err := opt(decrypter)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed applying options to decrypter")
}
}

Expand All @@ -33,7 +34,7 @@ func WithMasterKeyFile(filename string) OptFunc {
return func(d *Decrypter) error {
masterKey, err := file.ReadKey(filename)
if err != nil {
return err
return errors.Wrap(err, "failed reading master key file")
}

d.MasterKey = masterKey
Expand All @@ -47,7 +48,7 @@ func WithPartialKeyFiles(filenames []string) OptFunc {
for _, filename := range filenames {
err := WithPartialKeyFile(filename)(d)
if err != nil {
return err
return errors.Wrap(err, "failed reading partial key file")
}
}

Expand All @@ -59,7 +60,7 @@ func WithPartialKeyFile(filename string) OptFunc {
return func(d *Decrypter) error {
partialKey, err := file.ReadKey(filename)
if err != nil {
return err
return errors.Wrap(err, "failed reading partial key file")
}

d.Parts = append(d.Parts, sss.NewPart(partialKey))
Expand All @@ -73,7 +74,7 @@ func WithPartialKeyImageFile(filename string) OptFunc {
return func(d *Decrypter) error {
partialKey, err := file.ReadKey(filename)
if err != nil {
return err
return errors.Wrap(err, "failed reading partial key image file")
}

d.Parts = append(d.Parts, sss.NewPart(partialKey))
Expand All @@ -93,15 +94,20 @@ func (d *Decrypter) Decrypt(content []byte, filename string) error {
} else {
key, err = sss.Combine(d.Parts)
if err != nil {
return err
return errors.Wrap(err, "failed combining parts")
}
}

cleartext, err := sss.Decrypt(key, content)
if err != nil {
return err
return errors.Wrap(err, "failed decrypting content")
}

// TODO check checksum
return file.WriteFile(cleartext, strings.TrimSuffix(filename, ".enc"))
err = file.WriteFile(cleartext, strings.TrimSuffix(filename, ".enc"))
if err != nil {
return errors.Wrap(err, "failed writing decoded file")
}

return nil
}
37 changes: 19 additions & 18 deletions internal/encrypt/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ func (e *Encrypter) Encrypt(reader io.Reader, filename string) error {

masterKey, err := e.generateAndSaveMasterKey(filename)
if err != nil {
return err
return errors.Wrapf(err, "failed generating and saving master key '%s'", filename)
}

e.Logger.Debug("encryptAndSaveMessage")

err = e.encryptAndSaveMessage(masterKey, reader, filename)
if err != nil {
return err
return errors.Wrapf(err, "failed encrypting and saving message '%s'", filename)
}

if e.Parts <= 1 {
Expand All @@ -86,7 +86,7 @@ func (e *Encrypter) Encrypt(reader io.Reader, filename string) error {
if e.Parts > 1 {
err = e.splitAndSaveKey(masterKey)
if err != nil {
return err
return errors.Wrap(err, "failed splitting and saving master key")
}
}

Expand All @@ -98,16 +98,16 @@ const outDirName = "out"
func (e *Encrypter) generateAndSaveMasterKey(filename string) ([]byte, error) {
masterKey, err := sss.GenerateMasterKey()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed generating master key")
}

if err := os.MkdirAll(outDirName, 0o744); err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed creatind folder '%s'", outDirName)
}

err = file.WriteKey(masterKey, fmt.Sprintf("%s/%s.enc", outDirName, filename))
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed writing key file")
}

return masterKey, nil
Expand All @@ -116,27 +116,28 @@ func (e *Encrypter) generateAndSaveMasterKey(filename string) ([]byte, error) {
func (e *Encrypter) encryptAndSaveMessage(masterKey []byte, reader io.Reader, filename string) error {
message, err := io.ReadAll(reader)
if err != nil {
return err
return errors.Wrap(err, "failed reading message")
}

// FIX? is this a copy/paste bug?
err = file.WriteChecksum(message, fmt.Sprintf("%s/%s.enc", outDirName, filename))
if err != nil {
return err
return errors.Wrap(err, "failed writing checksum file of original message")
}

encryptedMessage, err := sss.Encrypt(masterKey, message)
if err != nil {
return err
return errors.Wrap(err, "failed encrypting message")
}

err = file.WriteFile(encryptedMessage, fmt.Sprintf("%s/%s.enc", outDirName, filename))
if err != nil {
return err
return errors.Wrap(err, "failed writing encoded file")
}

err = file.WriteChecksum(encryptedMessage, fmt.Sprintf("%s/%s.enc", outDirName, filename))
if err != nil {
return err
return errors.Wrap(err, "failed writing checksum file")
}

return nil
Expand All @@ -147,12 +148,12 @@ func (e *Encrypter) splitAndSaveKey(masterKey []byte) error {

parts, err := sss.Split(masterKey, e.Parts, e.Threshold)
if err != nil {
return err
return errors.Wrap(err, "failed splitting masterkey")
}

images, err := e.getImages(len(parts))
if err != nil {
return err
return errors.Wrap(err, "failed getting images")
}

if len(images) == 0 {
Expand All @@ -161,7 +162,7 @@ func (e *Encrypter) splitAndSaveKey(masterKey []byte) error {

err = e.saveKeysIntoImages(parts, images)
if err != nil {
return err
return errors.Wrap(err, "failed saving keys into images")
}

return nil
Expand All @@ -172,7 +173,7 @@ func (e *Encrypter) getImages(count int) ([]string, error) {

files, err := os.ReadDir(dir)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed reading folder '%s'", dir)
}

images := make([]string, 0, count)
Expand Down Expand Up @@ -209,7 +210,7 @@ func (e *Encrypter) saveKeysIntoImages(parts []sss.Part, images []string) error
// write .key file
err := file.WriteKey(part.Bytes(), partialKeyFilename)
if err != nil {
return err
return errors.Wrapf(err, "failed writing key file '%s'", partialKeyFilename)
}

// if the images are available hide the key inside them
Expand All @@ -218,12 +219,12 @@ func (e *Encrypter) saveKeysIntoImages(parts []sss.Part, images []string) error

err := image.EncodeSecretFromFile(part.Bytes(), images[i], imageOutName)
if err != nil {
return err
return errors.Wrapf(err, "failed encoding secret into image file '%s'", imageOutName)
}

err = file.WriteFileChecksum(imageOutName)
if err != nil {
return err
return errors.Wrapf(err, "failed writing checksum file '%s'", imageOutName)
}
}
}
Expand Down
Loading

0 comments on commit 3e9d3b4

Please sign in to comment.