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
@@ -256,7 +270,7 @@ func (c *CommandAuthConfig) ValidateAuthConfig() error {
256270 }
257271 if c .CommandAPIPath == "" {
258272 if apiPath , ok := os .LookupEnv (EnvKeyfactorAPIPath ); ok {
259- c .CommandAPIPath = apiPath
273+ c .CommandAPIPath = strings . Trim ( apiPath , "/" )
260274 } else {
261275 c .CommandAPIPath = DefaultCommandAPIPath
262276 }
@@ -449,6 +463,10 @@ func (c *CommandAuthConfig) Authenticate() error {
449463 if c .HttpClient == nil {
450464 c .SetClient (nil )
451465 }
466+
467+ if c .HttpProtocol == "" {
468+ c .HttpProtocol = DefaultHttpProtocol
469+ }
452470 //create headers for request
453471 headers := map [string ]string {
454472 "Content-Type" : "application/json" ,
@@ -462,11 +480,13 @@ func (c *CommandAuthConfig) Authenticate() error {
462480 }
463481
464482 endPoint := fmt .Sprintf (
465- "https://%s/%s/Status/Endpoints" ,
483+ "%s://%s/%s/Status/Endpoints" ,
484+ c .HttpProtocol ,
466485 c .CommandHostName ,
467486 //c.CommandPort,
468487 c .CommandAPIPath ,
469488 )
489+ log .Printf ("[DEBUG] testing auth using endpoint %s " , endPoint )
470490
471491 // create request object
472492 req , rErr := http .NewRequest ("GET" , endPoint , nil )
@@ -480,6 +500,11 @@ func (c *CommandAuthConfig) Authenticate() error {
480500 }
481501
482502 c .HttpClient .Timeout = time .Duration (c .HttpClientTimeout ) * time .Second
503+ curlStr , cErr := RequestToCurl (req )
504+ if cErr == nil {
505+ log .Printf ("[TRACE] curl command: %s" , curlStr )
506+ }
507+
483508 cResp , cErr := c .HttpClient .Do (req )
484509 if cErr != nil {
485510 return cErr
@@ -759,3 +784,33 @@ type contextKey string
759784// fmt.Println("Authentication successful")
760785// }
761786// }
787+
788+ func RequestToCurl (req * http.Request ) (string , error ) {
789+ var curlCommand strings.Builder
790+
791+ // Start with the cURL command
792+ curlCommand .WriteString (fmt .Sprintf ("curl -X %s " , req .Method ))
793+
794+ // Add the URL
795+ curlCommand .WriteString (fmt .Sprintf ("%q " , req .URL .String ()))
796+
797+ // Add headers
798+ for name , values := range req .Header {
799+ for _ , value := range values {
800+ curlCommand .WriteString (fmt .Sprintf ("-H %q " , fmt .Sprintf ("%s: %s" , name , value )))
801+ }
802+ }
803+
804+ // Add the body if it exists
805+ if req .Method == http .MethodPost || req .Method == http .MethodPut {
806+ body , err := io .ReadAll (req .Body )
807+ if err != nil {
808+ return "" , err
809+ }
810+ req .Body = io .NopCloser (bytes .NewBuffer (body )) // Restore the request body
811+
812+ curlCommand .WriteString (fmt .Sprintf ("--data %q " , string (body )))
813+ }
814+
815+ return curlCommand .String (), nil
816+ }
0 commit comments