Skip to content

Commit 5c77447

Browse files
committed
feat: add --insecure flag
Adding a flag to allow insecure https requests. This is useful for local development. fixes #18.
1 parent 0049225 commit 5c77447

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

cmd/aepcli/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func main() {
3333
func aepcli(args []string) (int, error) {
3434
var dryRun bool
3535
var logHTTP bool
36+
var insecure bool
3637
var logLevel string
3738
var fileAliasOrCore string
3839
var additionalArgs []string
@@ -63,6 +64,7 @@ func aepcli(args []string) (int, error) {
6364
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set the logging level (debug, info, warn, error)")
6465
rootCmd.PersistentFlags().BoolVar(&logHTTP, "log-http", false, "Set to true to log HTTP requests. This can be helpful when attempting to write your own code or debug.")
6566
rootCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Set to true to not make any changes. This can be helpful when paired with log-http to just view http requests instead of perform them.")
67+
rootCmd.PersistentFlags().BoolVar(&insecure, "insecure", false, "Set to true to skip TLS certificate verification. Use with caution.")
6668
rootCmd.PersistentFlags().StringVar(&pathPrefix, "path-prefix", "", "Specify a path prefix that is prepended to all paths in the openapi schema. This will strip them when evaluating the resource hierarchy paths.")
6769
rootCmd.PersistentFlags().StringVar(&serverURL, "server-url", "", "Specify a URL to use for the server. If not specified, the first server URL in the OpenAPI definition will be used.")
6870
rootCmd.PersistentFlags().StringVar(&configFileVar, "config", "", "Path to config file")
@@ -119,7 +121,7 @@ func aepcli(args []string) (int, error) {
119121
return CODE_ERR, fmt.Errorf("unable to parse headers: %w", err)
120122
}
121123

122-
s = service.NewServiceCommand(api, headersMap, dryRun, logHTTP)
124+
s = service.NewServiceCommand(api, headersMap, dryRun, logHTTP, insecure)
123125

124126
result, err := s.Execute(additionalArgs)
125127
returnCode := CODE_OK

internal/service/service.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"bytes"
5+
"crypto/tls"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -15,20 +16,31 @@ import (
1516
)
1617

1718
type ServiceCommand struct {
18-
API api.API
19-
Headers map[string]string
20-
DryRun bool
21-
LogHTTP bool
22-
Client *http.Client
19+
API api.API
20+
Headers map[string]string
21+
DryRun bool
22+
LogHTTP bool
23+
Insecure bool
24+
Client *http.Client
2325
}
2426

25-
func NewServiceCommand(api *api.API, headers map[string]string, dryRun bool, logHTTP bool) *ServiceCommand {
27+
func NewServiceCommand(api *api.API, headers map[string]string, dryRun bool, logHTTP bool, insecure bool) *ServiceCommand {
28+
client := &http.Client{}
29+
30+
if insecure {
31+
tr := &http.Transport{
32+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
33+
}
34+
client.Transport = tr
35+
}
36+
2637
return &ServiceCommand{
27-
API: *api,
28-
Headers: headers,
29-
DryRun: dryRun,
30-
LogHTTP: logHTTP,
31-
Client: &http.Client{},
38+
API: *api,
39+
Headers: headers,
40+
DryRun: dryRun,
41+
LogHTTP: logHTTP,
42+
Insecure: insecure,
43+
Client: client,
3244
}
3345
}
3446

internal/service/service_test.go

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package service
22

33
import (
4+
"net/http"
45
"strings"
56
"testing"
67
)
78

89
func TestService_ExecuteCommand_ListResources(t *testing.T) {
910
// Test setup
10-
svc := NewServiceCommand(getTestAPI(), nil, false, false)
11+
svc := NewServiceCommand(getTestAPI(), nil, false, false, false)
1112

1213
tests := []struct {
1314
name string
@@ -58,3 +59,53 @@ func TestService_ExecuteCommand_ListResources(t *testing.T) {
5859
})
5960
}
6061
}
62+
63+
func TestNewServiceCommand_Insecure(t *testing.T) {
64+
tests := []struct {
65+
name string
66+
insecure bool
67+
}{
68+
{
69+
name: "secure client",
70+
insecure: false,
71+
},
72+
{
73+
name: "insecure client",
74+
insecure: true,
75+
},
76+
}
77+
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
svc := NewServiceCommand(getTestAPI(), nil, false, false, tt.insecure)
81+
82+
if svc.Insecure != tt.insecure {
83+
t.Errorf("NewServiceCommand() insecure = %v, want %v", svc.Insecure, tt.insecure)
84+
}
85+
86+
// Check if the client has the correct TLS configuration
87+
if tt.insecure {
88+
transport, ok := svc.Client.Transport.(*http.Transport)
89+
if !ok {
90+
t.Error("Expected HTTP transport to be set for insecure client")
91+
return
92+
}
93+
if transport.TLSClientConfig == nil {
94+
t.Error("Expected TLS config to be set for insecure client")
95+
return
96+
}
97+
if !transport.TLSClientConfig.InsecureSkipVerify {
98+
t.Error("Expected InsecureSkipVerify to be true for insecure client")
99+
}
100+
} else {
101+
// For secure clients, we should have the default transport or no custom transport
102+
if svc.Client.Transport != nil {
103+
transport, ok := svc.Client.Transport.(*http.Transport)
104+
if ok && transport.TLSClientConfig != nil && transport.TLSClientConfig.InsecureSkipVerify {
105+
t.Error("Expected InsecureSkipVerify to be false for secure client")
106+
}
107+
}
108+
}
109+
})
110+
}
111+
}

0 commit comments

Comments
 (0)