| 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/url" |
| 25 |
"strings" |
| 26 |
|
| 27 |
"github.com/tink-crypto/tink-go/v2/core/registry" |
| 28 |
"github.com/tink-crypto/tink-go/v2/tink" |
| 29 |
) |
| 30 |
|
| 31 |
const ( |
| 32 |
vaultPrefix = "hcvault://" |
| 33 |
) |
| 34 |
|
| 35 |
// vaultClient represents a client that connects to the HashiCorp Vault backend. |
| 36 |
type vaultClient struct { |
| 37 |
keyURIPrefix string |
| 38 |
aeadOpts []AEADOption |
| 39 |
} |
| 40 |
|
| 41 |
var _ registry.KMSClient = (*vaultClient)(nil) |
| 42 |
|
| 43 |
// NewClient returns a new client to HashiCorp Vault. |
| 44 |
// |
| 45 |
// # Warning regarding associated data |
| 46 |
// |
| 47 |
// For backwards compatibility, the AEAD implementation returned by this client |
| 48 |
// uses the associated_data value to populate the "context" parameter in encrypt |
| 49 |
// and decrypt requests, instead of using the "associated_data" request parameter. |
| 50 |
// This is an artifact of the "associated_data" parameter not being present when |
| 51 |
// this package was implemented. |
| 52 |
// |
| 53 |
// Vault only uses the "context" parameter for keys which have derivation enabled |
| 54 |
// (with "derived=true") and ignores it otherwise. For such keys, the "context" |
| 55 |
// parameter is required to be non-empty. |
| 56 |
// |
| 57 |
// Therefore: |
| 58 |
// - for keys with "derived=false", you should only use empty associated data. |
| 59 |
// - for keys with "derived=true", you should only use non-empty associated data. |
| 60 |
// |
| 61 |
// With Tink's "KMS envelope AEAD", always use a key with "derived=false". |
| 62 |
// |
| 63 |
// For reference, see https://developer.hashicorp.com/vault/api-docs/secret/transit. |
| 64 |
// |
| 65 |
// Deprecated. Use NewClientWithAEADOptions instead. |
| 66 |
func NewClient(uriPrefix string, tlsCfg *tls.Config, token string) (registry.KMSClient, error) { |
| •67 |
return nil, fmt.Errorf("Debian Vault support incomplete") |
| 68 |
} |
| 69 |
|
| 70 |
// Supported returns true if this client does support keyURI. |
| 71 |
func (c *vaultClient) Supported(keyURI string) bool { |
| 72 |
return strings.HasPrefix(keyURI, c.keyURIPrefix) |
| 73 |
} |
| 74 |
|
| 75 |
// GetAEAD gets an AEAD backed by keyURI. |
| 76 |
func (c *vaultClient) GetAEAD(keyURI string) (tink.AEAD, error) { |
| 77 |
if !c.Supported(keyURI) { |
| 78 |
return nil, errors.New("unsupported keyURI") |
| 79 |
} |
| 80 |
u, err := url.Parse(keyURI) |
| 81 |
if err != nil || u.Scheme != "hcvault" { |
| 82 |
return nil, errors.New("malformed keyURI") |
| 83 |
} |
| •84 |
return nil, fmt.Errorf("Debian Vault support incomplete") |
| 85 |
} |