@@ -501,19 +501,21 @@ func (c *CommandAuthConfig) Authenticate() error {
501501 }
502502
503503 c .HttpClient .Timeout = time .Duration (c .HttpClientTimeout ) * time .Second
504- curlStr , cErr := RequestToCurl (req )
505- if cErr == nil {
504+
505+ cResp , cErr := c .HttpClient .Do (req )
506+ curlStr , curlErr := RequestToCurl (req )
507+ if curlErr == nil {
506508 log .Printf ("[TRACE] curl command: %s" , curlStr )
507509 }
508510
509- cResp , cErr := c .HttpClient .Do (req )
510511 if cErr != nil {
511512 return cErr
512513 } else if cResp == nil {
513514 return fmt .Errorf ("failed to authenticate, no response received from Keyfactor Command" )
514515 }
515516
516517 defer cResp .Body .Close ()
518+ log .Printf ("[DEBUG] request to Keyfactor Command API returned status code %d" , cResp .StatusCode )
517519
518520 // check if body is empty
519521 if cResp .Body == nil {
@@ -798,19 +800,56 @@ func RequestToCurl(req *http.Request) (string, error) {
798800 // Add headers
799801 for name , values := range req .Header {
800802 for _ , value := range values {
803+ // check if is Authorization header and skip it
804+ if strings .EqualFold (name , "Authorization" ) {
805+ // check if basic auth and skip it
806+ if strings .HasPrefix (value , "Basic " ) {
807+ // Remove credentials and put in env variables as placeholder
808+ log .Printf (
809+ "[DEBUG] Found Basic auth in Authorization header, " +
810+ "replacing with env variable references" ,
811+ )
812+ curlCommand .WriteString (
813+ fmt .Sprintf (
814+ "-H %q " , fmt .Sprintf (
815+ "%s: Basic $(echo -n $\" %s,$%s\" | base64)" , name ,
816+ EnvKeyfactorUsername , EnvKeyfactorPassword ,
817+ ),
818+ ),
819+ )
820+ continue
821+ } else if strings .HasPrefix (value , "Bearer " ) {
822+ // Remove credentials and put in env variables as placeholder
823+ log .Printf ("[DEBUG] Found Bearer token in Authorization header, replacing with kfutil command to fetch token" )
824+ curlCommand .WriteString (
825+ fmt .Sprintf (
826+ "-H %q " , fmt .Sprintf (
827+ "%s: Bearer $(kfutil auth fetch-oauth-token)" , name ,
828+ ),
829+ ),
830+ )
831+ continue
832+ } else {
833+ // Skip other Authorization headers
834+ log .Printf ("[ERROR] Skipping unhandled Authorization header: %s" , name )
835+ continue
836+ }
837+ }
801838 curlCommand .WriteString (fmt .Sprintf ("-H %q " , fmt .Sprintf ("%s: %s" , name , value )))
802839 }
803840 }
804841
805842 // Add the body if it exists
806843 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
844+ if req .Body != nil {
845+ body , err := io .ReadAll (req .Body )
846+ if err != nil {
847+ return "" , err
848+ }
849+ req .Body = io .NopCloser (bytes .NewBuffer (body )) // Restore the request body
812850
813- curlCommand .WriteString (fmt .Sprintf ("--data %q " , string (body )))
851+ curlCommand .WriteString (fmt .Sprintf ("--data %q " , string (body )))
852+ }
814853 }
815854
816855 return curlCommand .String (), nil
0 commit comments