forked from flyteorg/flyte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flyte_client config endpoint handler (flyteorg#44)
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |