Skip to content

Commit 71ea631

Browse files
authored
feat: implements kms v2beta1 APIs (#210)
Signed-off-by: Nilekh Chaudhari <1626598+nilekhc@users.noreply.github.com>
1 parent 5e07862 commit 71ea631

26 files changed

+1344
-970
lines changed

.pipelines/templates/e2e-kind-template.yml

+74-24
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@ jobs:
55
workspace:
66
clean: all
77
variables:
8-
- name: REGISTRY_NAME
9-
value: kind-registry
10-
- name: REGISTRY_PORT
11-
value: 5000
12-
- name: KUBERNETES_VERSION
13-
value: v1.23.5
14-
- name: KIND_CLUSTER_NAME
15-
value: kms
16-
- name: KIND_NETWORK
17-
value: kind
18-
# contains the following environment variables:
19-
# - AZURE_CLIENT_ID
20-
# - AZURE_CLIENT_SECRET
21-
# - AZURE_TENANT_ID
22-
# - KEYVAULT_NAME
23-
# - KEY_NAME
24-
# - KEY_VERSION
25-
- group: kubernetes-kms
8+
- name: REGISTRY_NAME
9+
value: kind-registry
10+
- name: REGISTRY_PORT
11+
value: 5000
12+
- name: KUBERNETES_VERSION
13+
value: v1.27.1
14+
- name: KIND_CLUSTER_NAME
15+
value: kms
16+
- name: KIND_NETWORK
17+
value: kind
18+
# contains the following environment variables:
19+
# - AZURE_CLIENT_ID
20+
# - AZURE_CLIENT_SECRET
21+
# - AZURE_TENANT_ID
22+
# - KEYVAULT_NAME
23+
# - KEY_NAME
24+
# - KEY_VERSION
25+
- group: kubernetes-kms
2626
strategy:
2727
matrix:
28-
kind_v1_23_13:
29-
KUBERNETES_VERSION: v1.23.13
30-
kind_v1_24_7:
28+
kmsv1_kind_v1_24_7:
3129
KUBERNETES_VERSION: v1.24.7
32-
kind_v1_25_3:
30+
kmsv1_kind_v1_25_3:
3331
KUBERNETES_VERSION: v1.25.3
34-
kind_v1_26_0:
32+
kmsv1_kind_v1_26_0:
3533
KUBERNETES_VERSION: v1.26.0
34+
kmsv1_kind_v1_27_1:
35+
KUBERNETES_VERSION: v1.27.1
3636
steps:
3737
- task: GoTool@0
3838
inputs:
@@ -53,5 +53,55 @@ jobs:
5353
- template: cluster-health-template.yml
5454
- template: kind-debug-template.yml
5555
- script: make e2e-test
56-
displayName: "Run e2e tests"
56+
displayName: "Run e2e tests for KMS v1"
57+
- template: cleanup-template.yml
58+
- job:
59+
timeoutInMinutes: 15
60+
cancelTimeoutInMinutes: 5
61+
workspace:
62+
clean: all
63+
variables:
64+
- name: REGISTRY_NAME
65+
value: kind-registry
66+
- name: REGISTRY_PORT
67+
value: 5000
68+
- name: KUBERNETES_VERSION
69+
value: v1.27.1
70+
- name: KIND_CLUSTER_NAME
71+
value: kms
72+
- name: KIND_NETWORK
73+
value: kind
74+
# contains the following environment variables:
75+
# - AZURE_CLIENT_ID
76+
# - AZURE_CLIENT_SECRET
77+
# - AZURE_TENANT_ID
78+
# - KEYVAULT_NAME
79+
# - KEY_NAME
80+
# - KEY_VERSION
81+
- group: kubernetes-kms
82+
strategy:
83+
matrix:
84+
kmsv2_kind_v1_27_1:
85+
KUBERNETES_VERSION: v1.27.1
86+
steps:
87+
- task: GoTool@0
88+
inputs:
89+
version: 1.19
90+
- script: make e2e-install-prerequisites
91+
displayName: "Install e2e test prerequisites"
92+
- script: |
93+
make e2e-kmsv2-setup-kind
94+
displayName: "Setup kind cluster with azure kms plugin"
95+
env:
96+
REGISTRY_NAME: $(REGISTRY_NAME)
97+
REGISTRY_PORT: $(REGISTRY_PORT)
98+
KUBERNETES_VERSION: $(KUBERNETES_VERSION)
99+
KIND_CLUSTER_NAME: $(KIND_CLUSTER_NAME)
100+
KIND_NETWORK: $(KIND_NETWORK)
101+
AZURE_CLIENT_ID: $(AZURE_CLIENT_ID)
102+
AZURE_CLIENT_SECRET: $(AZURE_CLIENT_SECRET)
103+
- template: cluster-health-template.yml
104+
- template: kind-debug-template.yml
105+
- script: make e2e-kmsv2-test
106+
displayName: "Run e2e tests for KMS v2"
57107
- template: cleanup-template.yml

Makefile

+11-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ DOCKER_BUILDKIT = 1
2929
export DOCKER_BUILDKIT
3030

3131
# Testing var
32-
KIND_VERSION ?= 0.15.0
33-
KUBERNETES_VERSION ?= v1.25.0
32+
KIND_VERSION ?= 0.18.0
33+
KUBERNETES_VERSION ?= v1.27.1
3434
BATS_VERSION ?= 1.4.1
3535

3636
## --------------------------------------
@@ -132,6 +132,11 @@ e2e-setup-kind: setup-local-registry
132132
./scripts/connect-registry.sh &
133133
sleep 90s
134134

135+
e2e-kmsv2-setup-kind: setup-local-registry
136+
./scripts/setup-kmsv2-kind-cluster.sh &
137+
./scripts/connect-registry.sh &
138+
sleep 90s
139+
135140
.PHONY: setup-local-registry
136141
setup-local-registry:
137142
./scripts/setup-local-registry.sh
@@ -148,3 +153,7 @@ e2e-delete-kind:
148153
e2e-test:
149154
# Run test suite with kind cluster
150155
bats -t tests/e2e/test.bats
156+
157+
e2e-kmsv2-test:
158+
# Run test suite with kind cluster
159+
bats -t tests/e2e/testkmsv2.bats

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Now that Azure KMS provider is running in your cluster and the encryption config
5555
sudo ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/certs/ca.crt --cert=/etc/kubernetes/certs/etcdclient.crt --key=/etc/kubernetes/certs/etcdclient.key get /registry/secrets/default/secret1
5656
```
5757

58-
3. Check that the stored secret is prefixed with `k8s:enc:kms:v1:azurekmsprovider`. This indicates the Azure KMS provider has encrypted the data.
58+
3. Check that the stored secret is prefixed with `k8s:enc:kms:v1:azurekmsprovider` when KMSv1 is used for encryption, or with `k8s:enc:kms:v2:azurekmsprovider` when KMSv2 is used. This prefix indicates that the data has been encrypted by the Azure KMS provider.
5959

6060
4. Verify the secret is decrypted correctly when retrieved via the Kubernetes API:
6161

@@ -69,6 +69,9 @@ Now that Azure KMS provider is running in your cluster and the encryption config
6969

7070
Refer to [doc](docs/rotation.md) for steps to rotate the KMS Key on an existing cluster.
7171

72+
## Metrics
73+
Refer to [doc](docs/metrics.md) for details on the metrics exposed by the KMS Key Vault plugin.
74+
7275
## Contributing
7376

7477
The KMS Plugin for Key Vault project welcomes contributions and suggestions. Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

cmd/server/main.go

+49-8
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@ import (
1616
"syscall"
1717
"time"
1818

19+
"github.com/Azure/kubernetes-kms/pkg/config"
1920
"github.com/Azure/kubernetes-kms/pkg/metrics"
2021
"github.com/Azure/kubernetes-kms/pkg/plugin"
2122
"github.com/Azure/kubernetes-kms/pkg/utils"
2223
"github.com/Azure/kubernetes-kms/pkg/version"
2324

2425
"google.golang.org/grpc"
25-
pb "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/v1beta1"
2626
logsapi "k8s.io/component-base/logs/api/v1"
2727
json "k8s.io/component-base/logs/json"
2828
"k8s.io/klog/v2"
29+
kmsv1 "k8s.io/kms/apis/v1beta1"
30+
kmsv2 "k8s.io/kms/apis/v2"
2931
)
3032

3133
var (
@@ -57,7 +59,12 @@ func main() {
5759

5860
if *logFormatJSON {
5961
jsonFactory := json.Factory{}
60-
logger, _ := jsonFactory.Create(logsapi.LoggingConfiguration{Format: "json"})
62+
logger, _ := jsonFactory.Create(
63+
logsapi.LoggingConfiguration{
64+
Format: "json",
65+
},
66+
logsapi.LoggingOptions{},
67+
)
6168
klog.SetLogger(logger)
6269
}
6370

@@ -80,7 +87,7 @@ func main() {
8087

8188
klog.InfoS("Starting KeyManagementServiceServer service", "version", version.BuildVersion, "buildDate", version.BuildDate)
8289

83-
pc := &plugin.Config{
90+
pluginConfig := &plugin.Config{
8491
KeyVaultName: *keyvaultName,
8592
KeyName: *keyName,
8693
KeyVersion: *keyVersion,
@@ -90,9 +97,25 @@ func main() {
9097
ProxyPort: *proxyPort,
9198
ConfigFilePath: *configFilePath,
9299
}
93-
kmsServer, err := plugin.New(pc)
100+
101+
azureConfig, err := config.GetAzureConfig(pluginConfig.ConfigFilePath)
94102
if err != nil {
95-
klog.ErrorS(err, "failed to create server")
103+
klog.ErrorS(err, "failed to get azure config")
104+
os.Exit(1)
105+
}
106+
107+
kvClient, err := plugin.NewKeyVaultClient(
108+
azureConfig,
109+
pluginConfig.KeyVaultName,
110+
pluginConfig.KeyName,
111+
pluginConfig.KeyVersion,
112+
pluginConfig.ProxyMode,
113+
pluginConfig.ProxyAddress,
114+
pluginConfig.ProxyPort,
115+
pluginConfig.ManagedHSM,
116+
)
117+
if err != nil {
118+
klog.ErrorS(err, "failed to create key vault client")
96119
os.Exit(1)
97120
}
98121

@@ -112,23 +135,41 @@ func main() {
112135
klog.ErrorS(err, "failed to listen", "addr", addr, "proto", proto)
113136
os.Exit(1)
114137
}
138+
115139
opts := []grpc.ServerOption{
116140
grpc.UnaryInterceptor(utils.UnaryServerInterceptor),
117141
}
118142

119143
s := grpc.NewServer(opts...)
120-
pb.RegisterKeyManagementServiceServer(s, kmsServer)
144+
145+
// register kms v1 server
146+
kmsV1Server, err := plugin.NewKMSv1Server(kvClient)
147+
if err != nil {
148+
klog.ErrorS(err, "failed to create server")
149+
os.Exit(1)
150+
}
151+
kmsv1.RegisterKeyManagementServiceServer(s, kmsV1Server)
152+
153+
// register kms v2 server
154+
kmsV2Server, err := plugin.NewKMSv2Server(kvClient)
155+
if err != nil {
156+
klog.ErrorS(err, "failed to create kms V2 server")
157+
os.Exit(1)
158+
}
159+
kmsv2.RegisterKeyManagementServiceServer(s, kmsV2Server)
121160

122161
klog.InfoS("Listening for connections", "addr", listener.Addr().String())
123162
go func() {
124163
if err := s.Serve(listener); err != nil {
125-
klog.ErrorS(err, "failed to serve")
164+
klog.ErrorS(err, "failed to serve kms server")
126165
os.Exit(1)
127166
}
128167
}()
129168

169+
// Health check for kms v1 and v2
130170
healthz := &plugin.HealthZ{
131-
KMSServer: kmsServer,
171+
KMSv1Server: kmsV1Server,
172+
KMSv2Server: kmsV2Server,
132173
HealthCheckURL: &url.URL{
133174
Host: net.JoinHostPort("", strconv.FormatUint(uint64(*healthzPort), 10)),
134175
Path: *healthzPath,

0 commit comments

Comments
 (0)