Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockgcp test for autokey config #2755

Closed
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8674475
Mockgcp test for autokey config
nb-goog Sep 19, 2024
30a7dc6
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Sep 23, 2024
30f2011
formatting changes
nb-goog Sep 23, 2024
0813d4f
Merge branch 'master' into mockgcp-autokeyconfig
nb-goog Sep 24, 2024
af8fc2d
Adding create and update.yaml for autokeyconfig
nb-goog Sep 24, 2024
e58b7f8
correcting Makefile for autokey config
nb-goog Sep 24, 2024
1ec8717
Merge branch 'master' into mockgcp-autokeyconfig
nb-goog Sep 26, 2024
03a2eb5
Controller implementation for autokey
nb-goog Sep 30, 2024
a11ce9f
Merge branch 'master' into mockgcp-autokeyconfig
nb-goog Sep 30, 2024
3da0a06
Deleting accidental changes
nb-goog Sep 30, 2024
f006006
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Oct 9, 2024
17e87ec
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Oct 11, 2024
1d5401c
Upgraded kms api to v1.20
nb-goog Oct 10, 2024
d3b5993
Record Gcp logs
nb-goog Oct 11, 2024
b7667bd
updating test
nb-goog Oct 11, 2024
adc77ae
Skip delete operation for autokeyConfig
nb-goog Oct 11, 2024
8e153a5
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Oct 14, 2024
e58e706
formating changes
nb-goog Oct 14, 2024
880b38a
correcting service.go for backward compatible with keyring and crypto…
nb-goog Oct 14, 2024
64d3d3e
Resetting gomod and gosum
nb-goog Oct 14, 2024
648f421
Make ready-pr
nb-goog Oct 14, 2024
e03f079
deleting accidentally commited files
nb-goog Oct 14, 2024
085cfc0
Resolving review comments
nb-goog Oct 15, 2024
05ccb4f
Make pr ready
nb-goog Oct 15, 2024
2bdd6ca
Addressing review comments-2
nb-goog Oct 17, 2024
5270ba8
using different project_id in update.yaml
nb-goog Oct 21, 2024
5eb5409
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Oct 22, 2024
7c6f9d0
Addressing review comments - 3
nb-goog Oct 22, 2024
7db151f
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Oct 23, 2024
c86273e
Addressing review comments - 4
nb-goog Oct 23, 2024
a94873d
Addressing review comment - 5
nb-goog Oct 23, 2024
e239d83
Merge branch 'GoogleCloudPlatform:master' into mockgcp-autokeyconfig
nb-goog Oct 23, 2024
6d42107
make pr ready
nb-goog Oct 23, 2024
51299c3
tests: add e2e for compute resources
justinsb Oct 19, 2024
e375c7d
tests: add cloudfunctions to e2e
justinsb Oct 21, 2024
5b48ffe
tests: add gke to e2e
justinsb Oct 21, 2024
0327805
build:add deploy commands
xiaoweim Oct 23, 2024
82b219b
tests: write goldens
acpana Oct 23, 2024
bb6f4cc
tests: add cloudidentity to e2e
justinsb Oct 21, 2024
09b183f
refactor: Move SQLInstance defaulting to be part of mapper
jasonvigil Oct 17, 2024
e870255
fix: Add missing SQLInstance defaults
jasonvigil Oct 17, 2024
f2fb87d
Add field mapper for attached clusters direct controller migration.
hankfreund Oct 21, 2024
f56daa7
dev:tools: add export changes
acpana Oct 17, 2024
53abae2
tests: log wrapped err
acpana Oct 23, 2024
12ce8b5
Merge branch 'master' into mockgcp-autokeyconfig
nb-goog Oct 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions apis/kms/v1alpha1/autokeyconfig_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"context"
"fmt"
"strings"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ refsv1beta1.ExternalNormalizer = &KMSAutokeyConfigRef{}

