2019-04-09 14:39:42 -04:00
|
|
|
// MinIO Cloud Storage, (C) 2015, 2016, 2017, 2018 MinIO, Inc.
|
2018-08-17 15:52:14 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2020-07-08 21:50:43 -04:00
|
|
|
"errors"
|
2018-08-17 15:52:14 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
vault "github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//ErrKMSAuthLogin is raised when there is a failure authenticating to KMS
|
2020-01-06 19:15:22 -05:00
|
|
|
ErrKMSAuthLogin = Errorf("Vault service did not return auth info")
|
2018-08-17 15:52:14 -04:00
|
|
|
)
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// VaultKey represents vault encryption key-ring.
|
2018-08-17 15:52:14 -04:00
|
|
|
type VaultKey struct {
|
2018-12-12 01:50:29 -05:00
|
|
|
Name string `json:"name"` // The name of the encryption key-ring
|
|
|
|
Version int `json:"version"` // The key version
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// VaultAuth represents vault authentication type.
|
|
|
|
// Currently the only supported authentication type is AppRole.
|
2018-08-17 15:52:14 -04:00
|
|
|
type VaultAuth struct {
|
2018-12-12 01:50:29 -05:00
|
|
|
Type string `json:"type"` // The authentication type
|
|
|
|
AppRole VaultAppRole `json:"approle"` // The AppRole authentication credentials
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// VaultAppRole represents vault AppRole authentication credentials
|
2018-08-17 15:52:14 -04:00
|
|
|
type VaultAppRole struct {
|
2018-12-12 01:50:29 -05:00
|
|
|
ID string `json:"id"` // The AppRole access ID
|
|
|
|
Secret string `json:"secret"` // The AppRole secret
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// VaultConfig represents vault configuration.
|
2018-08-17 15:52:14 -04:00
|
|
|
type VaultConfig struct {
|
2019-11-09 12:27:23 -05:00
|
|
|
Enabled bool `json:"-"`
|
2018-12-12 01:50:29 -05:00
|
|
|
Endpoint string `json:"endpoint"` // The vault API endpoint as URL
|
|
|
|
CAPath string `json:"-"` // The path to PEM-encoded certificate files used for mTLS. Currently not used in config file.
|
|
|
|
Auth VaultAuth `json:"auth"` // The vault authentication configuration
|
|
|
|
Key VaultKey `json:"key-id"` // The named key used for key-generation / decryption.
|
|
|
|
Namespace string `json:"-"` // The vault namespace of enterprise vault instances
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// vaultService represents a connection to a vault KMS.
|
|
|
|
type vaultService struct {
|
|
|
|
config *VaultConfig
|
|
|
|
client *vault.Client
|
2019-02-20 15:23:59 -05:00
|
|
|
secret *vault.Secret
|
2018-12-12 01:50:29 -05:00
|
|
|
leaseDuration time.Duration
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
var _ KMS = (*vaultService)(nil) // compiler check that *vaultService implements KMS
|
|
|
|
|
|
|
|
// Verify returns a nil error if the vault configuration
|
|
|
|
// is valid. A valid configuration is either empty or
|
|
|
|
// contains valid non-default values.
|
|
|
|
func (v *VaultConfig) Verify() (err error) {
|
|
|
|
switch {
|
|
|
|
case v.Endpoint == "":
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: missing hashicorp vault endpoint")
|
2018-12-12 01:50:29 -05:00
|
|
|
case strings.ToLower(v.Auth.Type) != "approle":
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: invalid hashicorp vault authentication type: %s is not supported", v.Auth.Type)
|
2018-12-12 01:50:29 -05:00
|
|
|
case v.Auth.AppRole.ID == "":
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: missing hashicorp vault AppRole ID")
|
2018-12-12 01:50:29 -05:00
|
|
|
case v.Auth.AppRole.Secret == "":
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: missing hashicorp vault AppSecret ID")
|
2018-12-12 01:50:29 -05:00
|
|
|
case v.Key.Name == "":
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: missing hashicorp vault key name")
|
2018-12-12 01:50:29 -05:00
|
|
|
case v.Key.Version < 0:
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: invalid hashicorp vault key version: The key version must not be negative")
|
2018-12-12 01:50:29 -05:00
|
|
|
}
|
|
|
|
return
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// NewVault initializes Hashicorp Vault KMS by authenticating
|
|
|
|
// to Vault with the credentials in config and gets a client
|
|
|
|
// token for future api calls.
|
|
|
|
func NewVault(config VaultConfig) (KMS, error) {
|
2019-11-09 12:27:23 -05:00
|
|
|
if !config.Enabled {
|
|
|
|
return nil, nil
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
2018-12-12 01:50:29 -05:00
|
|
|
if err := config.Verify(); err != nil {
|
|
|
|
return nil, err
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
vaultCfg := vault.Config{Address: config.Endpoint}
|
|
|
|
if err := vaultCfg.ConfigureTLS(&vault.TLSConfig{CAPath: config.CAPath}); err != nil {
|
2018-10-01 16:49:10 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-12 01:50:29 -05:00
|
|
|
client, err := vault.NewClient(&vaultCfg)
|
2018-08-17 15:52:14 -04:00
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return nil, Errorf("crypto: client error %w", err)
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
2018-12-12 01:50:29 -05:00
|
|
|
if config.Namespace != "" {
|
|
|
|
client.SetNamespace(config.Namespace)
|
2018-11-27 17:42:32 -05:00
|
|
|
}
|
2019-01-29 06:27:23 -05:00
|
|
|
v := &vaultService{client: client, config: &config}
|
|
|
|
if err := v.authenticate(); err != nil {
|
2018-08-17 15:52:14 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
v.renewToken()
|
2018-12-12 01:50:29 -05:00
|
|
|
return v, nil
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:23:59 -05:00
|
|
|
// renewToken starts a new go-routine which renews
|
|
|
|
// the vault authentication token periodically and re-authenticates
|
|
|
|
// if the token renewal fails
|
|
|
|
func (v *vaultService) renewToken() {
|
|
|
|
retryDelay := v.leaseDuration / 2
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if v.secret == nil {
|
|
|
|
if err := v.authenticate(); err != nil {
|
|
|
|
time.Sleep(retryDelay)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s, err := v.client.Auth().Token().RenewSelf(int(v.leaseDuration))
|
|
|
|
if err != nil || s == nil {
|
|
|
|
v.secret = nil
|
|
|
|
time.Sleep(retryDelay)
|
|
|
|
continue
|
2019-01-29 06:27:23 -05:00
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
if ok, err := s.TokenIsRenewable(); !ok || err != nil {
|
|
|
|
v.secret = nil
|
|
|
|
continue
|
2019-02-13 18:25:32 -05:00
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
ttl, err := s.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
v.secret = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
v.secret = s
|
|
|
|
retryDelay = ttl / 2
|
|
|
|
time.Sleep(retryDelay)
|
2019-01-29 06:27:23 -05:00
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
}()
|
2019-01-29 06:27:23 -05:00
|
|
|
}
|
|
|
|
|
2019-02-20 15:23:59 -05:00
|
|
|
// authenticate logs the app to vault, and starts the auto renewer
|
|
|
|
// before secret expires
|
|
|
|
func (v *vaultService) authenticate() (err error) {
|
2019-01-29 06:27:23 -05:00
|
|
|
payload := map[string]interface{}{
|
|
|
|
"role_id": v.config.Auth.AppRole.ID,
|
|
|
|
"secret_id": v.config.Auth.AppRole.Secret,
|
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
var tokenID string
|
|
|
|
var ttl time.Duration
|
|
|
|
var secret *vault.Secret
|
|
|
|
secret, err = v.client.Logical().Write("auth/approle/login", payload)
|
2019-01-29 06:27:23 -05:00
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
err = Errorf("crypto: client error %w", err)
|
2019-02-20 15:23:59 -05:00
|
|
|
return
|
2019-02-13 18:25:32 -05:00
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
if secret == nil {
|
|
|
|
err = ErrKMSAuthLogin
|
|
|
|
return
|
2019-01-29 06:27:23 -05:00
|
|
|
}
|
2019-02-13 18:25:32 -05:00
|
|
|
|
2019-02-20 15:23:59 -05:00
|
|
|
tokenID, err = secret.TokenID()
|
2019-02-13 18:25:32 -05:00
|
|
|
if err != nil {
|
2019-02-20 15:23:59 -05:00
|
|
|
err = ErrKMSAuthLogin
|
|
|
|
return
|
2019-01-29 06:27:23 -05:00
|
|
|
}
|
2019-02-20 15:23:59 -05:00
|
|
|
ttl, err = secret.TokenTTL()
|
|
|
|
if err != nil {
|
|
|
|
err = ErrKMSAuthLogin
|
|
|
|
return
|
|
|
|
}
|
|
|
|
v.client.SetToken(tokenID)
|
|
|
|
v.secret = secret
|
|
|
|
v.leaseDuration = ttl
|
|
|
|
return
|
2019-01-29 06:27:23 -05:00
|
|
|
}
|
|
|
|
|
2020-07-08 21:50:43 -04:00
|
|
|
// DefaultKeyID returns the default key ID that should be
|
|
|
|
// used for SSE-S3 or SSE-KMS when the S3 client does not
|
|
|
|
// provide an explicit key ID.
|
|
|
|
func (v *vaultService) DefaultKeyID() string {
|
2019-10-08 01:47:56 -04:00
|
|
|
return v.config.Key.Name
|
|
|
|
}
|
|
|
|
|
2020-07-08 21:50:43 -04:00
|
|
|
// Info returns some information about the Vault,
|
2020-08-31 21:10:52 -04:00
|
|
|
// configuration - like the endpoints or authentication
|
2020-07-08 21:50:43 -04:00
|
|
|
// method.
|
|
|
|
func (v *vaultService) Info() KMSInfo {
|
2019-12-11 17:27:03 -05:00
|
|
|
return KMSInfo{
|
2020-08-31 21:10:52 -04:00
|
|
|
Endpoints: []string{v.config.Endpoint},
|
|
|
|
Name: v.DefaultKeyID(),
|
|
|
|
AuthType: v.config.Auth.Type,
|
2019-12-11 17:27:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 21:50:43 -04:00
|
|
|
// CreateKey is a stub that exists such that the Vault
|
|
|
|
// client implements the KMS interface. It always returns
|
|
|
|
// a not-implemented error.
|
|
|
|
//
|
|
|
|
// Creating keys requires a KES instance between MinIO and Vault.
|
|
|
|
func (v *vaultService) CreateKey(keyID string) error {
|
|
|
|
// Creating new keys requires KES.
|
|
|
|
return errors.New("crypto: creating keys is not supported by Vault")
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// GenerateKey returns a new plaintext key, generated by the KMS,
|
|
|
|
// and a sealed version of this plaintext key encrypted using the
|
|
|
|
// named key referenced by keyID. It also binds the generated key
|
|
|
|
// cryptographically to the provided context.
|
2018-08-17 15:52:14 -04:00
|
|
|
func (v *vaultService) GenerateKey(keyID string, ctx Context) (key [32]byte, sealedKey []byte, err error) {
|
2018-12-12 01:50:29 -05:00
|
|
|
var contextStream bytes.Buffer
|
|
|
|
ctx.WriteTo(&contextStream)
|
2018-08-17 15:52:14 -04:00
|
|
|
|
|
|
|
payload := map[string]interface{}{
|
|
|
|
"context": base64.StdEncoding.EncodeToString(contextStream.Bytes()),
|
|
|
|
}
|
2018-12-12 01:50:29 -05:00
|
|
|
s, err := v.client.Logical().Write(fmt.Sprintf("/transit/datakey/plaintext/%s", keyID), payload)
|
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return key, sealedKey, Errorf("crypto: client error %w", err)
|
|
|
|
}
|
|
|
|
sealKey, ok := s.Data["ciphertext"].(string)
|
|
|
|
if !ok {
|
|
|
|
return key, sealedKey, Errorf("crypto: incorrect 'ciphertext' key type %v", s.Data["ciphertext"])
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
2020-01-06 19:15:22 -05:00
|
|
|
|
|
|
|
plainKeyB64, ok := s.Data["plaintext"].(string)
|
|
|
|
if !ok {
|
|
|
|
return key, sealedKey, Errorf("crypto: incorrect 'plaintext' key type %v", s.Data["plaintext"])
|
|
|
|
}
|
|
|
|
|
|
|
|
plainKey, err := base64.StdEncoding.DecodeString(plainKeyB64)
|
2018-08-17 15:52:14 -04:00
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return key, sealedKey, Errorf("crypto: invalid base64 key %w", err)
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
2020-01-06 19:15:22 -05:00
|
|
|
copy(key[:], plainKey)
|
2018-08-17 15:52:14 -04:00
|
|
|
return key, []byte(sealKey), nil
|
|
|
|
}
|
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
// UnsealKey returns the decrypted sealedKey as plaintext key.
|
|
|
|
// Therefore it sends the sealedKey to the KMS which decrypts
|
|
|
|
// it using the named key referenced by keyID and responses with
|
|
|
|
// the plaintext key.
|
|
|
|
//
|
|
|
|
// The context must be same context as the one provided while
|
|
|
|
// generating the plaintext key / sealedKey.
|
2018-08-17 15:52:14 -04:00
|
|
|
func (v *vaultService) UnsealKey(keyID string, sealedKey []byte, ctx Context) (key [32]byte, err error) {
|
2018-12-12 01:50:29 -05:00
|
|
|
var contextStream bytes.Buffer
|
|
|
|
ctx.WriteTo(&contextStream)
|
|
|
|
|
2018-08-17 15:52:14 -04:00
|
|
|
payload := map[string]interface{}{
|
|
|
|
"ciphertext": string(sealedKey),
|
|
|
|
"context": base64.StdEncoding.EncodeToString(contextStream.Bytes()),
|
|
|
|
}
|
2020-01-06 19:15:22 -05:00
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
s, err := v.client.Logical().Write(fmt.Sprintf("/transit/decrypt/%s", keyID), payload)
|
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return key, Errorf("crypto: client error %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
base64Key, ok := s.Data["plaintext"].(string)
|
|
|
|
if !ok {
|
|
|
|
return key, Errorf("crypto: incorrect 'plaintext' key type %v", s.Data["plaintext"])
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
2020-01-06 19:15:22 -05:00
|
|
|
|
2018-12-12 01:50:29 -05:00
|
|
|
plainKey, err := base64.StdEncoding.DecodeString(base64Key)
|
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return key, Errorf("crypto: invalid base64 key %w", err)
|
2018-08-17 15:52:14 -04:00
|
|
|
}
|
2020-01-06 19:15:22 -05:00
|
|
|
copy(key[:], plainKey)
|
2018-08-17 15:52:14 -04:00
|
|
|
return key, nil
|
|
|
|
}
|
2019-08-01 18:47:47 -04:00
|
|
|
|
|
|
|
// UpdateKey re-wraps the sealedKey if the master key referenced by the keyID
|
|
|
|
// has been changed by the KMS operator - i.e. the master key has been rotated.
|
|
|
|
// If the master key hasn't changed since the sealedKey has been created / updated
|
|
|
|
// it may return the same sealedKey as rotatedKey.
|
|
|
|
//
|
|
|
|
// The context must be same context as the one provided while
|
|
|
|
// generating the plaintext key / sealedKey.
|
|
|
|
func (v *vaultService) UpdateKey(keyID string, sealedKey []byte, ctx Context) (rotatedKey []byte, err error) {
|
|
|
|
var contextStream bytes.Buffer
|
|
|
|
ctx.WriteTo(&contextStream)
|
|
|
|
|
|
|
|
payload := map[string]interface{}{
|
|
|
|
"ciphertext": string(sealedKey),
|
|
|
|
"context": base64.StdEncoding.EncodeToString(contextStream.Bytes()),
|
|
|
|
}
|
|
|
|
s, err := v.client.Logical().Write(fmt.Sprintf("/transit/rewrap/%s", keyID), payload)
|
|
|
|
if err != nil {
|
2020-01-06 19:15:22 -05:00
|
|
|
return nil, Errorf("crypto: client error %w", err)
|
2019-08-01 18:47:47 -04:00
|
|
|
}
|
|
|
|
ciphertext, ok := s.Data["ciphertext"]
|
|
|
|
if !ok {
|
|
|
|
return nil, errMissingUpdatedKey
|
|
|
|
}
|
2019-08-11 12:50:25 -04:00
|
|
|
rotatedKey = []byte(ciphertext.(string))
|
2019-08-01 18:47:47 -04:00
|
|
|
return rotatedKey, nil
|
|
|
|
}
|