-
Notifications
You must be signed in to change notification settings - Fork 5
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.