// KMSAutokeyConfigRef defines the resource reference to KMSAutokeyConfig, which "External" field
// holds the GCP identifier for the KRM object.
type KMSAutokeyConfigRef struct {
// A reference to an externally managed KMSAutokeyConfig resource.
// Should be in the format "projects/<projectID>/locations/<location>/autokeyconfigs/<autokeyconfigID>".
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
External string `json:"external,omitempty"`

// The name of a KMSAutokeyConfig resource.
Name string `json:"name,omitempty"`

// The namespace of a KMSAutokeyConfig resource.
Namespace string `json:"namespace,omitempty"`

parent *KMSAutokeyConfigParent
}

// NormalizedExternal provision the "External" value for other resource that depends on KMSAutokeyConfig.
// If the "External" is given in the other resource's spec.KMSAutokeyConfigRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual KMSAutokeyConfig object from the cluster.
func (r *KMSAutokeyConfigRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) {
if r.External != "" && r.Name != "" {
return "", fmt.Errorf("cannot specify both name and external on %s reference", KMSAutokeyConfigGVK.Kind)
}
// From given External
if r.External != "" {
if _, err := ParseKMSAutokeyConfigExternal(r.External); err != nil {
return "", err
}
return r.External, nil
}

// From the Config Connector object
if r.Namespace == "" {
r.Namespace = otherNamespace
}
key := types.NamespacedName{Name: r.Name, Namespace: r.Namespace}
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(KMSAutokeyConfigGVK)
if err := reader.Get(ctx, key, u); err != nil {
if apierrors.IsNotFound(err) {
return "", k8s.NewReferenceNotFoundError(u.GroupVersionKind(), key)
}
return "", fmt.Errorf("reading referenced %s %s: %w", KMSAutokeyConfigGVK, key, err)
}
// Get external from status.externalRef. This is the most trustworthy place.
actualExternalRef, _, err := unstructured.NestedString(u.Object, "status", "externalRef")
if err != nil {
return "", fmt.Errorf("reading status.externalRef: %w", err)
}
if actualExternalRef == "" {
return "", k8s.NewReferenceNotReadyError(u.GroupVersionKind(), key)
}
r.External = actualExternalRef
return r.External, nil
}

// New builds a KMSAutokeyConfigRef from the Config Connector KMSAutokeyConfig object.
func NewKMSAutokeyConfigRef(ctx context.Context, reader client.Reader, obj *KMSAutokeyConfig) (*KMSAutokeyConfigRef, error) {
id := &KMSAutokeyConfigRef{}

// Get Parent
folderRef, err := refsv1beta1.ResolveFolder(ctx, reader, obj, obj.Spec.FolderRef)
if err != nil {
return nil, err
}
folderID := folderRef.FolderID
if folderID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
id.parent = &KMSAutokeyConfigParent{FolderID: folderID}

// Use approved External
externalRef := valueOf(obj.Status.ExternalRef)
if externalRef == "" {
id.External = asKMSAutokeyConfigExternal(id.parent)
return id, nil
}

// Validate desired with actual
actualParent, err := ParseKMSAutokeyConfigExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.FolderID != folderID {
return nil, fmt.Errorf("spec.folderRef changed, expect %s, got %s", actualParent.FolderID, folderID)
}
id.External = externalRef
id.parent = &KMSAutokeyConfigParent{FolderID: folderID}
return id, nil
}

func (r *KMSAutokeyConfigRef) Parent() (*KMSAutokeyConfigParent, error) {
if r.parent != nil {
return r.parent, nil
}
if r.External != "" {
parent, err := ParseKMSAutokeyConfigExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("KMSAutokeyConfigRef not initialized from `NewKMSAutokeyConfigRef` or `NormalizedExternal`")
}

type KMSAutokeyConfigParent struct {
FolderID string
Location string
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
}

func (p *KMSAutokeyConfigParent) String() string {
return "folders/" + p.FolderID
}

func asKMSAutokeyConfigExternal(parent *KMSAutokeyConfigParent) (external string) {
return parent.String() + "/autokeyConfig"
}

func ParseKMSAutokeyConfigExternal(external string) (parent *KMSAutokeyConfigParent, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 3 || tokens[0] != "folders" || tokens[2] != "autokeyConfig" {
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
return nil, fmt.Errorf("format of KMSAutokeyConfig external=%q was not known (use projects/<projectId>/locations/<location>/autokeyconfigs/<autokeyconfigID>)", external)
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
}
parent = &KMSAutokeyConfigParent{
FolderID: tokens[1],
}
return parent, nil
}

func valueOf[T any](t *T) T {
var zeroVal T
if t == nil {
return zeroVal
}
return *t
}
16 changes: 16 additions & 0 deletions apis/kms/v1alpha1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +kcc:proto=google.cloud.kms.v1
package v1alpha1
33 changes: 33 additions & 0 deletions apis/kms/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +kubebuilder:object:generate=true
// +groupName=kms.cnrm.cloud.google.com
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "kms.cnrm.cloud.google.com", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
98 changes: 98 additions & 0 deletions apis/kms/v1alpha1/kmsautokeyconfig_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/k8s/v1alpha1"

refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var KMSAutokeyConfigGVK = GroupVersion.WithKind("KMSAutokeyConfig")

// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// KMSAutokeyConfigSpec defines the desired state of KMSAutokeyConfig
// +kcc:proto=google.cloud.kms.v1.AutokeyConfig
type KMSAutokeyConfigSpec struct {

// NOTE: ResourceID field is not required for AutokeyConfig as its ID has the format folders/<folderID>/autokeyConfig i.e., it doesnt have any unique ID of its own and relies on folderID for uniqueness.

// Immutable. The folder that this resource belongs to.
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
FolderRef *refs.FolderRef `json:"folderRef"`

// +optional
KeyProject *refs.ProjectRef `json:"keyProject,omitempty"`
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
}

// KMSAutokeyConfigStatus defines the config connector machine state of KMSAutokeyConfig
type KMSAutokeyConfigStatus struct {
/* Conditions represent the latest available observations of the
object's current state. */
Conditions []v1alpha1.Condition `json:"conditions,omitempty"`

// ObservedGeneration is the generation of the resource that was most recently observed by the Config Connector controller. If this is equal to metadata.generation, then that means that the current reported status reflects the most recent desired state of the resource.
ObservedGeneration *int64 `json:"observedGeneration,omitempty"`
//KeyProject *refs.ProjectRef `json:"projectRef,omitempty"`
//State *string `json:"state,omitempty"`
nb-goog marked this conversation as resolved.
Show resolved Hide resolved

// A unique specifier for the KMSAutokeyConfig resource in GCP.
ExternalRef *string `json:"externalRef,omitempty"`

// ObservedState is the state of the resource as most recently observed in GCP.
ObservedState *KMSAutokeyConfigObservedState `json:"observedState,omitempty"`
}

// KMSAutokeyConfigSpec defines the desired state of KMSAutokeyConfig
// +kcc:proto=google.cloud.kms.v1.AutokeyConfig
type KMSAutokeyConfigObservedState struct {
// Output only. Current state of this AutokeyConfig.
// +optional
State *string `json:"state,omitempty"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
nb-goog marked this conversation as resolved.
Show resolved Hide resolved
// +kubebuilder:subresource:status
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/system=true"
// +kubebuilder:printcolumn:name="Age",JSONPath=".metadata.creationTimestamp",type="date"
// +kubebuilder:printcolumn:name="Ready",JSONPath=".status.conditions[?(@.type=='Ready')].status",type="string",description="When 'True', the most recent reconcile of the resource succeeded"
// +kubebuilder:printcolumn:name="Status",JSONPath=".status.conditions[?(@.type=='Ready')].reason",type="string",description="The reason for the value in 'Ready'"
// +kubebuilder:printcolumn:name="Status Age",JSONPath=".status.conditions[?(@.type=='Ready')].lastTransitionTime",type="date",description="The last transition time for the value in 'Status'"

// KMSAutokeyConfig is the Schema for the KMSAutokeyConfig API
// +k8s:openapi-gen=true
type KMSAutokeyConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec KMSAutokeyConfigSpec `json:"spec,omitempty"`
Status KMSAutokeyConfigStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// KMSAutokeyConfigList contains a list of KMSAutokeyConfig
type KMSAutokeyConfigList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []KMSAutokeyConfig `json:"items"`
}

func init() {
SchemeBuilder.Register(&KMSAutokeyConfig{}, &KMSAutokeyConfigList{})
}
42 changes: 42 additions & 0 deletions apis/kms/v1alpha1/types.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading