Skip to content

Commit

Permalink
Merge pull request #1 from yuwenma/keyhandle
Browse files Browse the repository at this point in the history
minor nits
  • Loading branch information
nb-goog authored Nov 2, 2024
2 parents e3bd7d7 + 2ec129f commit c30c21e
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 47 deletions.
2 changes: 1 addition & 1 deletion apis/kms/v1alpha1/keyhandle_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func NewKMSKeyHandleRef(ctx context.Context, reader client.Reader, obj *KMSKeyHa
return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, location)
}
if desiredHandleId != "" && (actualHandleId != desiredHandleId) {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
return nil, fmt.Errorf("cannot reset `spec.resourceID` to %s, since it has already assigned to %s",
desiredHandleId, actualHandleId)
}
id.External = externalRef
Expand Down
File renamed without changes.
22 changes: 0 additions & 22 deletions mockgcp/mockkms/keyhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"google.golang.org/protobuf/proto"

pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/kms/v1"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
)

type autokeyServer struct {
Expand Down Expand Up @@ -81,27 +80,6 @@ func (r *autokeyServer) CreateKeyHandle(ctx context.Context, req *pb.CreateKeyHa
})
}

func (r *autokeyServer) ListKeyHandles(ctx context.Context, req *pb.ListKeyHandlesRequest) (*pb.ListKeyHandlesResponse, error) {
parentName, err := r.parseParentName(req.GetParent())
if err != nil {
return nil, err
}
namePrefix := parentName.String() + "/keyHandles/"

response := &pb.ListKeyHandlesResponse{}
keyHandleKind := (&pb.KeyHandle{}).ProtoReflect().Descriptor()
if err := r.storage.List(ctx, keyHandleKind, storage.ListOptions{}, func(obj proto.Message) error {
keyHandle := obj.(*pb.KeyHandle)
if strings.HasPrefix(keyHandle.GetName(), namePrefix) {
response.KeyHandles = append(response.KeyHandles, keyHandle)
}
return nil
}); err != nil {
return nil, err
}
return response, nil
}

