| 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 hcvaultscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/ |
| 19 |
|
| 20 |
import ( |
| 21 |
"crypto/tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/" |
| 22 |
"errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 23 |
"fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 24 |
"net/urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/" |
| 25 |
"stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/" |
| 26 |
|
| 27 |
"github.com/tink-crypto/tink-go/v2/core/registryscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/core/registry`/" |
| 28 |
"github.com/tink-crypto/tink-go/v2/tinkscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/tink`/" |
| 29 |
) |
| 30 |
|
| 31 |
const ( |
| 32 |
vaultPrefixscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultPrefix. = "hcvault://" |
| 33 |
) |
| 34 |
|
| 35 |
// vaultClient represents a client that connects to the HashiCorp Vault backend. |
| 36 |
type vaultClientscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient# struct { |
| 37 |
keyURIPrefixscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#keyURIPrefix. string |
| 38 |
aeadOptsscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#aeadOpts. []AEADOptionscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/AEADOption# |
| 39 |
} |
| 40 |
|
| 41 |
var _ registryscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/core/registry`/.KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient# = (*vaultClientscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/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 NewClientscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/NewClient().(uriPrefix string, tlsCfg *tlsscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/.Configscip-go gomod github.com/golang/go/src go1.24.0 `crypto/tls`/Config#, token string) (registryscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/core/registry`/.KMSClientscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#, error) { |
| •67 |
return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("Debian Vault support incomplete") |
| 68 |
} |
| 69 |
|
| 70 |
// Supported returns true if this client does support keyURI. |
| 71 |
func (c *vaultClientscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#) Supportedscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#Supported().(keyURI string) bool { |
| 72 |
return stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/HasPrefix().(keyURI, c.keyURIPrefixscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#keyURIPrefix.) |
| 73 |
} |
| 74 |
|
| 75 |
// GetAEAD gets an AEAD backed by keyURI. |
| 76 |
func (c *vaultClientscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#) GetAEADscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#GetAEAD().(keyURI string) (tinkscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/tink`/.AEADscip-go gomod github.com/tink-crypto/tink-go/v2 v2.5.0 `github.com/tink-crypto/tink-go/v2/tink`/AEAD#, error) { |
| 77 |
if !c.Supportedscip-go gomod github.com/tink-crypto/tink-go-hcvault/v2 . `github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault`/vaultClient#Supported().(keyURI) { |
| 78 |
return nil, errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("unsupported keyURI") |
| 79 |
} |
| 80 |
u, err := urlscip-go gomod github.com/golang/go/src go1.24.0 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/Parse().(keyURI) |
| 81 |
if err != nil || u.Schemescip-go gomod github.com/golang/go/src go1.24.0 `net/url`/URL#Scheme. != "hcvault" { |
| 82 |
return nil, errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("malformed keyURI") |
| 83 |
} |
| •84 |
return nil, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("Debian Vault support incomplete") |
| 85 |
} |