Skip to content

Commit

Permalink
Merge pull request #3371 from yuwenma/secret-manager-wip
Browse files Browse the repository at this point in the history
feat: SecretManagerSecret full test coverage & manual replication field
  • Loading branch information
google-oss-prow[bot] authored Dec 16, 2024
2 parents 2b39da1 + 792894d commit ff59285
Show file tree
Hide file tree
Showing 41 changed files with 5,082 additions and 753 deletions.
3 changes: 1 addition & 2 deletions apis/secretmanager/v1beta1/secret_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ type SecretManagerSecretSpec struct {
// This is always provided on output, regardless of what was sent on input.
ExpireTime *string `json:"expireTime,omitempty"`

// Input only. The TTL for the
// [Secret][google.cloud.secretmanager.v1.Secret].
// Input only. A duration in seconds with up to nine fractional digits, ending with 's'. Example: "3.5s".
TTL *string `json:"ttl,omitempty"`

// Optional. Rotation policy attached to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ spec:
type: object
type: array
ttl:
description: Input only. The TTL for the [Secret][google.cloud.secretmanager.v1.Secret].
description: 'Input only. A duration in seconds with up to nine fractional
digits, ending with ''s''. Example: "3.5s".'
type: string
versionAliases:
additionalProperties:
Expand Down
2 changes: 1 addition & 1 deletion dev/tasks/run-e2e
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if [[ -z "${KUBEBUILDER_ASSETS:-}" ]]; then
fi

if [[ -z "${KCC_USE_DIRECT_RECONCILERS:-}" ]]; then
KCC_USE_DIRECT_RECONCILERS=ComputeForwardingRule,GKEHubFeatureMembership,SecretManagerSecret,SecretManagerSecretVersion
KCC_USE_DIRECT_RECONCILERS=ComputeForwardingRule,GKEHubFeatureMembership
fi
echo "Using direct controllers: $KCC_USE_DIRECT_RECONCILERS"
export KCC_USE_DIRECT_RECONCILERS
Expand Down
11 changes: 11 additions & 0 deletions mockgcp/mocksecretmanager/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func (s *SecretsV1) populateDefaultsForSecret(ctx context.Context, obj *pb.Secre
return fmt.Errorf("Aliases cannot be assigned to versions that don't exist")
}
}
// TTL and ExpireTime are OneOf, but the GCP service always converts TTL to expireTime before storing the object.
if obj.GetTtl() != nil {
expirateTime := timestamppb.Now().AsTime().Add(obj.GetTtl().AsDuration())
obj.Expiration = &pb.Secret_ExpireTime{
ExpireTime: timestamppb.New(expirateTime),
}
}
return nil
}

