Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NATS backend support #59

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions backends/nats/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package nats

import (
"crypto/rand"
"errors"
"golang.org/x/crypto/chacha20poly1305"
"os"
"time"
)

// Simple helper function to convert config int to time.Duration
func helperSecondsToDuration(seconds int) time.Duration {
return time.Duration(seconds) * time.Second
}

// Simple helper function to check file exists
func exists(path string, checkDirectory bool) (bool, error) {
info, err := os.Stat(path)
if !checkDirectory && err == nil && !info.IsDir() {
return true, nil
}
if checkDirectory && err == nil && info.IsDir() {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}

// Simple helper function to check file is readable
func fileExistsAndIsReadable(path string) error {
exists, err := exists(path, false)
if err != nil {
return err
}
if !exists {
return errors.New("file does not exist")
}
fil, err := os.OpenFile(path, os.O_RDONLY, 0)
if err != nil {
return err
}
fil.Close()
return nil
}

// Simple encryption helper
func helperEncrypt(key []byte, plaintext []byte) ([]byte, error) {
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot say I am charmed by undocumented DIY crypto implementations.

Also please read the points in #52 (comment)

Perhaps any client side encryption should be implemented as an swappable middleware layer between simpleblob and the application that can be used by multiple backends?

Copy link
Contributor Author

@udf2457 udf2457 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the subject of "undocumented DIY crypto implementations."

Far from the case. I would point out that it is (a) nothing but a thin-wrapper around Go stdlib crypto (b) the implementation is basically nothing more than a copy/paste of the example from the Go docs (https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305#example-NewX). The only significant difference is I don't panic() but return the error. 😉

Sadly NATS does not have built-in object-level SSE-C otherwise I would have gladly used that.

Thanks for all the feedback though, much appreciated food for thought.

var ciphertext []byte
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(plaintext)+aead.Overhead())
_, err = rand.Read(nonce)
if err != nil {
return nil, err
}
ciphertext = aead.Seal(nonce, nonce, plaintext, nil)
return ciphertext, nil
}

// Simple decryption helper
func helperDecrypt(key []byte, ciphertext []byte) ([]byte, error) {
aead, err := chacha20poly1305.NewX(key)
if err != nil {
return nil, err
}
if len(ciphertext) < aead.NonceSize() {
return nil, errors.New("ciphertext too short")
}
nonce, cipher := ciphertext[:aead.NonceSize()], ciphertext[aead.NonceSize():]
return aead.Open(nil, nonce, cipher, nil)
}
Loading
Loading