Skip to content

Commit

Permalink
Merge pull request #47 from castai/move_api_url_formation_to_config
Browse files Browse the repository at this point in the history
sanitize api url in config
  • Loading branch information
ernestas2k authored Dec 13, 2021
2 parents 80c2fd9 + b2b26b9 commit d57c2cd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
10 changes: 2 additions & 8 deletions internal/castai/castai.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -67,12 +66,7 @@ func NewDefaultClient() *resty.Client {

client := resty.New()

apiURL := cfg.URL
if !strings.HasPrefix(apiURL, "https://") && !strings.HasPrefix(apiURL, "http://") {
apiURL = fmt.Sprintf("https://%s", apiURL)
}

client.SetHostURL(apiURL)
client.SetHostURL(cfg.URL)
client.SetRetryCount(defaultRetryCount)
client.SetTimeout(defaultTimeout)
client.Header.Set(hdrAPIKey, cfg.Key)
Expand All @@ -90,7 +84,7 @@ func (c *client) SendDelta(ctx context.Context, clusterID string, delta *Delta)

cfg := config.Get().API

uri, err := url.Parse(fmt.Sprintf("https://%s/v1/kubernetes/clusters/%s/agent-deltas", cfg.URL, clusterID))
uri, err := url.Parse(fmt.Sprintf("%s/v1/kubernetes/clusters/%s/agent-deltas", cfg.URL, clusterID))
if err != nil {
return fmt.Errorf("invalid url: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand Down Expand Up @@ -116,6 +117,10 @@ func Get() Config {
required("API_URL")
}

if !strings.HasPrefix(cfg.API.URL, "https://") && !strings.HasPrefix(cfg.API.URL, "http://") {
cfg.API.URL = fmt.Sprintf("https://%s", cfg.API.URL)
}

if cfg.CASTAI != nil {
if cfg.CASTAI.ClusterID == "" {
requiredWhenDiscoveryDisabled("CASTAI_CLUSTER_ID")
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestConfig(t *testing.T) {
require.NoError(t, os.Setenv("API_KEY", "abc"))
require.NoError(t, os.Setenv("API_URL", "cast.ai"))
require.NoError(t, os.Setenv("API_URL", "api.cast.ai"))

require.NoError(t, os.Setenv("KUBECONFIG", "~/.kube/config"))

Expand All @@ -22,7 +22,7 @@ func TestConfig(t *testing.T) {
cfg := Get()

require.Equal(t, "abc", cfg.API.Key)
require.Equal(t, "cast.ai", cfg.API.URL)
require.Equal(t, "https://api.cast.ai", cfg.API.URL)

require.Equal(t, "~/.kube/config", cfg.Kubeconfig)

Expand Down

0 comments on commit d57c2cd

Please sign in to comment.