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

derivation_preview: add new proxy for derivation preview service #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions derivation_preview.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
context "context"
"fmt"
"io"
"net/http"
"encoding/json"
"bytes"

"github.com/estuary/data-plane-gateway/auth"
"github.com/urfave/negroni"
)

func NewDerivationPreviewServer(ctx context.Context) http.Handler {
previewHandler := negroni.Classic()
previewHandler.Use(negroni.HandlerFunc(cors))
previewHandler.UseHandler(derivationPreviewHandler)

return previewHandler
}

type PreviewRequest struct {
DraftId string `json:"draft_id"`
Collection string `json:"collection"`
NumDocuments int `json:"num_documents"`
}

// Will be used with both http and https
// Inspired partially by https://gist.github.com/yowu/f7dc34bd4736a65ff28d
var derivationPreviewHandler = http.HandlerFunc(func(writer http.ResponseWriter, proxy_req *http.Request) {
// Do auth
// Pull JWT from authz header
// See auth.go:authorized()
// decodeJWT(that bearer token) -> AuthorizedClaims
claims, err := auth.AuthenticateHttpReq(proxy_req, []byte(*jwtVerificationKey))
if err != nil {
http.Error(writer, err.Error(), http.StatusUnauthorized)
return
}

var req PreviewRequest

if reqBytes, err := io.ReadAll(proxy_req.Body); err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
} else if err := json.Unmarshal(reqBytes, &req); err != nil {
http.Error(writer, err.Error(), http.StatusBadRequest)
return
}

authorization_error := auth.EnforcePrefix(claims, req.Collection)

// enforcePrefix(claims, collection_name)
// collection_name comes from actual preview request
if authorization_error != nil {
http.Error(writer, authorization_error.Error(), http.StatusForbidden)
return
}

// Call preview
reqBytes, err := json.Marshal(req)
var reqReader = bytes.NewReader(reqBytes)

httpRequest, err := http.NewRequest("POST", fmt.Sprintf("http://%s/preview", *previewAddr), reqReader)
if err != nil {
http.Error(writer, fmt.Errorf("creating request to be sent to derivation preview: %w", err).Error(), http.StatusInternalServerError)
return
}

httpRequest.Header.Add("content-type", "application/json")
httpRequest.Header.Add("authorization", proxy_req.Header.Get("authorization"))

var httpClient = http.Client{}
preview_response, preview_error := httpClient.Do(httpRequest)

if preview_error != nil {
// An error is returned if there were too many redirects or if there was an HTTP protocol error.
// A non-2xx response doesn't cause an error.
http.Error(writer, preview_error.Error(), http.StatusInternalServerError)
return
}

defer preview_response.Body.Close()
// Return result

copyHeader(writer.Header(), preview_response.Header)
writer.WriteHeader(preview_response.StatusCode)
io.Copy(writer, preview_response.Body)
})
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
brokerAddr = flag.String("broker-address", "localhost:8080", "Target broker address")
consumerAddr = flag.String("consumer-address", "localhost:9000", "Target consumer address")
inferenceAddr = flag.String("inference-address", "localhost:9090", "Target schema inference service address")
previewAddr = flag.String("preview-address", "localhost:8098", "Target derivation preview service address")
corsOrigin = flag.String("cors-origin", "*", "CORS Origin")
controlPlaneAuthUrl = flag.String("control-plane-auth-url", "", "base url to use for redirecting unauthorized requests")
jwtVerificationKey = flag.String("verification-key", "supersecret", "Key used to verify JWTs signed by the Flow Control Plane")
Expand Down Expand Up @@ -112,11 +113,13 @@ func main() {

restHandler := NewRestServer(ctx, fmt.Sprintf("localhost:%s", *tlsPort))
schemaInferenceHandler := NewSchemaInferenceServer(ctx)
derivationPreviewHandler := NewDerivationPreviewServer(ctx)

// These routes will be exposed to the public internet and used for handling both http and https requests.
publicMux := http.NewServeMux()
publicMux.Handle("/healthz", healthHandler)
publicMux.Handle("/infer_schema", schemaInferenceHandler)
publicMux.Handle("/derivation_preview", derivationPreviewHandler)
publicMux.Handle("/", restHandler)

if *controlPlaneAuthUrl == "" {
Expand Down
Loading