Skip to content

Commit 502ffb1

Browse files
committed
Fix mockgcp for KMSImportJob
1 parent 93627a6 commit 502ffb1

File tree

10 files changed

+128
-90
lines changed

10 files changed

+128
-90
lines changed

mockgcp/mock_http_roundtrip.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ import (
8080
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgcpregistry"
8181
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgkehub"
8282
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockgkemulticloud"
83-
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockkms"
8483
_ "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockiam"
8584
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockkms"
86-
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockkms"
8785
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mocklogging"
8886
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmanagedkafka"
8987
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/mockmonitoring"
@@ -264,6 +262,7 @@ func NewMockRoundTripper(ctx context.Context, k8sClient client.Client, storage s
264262
services = append(services, mockdataplex.New(env, storage))
265263
services = append(services, mockclouddms.New(env, storage))
266264
services = append(services, mockvmwareengine.New(env, storage))
265+
services = append(services, mockkms.New(env, storage))
267266

268267
for _, service := range services {
269268
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: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// +tool:mockgcp-support
1616
// proto.service: google.cloud.kms.v1.KeyManagementService
17-
// proto.message: google.cloud.kms.v1.KeyRing
17+
// proto.message: google.cloud.kms.v1.ImportJob
1818

1919
package mockkms
2020

@@ -29,32 +29,52 @@ import (
2929
"google.golang.org/protobuf/proto"
3030
"google.golang.org/protobuf/types/known/timestamppb"
3131

32-
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
3332
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/kms/v1"
33+
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
3434
)
3535

36-
func (r *kmsServer) GetKeyRing(ctx context.Context, req *pb.GetKeyRingRequest) (*pb.KeyRing, error) {
37-
name, err := r.parseKeyRingName(req.Name)
36+
func (s *kmsServer) GetImportJob(ctx context.Context, req *pb.GetImportJobRequest) (*pb.ImportJob, error) {
37+
name, err := s.parseImportJobName(req.Name)
3838
if err != nil {
3939
return nil, err
4040
}
4141

4242
fqn := name.String()
4343

44-
obj := &pb.KeyRing{}
45-
if err := r.storage.Get(ctx, fqn, obj); err != nil {
44+
obj := &pb.ImportJob{}
45+
if err := s.storage.Get(ctx, fqn, obj); err != nil {
4646
if status.Code(err) == codes.NotFound {
47-
return nil, status.Errorf(codes.NotFound, "KeyRing %s not found.", fqn)
47+
return nil, status.Errorf(codes.NotFound, "ImportJob %s not found.", fqn)
4848
}
4949
return nil, err
5050
}
5151

5252
return obj, nil
5353
}
5454

55-
func (r *kmsServer) CreateKeyRing(ctx context.Context, req *pb.CreateKeyRingRequest) (*pb.KeyRing, error) {
56-
reqName := fmt.Sprintf("%s/keyRings/%s", req.GetParent(), req.GetKeyRingId())
57-
name, err := r.parseKeyRingName(reqName)
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)
5878
if err != nil {
5979
return nil, err
6080
}
@@ -63,53 +83,71 @@ func (r *kmsServer) CreateKeyRing(ctx context.Context, req *pb.CreateKeyRingRequ
6383

6484
now := time.Now()
6585

66-
obj := proto.Clone(req.GetKeyRing()).(*pb.KeyRing)
86+
obj := proto.Clone(req.GetImportJob()).(*pb.ImportJob)
6787
obj.Name = fqn
6888
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+
}
69116

70-
r.populateDefaultsForKeyRing(name, obj)
71-
72-
if err := r.storage.Create(ctx, fqn, obj); err != nil {
117+
if err := s.storage.Create(ctx, fqn, obj); err != nil {
73118
return nil, err
74119
}
75120

76-
return obj, nil
121+
return result, nil
77122
}
78123

79-
func (r *kmsServer) populateDefaultsForKeyRing(name *KeyRingName, obj *pb.KeyRing) {
80-
124+
type importJobName struct {
125+
KeyRingName
126+
ImportJobID string
81127
}
82128

83-
type KeyRingName struct {
84-
Project *projects.ProjectData
85-
Location string
86-
KeyRingID string
129+
func (n *importJobName) String() string {
130+
return n.KeyRingName.String() + "/importJobs/" + n.ImportJobID
87131
}
88132

89-
func (n *KeyRingName) String() string {
90-
return "projects/" + n.Project.ID + "/locations/" + n.Location + "/keyRings/" + n.KeyRingID
91-
}
92-
93-
// parseKeyRingName parses a string into an KeyRingName.
94-
// The expected form is `projects/*/locations/*/keyRings/*`.
95-
func (r *kmsServer) parseKeyRingName(name string) (*KeyRingName, error) {
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) {
96136
tokens := strings.Split(name, "/")
97137

98-
if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "locations" && tokens[4] == "keyRings" {
99-
project, err := r.Projects.GetProjectByID(tokens[1])
138+
if len(tokens) == 8 && tokens[6] == "importJobs" {
139+
keyRingName, err := s.parseKeyRingName(strings.Join(tokens[0:6], "/"))
100140
if err != nil {
101141
return nil, err
102142
}
103143

104-
name := &KeyRingName{
105-
Project: project,
106-
Location: tokens[3],
107-
KeyRingID: tokens[5],
144+
name := &importJobName{
145+
KeyRingName: *keyRingName,
146+
ImportJobID: tokens[7],
108147
}
109148

110149
return name, nil
111150
}
112151

113152
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
114153
}
115-

mockgcp/mockkms/service.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 Google LLC
1+
// Copyright 2024 Google LLC
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,39 +12,29 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package mockcloudkms
16-
17-
// +tool:mockgcp-service
18-
// http.host: cloudkms.googleapis.com
19-
// proto.service: google.cloud.kms.v1.KeyManagementService
15+
package mockkms
2016

2117
import (
2218
"context"
2319
"net/http"
20+
"strings"
2421

2522
"google.golang.org/grpc"
2623

2724
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common"
2825
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/httpmux"
2926
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/operations"
3027
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/kms/v1"
31-
3228
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
3329
)
3430

3531
// MockService represents a mocked kms service.
3632
type MockService struct {
3733
*common.MockEnvironment
38-
storage storage.Storage
39-
40-
operations *operations.Operations
41-
42-
v1 *KMSV1
43-
}
44-
45-
type KMSV1 struct {
46-
*MockService
47-
pb.UnimplementedKeyManagementServiceServer
34+
storage storage.Storage
35+
operations *operations.Operations
36+
v1AutokeyAdminServer *autokeyAdminServer
37+
v1AutokeyServer *autokeyServer
4838
}
4939

5040
// New creates a MockService.
@@ -54,7 +44,8 @@ func New(env *common.MockEnvironment, storage storage.Storage) *MockService {
5444
storage: storage,
5545
operations: operations.NewOperationsService(storage),
5646
}
57-
s.v1 = &KMSV1{MockService: s}
47+
s.v1AutokeyAdminServer = &autokeyAdminServer{MockService: s}
48+
s.v1AutokeyServer = &autokeyServer{MockService: s}
5849
return s
5950
}
6051

@@ -63,19 +54,27 @@ func (s *MockService) ExpectedHosts() []string {
6354
}
6455

6556
func (s *MockService) Register(grpcServer *grpc.Server) {
66-
pb.RegisterKeyManagementServiceServer(grpcServer, s.v1)
57+
pb.RegisterKeyManagementServiceServer(grpcServer, &kmsServer{MockService: s})
58+
pb.RegisterAutokeyAdminServer(grpcServer, s.v1AutokeyAdminServer)
59+
pb.RegisterAutokeyServer(grpcServer, s.v1AutokeyServer)
6760
}
6861

6962
func (s *MockService) NewHTTPMux(ctx context.Context, conn *grpc.ClientConn) (http.Handler, error) {
7063
mux, err := httpmux.NewServeMux(ctx, conn, httpmux.Options{},
7164
pb.RegisterKeyManagementServiceHandler,
72-
s.operations.RegisterOperationsPath("/v1/{prefix=**}/operations/{name}"))
73-
65+
pb.RegisterAutokeyAdminHandler,
66+
pb.RegisterAutokeyHandler,
67+
s.operations.RegisterOperationsPath("/v1/{prefix=**}/operations/{name}"),
68+
)
7469
if err != nil {
7570
return nil, err
7671
}
7772

73+
// Returns slightly non-standard errors
74+
mux.RewriteError = func(ctx context.Context, error *httpmux.ErrorResponse) {
75+
if error.Code == 404 && (strings.Contains(error.Message, "KeyRing") || strings.Contains(error.Message, "CryptoKey")) {
76+
error.Errors = nil
77+
}
78+
}
7879
return mux, nil
7980
}
80-
81-

0 commit comments

Comments
 (0)