Skip to content

Commit b17d0cd

Browse files
Merge pull request #4039 from maqiuyujoyce/resource-kms-importjob
Support mockgcp for KMSImportJob
2 parents df3a1a5 + 502ffb1 commit b17d0cd

File tree

9 files changed

+324
-11
lines changed

9 files changed

+324
-11
lines changed

mockgcp/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ gen-proto-no-fixup:
172172
./third_party/googleapis/mockgcp/cloud/essentialcontacts/v1/*.proto \
173173
./third_party/googleapis/mockgcp/cloud/networkmanagement/v1/*.proto \
174174
./third_party/googleapis/mockgcp/cloud/netapp/v1/*.proto \
175-
./third_party/googleapis/mockgcp/cloud/dataplex/v1/*.proto
176-
./third_party/googleapis/mockgcp/cloud/vmwareengine/v1/*.proto
175+
./third_party/googleapis/mockgcp/cloud/dataplex/v1/*.proto \
176+
./third_party/googleapis/mockgcp/cloud/vmwareengine/v1/*.proto \
177+
./third_party/googleapis/mockgcp/cloud/kms/v1/resources.proto
177178

178179
.PHONY: generate-grpc-for-google-protos
179180
generate-grpc-for-google-protos:

mockgcp/mock_http_roundtrip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ func NewMockRoundTripper(ctx context.Context, k8sClient client.Client, storage s
210210
services = append(services, mockedgecontainer.New(env, storage))
211211
services = append(services, mockfirestore.New(env, storage))
212212
services = append(services, mockgkemulticloud.New(env, storage))
213-
services = append(services, mockkms.New(env, storage))
214213
services = append(services, mocklogging.New(env, storage))
215214
services = append(services, mockmanagedkafka.New(env, storage))
216215
services = append(services, mocknetworkmanagement.New(env, storage))
@@ -263,6 +262,7 @@ func NewMockRoundTripper(ctx context.Context, k8sClient client.Client, storage s
263262
services = append(services, mockdataplex.New(env, storage))
264263
services = append(services, mockclouddms.New(env, storage))
265264
services = append(services, mockvmwareengine.New(env, storage))
265+
services = append(services, mockkms.New(env, storage))
266266

267267
for _, service := range services {
268268
service.Register(server)

mockgcp/mockaiplatform/testdata/schedule/crud/_http.log

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ X-Xss-Protection: 0
9292
},
9393
"parent": "projects/${projectId}/locations/us-central1"
9494
},
95-
"createTime": "2024-01-01T12:34:56.123456Z",
95+
"createTime": "2024-04-01T12:34:56.123456Z",
9696
"cron": "* * * * *",
9797
"displayName": "test-${uniqueId}",
9898
"maxConcurrentRunCount": "1",
9999
"name": "projects/${projectNumber}/locations/us-central1/schedules/test-${uniqueId}",
100100
"nextRunTime": "2024-04-01T12:34:56.123456Z",
101-
"startTime": "2024-01-03T12:34:56.123456Z",
101+
"startTime": "2024-04-01T12:34:56.123456Z",
102102
"state": "ACTIVE"
103103
}
104104
]
@@ -179,13 +179,13 @@ X-Xss-Protection: 0
179179
},
180180
"parent": "projects/${projectId}/locations/us-central1"
181181
},
182-
"createTime": "2024-01-01T12:34:56.123456Z",
182+
"createTime": "2024-04-01T12:34:56.123456Z",
183183
"cron": "* * * * *",
184184
"displayName": "test-${uniqueId}",
185185
"maxConcurrentRunCount": "1",
186186
"name": "projects/${projectNumber}/locations/us-central1/schedules/test-${uniqueId}",
187187
"nextRunTime": "2024-04-01T12:34:56.123456Z",
188-
"startTime": "2024-01-03T12:34:56.123456Z",
188+
"startTime": "2024-04-01T12:34:56.123456Z",
189189
"state": "ACTIVE"
190190
}
191191
]

mockgcp/mockkms/importjob.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// +tool:mockgcp-support
16+
// proto.service: google.cloud.kms.v1.KeyManagementService
17+
// proto.message: google.cloud.kms.v1.ImportJob
18+
19+
package mockkms
20+
21+
import (
22+
"context"
23+
"fmt"
24+
"strings"
25+
"time"
26+
27+
"google.golang.org/grpc/codes"
28+
"google.golang.org/grpc/status"
29+
"google.golang.org/protobuf/proto"
30+
"google.golang.org/protobuf/types/known/timestamppb"
31+
32+
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/kms/v1"
33+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
34+
)
35+
36+
func (s *kmsServer) GetImportJob(ctx context.Context, req *pb.GetImportJobRequest) (*pb.ImportJob, error) {
37+
name, err := s.parseImportJobName(req.Name)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
fqn := name.String()
43+
44+
obj := &pb.ImportJob{}
45+
if err := s.storage.Get(ctx, fqn, obj); err != nil {
46+
if status.Code(err) == codes.NotFound {
47+
return nil, status.Errorf(codes.NotFound, "ImportJob %s not found.", fqn)
48+
}
49+
return nil, err
50+
}
51+
52+
return obj, nil
53+
}
54+
55+
func (s *kmsServer) ListImportJobs(ctx context.Context, req *pb.ListImportJobsRequest) (*pb.ListImportJobsResponse, error) {
56+
var importJobs []*pb.ImportJob
57+
58+
importJobKind := (&pb.ImportJob{}).ProtoReflect().Descriptor()
59+
if err := s.storage.List(ctx, importJobKind, storage.ListOptions{}, func(obj proto.Message) error {
60+
importJob := obj.(*pb.ImportJob)
61+
if strings.HasPrefix(importJob.GetName(), req.Parent) {
62+
importJobs = append(importJobs, importJob)
63+
}
64+
return nil
65+
}); err != nil {
66+
return nil, err
67+
}
68+
69+
return &pb.ListImportJobsResponse{
70+
ImportJobs: importJobs,
71+
TotalSize: int32(len(importJobs)),
72+
}, nil
73+
}
74+
75+
func (s *kmsServer) CreateImportJob(ctx context.Context, req *pb.CreateImportJobRequest) (*pb.ImportJob, error) {
76+
reqName := fmt.Sprintf("%s/importJobs/%s", req.GetParent(), req.GetImportJobId())
77+
name, err := s.parseImportJobName(reqName)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
fqn := name.String()
83+
84+
now := time.Now()
85+
86+
obj := proto.Clone(req.GetImportJob()).(*pb.ImportJob)
87+
obj.Name = fqn
88+
obj.CreateTime = timestamppb.New(now)
89+
obj.ExpireTime = timestamppb.New(now)
90+
obj.ImportMethod = pb.ImportJob_RSA_OAEP_3072_SHA1_AES_256
91+
obj.State = pb.ImportJob_PENDING_GENERATION
92+
93+
result := proto.Clone(obj).(*pb.ImportJob)
94+
95+
obj.GenerateTime = timestamppb.New(now)
96+
obj.State = pb.ImportJob_ACTIVE
97+
obj.Attestation = &pb.KeyOperationAttestation{
98+
CertChains: &pb.KeyOperationAttestation_CertificateChains{
99+
CaviumCerts: []string{
100+
"-----BEGIN CERTIFICATE-----\ncertificate 1\n-----END CERTIFICATE-----\n",
101+
"-----BEGIN CERTIFICATE-----\ncertificate 2\n-----END CERTIFICATE-----\n",
102+
},
103+
GoogleCardCerts: []string{
104+
"-----BEGIN CERTIFICATE-----\ncertificate 3\n-----END CERTIFICATE-----\n",
105+
},
106+
GooglePartitionCerts: []string{
107+
"-----BEGIN CERTIFICATE-----\ncertificate 4\n-----END CERTIFICATE-----\n",
108+
},
109+
},
110+
Content: []byte("content"),
111+
Format: pb.KeyOperationAttestation_CAVIUM_V2_COMPRESSED,
112+
}
113+
obj.PublicKey = &pb.ImportJob_WrappingPublicKey{
114+
Pem: "-----BEGIN PUBLIC KEY-----\npublic key\n-----END PUBLIC KEY-----\n",
115+
}
116+
117+
if err := s.storage.Create(ctx, fqn, obj); err != nil {
118+
return nil, err
119+
}
120+
121+
return result, nil
122+
}
123+
124+
type importJobName struct {
125+
KeyRingName
126+
ImportJobID string
127+
}
128+
129+
func (n *importJobName) String() string {
130+
return n.KeyRingName.String() + "/importJobs/" + n.ImportJobID
131+
}
132+
133+
// parseImportJobName parses a string into an ImportJobName.
134+
// The expected form is `projects/*/locations/*/keyRings/*/importJobs/*`.
135+
func (s *kmsServer) parseImportJobName(name string) (*importJobName, error) {
136+
tokens := strings.Split(name, "/")
137+
138+
if len(tokens) == 8 && tokens[6] == "importJobs" {
139+
keyRingName, err := s.parseKeyRingName(strings.Join(tokens[0:6], "/"))
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
name := &importJobName{
145+
KeyRingName: *keyRingName,
146+
ImportJobID: tokens[7],
147+
}
148+
149+
return name, nil
150+
}
151+
152+
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
153+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Please add the generation for `google/cloud/kms/v1/resources.proto` to the `gen-proto-no-fixup` target in `Makefile`.
2+
3+
Hints:
4+
5+
* Use the ReadFile command to read the contents of the file.
6+
7+
* Use the EditFile command to insert the appropriate third_party directory into the list of paths.
8+
9+
* The gen-proto-no-fixup command contains a long protoc command, split across multiple lines. There should be a backslash character (\) on all lines but the last. Make sure there is a space before the backslash.
10+
11+
* The generation path being added should begin with `./third_party/googleapis/mockgcp/importjob` and should not contain google after mockgcp.
12+
13+
* This is not a correct path: `./third_party/googleapis/mockgcp/google/cloud/metastore/...`
14+
15+
* This is the correct path: `./third_party/googleapis/mockgcp/cloud/metastore/...`
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Please add the services in `mockkms` to `mock_http_roundtrip.go`
2+
3+
* Use the ReadFile command to read the contents of the file.
4+
* Use the EditFile command to insert mockkms into the list of services.
5+
* Please keep the list of services in alphabetical order.
6+
* Don't forget to import the package!
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
POST https://cloudkms.googleapis.com/v1/projects/${projectId}/locations/us-central1/keyRings/test-keyring-${uniqueId}/importJobs?alt=json&importJobId=test-${uniqueId}
2+
Accept: application/json
3+
Authorization: (removed)
4+
Connection: keep-alive
5+
Content-Type: application/json
6+
7+
{
8+
"importMethod": "RSA_OAEP_3072_SHA1_AES_256",
9+
"protectionLevel": "HSM"
10+
}
11+
12+
200 OK
13+
Content-Type: application/json; charset=UTF-8
14+
Server: ESF
15+
Vary: Origin
16+
Vary: X-Origin
17+
Vary: Referer
18+
X-Content-Type-Options: nosniff
19+
X-Frame-Options: SAMEORIGIN
20+
X-Xss-Protection: 0
21+
22+
{
23+
"createTime": "2024-04-01T12:34:56.123456Z",
24+
"expireTime": "2024-04-01T12:34:56.123456Z",
25+
"importMethod": "RSA_OAEP_3072_SHA1_AES_256",
26+
"name": "projects/${projectId}/locations/us-central1/keyRings/test-keyring-${uniqueId}/importJobs/test-${uniqueId}",
27+
"protectionLevel": "HSM",
28+
"state": "PENDING_GENERATION"
29+
}
30+
31+
---
32+
33+
GET https://cloudkms.googleapis.com/v1/projects/${projectId}/locations/us-central1/keyRings/test-keyring-${uniqueId}/importJobs/test-${uniqueId}?alt=json
34+
Accept: application/json
35+
Authorization: (removed)
36+
Connection: keep-alive
37+
38+
200 OK
39+
Content-Type: application/json; charset=UTF-8
40+
Server: ESF
41+
Vary: Origin
42+
Vary: X-Origin
43+
Vary: Referer
44+
X-Content-Type-Options: nosniff
45+
X-Frame-Options: SAMEORIGIN
46+
X-Xss-Protection: 0
47+
48+
{
49+
"attestation": {
50+
"certChains": {
51+
"caviumCerts": [
52+
"-----BEGIN CERTIFICATE-----\ncertificate 1\n-----END CERTIFICATE-----\n",
53+
"-----BEGIN CERTIFICATE-----\ncertificate 2\n-----END CERTIFICATE-----\n"
54+
],
55+
"googleCardCerts": [
56+
"-----BEGIN CERTIFICATE-----\ncertificate 3\n-----END CERTIFICATE-----\n"
57+
],
58+
"googlePartitionCerts": [
59+
"-----BEGIN CERTIFICATE-----\ncertificate 4\n-----END CERTIFICATE-----\n"
60+
]
61+
},
62+
"content": "Y29udGVudA==",
63+
"format": "CAVIUM_V2_COMPRESSED"
64+
},
65+
"createTime": "2024-04-01T12:34:56.123456Z",
66+
"expireTime": "2024-04-01T12:34:56.123456Z",
67+
"generateTime": "2024-04-01T12:34:56.123456Z",
68+
"importMethod": "RSA_OAEP_3072_SHA1_AES_256",
69+
"name": "projects/${projectId}/locations/us-central1/keyRings/test-keyring-${uniqueId}/importJobs/test-${uniqueId}",
70+
"protectionLevel": "HSM",
71+
"publicKey": {
72+
"pem": "-----BEGIN PUBLIC KEY-----\npublic key\n-----END PUBLIC KEY-----\n"
73+
},
74+
"state": "ACTIVE"
75+
}
76+
77+
---
78+
79+
GET https://cloudkms.googleapis.com/v1/projects/${projectId}/locations/us-central1/keyRings/test-keyring-${uniqueId}/importJobs?alt=json&pageSize=100
80+
Accept: application/json
81+
Authorization: (removed)
82+
Connection: keep-alive
83+
84+
200 OK
85+
Content-Type: application/json; charset=UTF-8
86+
Server: ESF
87+
Vary: Origin
88+
Vary: X-Origin
89+
Vary: Referer
90+
X-Content-Type-Options: nosniff
91+
X-Frame-Options: SAMEORIGIN
92+
X-Xss-Protection: 0
93+
94+
{
95+
"importJobs": [
96+
{
97+
"attestation": {
98+
"certChains": {
99+
"caviumCerts": [
100+
"-----BEGIN CERTIFICATE-----\ncertificate 1\n-----END CERTIFICATE-----\n",
101+
"-----BEGIN CERTIFICATE-----\ncertificate 2\n-----END CERTIFICATE-----\n"
102+
],
103+
"googleCardCerts": [
104+
"-----BEGIN CERTIFICATE-----\ncertificate 3\n-----END CERTIFICATE-----\n"
105+
],
106+
"googlePartitionCerts": [
107+
"-----BEGIN CERTIFICATE-----\ncertificate 4\n-----END CERTIFICATE-----\n"
108+
]
109+
},
110+
"content": "Y29udGVudA==",
111+
"format": "CAVIUM_V2_COMPRESSED"
112+
},
113+
"createTime": "2024-04-01T12:34:56.123456Z",
114+
"expireTime": "2024-04-01T12:34:56.123456Z",
115+
"generateTime": "2024-04-01T12:34:56.123456Z",
116+
"importMethod": "RSA_OAEP_3072_SHA1_AES_256",
117+
"name": "projects/${projectId}/locations/us-central1/keyRings/test-keyring-${uniqueId}/importJobs/test-${uniqueId}",
118+
"protectionLevel": "HSM",
119+
"publicKey": {
120+
"pem": "-----BEGIN PUBLIC KEY-----\npublic key\n-----END PUBLIC KEY-----\n"
121+
},
122+
"state": "ACTIVE"
123+
}
124+
],
125+
"totalSize": 1
126+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- pre: gcloud kms keyrings create test-keyring-${uniqueId} --location=us-central1
2+
- exec: gcloud kms import-jobs create test-${uniqueId} --location=us-central1 --keyring=test-keyring-${uniqueId} --import-method=rsa-oaep-3072-sha1-aes-256 --protection-level=hsm
3+
- exec: gcloud kms import-jobs describe test-${uniqueId} --location=us-central1 --keyring=test-keyring-${uniqueId}
4+
- exec: gcloud kms import-jobs list --location=us-central1 --keyring=test-keyring-${uniqueId}

tests/e2e/normalize.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,13 +963,21 @@ func normalizeHTTPResponses(t *testing.T, normalizer mockgcpregistry.Normalizer,
963963

964964
// AI Platform
965965
{
966-
visitor.ReplacePath(".createTime", "2024-01-01T12:34:56.123456Z")
967-
visitor.ReplacePath(".updateTime", "2024-01-02T12:34:56.123456Z")
966+
visitor.ReplacePath(".updateTime", "2024-04-01T12:34:56.123456Z")
968967
visitor.ReplacePath(".nextRunTime", "2024-04-01T12:34:56.123456Z")
969968
visitor.ReplacePath(".expirationTime", "2024-09-01T12:34:56.123456Z")
970-
visitor.ReplacePath(".schedules[].createTime", "2024-01-01T12:34:56.123456Z")
969+
visitor.ReplacePath(".schedules[].createTime", "2024-04-01T12:34:56.123456Z")
971970
visitor.ReplacePath(".schedules[].nextRunTime", "2024-04-01T12:34:56.123456Z")
972-
visitor.ReplacePath(".schedules[].startTime", "2024-01-03T12:34:56.123456Z")
971+
visitor.ReplacePath(".schedules[].startTime", "2024-04-01T12:34:56.123456Z")
972+
}
973+
974+
// KMS
975+
{
976+
visitor.ReplacePath(".expireTime", "2024-04-01T12:34:56.123456Z")
977+
visitor.ReplacePath(".generateTime", "2024-04-01T12:34:56.123456Z")
978+
visitor.ReplacePath(".importJobs[].createTime", "2024-04-01T12:34:56.123456Z")
979+
visitor.ReplacePath(".importJobs[].expireTime", "2024-04-01T12:34:56.123456Z")
980+
visitor.ReplacePath(".importJobs[].generateTime", "2024-04-01T12:34:56.123456Z")
973981
}
974982

975983
// Network Management

0 commit comments

Comments
 (0)