Skip to content

Commit

Permalink
Fix issue with insecure (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
strokyl authored Apr 29, 2024
1 parent e9a2ae3 commit d06b763
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CDK_TOKEN=<admin-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:
Expand Down
15 changes: 8 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
18 changes: 9 additions & 9 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d06b763

Please sign in to comment.