Skip to content

Commit 9c4a730

Browse files
authored
Merge 7be00ce into 17301f6
2 parents 17301f6 + 7be00ce commit 9c4a730

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

auth_providers/auth_core.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
package auth_providers
1616

1717
import (
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.
161169
func (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
@@ -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+
}

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ module github.com/Keyfactor/keyfactor-auth-client-go
1717
go 1.22
1818

1919
require (
20-
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
20+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1
2121
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.3.0
2222
github.com/stretchr/testify v1.10.0
23-
golang.org/x/oauth2 v0.24.0
23+
golang.org/x/oauth2 v0.25.0
2424
gopkg.in/yaml.v2 v2.4.0
2525
)
2626

2727
require (
28-
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 // indirect
28+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
2929
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
3030
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.1.0 // indirect
3131
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
@@ -35,9 +35,9 @@ require (
3535
github.com/kylelemons/godebug v1.1.0 // indirect
3636
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
3737
github.com/pmezard/go-difflib v1.0.0 // indirect
38-
golang.org/x/crypto v0.30.0 // indirect
39-
golang.org/x/net v0.32.0 // indirect
40-
golang.org/x/sys v0.28.0 // indirect
38+
golang.org/x/crypto v0.32.0 // indirect
39+
golang.org/x/net v0.34.0 // indirect
40+
golang.org/x/sys v0.29.0 // indirect
4141
golang.org/x/text v0.21.0 // indirect
4242
gopkg.in/yaml.v3 v3.0.1 // indirect
4343
)

go.sum

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 h1:JZg6HRh6W6U4OLl6lk7BZ7BLisIzM9dG1R50zUk9C/M=
22
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0/go.mod h1:YL1xnZ6QejvQHWJrX/AvhFl4WW4rqHVoKspWNVwFk0M=
3+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
4+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
35
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 h1:B/dfvscEQtew9dVuoxqxrUKKv8Ih2f55PydknDamU+g=
46
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0/go.mod h1:fiPSssYvltE08HJchL04dOy+RD4hgrjph0cwGGMntdI=
7+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1 h1:1mvYtZfWQAnwNah/C+Z+Jb9rQH95LPE2vlmMuWAHJk8=
8+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1/go.mod h1:75I/mXtme1JyWFtz8GocPHVFyH421IBoZErnO16dd0k=
59
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0 h1:+m0M/LFxN43KvULkDNfdXOgrjtg6UYJPFBJyuEcRCAw=
610
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0/go.mod h1:PwOyop78lveYMRs6oCxjiVyBdyCgIYH6XHIVZO9/SFQ=
711
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
@@ -46,13 +50,21 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
4650
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4751
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
4852
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
53+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
54+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
4955
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
5056
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
57+
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
58+
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
5159
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
5260
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
61+
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
62+
golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
5363
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5464
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
5565
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
66+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
67+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5668
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
5769
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
5870
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)