Skip to content

Commit

Permalink
Add flyte_client config endpoint handler (flyteorg#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
katrogan authored Dec 20, 2019
1 parent c0b828d commit d6cfc58
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/entrypoints/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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{}))
Expand Down
36 changes: 36 additions & 0 deletions pkg/rpc/config/flyte_client.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
44 changes: 44 additions & 0 deletions pkg/rpc/config/flyte_client_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d6cfc58

Please sign in to comment.