sops/azkv/keysource.go

237 строки
8.1 KiB
Go
Исходник Постоянная ссылка Обычный вид История

2018-06-17 23:50:30 +03:00
/*
Package azkv contains an implementation of the github.com/getsops/sops/v3/keys.MasterKey
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
interface that encrypts and decrypts the data key using Azure Key Vault with the
Azure Key Vault Keys client module for Go.
2018-06-17 23:50:30 +03:00
*/
package azkv // import "github.com/getsops/sops/v3/azkv"
2018-06-17 23:50:30 +03:00
import (
"context"
"encoding/base64"
"fmt"
"regexp"
"strings"
"time"
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys"
2018-06-17 23:50:30 +03:00
"github.com/sirupsen/logrus"
"github.com/getsops/sops/v3/logging"
2018-06-17 23:50:30 +03:00
)
const (
// KeyTypeIdentifier is the string used to identify an Azure Key Vault MasterKey.
KeyTypeIdentifier = "azure_kv"
)
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
var (
// log is the global logger for any Azure Key Vault MasterKey.
log *logrus.Logger
// azkvTTL is the duration after which a MasterKey requires rotation.
azkvTTL = time.Hour * 24 * 30 * 6
)
2018-06-17 23:50:30 +03:00
func init() {
log = logging.NewLogger("AZKV")
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// MasterKey is an Azure Key Vault Key used to Encrypt and Decrypt SOPS'
// data key.
2018-06-17 23:50:30 +03:00
type MasterKey struct {
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// VaultURL of the Azure Key Vault. For example:
// "https://myvault.vault.azure.net/".
2018-06-17 23:50:30 +03:00
VaultURL string
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// Name of the Azure Key Vault key in the VaultURL.
Name string
// Version of the Azure Key Vault key. Can be empty.
Version string
// EncryptedKey contains the SOPS data key encrypted with the Azure Key
// Vault key.
2018-06-17 23:50:30 +03:00
EncryptedKey string
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// CreationDate of the MasterKey, used to determine if the EncryptedKey
// needs rotation.
2018-06-17 23:50:30 +03:00
CreationDate time.Time
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// tokenCredential contains the azcore.TokenCredential used by the Azure
// client. It can be injected by a (local) keyservice.KeyServiceServer
// using TokenCredential.ApplyToMasterKey.
// If nil, azidentity.NewDefaultAzureCredential is used.
tokenCredential azcore.TokenCredential
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// NewMasterKey creates a new MasterKey from a URL, key name and version,
// setting the creation date to the current date.
2018-06-17 23:50:30 +03:00
func NewMasterKey(vaultURL string, keyName string, keyVersion string) *MasterKey {
return &MasterKey{
VaultURL: vaultURL,
Name: keyName,
Version: keyVersion,
CreationDate: time.Now().UTC(),
}
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// NewMasterKeyFromURL takes an Azure Key Vault key URL, and returns a new
// MasterKey. The URL format is {vaultUrl}/keys/{keyName}/{keyVersion}.
func NewMasterKeyFromURL(url string) (*MasterKey, error) {
url = strings.TrimSpace(url)
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
re := regexp.MustCompile("^(https://[^/]+)/keys/([^/]+)/([^/]+)$")
parts := re.FindStringSubmatch(url)
if parts == nil || len(parts) < 3 {
return nil, fmt.Errorf("could not parse %q into a valid Azure Key Vault MasterKey", url)
}
return NewMasterKey(parts[1], parts[2], parts[3]), nil
}
// MasterKeysFromURLs takes a comma separated list of Azure Key Vault URLs,
// and returns a slice of new MasterKeys.
2018-06-18 17:03:24 +03:00
func MasterKeysFromURLs(urls string) ([]*MasterKey, error) {
2018-06-17 23:50:30 +03:00
var keys []*MasterKey
if urls == "" {
2018-06-18 17:03:24 +03:00
return keys, nil
2018-06-17 23:50:30 +03:00
}
for _, s := range strings.Split(urls, ",") {
2018-06-18 17:03:24 +03:00
k, err := NewMasterKeyFromURL(s)
if err != nil {
return nil, err
}
keys = append(keys, k)
2018-06-17 23:50:30 +03:00
}
2018-06-18 17:03:24 +03:00
return keys, nil
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// TokenCredential is an azcore.TokenCredential used for authenticating towards Azure Key
// Vault.
type TokenCredential struct {
token azcore.TokenCredential
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// NewTokenCredential creates a new TokenCredential with the provided azcore.TokenCredential.
func NewTokenCredential(token azcore.TokenCredential) *TokenCredential {
return &TokenCredential{token: token}
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// ApplyToMasterKey configures the TokenCredential on the provided key.
func (t TokenCredential) ApplyToMasterKey(key *MasterKey) {
key.tokenCredential = t.token
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// Encrypt takes a SOPS data key, encrypts it with Azure Key Vault, and stores
// the result in the EncryptedKey field.
2018-06-17 23:50:30 +03:00
func (key *MasterKey) Encrypt(dataKey []byte) error {
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
token, err := key.getTokenCredential()
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Encryption failed")
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
return fmt.Errorf("failed to get Azure token credential to encrypt data: %w", err)
}
2018-06-17 23:50:30 +03:00
azkv: update Azure SDK to v0.9.0 Besides some signature changes, really nothing special. ``` $ go test --tags integration ./... -v === RUN TestMasterKey_Encrypt --- PASS: TestMasterKey_Encrypt (1.34s) === RUN TestMasterKey_Decrypt --- PASS: TestMasterKey_Decrypt (0.68s) === RUN TestMasterKey_EncryptDecrypt_RoundTrip --- PASS: TestMasterKey_EncryptDecrypt_RoundTrip (0.40s) === RUN TestNewMasterKeyFromURL === RUN TestNewMasterKeyFromURL/URL === RUN TestNewMasterKeyFromURL/malformed_URL --- PASS: TestNewMasterKeyFromURL (0.00s) --- PASS: TestNewMasterKeyFromURL/URL (0.00s) --- PASS: TestNewMasterKeyFromURL/malformed_URL (0.00s) === RUN TestMasterKeysFromURLs === RUN TestMasterKeysFromURLs/single_URL === RUN TestMasterKeysFromURLs/multiple_URLs === RUN TestMasterKeysFromURLs/multiple_URLs,_one_malformed === RUN TestMasterKeysFromURLs/empty --- PASS: TestMasterKeysFromURLs (0.00s) --- PASS: TestMasterKeysFromURLs/single_URL (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs,_one_malformed (0.00s) --- PASS: TestMasterKeysFromURLs/empty (0.00s) === RUN TestTokenCredential_ApplyToMasterKey --- PASS: TestTokenCredential_ApplyToMasterKey (0.00s) === RUN TestMasterKey_EncryptedDataKey --- PASS: TestMasterKey_EncryptedDataKey (0.00s) === RUN TestMasterKey_SetEncryptedDataKey --- PASS: TestMasterKey_SetEncryptedDataKey (0.00s) === RUN TestMasterKey_EncryptIfNeeded === RUN TestMasterKey_EncryptIfNeeded/not_encrypted [AZKV] time="2022-11-21T20:41:58Z" level=error msg="Encryption failed" key=test-key version=a2a690a4fcc04166b739da342a912c90 === RUN TestMasterKey_EncryptIfNeeded/already_encrypted --- PASS: TestMasterKey_EncryptIfNeeded (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/not_encrypted (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/already_encrypted (0.00s) === RUN TestMasterKey_NeedsRotation --- PASS: TestMasterKey_NeedsRotation (0.00s) === RUN TestMasterKey_ToString --- PASS: TestMasterKey_ToString (0.00s) === RUN TestMasterKey_ToMap --- PASS: TestMasterKey_ToMap (0.00s) === RUN TestMasterKey_getTokenCredential === RUN TestMasterKey_getTokenCredential/with_TokenCredential === RUN TestMasterKey_getTokenCredential/default --- PASS: TestMasterKey_getTokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/with_TokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/default (0.00s) PASS ok go.mozilla.org/sops/v3/azkv 4.551s ``` Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-11-21 23:21:32 +03:00
c, err := azkeys.NewClient(key.VaultURL, token, nil)
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Encryption failed")
azkv: update Azure SDK to v0.9.0 Besides some signature changes, really nothing special. ``` $ go test --tags integration ./... -v === RUN TestMasterKey_Encrypt --- PASS: TestMasterKey_Encrypt (1.34s) === RUN TestMasterKey_Decrypt --- PASS: TestMasterKey_Decrypt (0.68s) === RUN TestMasterKey_EncryptDecrypt_RoundTrip --- PASS: TestMasterKey_EncryptDecrypt_RoundTrip (0.40s) === RUN TestNewMasterKeyFromURL === RUN TestNewMasterKeyFromURL/URL === RUN TestNewMasterKeyFromURL/malformed_URL --- PASS: TestNewMasterKeyFromURL (0.00s) --- PASS: TestNewMasterKeyFromURL/URL (0.00s) --- PASS: TestNewMasterKeyFromURL/malformed_URL (0.00s) === RUN TestMasterKeysFromURLs === RUN TestMasterKeysFromURLs/single_URL === RUN TestMasterKeysFromURLs/multiple_URLs === RUN TestMasterKeysFromURLs/multiple_URLs,_one_malformed === RUN TestMasterKeysFromURLs/empty --- PASS: TestMasterKeysFromURLs (0.00s) --- PASS: TestMasterKeysFromURLs/single_URL (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs,_one_malformed (0.00s) --- PASS: TestMasterKeysFromURLs/empty (0.00s) === RUN TestTokenCredential_ApplyToMasterKey --- PASS: TestTokenCredential_ApplyToMasterKey (0.00s) === RUN TestMasterKey_EncryptedDataKey --- PASS: TestMasterKey_EncryptedDataKey (0.00s) === RUN TestMasterKey_SetEncryptedDataKey --- PASS: TestMasterKey_SetEncryptedDataKey (0.00s) === RUN TestMasterKey_EncryptIfNeeded === RUN TestMasterKey_EncryptIfNeeded/not_encrypted [AZKV] time="2022-11-21T20:41:58Z" level=error msg="Encryption failed" key=test-key version=a2a690a4fcc04166b739da342a912c90 === RUN TestMasterKey_EncryptIfNeeded/already_encrypted --- PASS: TestMasterKey_EncryptIfNeeded (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/not_encrypted (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/already_encrypted (0.00s) === RUN TestMasterKey_NeedsRotation --- PASS: TestMasterKey_NeedsRotation (0.00s) === RUN TestMasterKey_ToString --- PASS: TestMasterKey_ToString (0.00s) === RUN TestMasterKey_ToMap --- PASS: TestMasterKey_ToMap (0.00s) === RUN TestMasterKey_getTokenCredential === RUN TestMasterKey_getTokenCredential/with_TokenCredential === RUN TestMasterKey_getTokenCredential/default --- PASS: TestMasterKey_getTokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/with_TokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/default (0.00s) PASS ok go.mozilla.org/sops/v3/azkv 4.551s ``` Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-11-21 23:21:32 +03:00
return fmt.Errorf("failed to construct Azure Key Vault client to encrypt data: %w", err)
}
resp, err := c.Encrypt(context.Background(), key.Name, key.Version, azkeys.KeyOperationParameters{
Algorithm: to.Ptr(azkeys.EncryptionAlgorithmRSAOAEP256),
Value: dataKey,
}, nil)
2018-06-17 23:50:30 +03:00
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Encryption failed")
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
return fmt.Errorf("failed to encrypt sops data key with Azure Key Vault key '%s': %w", key.ToString(), err)
2018-06-17 23:50:30 +03:00
}
encodedEncryptedKey := base64.RawURLEncoding.EncodeToString(resp.KeyOperationResult.Result)
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
key.SetEncryptedDataKey([]byte(encodedEncryptedKey))
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Encryption succeeded")
return nil
}
2018-06-17 23:50:30 +03:00
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// EncryptedDataKey returns the encrypted data key this master key holds.
func (key *MasterKey) EncryptedDataKey() []byte {
return []byte(key.EncryptedKey)
}
2018-06-17 23:50:30 +03:00
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// SetEncryptedDataKey sets the encrypted data key for this master key.
func (key *MasterKey) SetEncryptedDataKey(enc []byte) {
key.EncryptedKey = string(enc)
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// EncryptIfNeeded encrypts the provided SOPS data key, if it has not been
// encrypted yet.
2018-06-17 23:50:30 +03:00
func (key *MasterKey) EncryptIfNeeded(dataKey []byte) error {
if key.EncryptedKey == "" {
return key.Encrypt(dataKey)
}
return nil
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// Decrypt decrypts the EncryptedKey field with Azure Key Vault and returns
// the result.
2018-06-17 23:50:30 +03:00
func (key *MasterKey) Decrypt() ([]byte, error) {
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
token, err := key.getTokenCredential()
2018-06-17 23:50:30 +03:00
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Decryption failed")
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
return nil, fmt.Errorf("failed to get Azure token credential to decrypt: %w", err)
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
rawEncryptedKey, err := base64.RawURLEncoding.DecodeString(key.EncryptedKey)
2018-06-17 23:50:30 +03:00
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Decryption failed")
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
return nil, fmt.Errorf("failed to base64 decode Azure Key Vault encrypted key: %w", err)
2018-06-17 23:50:30 +03:00
}
azkv: update Azure SDK to v0.9.0 Besides some signature changes, really nothing special. ``` $ go test --tags integration ./... -v === RUN TestMasterKey_Encrypt --- PASS: TestMasterKey_Encrypt (1.34s) === RUN TestMasterKey_Decrypt --- PASS: TestMasterKey_Decrypt (0.68s) === RUN TestMasterKey_EncryptDecrypt_RoundTrip --- PASS: TestMasterKey_EncryptDecrypt_RoundTrip (0.40s) === RUN TestNewMasterKeyFromURL === RUN TestNewMasterKeyFromURL/URL === RUN TestNewMasterKeyFromURL/malformed_URL --- PASS: TestNewMasterKeyFromURL (0.00s) --- PASS: TestNewMasterKeyFromURL/URL (0.00s) --- PASS: TestNewMasterKeyFromURL/malformed_URL (0.00s) === RUN TestMasterKeysFromURLs === RUN TestMasterKeysFromURLs/single_URL === RUN TestMasterKeysFromURLs/multiple_URLs === RUN TestMasterKeysFromURLs/multiple_URLs,_one_malformed === RUN TestMasterKeysFromURLs/empty --- PASS: TestMasterKeysFromURLs (0.00s) --- PASS: TestMasterKeysFromURLs/single_URL (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs,_one_malformed (0.00s) --- PASS: TestMasterKeysFromURLs/empty (0.00s) === RUN TestTokenCredential_ApplyToMasterKey --- PASS: TestTokenCredential_ApplyToMasterKey (0.00s) === RUN TestMasterKey_EncryptedDataKey --- PASS: TestMasterKey_EncryptedDataKey (0.00s) === RUN TestMasterKey_SetEncryptedDataKey --- PASS: TestMasterKey_SetEncryptedDataKey (0.00s) === RUN TestMasterKey_EncryptIfNeeded === RUN TestMasterKey_EncryptIfNeeded/not_encrypted [AZKV] time="2022-11-21T20:41:58Z" level=error msg="Encryption failed" key=test-key version=a2a690a4fcc04166b739da342a912c90 === RUN TestMasterKey_EncryptIfNeeded/already_encrypted --- PASS: TestMasterKey_EncryptIfNeeded (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/not_encrypted (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/already_encrypted (0.00s) === RUN TestMasterKey_NeedsRotation --- PASS: TestMasterKey_NeedsRotation (0.00s) === RUN TestMasterKey_ToString --- PASS: TestMasterKey_ToString (0.00s) === RUN TestMasterKey_ToMap --- PASS: TestMasterKey_ToMap (0.00s) === RUN TestMasterKey_getTokenCredential === RUN TestMasterKey_getTokenCredential/with_TokenCredential === RUN TestMasterKey_getTokenCredential/default --- PASS: TestMasterKey_getTokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/with_TokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/default (0.00s) PASS ok go.mozilla.org/sops/v3/azkv 4.551s ``` Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-11-21 23:21:32 +03:00
c, err := azkeys.NewClient(key.VaultURL, token, nil)
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Decryption failed")
azkv: update Azure SDK to v0.9.0 Besides some signature changes, really nothing special. ``` $ go test --tags integration ./... -v === RUN TestMasterKey_Encrypt --- PASS: TestMasterKey_Encrypt (1.34s) === RUN TestMasterKey_Decrypt --- PASS: TestMasterKey_Decrypt (0.68s) === RUN TestMasterKey_EncryptDecrypt_RoundTrip --- PASS: TestMasterKey_EncryptDecrypt_RoundTrip (0.40s) === RUN TestNewMasterKeyFromURL === RUN TestNewMasterKeyFromURL/URL === RUN TestNewMasterKeyFromURL/malformed_URL --- PASS: TestNewMasterKeyFromURL (0.00s) --- PASS: TestNewMasterKeyFromURL/URL (0.00s) --- PASS: TestNewMasterKeyFromURL/malformed_URL (0.00s) === RUN TestMasterKeysFromURLs === RUN TestMasterKeysFromURLs/single_URL === RUN TestMasterKeysFromURLs/multiple_URLs === RUN TestMasterKeysFromURLs/multiple_URLs,_one_malformed === RUN TestMasterKeysFromURLs/empty --- PASS: TestMasterKeysFromURLs (0.00s) --- PASS: TestMasterKeysFromURLs/single_URL (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs (0.00s) --- PASS: TestMasterKeysFromURLs/multiple_URLs,_one_malformed (0.00s) --- PASS: TestMasterKeysFromURLs/empty (0.00s) === RUN TestTokenCredential_ApplyToMasterKey --- PASS: TestTokenCredential_ApplyToMasterKey (0.00s) === RUN TestMasterKey_EncryptedDataKey --- PASS: TestMasterKey_EncryptedDataKey (0.00s) === RUN TestMasterKey_SetEncryptedDataKey --- PASS: TestMasterKey_SetEncryptedDataKey (0.00s) === RUN TestMasterKey_EncryptIfNeeded === RUN TestMasterKey_EncryptIfNeeded/not_encrypted [AZKV] time="2022-11-21T20:41:58Z" level=error msg="Encryption failed" key=test-key version=a2a690a4fcc04166b739da342a912c90 === RUN TestMasterKey_EncryptIfNeeded/already_encrypted --- PASS: TestMasterKey_EncryptIfNeeded (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/not_encrypted (2.12s) --- PASS: TestMasterKey_EncryptIfNeeded/already_encrypted (0.00s) === RUN TestMasterKey_NeedsRotation --- PASS: TestMasterKey_NeedsRotation (0.00s) === RUN TestMasterKey_ToString --- PASS: TestMasterKey_ToString (0.00s) === RUN TestMasterKey_ToMap --- PASS: TestMasterKey_ToMap (0.00s) === RUN TestMasterKey_getTokenCredential === RUN TestMasterKey_getTokenCredential/with_TokenCredential === RUN TestMasterKey_getTokenCredential/default --- PASS: TestMasterKey_getTokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/with_TokenCredential (0.00s) --- PASS: TestMasterKey_getTokenCredential/default (0.00s) PASS ok go.mozilla.org/sops/v3/azkv 4.551s ``` Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-11-21 23:21:32 +03:00
return nil, fmt.Errorf("failed to construct Azure Key Vault client to decrypt data: %w", err)
}
resp, err := c.Decrypt(context.Background(), key.Name, key.Version, azkeys.KeyOperationParameters{
Algorithm: to.Ptr(azkeys.EncryptionAlgorithmRSAOAEP256),
Value: rawEncryptedKey,
}, nil)
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
if err != nil {
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Decryption failed")
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
return nil, fmt.Errorf("failed to decrypt sops data key with Azure Key Vault key '%s': %w", key.ToString(), err)
}
log.WithFields(logrus.Fields{"key": key.Name, "version": key.Version}).Info("Decryption succeeded")
return resp.KeyOperationResult.Result, nil
2018-06-17 23:50:30 +03:00
}
// NeedsRotation returns whether the data key needs to be rotated or not.
func (key *MasterKey) NeedsRotation() bool {
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
return time.Since(key.CreationDate) > (azkvTTL)
2018-06-17 23:50:30 +03:00
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// ToString converts the key to a string representation.
2018-06-17 23:50:30 +03:00
func (key *MasterKey) ToString() string {
return fmt.Sprintf("%s/keys/%s/%s", key.VaultURL, key.Name, key.Version)
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// ToMap converts the MasterKey to a map for serialization purposes.
2018-06-17 23:50:30 +03:00
func (key MasterKey) ToMap() map[string]interface{} {
out := make(map[string]interface{})
out["vaultUrl"] = key.VaultURL
out["key"] = key.Name
out["version"] = key.Version
out["created_at"] = key.CreationDate.UTC().Format(time.RFC3339)
out["enc"] = key.EncryptedKey
return out
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// TypeToIdentifier returns the string identifier for the MasterKey type.
func (key *MasterKey) TypeToIdentifier() string {
return KeyTypeIdentifier
}
azkv: update SDK to latest, add tests, tidy This updates the Azure SDK to latest[1], while dropping the custom authentication flow in favor of the SDK default[2]. It includes integration tests, which require the `integration` Go build tag and a set of environmental variables to be configured to be run: ``` PASS coverage: 81.2% of statements ok go.mozilla.org/sops/v3/azkv 5.376s coverage: 81.2% of statements ``` The improvements are based on a fork of the key source in the Flux project's kustomize-controller, built due to SOPS' limitation around credential managment without relying on runtime environment variables. - Azure SDK has been updated to latest, including integration test coverage. - Custom authentication flow has been dropped in favor of the SDK default[2]. This should work well on almost any system and is generally the go-to way of setting this up, including on cloud environments, etc. - It introduces a `TokenCredential` type which holds an `azcore.TokenCredential`, and can be applied to a `MasterKey`. When applied, the token is used instead of the SDK default. This is most useful when working with SOPS as an SDK, in combination with e.g. a local key service server implementation. - Extensive test coverage. The forked version of this has compatibility tests to ensure it works with current SOPS: - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L89 - https://github.com/fluxcd/kustomize-controller/blob/327a3560b35c5994647938958aa5523f319094b8/internal/sops/azkv/keysource_integration_test.go#L117 [1]: https://github.com/Azure/azure-sdk-for-go/tree/main/sdk/keyvault/azkeys [2]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential Signed-off-by: Hidde Beydals <hello@hidde.co>
2022-05-30 14:27:55 +03:00
// getTokenCredential returns the tokenCredential of the MasterKey, or
// azidentity.NewDefaultAzureCredential.
func (key *MasterKey) getTokenCredential() (azcore.TokenCredential, error) {
if key.tokenCredential == nil {
return azidentity.NewDefaultAzureCredential(nil)
}
return key.tokenCredential, nil
}