Expand Down Expand Up @@ -154,6 +161,10 @@ func (s *SecretsV1) UpdateSecret(ctx context.Context, req *pb.UpdateSecretReques
updated.Expiration = &pb.Secret_ExpireTime{
ExpireTime: req.Secret.GetExpireTime(),
}
case "ttl":
updated.Expiration = &pb.Secret_Ttl{
Ttl: req.Secret.GetTtl(),
}
case "expiration":
updated.Expiration = req.Secret.GetExpiration()
case "rotation.nextRotationTime":
Expand Down
17 changes: 16 additions & 1 deletion pkg/controller/direct/secretmanager/secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ func normalizeExternal(ctx context.Context, reader client.Reader, src client.Obj
secret.Spec.Replication.LegacyAutomatic.CustomerManagedEncryption.KmsKeyRef = kmsKeyRef
}
}
if secret.Spec.Replication.UserManaged != nil {
for _, r := range secret.Spec.Replication.UserManaged.Replicas {
if r.CustomerManagedEncryption != nil {
kmsKeyRef := r.CustomerManagedEncryption.KmsKeyRef
kmsKeyRef, err := refs.ResolveKMSCryptoKeyRef(ctx, reader, src, kmsKeyRef)
if err != nil {
return err
}
r.CustomerManagedEncryption.KmsKeyRef = kmsKeyRef
}
}
}
}
if len(secret.Spec.TopicRefs) != 0 {
for _, topicRef := range secret.Spec.TopicRefs {
Expand Down Expand Up @@ -246,7 +258,10 @@ func (a *Adapter) Update(ctx context.Context, op *directbase.UpdateOperation) er
if err != nil {
return err
}

if paths.Has("ttl") {
paths = paths.Delete("ttl")
resource.Expiration = a.actual.Expiration
}
if len(paths) == 0 {
log.V(2).Info("no field needs update", "name", a.id)
return nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/direct/secretmanager/secret_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func SecretManagerSecretSpec_ToProto(mapCtx *direct.MapContext, in *krm.SecretMa
if oneof := direct.StringTimestamp_ToProto(mapCtx, in.ExpireTime); oneof != nil {
out.Expiration = &pb.Secret_ExpireTime{ExpireTime: oneof}
}
if oneof := direct.Duration_ToProto(mapCtx, in.TTL); oneof != nil {
out.Expiration = &pb.Secret_Ttl{Ttl: oneof}
}
// MISSING: Etag
out.Rotation = Rotation_ToProto(mapCtx, in.Rotation)
out.VersionAliases = MapStringString_ToMapStringInt64(mapCtx, in.VersionAliases)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: secretmanager.cnrm.cloud.google.com/v1beta1
kind: SecretManagerSecret
metadata:
annotations:
alpha.cnrm.cloud.google.com/reconciler: direct
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
finalizers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ kind: SecretManagerSecret
metadata:
annotations:
cnrm.cloud.google.com/project-id: ${projectId}
alpha.cnrm.cloud.google.com/reconciler: "direct"
labels:
label-one: value-one
name: secretmanagersecret-${uniqueId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: secretmanager.cnrm.cloud.google.com/v1beta1
kind: SecretManagerSecret
metadata:
annotations:
alpha.cnrm.cloud.google.com/reconciler: direct
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
finalizers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ x-goog-request-params: parent=projects%2F${projectId}
"annotations": {
"foo": "secretmanagersecret"
},
"expireTime": "2025-10-02T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down Expand Up @@ -733,7 +733,7 @@ X-Xss-Protection: 0
},
"createTime": "2024-04-01T12:34:56.123456Z",
"etag": "abcdef0123A=",
"expireTime": "2025-10-02T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down Expand Up @@ -782,7 +782,7 @@ X-Xss-Protection: 0
},
"createTime": "2024-04-01T12:34:56.123456Z",
"etag": "abcdef0123A=",
"expireTime": "2025-10-02T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down Expand Up @@ -820,7 +820,7 @@ x-goog-request-params: secret.name=projects%2F${projectId}%2Fsecrets%2Fsecretman
"foo": "secretmanagersecret"
},
"etag": "abcdef0123A=",
"expireTime": "2025-10-03T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down Expand Up @@ -864,7 +864,7 @@ X-Xss-Protection: 0
},
"createTime": "2024-04-01T12:34:56.123456Z",
"etag": "abcdef0123A=",
"expireTime": "2025-10-03T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down Expand Up @@ -914,7 +914,7 @@ X-Xss-Protection: 0
},
"createTime": "2024-04-01T12:34:56.123456Z",
"etag": "abcdef0123A=",
"expireTime": "2025-10-03T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down Expand Up @@ -965,7 +965,7 @@ X-Xss-Protection: 0
},
"createTime": "2024-04-01T12:34:56.123456Z",
"etag": "abcdef0123A=",
"expireTime": "2025-10-03T15:01:23Z",
"expireTime": "2024-04-01T12:34:56.123456Z",
"labels": {
"cnrm-test": "true",
"label-one": "value-one",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ kind: SecretManagerSecret
metadata:
annotations:
cnrm.cloud.google.com/project-id: ${projectId}
alpha.cnrm.cloud.google.com/reconciler: "direct"
labels:
label-one: value-one
name: secretmanagersecret-${uniqueId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ kind: SecretManagerSecret
metadata:
annotations:
cnrm.cloud.google.com/project-id: ${projectId}
alpha.cnrm.cloud.google.com/reconciler: "direct"
labels:
label-one: value-one
label-two: value-two
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: secretmanager.cnrm.cloud.google.com/v1beta1
kind: SecretManagerSecret
metadata:
annotations:
cnrm.cloud.google.com/project-id: ${projectId}
labels:
cnrm-test: "true"
label-one: value-one
label-two: value-two
managed-by-cnrm: "true"
name: secretmanagersecret-${uniqueId}
spec:
annotations:
bar: secretmanagersecret-bar
foo: secretmanagersecret
expireTime: "2025-10-03T15:01:23Z"
resourceID: secretmanagersecret-${uniqueId}
rotation:
nextRotationTime: "2025-10-03T15:01:23Z"
rotationPeriod: 3600s
topics:
- topicRef:
external: projects/${projectId}/topics/topic-2-${uniqueId}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: secretmanager.cnrm.cloud.google.com/v1beta1
kind: SecretManagerSecret
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
cnrm.cloud.google.com/state-into-spec: absent
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 3
labels:
cnrm-test: "true"
label-one: value-one
label-two: value-two
name: secretmanagersecret-${uniqueId}
namespace: ${uniqueId}
spec:
annotations:
bar: secretmanagersecret-bar
foo: secretmanagersecret
expireTime: "2025-10-03T15:01:23Z"
replication:
automatic: true
resourceID: secretmanagersecret-${uniqueId}
rotation:
nextRotationTime: "2025-10-03T15:01:23Z"
rotationPeriod: 3600s
topics:
- topicRef:
name: topic-2-${uniqueId}
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
name: projects/${projectNumber}/secrets/secretmanagersecret-${uniqueId}
observedGeneration: 3
Loading

0 comments on commit ff59285

Please sign in to comment.