type parentName struct {
projectID string
location string
Expand Down
6 changes: 2 additions & 4 deletions pkg/controller/direct/kms/keyhandle/keyhandle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package keyhandle
import (
"context"
"fmt"
"strings"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/kms/v1alpha1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
Expand Down Expand Up @@ -126,7 +125,7 @@ func (a *Adapter) Find(ctx context.Context) (bool, error) {

func (a *Adapter) Create(ctx context.Context, createOp *directbase.CreateOperation) error {
log := klog.FromContext(ctx).WithName(ctrlName)
log.V(2).Info("creating KeyHandle", "name", a.id.External)
log.V(2).Info("creating KeyHandle")
mapCtx := &direct.MapContext{}

desired := a.desired.DeepCopy()
Expand Down Expand Up @@ -158,8 +157,7 @@ func (a *Adapter) Create(ctx context.Context, createOp *directbase.CreateOperati
if mapCtx.Err() != nil {
return mapCtx.Err()
}
tokens := strings.Split(created.Name, "/")
externalRef := parent.String() + "/keyHandles/" + tokens[5]
externalRef := created.Name
status.ExternalRef = &externalRef
return createOp.UpdateStatus(ctx, status, nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ metadata:
spec:
location: us-central1
projectRef:
external: projects/autokey-resource
external: projects/${uniqueId}
resourceTypeSelector: compute.googleapis.com/Disk
status:
conditions:
Expand All @@ -21,7 +21,6 @@ status:
reason: UpToDate
status: "True"
type: Ready
externalRef: projects/autokey-resource/locations/us-central1/keyHandles/22542ecb-fd0a-4d25-a2bf-caf8834cfba9
externalRef: projects/${uniqueId}/locations/us-central1/keyHandles/5fe9854c-4a75-4ec9-8c27-c235754b981d
observedGeneration: 1
observedState:
kmsKey: projects/${projectId}/locations/us-central1/keyRings/autokey/cryptoKeys/190012159962-compute-disk-78d515aaefe53b68
observedState: {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
POST https://cloudkms.googleapis.com/v1/projects/autokey-resource/locations/us-central1/keyHandles?%24alt=json%3Benum-encoding%3Dint
POST https://cloudkms.googleapis.com/v1/projects/${uniqueId}/locations/us-central1/keyHandles?%24alt=json%3Benum-encoding%3Dint
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: parent=projects%2Fautokey-resource%2Flocations%2Fus-central1
x-goog-request-params: parent=projects%2F${uniqueId}%2Flocations%2Fus-central1

{
"resourceTypeSelector": "compute.googleapis.com/Disk"
Expand All @@ -19,18 +19,15 @@ X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"metadata": {
"@type": "type.googleapis.com/google.cloud.kms.v1.CreateKeyHandleMetadata"
},
"name": "projects/autokey-resource/locations/us-central1/operations/${operationID}"
"name": "projects/${uniqueId}/locations/us-central1/operations/${operationID}"
}

---

GET https://cloudkms.googleapis.com/v1/projects/autokey-resource/locations/us-central1/operations/${operationID}
GET https://cloudkms.googleapis.com/v1/projects/${uniqueId}/locations/us-central1/operations/${operationID}
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: name=projects%2Fautokey-resource%2Flocations%2Fus-central1%2Foperations%2F${operationID}
x-goog-request-params: name=projects%2F${uniqueId}%2Flocations%2Fus-central1%2Foperations%2F${operationID}

200 OK
Cache-Control: private
Expand All @@ -45,21 +42,20 @@ X-Xss-Protection: 0

{
"done": true,
"name": "projects/autokey-resource/locations/us-central1/operations/${operationID}",
"name": "projects/${uniqueId}/locations/us-central1/operations/${operationID}",
"response": {
"@type": "type.googleapis.com/google.cloud.kms.v1.KeyHandle",
"kmsKey": "projects/${projectId}/locations/us-central1/keyRings/autokey/cryptoKeys/190012159962-compute-disk-78d515aaefe53b68",
"name": "projects/autokey-resource/locations/us-central1/keyHandles/22542ecb-fd0a-4d25-a2bf-caf8834cfba9",
"name": "projects/${uniqueId}/locations/us-central1/keyHandles/5fe9854c-4a75-4ec9-8c27-c235754b981d",
"resourceTypeSelector": "compute.googleapis.com/Disk"
}
}

---

GET https://cloudkms.googleapis.com/v1/projects/autokey-resource/locations/us-central1/keyHandles/22542ecb-fd0a-4d25-a2bf-caf8834cfba9?%24alt=json%3Benum-encoding%3Dint
GET https://cloudkms.googleapis.com/v1/projects/${uniqueId}/locations/us-central1/keyHandles/5fe9854c-4a75-4ec9-8c27-c235754b981d?%24alt=json%3Benum-encoding%3Dint
Content-Type: application/json
User-Agent: kcc/controller-manager
x-goog-request-params: name=projects%2Fautokey-resource%2Flocations%2Fus-central1%2FkeyHandles%2F22542ecb-fd0a-4d25-a2bf-caf8834cfba9
x-goog-request-params: name=projects%2F${uniqueId}%2Flocations%2Fus-central1%2FkeyHandles%2F5fe9854c-4a75-4ec9-8c27-c235754b981d

200 OK
Cache-Control: private
Expand All @@ -73,7 +69,6 @@ X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

{
"kmsKey": "projects/${projectId}/locations/us-central1/keyRings/autokey/cryptoKeys/190012159962-compute-disk-78d515aaefe53b68",
"name": "projects/autokey-resource/locations/us-central1/keyHandles/22542ecb-fd0a-4d25-a2bf-caf8834cfba9",
"name": "projects/${uniqueId}/locations/us-central1/keyHandles/5fe9854c-4a75-4ec9-8c27-c235754b981d",
"resourceTypeSelector": "compute.googleapis.com/Disk"
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ kind: KMSKeyHandle
metadata:
name: keyhandle-${uniqueId}
spec:
resourceID: 5fe9854c-4a75-4ec9-8c27-c235754b981d
projectRef:
external: projects/${uniqueId}
location: us-central1
Expand Down

0 comments on commit c30c21e

Please sign in to comment.