diff --git a/README.md b/README.md index d08547c..200bd60 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ CDK_TOKEN= ### Commands Usage ```` You need to define the CDK_TOKEN and CDK_BASE_URL environment variables to use this tool. -You can also use the CDK_KEY,CDK_CERT instead of --key and --cert flags to use a certificate for tls authentication. +You can also use the CDK_KEY,CDK_CERT to use a certificate for tls authentication. If you have an untrusted certificate you can use the CDK_INSECURE=true variable to disable tls verification Usage: diff --git a/client/client.go b/client/client.go index e230eed..ed3fa34 100644 --- a/client/client.go +++ b/client/client.go @@ -20,10 +20,10 @@ type Client struct { kinds schema.KindCatalog } -func Make(token string, baseUrl string, debug bool, key, cert string) (*Client, error) { +func Make(token string, baseUrl string, debug bool, key, cert string, insecure bool) (*Client, error) { //token is set later because it's not mandatory for getting the openapi and parsing different kind restyClient := resty.New().SetDebug(debug).SetHeader("X-CDK-CLIENT", "CLI/"+utils.GetConduktorVersion()) - + if (key == "" && cert != "") || (key != "" && cert == "") { return nil, fmt.Errorf("key and cert must be provided together") } else if key != "" && cert != "" { @@ -48,6 +48,10 @@ func Make(token string, baseUrl string, debug bool, key, cert string) (*Client, //so aim is not fail when CDK_TOKEN is not set before printing the cmd help } + if insecure { + result.IgnoreUntrustedCertificate() + } + err := result.initKindFromApi() if err != nil { fmt.Fprintf(os.Stderr, "Cannot init kinds from api: %s\nLet's switch to default from ctl binary\n", err) @@ -65,15 +69,12 @@ func MakeFromEnv() (*Client, error) { debug := strings.ToLower(os.Getenv("CDK_DEBUG")) == "true" key := os.Getenv("CDK_KEY") cert := os.Getenv("CDK_CERT") + insecure := strings.ToLower(os.Getenv("CDK_INSECURE")) == "true" - client, err := Make("", baseUrl, debug, key, cert) + client, err := Make("", baseUrl, debug, key, cert, insecure) if err != nil { return nil, fmt.Errorf("Cannot create client: %s", err) } - insecure := strings.ToLower(os.Getenv("CDK_INSECURE")) == "true" - if insecure { - client.IgnoreUntrustedCertificate() - } return client, nil } diff --git a/client/client_test.go b/client/client_test.go index 31a3910..0fad931 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -11,7 +11,7 @@ func TestApplyShouldWork(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -54,7 +54,7 @@ func TestApplyWithDryModeShouldWork(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -92,7 +92,7 @@ func TestApplyShouldFailIfNo2xx(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -130,7 +130,7 @@ func TestGetShouldWork(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -162,7 +162,7 @@ func TestGetShouldFailIfN2xx(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -193,7 +193,7 @@ func TestDescribeShouldWork(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -225,7 +225,7 @@ func TestDescribeShouldFailIfNo2xx(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl/api" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -256,7 +256,7 @@ func TestDeleteShouldWork(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } @@ -287,7 +287,7 @@ func TestDeleteShouldFailOnNot2XX(t *testing.T) { defer httpmock.Reset() baseUrl := "http://baseUrl" token := "aToken" - client, err := Make(token, baseUrl, false, "", "") + client, err := Make(token, baseUrl, false, "", "", false) if err != nil { panic(err) } diff --git a/cmd/root.go b/cmd/root.go index 8a47b10..481a00e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,7 +25,7 @@ var rootCmd = &cobra.Command{ Use: "conduktor", Short: "Command line tools for conduktor", Long: `You need to define the CDK_TOKEN and CDK_BASE_URL environment variables to use this tool. -You can also use the CDK_KEY,CDK_CERT instead of --key and --cert flags to use a certificate for tls authentication. +You can also use the CDK_KEY,CDK_CERT to use a certificate for tls authentication. If you have an untrusted certificate you can use the CDK_INSECURE=true variable to disable tls verification`, PersistentPreRun: func(cmd *cobra.Command, args []string) { if *debug {