From d6cfc58950c990297f4bd148cb40482f46ec1d13 Mon Sep 17 00:00:00 2001 From: Katrina Rogan Date: Thu, 19 Dec 2019 17:05:39 -0800 Subject: [PATCH] Add flyte_client config endpoint handler (#44) --- cmd/entrypoints/serve.go | 4 +++ pkg/rpc/config/flyte_client.go | 36 +++++++++++++++++++++++ pkg/rpc/config/flyte_client_test.go | 44 +++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 pkg/rpc/config/flyte_client.go create mode 100644 pkg/rpc/config/flyte_client_test.go diff --git a/cmd/entrypoints/serve.go b/cmd/entrypoints/serve.go index d0abe1ab11..6322b1935f 100644 --- a/cmd/entrypoints/serve.go +++ b/cmd/entrypoints/serve.go @@ -28,6 +28,7 @@ import ( "github.com/lyft/flyteadmin/pkg/config" "github.com/lyft/flyteadmin/pkg/rpc/adminservice" + rpcConfig "github.com/lyft/flyteadmin/pkg/rpc/config" "github.com/spf13/cobra" @@ -128,6 +129,9 @@ func newHTTPServer(ctx context.Context, cfg *config.ServerConfig, authContext in // This endpoint will serve the OpenAPI2 spec generated by the swagger protoc plugin, and bundled by go-bindata mux.HandleFunc("/api/v1/openapi", GetHandleOpenapiSpec(ctx)) + // Handles serving config values required to initialize a flyte-client config + mux.HandleFunc("/config/v1/flyte_client", rpcConfig.HandleFlyteCliConfigFunc(ctx, cfg)) + var gwmuxOptions = make([]runtime.ServeMuxOption, 0) // This option means that http requests are served with protobufs, instead of json. We always want this. gwmuxOptions = append(gwmuxOptions, runtime.WithMarshalerOption("application/octet-stream", &runtime.ProtoMarshaller{})) diff --git a/pkg/rpc/config/flyte_client.go b/pkg/rpc/config/flyte_client.go new file mode 100644 index 0000000000..6216cc9029 --- /dev/null +++ b/pkg/rpc/config/flyte_client.go @@ -0,0 +1,36 @@ +package config + +import ( + "context" + "encoding/json" + "net/http" + + "github.com/lyft/flyteadmin/pkg/config" + "github.com/lyft/flytestdlib/logger" +) + +const ( + clientID = "client_id" + redirectURI = "redirect_uri" + authMetadataKey = "authorization_metadata_key" +) + +func HandleFlyteCliConfigFunc(ctx context.Context, cfg *config.ServerConfig) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + configValues := map[string]string{ + clientID: cfg.Security.Oauth.ClientID, + redirectURI: cfg.Security.Oauth.CallbackURL, + authMetadataKey: cfg.Security.Oauth.GrpcAuthorizationHeader, + } + configJSON, err := json.Marshal(configValues) + if err != nil { + logger.Infof(ctx, "Failed to marshal flyte_client config to JSON with err: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + _, err = w.Write(configJSON) + if err != nil { + logger.Warningf(ctx, "Failed to write config json [%+v] with err: %v", string(configJSON), err) + } + } +} diff --git a/pkg/rpc/config/flyte_client_test.go b/pkg/rpc/config/flyte_client_test.go new file mode 100644 index 0000000000..d0e06116a8 --- /dev/null +++ b/pkg/rpc/config/flyte_client_test.go @@ -0,0 +1,44 @@ +package config + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + authConfig "github.com/lyft/flyteadmin/pkg/auth/config" + "github.com/lyft/flyteadmin/pkg/config" + "github.com/stretchr/testify/assert" +) + +func TestHandleFlyteCliConfigFunc(t *testing.T) { + testClientID := "12345" + testRedirectURI := "localhost:12345/callback" + testAuthMetadataKey := "flyte-authorization" + + handleFlyteCliConfigFunc := HandleFlyteCliConfigFunc(context.Background(), &config.ServerConfig{ + Security: config.ServerSecurityOptions{ + Oauth: authConfig.OAuthOptions{ + ClientID: testClientID, + CallbackURL: testRedirectURI, + GrpcAuthorizationHeader: testAuthMetadataKey, + }, + }, + }) + + responseRecorder := httptest.NewRecorder() + handleFlyteCliConfigFunc(responseRecorder, nil) + assert.Equal(t, http.StatusOK, responseRecorder.Code) + responseBody := responseRecorder.Body + var responseBodyMap map[string]string + err := json.Unmarshal(responseBody.Bytes(), &responseBodyMap) + if err != nil { + t.Fatalf("Failed to unmarshal response body with err: %v", err) + } + assert.EqualValues(t, map[string]string{ + clientID: testClientID, + redirectURI: testRedirectURI, + authMetadataKey: testAuthMetadataKey, + }, responseBodyMap) +}