Skip to content

Commit 53a99c3

Browse files
authored
Verify cortex operator with endpoint check (#877)
1 parent cae90f8 commit 53a99c3

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

cli/cmd/errors.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
ErrClusterRefresh = "cli.cluster_refresh"
5555
ErrClusterDown = "cli.cluster_down"
5656
ErrDuplicateCLIEnvNames = "cli.duplicate_cli_env_names"
57+
ErrInvalidOperatorEndpoint = "cli.invalid_operator_endpoint"
5758
)
5859

5960
func ErrorCLINotConfigured(env string) error {
@@ -211,3 +212,10 @@ func ErrorDuplicateCLIEnvNames(environment string) error {
211212
Message: fmt.Sprintf("duplicate environment names: %s is defined more than once", s.UserStr(environment)),
212213
})
213214
}
215+
216+
func ErrorInvalidOperatorEndpoint(endpoint string) error {
217+
return errors.WithStack(&errors.Error{
218+
Kind: ErrInvalidOperatorEndpoint,
219+
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),
220+
})
221+
}

cli/cmd/lib_cli_config.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"crypto/tls"
2021
"fmt"
22+
"net/http"
2123
"os"
2224

2325
cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
2426
"github.com/cortexlabs/cortex/pkg/lib/errors"
27+
"github.com/cortexlabs/cortex/pkg/lib/exit"
2528
"github.com/cortexlabs/cortex/pkg/lib/files"
2629
"github.com/cortexlabs/cortex/pkg/lib/prompt"
2730
"github.com/cortexlabs/cortex/pkg/lib/sets/strset"
31+
"github.com/cortexlabs/cortex/pkg/lib/urls"
2832
"github.com/cortexlabs/yaml"
2933
)
3034

@@ -115,7 +119,7 @@ func cliEnvPromptValidation(defaults *CLIEnvConfig) *cr.PromptValidation {
115119
StringValidation: &cr.StringValidation{
116120
Required: true,
117121
Default: defaults.OperatorEndpoint,
118-
Validator: cr.GetURLValidator(false, false),
122+
Validator: validateOperatorEndpoint,
119123
},
120124
},
121125
{
@@ -144,6 +148,44 @@ func cliEnvPromptValidation(defaults *CLIEnvConfig) *cr.PromptValidation {
144148
}
145149
}
146150

151+
func validateOperatorEndpoint(endpoint string) (string, error) {
152+
url, err := cr.GetURLValidator(false, false)(endpoint)
153+
if err != nil {
154+
return "", err
155+
}
156+
157+
parsedURL, err := urls.Parse(url)
158+
if err != nil {
159+
return "", err
160+
}
161+
162+
parsedURL.Scheme = "https"
163+
164+
url = parsedURL.String()
165+
166+
req, err := http.NewRequest("GET", urls.Join(url, "/verifycortex"), nil)
167+
if err != nil {
168+
return "", errors.Wrap(err, "verifying operator endpoint", url)
169+
}
170+
req.Header.Set("Content-Type", "application/json")
171+
172+
client := http.Client{
173+
Transport: &http.Transport{
174+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
175+
},
176+
}
177+
response, err := client.Do(req)
178+
if err != nil {
179+
exit.Error(ErrorInvalidOperatorEndpoint(url))
180+
}
181+
182+
if response.StatusCode != 200 {
183+
exit.Error(ErrorInvalidOperatorEndpoint(url))
184+
}
185+
186+
return url, nil
187+
}
188+
147189
func readTelemetryConfig() (bool, error) {
148190
cliConfig, err := readCLIConfig()
149191
if err != nil {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2020 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package endpoints
18+
19+
import (
20+
"net/http"
21+
)
22+
23+
func VerifyCortex(w http.ResponseWriter, r *http.Request) {
24+
respond(w, "ok")
25+
}

pkg/operator/main.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,25 @@ func main() {
3939
}
4040

4141
router := mux.NewRouter()
42-
router.Use(endpoints.PanicMiddleware)
43-
router.Use(endpoints.ClientIDMiddleware)
44-
router.Use(endpoints.APIVersionCheckMiddleware)
45-
router.Use(endpoints.AuthMiddleware)
46-
47-
router.HandleFunc("/info", endpoints.Info).Methods("GET")
48-
router.HandleFunc("/deploy", endpoints.Deploy).Methods("POST")
49-
router.HandleFunc("/refresh/{apiName}", endpoints.Refresh).Methods("POST")
50-
router.HandleFunc("/delete/{apiName}", endpoints.Delete).Methods("DELETE")
51-
router.HandleFunc("/get", endpoints.GetAPIs).Methods("GET")
52-
router.HandleFunc("/get/{apiName}", endpoints.GetAPI).Methods("GET")
53-
router.HandleFunc("/logs/{apiName}", endpoints.ReadLogs)
42+
43+
routerWithoutAuth := router.NewRoute().Subrouter()
44+
routerWithoutAuth.Use(endpoints.PanicMiddleware)
45+
routerWithoutAuth.HandleFunc("/verifycortex", endpoints.VerifyCortex).Methods("GET")
46+
47+
routerWithAuth := router.NewRoute().Subrouter()
48+
49+
routerWithAuth.Use(endpoints.PanicMiddleware)
50+
routerWithAuth.Use(endpoints.ClientIDMiddleware)
51+
routerWithAuth.Use(endpoints.APIVersionCheckMiddleware)
52+
routerWithAuth.Use(endpoints.AuthMiddleware)
53+
54+
routerWithAuth.HandleFunc("/info", endpoints.Info).Methods("GET")
55+
routerWithAuth.HandleFunc("/deploy", endpoints.Deploy).Methods("POST")
56+
routerWithAuth.HandleFunc("/refresh/{apiName}", endpoints.Refresh).Methods("POST")
57+
routerWithAuth.HandleFunc("/delete/{apiName}", endpoints.Delete).Methods("DELETE")
58+
routerWithAuth.HandleFunc("/get", endpoints.GetAPIs).Methods("GET")
59+
routerWithAuth.HandleFunc("/get/{apiName}", endpoints.GetAPI).Methods("GET")
60+
routerWithAuth.HandleFunc("/logs/{apiName}", endpoints.ReadLogs)
5461

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

0 commit comments

Comments
 (0)