Skip to content
8 changes: 8 additions & 0 deletions cli/cmd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
ErrClusterRefresh = "cli.cluster_refresh"
ErrClusterDown = "cli.cluster_down"
ErrDuplicateCLIEnvNames = "cli.duplicate_cli_env_names"
ErrInvalidOperatorEndpoint = "cli.invalid_operator_endpoint"
)

func ErrorCLINotConfigured(env string) error {
Expand Down Expand Up @@ -211,3 +212,10 @@ func ErrorDuplicateCLIEnvNames(environment string) error {
Message: fmt.Sprintf("duplicate environment names: %s is defined more than once", s.UserStr(environment)),
})
}

func ErrorInvalidOperatorEndpoint(endpoint string) error {
return errors.WithStack(&errors.Error{
Kind: ErrInvalidOperatorEndpoint,
Message: fmt.Sprintf("%s is not a cortex operator endpoint; run `cortex cluster info` to show your operator endpoint or run `cortex cluster up` to spin up a new cluster", endpoint),
})
}
44 changes: 43 additions & 1 deletion cli/cmd/lib_cli_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ limitations under the License.
package cmd

import (
"crypto/tls"
"fmt"
"net/http"
"os"

cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/exit"
"github.com/cortexlabs/cortex/pkg/lib/files"
"github.com/cortexlabs/cortex/pkg/lib/prompt"
"github.com/cortexlabs/cortex/pkg/lib/sets/strset"
"github.com/cortexlabs/cortex/pkg/lib/urls"
"github.com/cortexlabs/yaml"
)

Expand Down Expand Up @@ -115,7 +119,7 @@ func cliEnvPromptValidation(defaults *CLIEnvConfig) *cr.PromptValidation {
StringValidation: &cr.StringValidation{
Required: true,
Default: defaults.OperatorEndpoint,
Validator: cr.GetURLValidator(false, false),
Validator: validateOperatorEndpoint,
},
},
{
Expand Down Expand Up @@ -144,6 +148,44 @@ func cliEnvPromptValidation(defaults *CLIEnvConfig) *cr.PromptValidation {
}
}

func validateOperatorEndpoint(endpoint string) (string, error) {
url, err := cr.GetURLValidator(false, false)(endpoint)
if err != nil {
return "", err
}

parsedURL, err := urls.Parse(url)
if err != nil {
return "", err
}

parsedURL.Scheme = "https"

url = parsedURL.String()

req, err := http.NewRequest("GET", urls.Join(url, "/verifycortex"), nil)
if err != nil {
return "", errors.Wrap(err, "verifying operator endpoint", url)
}
req.Header.Set("Content-Type", "application/json")

client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
response, err := client.Do(req)
if err != nil {
exit.Error(ErrorInvalidOperatorEndpoint(url))
}

if response.StatusCode != 200 {
exit.Error(ErrorInvalidOperatorEndpoint(url))
}

return url, nil
}

func readTelemetryConfig() (bool, error) {
cliConfig, err := readCLIConfig()
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/operator/endpoints/verify_cortex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright 2020 Cortex Labs, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package endpoints

import (
"net/http"
)

func VerifyCortex(w http.ResponseWriter, r *http.Request) {
respond(w, "ok")
}
31 changes: 19 additions & 12 deletions pkg/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,25 @@ func main() {
}

router := mux.NewRouter()
router.Use(endpoints.PanicMiddleware)
router.Use(endpoints.ClientIDMiddleware)
router.Use(endpoints.APIVersionCheckMiddleware)
router.Use(endpoints.AuthMiddleware)

router.HandleFunc("/info", endpoints.Info).Methods("GET")
router.HandleFunc("/deploy", endpoints.Deploy).Methods("POST")
router.HandleFunc("/refresh/{apiName}", endpoints.Refresh).Methods("POST")
router.HandleFunc("/delete/{apiName}", endpoints.Delete).Methods("DELETE")
router.HandleFunc("/get", endpoints.GetAPIs).Methods("GET")
router.HandleFunc("/get/{apiName}", endpoints.GetAPI).Methods("GET")
router.HandleFunc("/logs/{apiName}", endpoints.ReadLogs)

routerWithoutAuth := router.NewRoute().Subrouter()
routerWithoutAuth.Use(endpoints.PanicMiddleware)
routerWithoutAuth.HandleFunc("/verifycortex", endpoints.VerifyCortex).Methods("GET")

routerWithAuth := router.NewRoute().Subrouter()

routerWithAuth.Use(endpoints.PanicMiddleware)
routerWithAuth.Use(endpoints.ClientIDMiddleware)
routerWithAuth.Use(endpoints.APIVersionCheckMiddleware)
routerWithAuth.Use(endpoints.AuthMiddleware)

routerWithAuth.HandleFunc("/info", endpoints.Info).Methods("GET")
routerWithAuth.HandleFunc("/deploy", endpoints.Deploy).Methods("POST")
routerWithAuth.HandleFunc("/refresh/{apiName}", endpoints.Refresh).Methods("POST")
routerWithAuth.HandleFunc("/delete/{apiName}", endpoints.Delete).Methods("DELETE")
routerWithAuth.HandleFunc("/get", endpoints.GetAPIs).Methods("GET")
routerWithAuth.HandleFunc("/get/{apiName}", endpoints.GetAPI).Methods("GET")
routerWithAuth.HandleFunc("/logs/{apiName}", endpoints.ReadLogs)

log.Print("Running on port " + _operatorPortStr)
log.Fatal(http.ListenAndServe(":"+_operatorPortStr, router))
Expand Down