Skip to content

Commit

Permalink
Merge 6ce97b8 into 17301f6
Browse files Browse the repository at this point in the history
  • Loading branch information
spbsoluble authored Jan 23, 2025
2 parents 17301f6 + 6ce97b8 commit 9d43210
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 25 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# v1.2.0

## Fixes
- `oauth` set `DefaultScopes` to empty slice of string.

## Features
- Add logging of the authentication test request as a `curl` string. ([7be00ce](https://github.com/Keyfactor/keyfactor-auth-client-go/commit/7be00ce82b6dd7880449e6585590ec702992a388))

## Bug fixes
- Ensure `CommandAPIPath` is always trimmed of any leading or trailing `/`. ([45023c9](https://github.com/Keyfactor/keyfactor-auth-client-go/commit/45023c94e9be0ae9b307f38af972bbc0b40998d4))

## Chores
- Bump Go version to `1.23`. ([9e62e2a](https://github.com/Keyfactor/keyfactor-auth-client-go/commit/9e62e2ab3a5c8ea0883df5a5902eaa91f2776f23))

# v1.1.1

## Bug fixes
Expand Down
58 changes: 57 additions & 1 deletion auth_providers/auth_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
package auth_providers

import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -54,6 +56,9 @@ const (
// DefaultClientTimeout is the default timeout for the http Client
DefaultClientTimeout = 60

//Default HTTP protocol
DefaultHttpProtocol = "https"

// EnvKeyfactorHostName is the environment variable for the Keyfactor Command hostname
EnvKeyfactorHostName = "KEYFACTOR_HOSTNAME"

Expand Down Expand Up @@ -139,6 +144,9 @@ type CommandAuthConfig struct {
// Debug
Debug bool `json:"debug,omitempty" yaml:"debug,omitempty"`

// HTTPProtocol
HttpProtocol string `json:"http_protocol,omitempty" yaml:"http_protocol,omitempty"`

// HttpClient is the http Client to be used for authentication to Keyfactor Command API
HttpClient *http.Client
//DefaultHttpClient *http.Client
Expand All @@ -159,6 +167,12 @@ func cleanHostName(hostName string) string {

// WithCommandHostName sets the hostname for authentication to Keyfactor Command API.
func (c *CommandAuthConfig) WithCommandHostName(hostName string) *CommandAuthConfig {

//check for http or https prefix
if strings.Contains(hostName, "http://") {
c.HttpProtocol = "http"
}

hostName = cleanHostName(hostName)
c.CommandHostName = hostName
return c
Expand Down Expand Up @@ -261,6 +275,7 @@ func (c *CommandAuthConfig) ValidateAuthConfig() error {
c.CommandAPIPath = DefaultCommandAPIPath
}
}
c.CommandAPIPath = strings.Trim(c.CommandAPIPath, "/")
if c.HttpClientTimeout <= 0 {
if timeout, ok := os.LookupEnv(EnvKeyfactorClientTimeout); ok {
configTimeout, tErr := strconv.Atoi(timeout)
Expand Down Expand Up @@ -449,6 +464,10 @@ func (c *CommandAuthConfig) Authenticate() error {
if c.HttpClient == nil {
c.SetClient(nil)
}

if c.HttpProtocol == "" {
c.HttpProtocol = DefaultHttpProtocol
}
//create headers for request
headers := map[string]string{
"Content-Type": "application/json",
Expand All @@ -462,11 +481,13 @@ func (c *CommandAuthConfig) Authenticate() error {
}

endPoint := fmt.Sprintf(
"https://%s/%s/Status/Endpoints",
"%s://%s/%s/Status/Endpoints",
c.HttpProtocol,
c.CommandHostName,
//c.CommandPort,
c.CommandAPIPath,
)
log.Printf("[DEBUG] testing auth using endpoint %s ", endPoint)

// create request object
req, rErr := http.NewRequest("GET", endPoint, nil)
Expand All @@ -480,6 +501,11 @@ func (c *CommandAuthConfig) Authenticate() error {
}

c.HttpClient.Timeout = time.Duration(c.HttpClientTimeout) * time.Second
curlStr, cErr := RequestToCurl(req)
if cErr == nil {
log.Printf("[TRACE] curl command: %s", curlStr)
}

cResp, cErr := c.HttpClient.Do(req)
if cErr != nil {
return cErr
Expand Down Expand Up @@ -759,3 +785,33 @@ type contextKey string
// fmt.Println("Authentication successful")
// }
// }

func RequestToCurl(req *http.Request) (string, error) {
var curlCommand strings.Builder

// Start with the cURL command
curlCommand.WriteString(fmt.Sprintf("curl -X %s ", req.Method))

// Add the URL
curlCommand.WriteString(fmt.Sprintf("%q ", req.URL.String()))

// Add headers
for name, values := range req.Header {
for _, value := range values {
curlCommand.WriteString(fmt.Sprintf("-H %q ", fmt.Sprintf("%s: %s", name, value)))
}
}

// Add the body if it exists
if req.Method == http.MethodPost || req.Method == http.MethodPut {
body, err := io.ReadAll(req.Body)
if err != nil {
return "", err
}
req.Body = io.NopCloser(bytes.NewBuffer(body)) // Restore the request body

curlCommand.WriteString(fmt.Sprintf("--data %q ", string(body)))
}

return curlCommand.String(), nil
}
2 changes: 1 addition & 1 deletion auth_providers/auth_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const (

var (
// DefaultScopes is the default scopes for Keyfactor authentication
DefaultScopes = []string{"openid"}
DefaultScopes []string
)

// OAuth Authenticator
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

module github.com/Keyfactor/keyfactor-auth-client-go

go 1.22
go 1.23

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

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.1.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.3.2 // indirect
Expand All @@ -35,9 +35,9 @@ require (
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.30.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
32 changes: 16 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 h1:JZg6HRh6W6U4OLl6lk7BZ7BLisIzM9dG1R50zUk9C/M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0/go.mod h1:YL1xnZ6QejvQHWJrX/AvhFl4WW4rqHVoKspWNVwFk0M=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 h1:B/dfvscEQtew9dVuoxqxrUKKv8Ih2f55PydknDamU+g=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0/go.mod h1:fiPSssYvltE08HJchL04dOy+RD4hgrjph0cwGGMntdI=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0 h1:+m0M/LFxN43KvULkDNfdXOgrjtg6UYJPFBJyuEcRCAw=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0/go.mod h1:PwOyop78lveYMRs6oCxjiVyBdyCgIYH6XHIVZO9/SFQ=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0 h1:g0EZJwz7xkXQiZAI5xi9f3WWFYBlX1CPTrR+NDToRkQ=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.17.0/go.mod h1:XCW7KnZet0Opnr7HccfUw1PLc4CjHqpcaxW8DHklNkQ=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1 h1:1mvYtZfWQAnwNah/C+Z+Jb9rQH95LPE2vlmMuWAHJk8=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.1/go.mod h1:75I/mXtme1JyWFtz8GocPHVFyH421IBoZErnO16dd0k=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.1 h1:Bk5uOhSAenHyR5P61D/NzeQCv+4fEVV8mOkJ82NqpWw=
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.1/go.mod h1:QZ4pw3or1WPmRBxf0cHd1tknzrT54WPBOQoGutCPvSU=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.3.0 h1:WLUIpeyv04H0RCcQHaA4TNoyrQ39Ox7V+re+iaqzTe0=
Expand Down Expand Up @@ -38,21 +38,21 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmd
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
golang.org/x/oauth2 v0.25.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 9d43210

Please sign in to comment.