1515package auth_providers
1616
1717import (
18+ "bytes"
1819 "crypto/tls"
1920 "crypto/x509"
2021 "encoding/json"
2122 "encoding/pem"
2223 "fmt"
2324 "io"
25+ "log"
2426 "net/http"
2527 "os"
2628 "path/filepath"
@@ -54,6 +56,9 @@ const (
5456 // DefaultClientTimeout is the default timeout for the http Client
5557 DefaultClientTimeout = 60
5658
59+ //Default HTTP protocol
60+ DefaultHttpProtocol = "https"
61+
5762 // EnvKeyfactorHostName is the environment variable for the Keyfactor Command hostname
5863 EnvKeyfactorHostName = "KEYFACTOR_HOSTNAME"
5964
@@ -139,6 +144,9 @@ type CommandAuthConfig struct {
139144 // Debug
140145 Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`
141146
147+ // HTTPProtocol
148+ HttpProtocol string `json:"http_protocol,omitempty" yaml:"http_protocol,omitempty"`
149+
142150 // HttpClient is the http Client to be used for authentication to Keyfactor Command API
143151 HttpClient * http.Client
144152 //DefaultHttpClient *http.Client
@@ -159,6 +167,12 @@ func cleanHostName(hostName string) string {
159167
160168// WithCommandHostName sets the hostname for authentication to Keyfactor Command API.
161169func (c * CommandAuthConfig ) WithCommandHostName (hostName string ) * CommandAuthConfig {
170+
171+ //check for http or https prefix
172+ if strings .Contains (hostName , "http://" ) {
173+ c .HttpProtocol = "http"
174+ }
175+
162176 hostName = cleanHostName (hostName )
163177 c .CommandHostName = hostName
164178 return c
@@ -261,6 +275,7 @@ func (c *CommandAuthConfig) ValidateAuthConfig() error {
261275 c .CommandAPIPath = DefaultCommandAPIPath
262276 }
263277 }
278+ c .CommandAPIPath = strings .Trim (c .CommandAPIPath , "/" )
264279 if c .HttpClientTimeout <= 0 {
265280 if timeout , ok := os .LookupEnv (EnvKeyfactorClientTimeout ); ok {
266281 configTimeout , tErr := strconv .Atoi (timeout )
@@ -449,6 +464,10 @@ func (c *CommandAuthConfig) Authenticate() error {
449464 if c .HttpClient == nil {
450465 c .SetClient (nil )
451466 }
467+
468+ if c .HttpProtocol == "" {
469+ c .HttpProtocol = DefaultHttpProtocol
470+ }
452471 //create headers for request
453472 headers := map [string ]string {
454473 "Content-Type" : "application/json" ,
@@ -462,11 +481,13 @@ func (c *CommandAuthConfig) Authenticate() error {
462481 }
463482
464483 endPoint := fmt .Sprintf (
465- "https://%s/%s/Status/Endpoints" ,
484+ "%s://%s/%s/Status/Endpoints" ,
485+ c .HttpProtocol ,
466486 c .CommandHostName ,
467487 //c.CommandPort,
468488 c .CommandAPIPath ,
469489 )
490+ log .Printf ("[DEBUG] testing auth using endpoint %s " , endPoint )
470491
471492 // create request object
472493 req , rErr := http .NewRequest ("GET" , endPoint , nil )
@@ -480,6 +501,11 @@ func (c *CommandAuthConfig) Authenticate() error {
480501 }
481502
482503 c .HttpClient .Timeout = time .Duration (c .HttpClientTimeout ) * time .Second
504+ curlStr , cErr := RequestToCurl (req )
505+ if cErr == nil {
506+ log .Printf ("[TRACE] curl command: %s" , curlStr )
507+ }
508+
483509 cResp , cErr := c .HttpClient .Do (req )
484510 if cErr != nil {
485511 return cErr
@@ -759,3 +785,33 @@ type contextKey string
759785// fmt.Println("Authentication successful")
760786// }
761787// }
788+
789+ func RequestToCurl (req * http.Request ) (string , error ) {
790+ var curlCommand strings.Builder
791+
792+ // Start with the cURL command
793+ curlCommand .WriteString (fmt .Sprintf ("curl -X %s " , req .Method ))
794+
795+ // Add the URL
796+ curlCommand .WriteString (fmt .Sprintf ("%q " , req .URL .String ()))
797+
798+ // Add headers
799+ for name , values := range req .Header {
800+ for _ , value := range values {
801+ curlCommand .WriteString (fmt .Sprintf ("-H %q " , fmt .Sprintf ("%s: %s" , name , value )))
802+ }
803+ }
804+
805+ // Add the body if it exists
806+ if req .Method == http .MethodPost || req .Method == http .MethodPut {
807+ body , err := io .ReadAll (req .Body )
808+ if err != nil {
809+ return "" , err
810+ }
811+ req .Body = io .NopCloser (bytes .NewBuffer (body )) // Restore the request body
812+
813+ curlCommand .WriteString (fmt .Sprintf ("--data %q " , string (body )))
814+ }
815+
816+ return curlCommand .String (), nil
817+ }
0 commit comments