14
14
15
15
// +tool:mockgcp-support
16
16
// proto.service: google.cloud.kms.v1.KeyManagementService
17
- // proto.message: google.cloud.kms.v1.KeyRing
17
+ // proto.message: google.cloud.kms.v1.ImportJob
18
18
19
19
package mockkms
20
20
@@ -29,32 +29,52 @@ import (
29
29
"google.golang.org/protobuf/proto"
30
30
"google.golang.org/protobuf/types/known/timestamppb"
31
31
32
- "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
33
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
34
)
35
35
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 )
38
38
if err != nil {
39
39
return nil , err
40
40
}
41
41
42
42
fqn := name .String ()
43
43
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 {
46
46
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 )
48
48
}
49
49
return nil , err
50
50
}
51
51
52
52
return obj , nil
53
53
}
54
54
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 )
58
78
if err != nil {
59
79
return nil , err
60
80
}
@@ -63,53 +83,71 @@ func (r *kmsServer) CreateKeyRing(ctx context.Context, req *pb.CreateKeyRingRequ
63
83
64
84
now := time .Now ()
65
85
66
- obj := proto .Clone (req .GetKeyRing ()).(* pb.KeyRing )
86
+ obj := proto .Clone (req .GetImportJob ()).(* pb.ImportJob )
67
87
obj .Name = fqn
68
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-----\n certificate 1\n -----END CERTIFICATE-----\n " ,
101
+ "-----BEGIN CERTIFICATE-----\n certificate 2\n -----END CERTIFICATE-----\n " ,
102
+ },
103
+ GoogleCardCerts : []string {
104
+ "-----BEGIN CERTIFICATE-----\n certificate 3\n -----END CERTIFICATE-----\n " ,
105
+ },
106
+ GooglePartitionCerts : []string {
107
+ "-----BEGIN CERTIFICATE-----\n certificate 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-----\n public key\n -----END PUBLIC KEY-----\n " ,
115
+ }
69
116
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 {
73
118
return nil , err
74
119
}
75
120
76
- return obj , nil
121
+ return result , nil
77
122
}
78
123
79
- func (r * kmsServer ) populateDefaultsForKeyRing (name * KeyRingName , obj * pb.KeyRing ) {
80
-
124
+ type importJobName struct {
125
+ KeyRingName
126
+ ImportJobID string
81
127
}
82
128
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
87
131
}
88
132
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 ) {
96
136
tokens := strings .Split (name , "/" )
97
137
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 ], "/" ) )
100
140
if err != nil {
101
141
return nil , err
102
142
}
103
143
104
- name := & KeyRingName {
105
- Project : project ,
106
- Location : tokens [3 ],
107
- KeyRingID : tokens [5 ],
144
+ name := & importJobName {
145
+ KeyRingName : * keyRingName ,
146
+ ImportJobID : tokens [7 ],
108
147
}
109
148
110
149
return name , nil
111
150
}
112
151
113
152
return nil , status .Errorf (codes .InvalidArgument , "name %q is not valid" , name )
114
153
}
115
-
0 commit comments