Skip to content

Commit 98e2819

Browse files
committed
feat(auth): Add support for plugin to use oAuth to authenticate to Keyfactor Command
1 parent 1c8de0f commit 98e2819

File tree

8 files changed

+640
-368
lines changed

8 files changed

+640
-368
lines changed

.github/workflows/release.yml

Lines changed: 0 additions & 43 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- 1.4.0
2+
- Added support for oAuth2 authentication to Keyfactor Command.
3+
14
- 1.3.1
25
- Fix for issue where plugin was not enforcing plugin-side role limitations for AllowedDomains and AllowSubDomains, and was relying exclusively on the certificate template for these values.
36

README.md

Lines changed: 201 additions & 126 deletions
Large diffs are not rendered by default.

client.go

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ package keyfactor
1111

1212
import (
1313
"errors"
14-
"fmt"
1514
"log"
1615
"strings"
1716

18-
"github.com/Keyfactor/keyfactor-go-client/api"
17+
"github.com/Keyfactor/keyfactor-auth-client-go/auth_providers"
18+
"github.com/Keyfactor/keyfactor-go-client/v3/api"
1919
)
2020

2121
type keyfactorClient struct {
@@ -27,33 +27,58 @@ func newClient(config *keyfactorConfig) (*api.Client, error) {
2727
return nil, errors.New("client configuration was nil")
2828
}
2929

30-
if config.Username == "" {
31-
return nil, errors.New("client username was not defined")
32-
}
33-
34-
if config.Password == "" {
35-
return nil, errors.New("client password was not defined")
36-
}
37-
3830
if config.KeyfactorUrl == "" {
3931
return nil, errors.New("client URL was not defined")
4032
}
41-
username := strings.Split(config.Username, "//")[1]
42-
domain := strings.Split(config.Username, "//")[1]
4333
hostname := config.KeyfactorUrl
4434
if strings.HasPrefix(config.KeyfactorUrl, "http") {
4535
hostname = strings.Split(config.KeyfactorUrl, "//")[1] //extract just the domain
4636
}
4737

48-
var clientAuth api.AuthConfig
49-
clientAuth.Username = username
50-
clientAuth.Password = config.Password
51-
clientAuth.Domain = domain
52-
clientAuth.Hostname = hostname
38+
isBasicAuth := config.Username != "" && config.Password != ""
39+
isOAuth := (config.ClientId != "" && config.ClientSecret != "" && config.TokenUrl != "") || config.AccessToken != ""
40+
41+
if !isBasicAuth && !isOAuth {
42+
return nil, errors.New(
43+
"invalid Keyfactor Command client configuration, " +
44+
"please provide a valid Basic auth or OAuth configuration",
45+
)
46+
}
5347

54-
fmt.Printf("clientAuth values: \n %s", clientAuth)
48+
serverConfig := &auth_providers.Server{}
49+
if isBasicAuth {
50+
basicAuthConfig := &auth_providers.CommandAuthConfigBasic{}
51+
_ = basicAuthConfig.WithCommandHostName(hostname).
52+
WithCommandAPIPath(config.CommandAPIPath)
53+
54+
bErr := basicAuthConfig.
55+
WithUsername(config.Username).
56+
WithPassword(config.Password).
57+
Authenticate()
58+
59+
if bErr != nil {
60+
return nil, bErr
61+
}
62+
serverConfig = basicAuthConfig.GetServerConfig()
63+
} else if isOAuth {
64+
oauthConfig := &auth_providers.CommandConfigOauth{}
65+
_ = oauthConfig.WithCommandHostName(hostname).
66+
WithCommandAPIPath(config.CommandAPIPath)
67+
68+
oErr := oauthConfig.
69+
WithClientId(config.ClientId).
70+
WithClientSecret(config.ClientSecret).
71+
WithTokenUrl(config.TokenUrl).
72+
WithAccessToken(config.AccessToken).
73+
Authenticate()
74+
75+
if oErr != nil {
76+
return nil, oErr
77+
}
78+
serverConfig = oauthConfig.GetServerConfig()
79+
}
5580

56-
c, err := api.NewKeyfactorClient(&clientAuth)
81+
c, err := api.NewKeyfactorClient(serverConfig, nil)
5782
if err != nil {
5883
log.Fatalf("[ERROR] creating Keyfactor client: %s", err)
5984
}

go.mod

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module github.com/keyfactor/hashicorp-vault-secrets-engine
22

3-
go 1.20
3+
go 1.23
4+
5+
toolchain go1.23.3
46

57
require (
6-
github.com/Keyfactor/keyfactor-go-client v1.2.0
8+
github.com/Keyfactor/keyfactor-auth-client-go v1.0.0-rc.2
9+
github.com/Keyfactor/keyfactor-go-client/v3 v3.0.0
710
github.com/hashicorp/errwrap v1.0.0
8-
github.com/hashicorp/go-hclog v0.16.2
11+
github.com/hashicorp/go-hclog v1.5.0
912
github.com/hashicorp/vault/api v1.1.1
1013
github.com/hashicorp/vault/sdk v0.2.1
1114
)
@@ -14,7 +17,7 @@ require (
1417
github.com/armon/go-metrics v0.3.3 // indirect
1518
github.com/armon/go-radix v1.0.0 // indirect
1619
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
17-
github.com/fatih/color v1.7.0 // indirect
20+
github.com/fatih/color v1.13.0 // indirect
1821
github.com/golang/protobuf v1.4.2 // indirect
1922
github.com/golang/snappy v0.0.1 // indirect
2023
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
@@ -29,27 +32,30 @@ require (
2932
github.com/hashicorp/go-version v1.2.0 // indirect
3033
github.com/hashicorp/golang-lru v0.5.3 // indirect
3134
github.com/hashicorp/hcl v1.0.0 // indirect
35+
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
3236
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
33-
github.com/mattn/go-colorable v0.1.6 // indirect
34-
github.com/mattn/go-isatty v0.0.12 // indirect
37+
github.com/mattn/go-colorable v0.1.13 // indirect
38+
github.com/mattn/go-isatty v0.0.19 // indirect
3539
github.com/mitchellh/copystructure v1.0.0 // indirect
3640
github.com/mitchellh/go-homedir v1.1.0 // indirect
37-
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
41+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
3842
github.com/mitchellh/mapstructure v1.3.2 // indirect
3943
github.com/mitchellh/reflectwalk v1.0.0 // indirect
4044
github.com/oklog/run v1.0.0 // indirect
4145
github.com/pierrec/lz4 v2.5.2+incompatible // indirect
4246
github.com/ryanuber/go-glob v1.0.0 // indirect
43-
github.com/spbsoluble/go-pkcs12 v0.3.1 // indirect
44-
go.mozilla.org/pkcs7 v0.0.0-20210826202110-33d05740a352 // indirect
47+
github.com/spbsoluble/go-pkcs12 v0.3.3 // indirect
48+
go.mozilla.org/pkcs7 v0.9.0 // indirect
4549
go.uber.org/atomic v1.6.0 // indirect
46-
golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect
47-
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
48-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
49-
golang.org/x/text v0.3.6 // indirect
50+
golang.org/x/crypto v0.11.0 // indirect
51+
golang.org/x/net v0.10.0 // indirect
52+
golang.org/x/oauth2 v0.23.0 // indirect
53+
golang.org/x/sys v0.12.0 // indirect
54+
golang.org/x/text v0.11.0 // indirect
5055
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
5156
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
5257
google.golang.org/grpc v1.29.1 // indirect
5358
google.golang.org/protobuf v1.25.0 // indirect
5459
gopkg.in/square/go-jose.v2 v2.5.1 // indirect
60+
gopkg.in/yaml.v2 v2.4.0 // indirect
5561
)

0 commit comments

Comments
 (0)