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

transitapi: add http handler enc/dec #1199

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jmxnzo
Copy link
Contributor

@jmxnzo jmxnzo commented Jan 31, 2025

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

  • add logic to extract name parameter of URL, specifying the workloadSecretID
  • authorize transit engine requests to name parameter with client cert policy hash
  • update seedengine logic
  • unit testing of url routing

@jmxnzo jmxnzo added no changelog PRs not listed in the release notes do not merge This shouldn't be merged at this point labels Jan 31, 2025
@jmxnzo jmxnzo marked this pull request as ready for review January 31, 2025 16:28
@jmxnzo jmxnzo requested a review from burgerdev as a code owner January 31, 2025 16:28
@jmxnzo jmxnzo changed the title transitapi: add hhtp handler enc/dec transitapi: add http handler enc/dec Jan 31, 2025
coordinator/internal/seedengine/seedengine.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/transitengine.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto.go Show resolved Hide resolved
coordinator/internal/transitengine/crypto.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto_test.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto_test.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto_test.go Outdated Show resolved Hide resolved
@jmxnzo jmxnzo force-pushed the http-handler/transit-engine-api branch 3 times, most recently from eed6fd3 to 2b3b3ce Compare February 5, 2025 14:55
@jmxnzo jmxnzo force-pushed the http-handler/transit-engine-api branch from 2b3b3ce to 9cd6b17 Compare February 6, 2025 15:11
// Copyright 2024 Edgeless Systems GmbH
// SPDX-License-Identifier: AGPL-3.0-only

package transitengine
Copy link
Contributor

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.

coordinator/internal/transitengine/transitengine.go Outdated Show resolved Hide resolved
coordinator/internal/transitengine/crypto.go Show resolved Hide resolved
if err != nil {
return nil, err
}
ciphertext := gcm.Seal(nil, nonce, plaintext, nil)
Copy link
Contributor

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) {
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

}

// symmetricDecryptRaw returns the decrypted ciphertext based on the symmetric options and encryption keys handed in.
func symmetricDecryptRaw(decKey, ciphertext []byte, opts symOpts) ([]byte, error) {
Copy link
Contributor

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

  1. The nonce is a separate output of encrypt, and a separate input of decrypt.
  2. The nonce is prepended to the ciphertext by encrypt, and stripped from the ciphertext by decrypt.
  3. 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.

Comment on lines 37 to 42
pathParts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(pathParts) < 4 || pathParts[1] != "transit" {
http.NotFound(w, r)
return
}
action := pathParts[2]
Copy link
Contributor

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
Copy link
Contributor

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.

Comment on lines +133 to +143
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
})

Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do not merge This shouldn't be merged at this point no changelog PRs not listed in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants