-
Notifications
You must be signed in to change notification settings - Fork 9
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
transitapi: add http handler enc/dec #1199
base: main
Are you sure you want to change the base?
Conversation
eed6fd3
to
2b3b3ce
Compare
2b3b3ce
to
9cd6b17
Compare
// Copyright 2024 Edgeless Systems GmbH | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package transitengine |
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.
There should be a package comment that explains what this is and where it comes from.
if err != nil { | ||
return nil, err | ||
} | ||
ciphertext := gcm.Seal(nil, nonce, plaintext, nil) |
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.
We want to pass additionalData here, right?
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCryptoAPICyclic(t *testing.T) { |
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.
This does not test crypto.go
, but transitengine.go
. May I suggest moving this test to transitengine_test.go
and add a cyclic test for symmetric*cryptRaw
here?
|
||
const ( | ||
// aesGCMNonceSize specifies the default nonce size in bytes used in AES GCM. | ||
aesGCMNonceSize = 12 |
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.
https://pkg.go.dev/crypto/cipher#AEAD has a NonceSize()
.
} | ||
|
||
// symmetricDecryptRaw returns the decrypted ciphertext based on the symmetric options and encryption keys handed in. | ||
func symmetricDecryptRaw(decKey, ciphertext []byte, opts symOpts) ([]byte, error) { |
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.
It's a bit weird that the encryption function outputs nonce:ciphertext
but we get the nonce here through symOpts
. I'd have expected either
- The nonce is a separate output of encrypt, and a separate input of decrypt.
- The nonce is prepended to the ciphertext by encrypt, and stripped from the ciphertext by decrypt.
- encrypt returns a struct containing ciphertext and nonce, decrypt accepts such struct. This would likely replace
prefixb64Ciphertext
.
No. 3 might actually be best considering that we want to serialize the output - could be a method on that struct.
pathParts := strings.Split(strings.Trim(r.URL.Path, "/"), "/") | ||
if len(pathParts) < 4 || pathParts[1] != "transit" { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
action := pathParts[2] |
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.
You can register the routes directly:
mux.Handle("/transit/encrypt/{name}", getEncryptHandler())
mux.Handle("/transit/decrypt/{name}", getDecryptHandler())
Then get the secret name with https://pkg.go.dev/net/http#Request.PathValue.
if err := json.NewDecoder(r.Body).Decode(&plaintext); err != nil { | ||
return b64Plaintext{}, symOpts{}, err | ||
} | ||
// TODO(jmxnzo): Read symOpts from HTTP request params |
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 think we should also fail if there's an option that we don't implement/understand.
eg.Go(func() error { | ||
mux := transitengine.NewTransitEngineAPI(meshAuth, logger) | ||
logger.Info("Transit Engine API initialized") | ||
port := 8200 | ||
fmt.Printf("Serving transit engine API on port %d\n", port) | ||
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), mux); err != nil { | ||
logger.Error("Failed to start transit engine API", "err", err) | ||
} | ||
return nil | ||
}) | ||
|
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.
This should only be enabled after authorization.
This PR adds basic http handling functionality for the encrypt and decrypt endpoints of the transit engine API, allowing the auto-unsealing process for user-managed Vaults. Additionally a cyclic cryptographic unit test, testing the implemented handler functions for encrypt and decrypt was added.
ToDo's