Skip to content

Commit e8dceb0

Browse files
authored
DGS-22404 Add AppRole auth for HC Vault (#1478)
* DGS-22404 Add AppRole auth for HC Vault * Minor cleanup * go mod tidy on examples
1 parent ad7e70b commit e8dceb0

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

examples/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ toolchain go1.21.0
77
replace github.com/confluentinc/confluent-kafka-go/v2 => ../
88

99
require (
10-
github.com/actgardner/gogen-avro/v10 v10.2.1
1110
github.com/alecthomas/kingpin v2.2.6+incompatible
1211
github.com/confluentinc/confluent-kafka-go/v2 v2.12.0-RC2
1312
github.com/gdamore/tcell v1.4.0
@@ -67,7 +66,7 @@ require (
6766
github.com/hashicorp/go-sockaddr v1.0.6 // indirect
6867
github.com/hashicorp/hcl v1.0.0 // indirect
6968
github.com/hashicorp/vault/api v1.15.0 // indirect
70-
github.com/heetch/avro v0.4.5 // indirect
69+
github.com/hashicorp/vault/api/auth/approle v0.8.0 // indirect
7170
github.com/invopop/jsonschema v0.12.0 // indirect
7271
github.com/jhump/protoreflect v1.15.6 // indirect
7372
github.com/json-iterator/go v1.1.12 // indirect

examples/go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
144144
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
145145
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
146146
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
147-
github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss=
148-
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=
149147
github.com/fsnotify/fsevents v0.2.0 h1:BRlvlqjvNTfogHfeBOFvSC9N0Ddy+wzQCQukyoD7o/c=
150148
github.com/fsnotify/fsevents v0.2.0/go.mod h1:B3eEk39i4hz8y1zaWS/wPrAP4O6wkIl7HQwKBr1qH/w=
151149
github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo=
@@ -261,8 +259,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
261259
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
262260
github.com/hashicorp/vault/api v1.15.0 h1:O24FYQCWwhwKnF7CuSqP30S51rTV7vz1iACXE/pj5DA=
263261
github.com/hashicorp/vault/api v1.15.0/go.mod h1:+5YTO09JGn0u+b6ySD/LLVf8WkJCPLAL2Vkmrn2+CM8=
264-
github.com/heetch/avro v0.4.5 h1:BSnj4wEeUG1IjMTm9/tBwQnV3euuIVa1mRWHnm1t8VU=
265-
github.com/heetch/avro v0.4.5/go.mod h1:gxf9GnbjTXmWmqxhdNbAMcZCjpye7RV5r9t3Q0dL6ws=
262+
github.com/hashicorp/vault/api/auth/approle v0.8.0 h1:FuVtWZ0xD6+wz1x0l5s0b4852RmVXQNEiKhVXt6lfQY=
263+
github.com/hashicorp/vault/api/auth/approle v0.8.0/go.mod h1:NV7O9r5JUtNdVnqVZeMHva81AIdpG0WoIQohNt1VCPM=
266264
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
267265
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
268266
github.com/in-toto/in-toto-golang v0.5.0 h1:hb8bgwr0M2hGdDsLjkJ3ZqJ8JFLL/tgYdAxF/XEFBbY=

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ require (
1313
github.com/aws/aws-sdk-go-v2/config v1.27.10
1414
github.com/aws/aws-sdk-go-v2/credentials v1.17.10
1515
github.com/aws/aws-sdk-go-v2/service/kms v1.30.1
16+
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6
1617
github.com/golang/protobuf v1.5.4
1718
github.com/google/cel-go v0.20.1
1819
github.com/google/uuid v1.6.0
1920
github.com/hamba/avro/v2 v2.24.0
2021
github.com/hashicorp/vault/api v1.15.0
22+
github.com/hashicorp/vault/api/auth/approle v0.8.0
2123
github.com/heetch/avro v0.4.5
2224
github.com/invopop/jsonschema v0.12.0
2325
github.com/jhump/protoreflect v1.15.6
@@ -29,6 +31,7 @@ require (
2931
github.com/tink-crypto/tink-go-hcvault/v2 v2.1.0
3032
github.com/tink-crypto/tink-go/v2 v2.1.0
3133
github.com/xiatechs/jsonata-go v1.8.5
34+
golang.org/x/oauth2 v0.18.0
3235
google.golang.org/api v0.169.0
3336
google.golang.org/genproto v0.0.0-20240325203815-454cdb8f5daa
3437
google.golang.org/protobuf v1.33.0
@@ -57,7 +60,6 @@ require (
5760
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
5861
github.com/aws/aws-sdk-go-v2/service/sso v1.20.4 // indirect
5962
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
60-
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
6163
github.com/aws/smithy-go v1.20.2 // indirect
6264
github.com/bahlo/generic-list-go v0.2.0 // indirect
6365
github.com/beorn7/perks v1.0.1 // indirect
@@ -221,7 +223,6 @@ require (
221223
golang.org/x/crypto v0.27.0 // indirect
222224
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
223225
golang.org/x/net v0.29.0 // indirect
224-
golang.org/x/oauth2 v0.18.0 // indirect
225226
golang.org/x/sync v0.8.0 // indirect
226227
golang.org/x/sys v0.25.0 // indirect
227228
golang.org/x/term v0.24.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
332332
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
333333
github.com/hashicorp/vault/api v1.15.0 h1:O24FYQCWwhwKnF7CuSqP30S51rTV7vz1iACXE/pj5DA=
334334
github.com/hashicorp/vault/api v1.15.0/go.mod h1:+5YTO09JGn0u+b6ySD/LLVf8WkJCPLAL2Vkmrn2+CM8=
335+
github.com/hashicorp/vault/api/auth/approle v0.8.0 h1:FuVtWZ0xD6+wz1x0l5s0b4852RmVXQNEiKhVXt6lfQY=
336+
github.com/hashicorp/vault/api/auth/approle v0.8.0/go.mod h1:NV7O9r5JUtNdVnqVZeMHva81AIdpG0WoIQohNt1VCPM=
335337
github.com/heetch/avro v0.4.5 h1:BSnj4wEeUG1IjMTm9/tBwQnV3euuIVa1mRWHnm1t8VU=
336338
github.com/heetch/avro v0.4.5/go.mod h1:gxf9GnbjTXmWmqxhdNbAMcZCjpye7RV5r9t3Q0dL6ws=
337339
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=

schemaregistry/rules/encryption/hcvault/hcvault_client.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
// vaultClient represents a client that connects to the HashiCorp Vault backend.
3535
type vaultClient struct {
3636
keyURIPrefix string
37-
client *api.Logical
37+
client *api.Client
3838
}
3939

4040
var _ registry.KMSClient = (*vaultClient)(nil)
@@ -86,10 +86,15 @@ func NewClient(uriPrefix string, tlsCfg *tls.Config, namespace string, token str
8686
client.SetToken(token)
8787
return &vaultClient{
8888
keyURIPrefix: uriPrefix,
89-
client: client.Logical(),
89+
client: client,
9090
}, nil
9191
}
9292

93+
// Client returns the underlying Vault client.
94+
func (c *vaultClient) Client() *api.Client {
95+
return c.client
96+
}
97+
9398
// Supported returns true if this client does support keyURI.
9499
func (c *vaultClient) Supported(keyURI string) bool {
95100
return strings.HasPrefix(keyURI, c.keyURIPrefix)
@@ -106,5 +111,5 @@ func (c *vaultClient) GetAEAD(keyURI string) (tink.AEAD, error) {
106111
return nil, errors.New("malformed keyURI")
107112
}
108113
keyPath := u.EscapedPath()
109-
return vault.NewAEAD(keyPath, c.client)
114+
return vault.NewAEAD(keyPath, c.client.Logical())
110115
}

schemaregistry/rules/encryption/hcvault/hcvault_driver.go

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@
1717
package hcvault
1818

1919
import (
20+
"context"
21+
"fmt"
2022
"github.com/confluentinc/confluent-kafka-go/v2/schemaregistry/rules/encryption"
2123
"github.com/tink-crypto/tink-go/v2/core/registry"
2224
"os"
25+
26+
auth "github.com/hashicorp/vault/api/auth/approle"
2327
)
2428

2529
const (
26-
prefix = "hcvault://"
27-
tokenID = "token.id"
28-
namespace = "namespace"
30+
prefix = "hcvault://"
31+
tokenID = "token.id"
32+
namespace = "namespace"
33+
approleRoleID = "approle.role.id"
34+
approleSecretID = "approle.secret.id"
2935
)
3036

3137
func init() {
@@ -50,11 +56,44 @@ func (l *hcvaultDriver) NewKMSClient(config map[string]string, keyURL *string) (
5056
if keyURL != nil {
5157
uriPrefix = *keyURL
5258
}
53-
ns := config[namespace]
5459
token := config[tokenID]
5560
if token == "" {
56-
ns = os.Getenv("VAULT_NAMESPACE")
5761
token = os.Getenv("VAULT_TOKEN")
5862
}
59-
return NewClient(uriPrefix, nil, ns, token)
63+
ns := config[namespace]
64+
if ns == "" {
65+
ns = os.Getenv("VAULT_NAMESPACE")
66+
}
67+
client, err := NewClient(uriPrefix, nil, ns, token)
68+
if err != nil {
69+
return nil, err
70+
}
71+
roleID := config[approleRoleID]
72+
if roleID == "" {
73+
roleID = os.Getenv("VAULT_APPROLE_ROLE_ID")
74+
}
75+
secretID := config[approleSecretID]
76+
if secretID == "" {
77+
secretID = os.Getenv("VAULT_APPROLE_SECRET_ID")
78+
}
79+
if roleID != "" && secretID != "" {
80+
roleSecretID := &auth.SecretID{FromString: secretID}
81+
appRoleAuth, err := auth.NewAppRoleAuth(
82+
roleID,
83+
roleSecretID,
84+
)
85+
if err != nil {
86+
return nil, fmt.Errorf("unable to initialize AppRole auth method: %w", err)
87+
}
88+
89+
vaultClient := client.(*vaultClient).Client()
90+
authInfo, err := vaultClient.Auth().Login(context.Background(), appRoleAuth)
91+
if err != nil {
92+
return nil, fmt.Errorf("unable to login to AppRole auth method: %w", err)
93+
}
94+
if authInfo == nil {
95+
return nil, fmt.Errorf("no auth info was returned after login")
96+
}
97+
}
98+
return client, nil
6099
}

0 commit comments

Comments
 (0)