From 015e1cbdb9669b280e597dbcfe7599d4ba269d52 Mon Sep 17 00:00:00 2001 From: Christian Zunker Date: Tue, 5 Dec 2023 12:18:04 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=B9=20Allow=20to=20override=20the?= =?UTF-8?q?=20API=20endpoint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #9 Signed-off-by: Christian Zunker --- README.md | 8 ++++++++ gen/gen.go | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 34fc24e..eb55fa8 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,14 @@ make test For any requests, bug or comments, please [open an issue][issues] or [submit a pull request][pulls]. +## Development + +When developing new APIs locally, you can overwrite the API endpoint. +Specify `MONDOO_API_ENDPOINT` environment variable for the `generate` command, e.g.,: +``` +MONDOO_API_ENDPOINT=http://127.0.0.1 make generate +``` + ## Kudos This implementation is heavily inspired by the [GitHub GraphQL Go Client](https://github.com/shurcooL/githubv4). diff --git a/gen/gen.go b/gen/gen.go index edfdd63..8fb6841 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -13,6 +13,7 @@ import ( "io" "log" "net/http" + "net/url" "os" "path/filepath" "sort" @@ -176,10 +177,19 @@ fragment TypeRef on __Type { } } ` + apiEndpoint := "https://" + apiHost + "/query" + endpoint, ok := os.LookupEnv("MONDOO_API_ENDPOINT") + if ok { + apiEndpoint, err = url.JoinPath(endpoint, "/query") + if err != nil { + log.Fatalf("invalid MONDOO_API_ENDPOINT: %v", err) + } + } + fmt.Printf("using endpoint %s\n", apiEndpoint) // do introspection query req, err := http.NewRequest( "POST", - "https://"+apiHost+"/query", + apiEndpoint, strings.NewReader(`{"query":`+strconv.Quote(introspection)+`}`), ) if err != nil { From 80b5674d645758a2bddd042a838a42dbaf64dfdf Mon Sep 17 00:00:00 2001 From: Christian Zunker Date: Tue, 5 Dec 2023 14:16:03 +0100 Subject: [PATCH 2/2] Add url parsing for both cases Signed-off-by: Christian Zunker --- gen/gen.go | 34 +++++++++++++++++++++++++--------- gen/gen_test.go | 23 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/gen/gen.go b/gen/gen.go index 8fb6841..c679185 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -75,7 +75,6 @@ func generateSchema(token string, basePath string) error { // loadSchema loads the GraphQL schema from the Mondoo API. func loadSchema(token string) (schema interface{}, err error) { - apiHost := "us.api.mondoo.com" introspection := ` { __schema { @@ -177,14 +176,7 @@ fragment TypeRef on __Type { } } ` - apiEndpoint := "https://" + apiHost + "/query" - endpoint, ok := os.LookupEnv("MONDOO_API_ENDPOINT") - if ok { - apiEndpoint, err = url.JoinPath(endpoint, "/query") - if err != nil { - log.Fatalf("invalid MONDOO_API_ENDPOINT: %v", err) - } - } + apiEndpoint, apiHost := getAPIEndpoint() fmt.Printf("using endpoint %s\n", apiEndpoint) // do introspection query req, err := http.NewRequest( @@ -348,3 +340,27 @@ func parseTemplate(text string) *template.Template { }, }).Parse(text)) } + +func getAPIEndpoint() (string, string) { + apiHost := "us.api.mondoo.com" + apiEndpoint, err := url.JoinPath("https://", apiHost, "/query") + if err != nil { + log.Fatalf("invalid MONDOO_API_ENDPOINT: %v", err) + } + endpoint, ok := os.LookupEnv("MONDOO_API_ENDPOINT") + if ok { + if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") { + endpoint = "https://" + endpoint + } + apiEndpoint, err = url.JoinPath(endpoint, "/query") + if err != nil { + log.Fatalf("invalid MONDOO_API_ENDPOINT: %v", err) + } + } + parsedUrl, err := url.Parse(apiEndpoint) + if err != nil { + log.Fatalf("invalid API url: %v", err) + } + + return apiEndpoint, parsedUrl.Host +} diff --git a/gen/gen_test.go b/gen/gen_test.go index 4119e0c..87097b2 100644 --- a/gen/gen_test.go +++ b/gen/gen_test.go @@ -18,3 +18,26 @@ func TestRun(t *testing.T) { err := generateSchema(token, "..") require.NoError(t, err) } +func TestGetAPIEndpoint(t *testing.T) { + // Test case 1: MONDOO_API_ENDPOINT not set + expectedEndpoint := "https://us.api.mondoo.com/query" + expectedHost := "us.api.mondoo.com" + actualEndpoint, actualHost := getAPIEndpoint() + require.Equal(t, expectedEndpoint, actualEndpoint, "Unexpected API endpoint") + require.Equal(t, expectedHost, actualHost, "Unexpected host") + + // Test case 2: MONDOO_API_ENDPOINT set + os.Setenv("MONDOO_API_ENDPOINT", "custom.api.endpoint") + expectedEndpoint = "https://custom.api.endpoint/query" + expectedHost = "custom.api.endpoint" + actualEndpoint, actualHost = getAPIEndpoint() + require.Equal(t, expectedEndpoint, actualEndpoint, "Unexpected API endpoint") + require.Equal(t, expectedHost, actualHost, "Unexpected host") + + os.Setenv("MONDOO_API_ENDPOINT", "http://custom.api.endpoint:1234") + expectedEndpoint = "http://custom.api.endpoint:1234/query" + expectedHost = "custom.api.endpoint:1234" + actualEndpoint, actualHost = getAPIEndpoint() + require.Equal(t, expectedEndpoint, actualEndpoint, "Unexpected API endpoint") + require.Equal(t, expectedHost, actualHost, "Unexpected host") +}