Debian Codegraph @

integration/hcvault/hcvault_client.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Package hcvault provides integration with the [HashiCorp Vault].
16 //
17 // [HashiCorp Vault]: https://www.vaultproject.io/.
18 package hcvault
19
20 import (
21 "crypto/tls"
22 "errors"
23 "fmt"
24 "net/http"
25 "net/url"
26 "strings"
27
28 "github.com/hashicorp/vault/api"
29 "github.com/tink-crypto/tink-go/v2/core/registry"
30 "github.com/tink-crypto/tink-go/v2/tink"
31 )
32
33 const (
34 vaultPrefix = "hcvault://"
35 )
36
37 // vaultClient represents a client that connects to the HashiCorp Vault backend.
38 type vaultClient struct {
39 keyURIPrefix string
40 client *api.Logical
41 aeadOpts []AEADOption
42 }
43
44 var _ registry.KMSClient = (*vaultClient)(nil)
45
46 // NewClientWithAEADOptions returns a new client.
47 // uriPrefix parameter is a valid URI which must have "hcvault" scheme and
48 // vault server address and port. Specific key URIs will be matched against this
49 // prefix to determine if the client supports the key or not.
50 func NewClientWithAEADOptions(uriPrefix string, client *api.Logical, aeadOpts ...AEADOption) (registry.KMSClient, error) {
51 if !strings.HasPrefix(strings.ToLower(uriPrefix), vaultPrefix) {
52 return nil, fmt.Errorf("key URI must start with %s", vaultPrefix)
53 }
54
55 return &vaultClient{
56 keyURIPrefix: uriPrefix,
57 client: client,
58 aeadOpts: aeadOpts,
59 }, nil
60 }
61
62 // NewClient returns a new client to HashiCorp Vault.
63 //
64 // # Warning regarding associated data
65 //
66 // For backwards compatibility, the AEAD implementation returned by this client
67 // uses the associated_data value to populate the "context" parameter in encrypt
68 // and decrypt requests, instead of using the "associated_data" request parameter.
69 // This is an artifact of the "associated_data" parameter not being present when
70 // this package was implemented.
71 //
72 // Vault only uses the "context" parameter for keys which have derivation enabled
73 // (with "derived=true") and ignores it otherwise. For such keys, the "context"
74 // parameter is required to be non-empty.
75 //
76 // Therefore:
77 // - for keys with "derived=false", you should only use empty associated data.
78 // - for keys with "derived=true", you should only use non-empty associated data.
79 //
80 // With Tink's "KMS envelope AEAD", always use a key with "derived=false".
81 //
82 // For reference, see https://developer.hashicorp.com/vault/api-docs/secret/transit.
83 //
84 // Deprecated. Use NewClientWithAEADOptions instead.
85 func NewClient(uriPrefix string, tlsCfg *tls.Config, token string) (registry.KMSClient, error) {
86 httpClient := api.DefaultConfig().HttpClient
87 transport := httpClient.Transport.(*http.Transport)
88 if tlsCfg == nil {
89 tlsCfg = &tls.Config{}
90 } else {
91 tlsCfg = tlsCfg.Clone()
92 }
93 transport.TLSClientConfig = tlsCfg
94
95 vurl, err := url.Parse(uriPrefix)
96 if err != nil {
97 return nil, err
98 }
99
100 cfg := &api.Config{
101 Address: "https://" + vurl.Host,
102 HttpClient: httpClient,
103 }
104
105 client, err := api.NewClient(cfg)
106 if err != nil {
107 return nil, err
108 }
109
110 client.SetToken(token)
111 return NewClientWithAEADOptions(uriPrefix, client.Logical(), WithLegacyContextParamater())
112 }
113
114 // Supported returns true if this client does support keyURI.
115 func (c *vaultClient) Supported(keyURI string) bool {
116 return strings.HasPrefix(keyURI, c.keyURIPrefix)
117 }
118
119 // GetAEAD gets an AEAD backed by keyURI.
120 func (c *vaultClient) GetAEAD(keyURI string) (tink.AEAD, error) {
121 if !c.Supported(keyURI) {
122 return nil, errors.New("unsupported keyURI")
123 }
124 u, err := url.Parse(keyURI)
125 if err != nil || u.Scheme != "hcvault" {
126 return nil, errors.New("malformed keyURI")
127 }
128 keyPath := u.EscapedPath()
129 return NewAEAD(keyPath, c.client, c.aeadOpts...)
130 }