Secrets: Dont update createdBy when updating a secure value (#115760)
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
SELECT
|
||||
{{ .Ident "created" }},
|
||||
{{ .Ident "created_by" }},
|
||||
{{ .Ident "version" }},
|
||||
{{ .Ident "active" }},
|
||||
{{ .Ident "namespace" }},
|
||||
{{ .Ident "name" }}
|
||||
FROM
|
||||
{{ .Ident "secret_secure_value" }}
|
||||
WHERE
|
||||
WHERE
|
||||
{{ .Ident "namespace" }} = {{ .Arg .Namespace }} AND
|
||||
{{ .Ident "name" }} = {{ .Arg .Name }}
|
||||
ORDER BY {{ .Ident "version" }} DESC
|
||||
|
||||
@@ -122,7 +122,7 @@ func (sv *secureValueDB) toKubernetes() (*secretv1beta1.SecureValue, error) {
|
||||
}
|
||||
|
||||
// toCreateRow maps a Kubernetes resource into a DB row for new resources being created/inserted.
|
||||
func toCreateRow(createdAt, updatedAt int64, keeper string, sv *secretv1beta1.SecureValue, actorUID string) (*secureValueDB, error) {
|
||||
func toCreateRow(createdAt, updatedAt int64, keeper string, sv *secretv1beta1.SecureValue, createdBy, updatedBy string) (*secureValueDB, error) {
|
||||
row, err := toRow(keeper, sv, "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert SecureValue to secureValueDB: %w", err)
|
||||
@@ -130,9 +130,9 @@ func toCreateRow(createdAt, updatedAt int64, keeper string, sv *secretv1beta1.Se
|
||||
|
||||
row.GUID = uuid.New().String()
|
||||
row.Created = createdAt
|
||||
row.CreatedBy = actorUID
|
||||
row.CreatedBy = createdBy
|
||||
row.Updated = updatedAt
|
||||
row.UpdatedBy = actorUID
|
||||
row.UpdatedBy = updatedBy
|
||||
|
||||
return row, nil
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ func (s *secureValueMetadataStorage) Create(ctx context.Context, keeper string,
|
||||
var row *secureValueDB
|
||||
|
||||
err := s.db.Transaction(ctx, func(ctx context.Context) error {
|
||||
latest, err := s.getLatestVersionAndCreatedAt(ctx, xkube.Namespace(sv.Namespace), sv.Name)
|
||||
latest, err := s.getLatestVersionAndCreated(ctx, xkube.Namespace(sv.Namespace), sv.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fetching latest secure value version: %w", err)
|
||||
}
|
||||
@@ -110,7 +110,13 @@ func (s *secureValueMetadataStorage) Create(ctx context.Context, keeper string,
|
||||
}
|
||||
updatedAt := now
|
||||
|
||||
row, err = toCreateRow(createdAt, updatedAt, keeper, sv, actorUID)
|
||||
createdBy := actorUID
|
||||
if latest.createdBy != "" {
|
||||
createdBy = latest.createdBy
|
||||
}
|
||||
updatedBy := actorUID
|
||||
|
||||
row, err = toCreateRow(createdAt, updatedAt, keeper, sv, createdBy, updatedBy)
|
||||
if err != nil {
|
||||
return fmt.Errorf("to create row: %w", err)
|
||||
}
|
||||
@@ -161,13 +167,14 @@ func (s *secureValueMetadataStorage) Create(ctx context.Context, keeper string,
|
||||
return createdSecureValue, nil
|
||||
}
|
||||
|
||||
type versionAndCreatedAt struct {
|
||||
type versionAndCreated struct {
|
||||
createdAt int64
|
||||
createdBy string
|
||||
version int64
|
||||
}
|
||||
|
||||
func (s *secureValueMetadataStorage) getLatestVersionAndCreatedAt(ctx context.Context, namespace xkube.Namespace, name string) (versionAndCreatedAt, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "SecureValueMetadataStorage.getLatestVersionAndCreatedAt", trace.WithAttributes(
|
||||
func (s *secureValueMetadataStorage) getLatestVersionAndCreated(ctx context.Context, namespace xkube.Namespace, name string) (versionAndCreated, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "SecureValueMetadataStorage.getLatestVersionAndCreated", trace.WithAttributes(
|
||||
attribute.String("name", name),
|
||||
attribute.String("namespace", namespace.String()),
|
||||
))
|
||||
@@ -181,45 +188,48 @@ func (s *secureValueMetadataStorage) getLatestVersionAndCreatedAt(ctx context.Co
|
||||
|
||||
q, err := sqltemplate.Execute(sqlGetLatestSecureValueVersionAndCreatedAt, req)
|
||||
if err != nil {
|
||||
return versionAndCreatedAt{}, fmt.Errorf("execute template %q: %w", sqlGetLatestSecureValueVersionAndCreatedAt.Name(), err)
|
||||
return versionAndCreated{}, fmt.Errorf("execute template %q: %w", sqlGetLatestSecureValueVersionAndCreatedAt.Name(), err)
|
||||
}
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, q, req.GetArgs()...)
|
||||
if err != nil {
|
||||
return versionAndCreatedAt{}, fmt.Errorf("fetching latest version for secure value: namespace=%+v name=%+v %w", namespace, name, err)
|
||||
return versionAndCreated{}, fmt.Errorf("fetching latest version for secure value: namespace=%+v name=%+v %w", namespace, name, err)
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return versionAndCreatedAt{}, fmt.Errorf("error executing query: %w", err)
|
||||
return versionAndCreated{}, fmt.Errorf("error executing query: %w", err)
|
||||
}
|
||||
|
||||
if !rows.Next() {
|
||||
return versionAndCreatedAt{}, nil
|
||||
return versionAndCreated{}, nil
|
||||
}
|
||||
|
||||
var (
|
||||
createdAt int64
|
||||
createdBy string
|
||||
version int64
|
||||
active bool
|
||||
namespaceFromDB string
|
||||
nameFromDB string
|
||||
)
|
||||
if err := rows.Scan(&createdAt, &version, &active, &namespaceFromDB, &nameFromDB); err != nil {
|
||||
return versionAndCreatedAt{}, fmt.Errorf("scanning version from returned rows: %w", err)
|
||||
if err := rows.Scan(&createdAt, &createdBy, &version, &active, &namespaceFromDB, &nameFromDB); err != nil {
|
||||
return versionAndCreated{}, fmt.Errorf("scanning version and created from returned rows: %w", err)
|
||||
}
|
||||
|
||||
if namespaceFromDB != namespace.String() || nameFromDB != name {
|
||||
return versionAndCreatedAt{}, fmt.Errorf("bug: expected to find latest version for namespace=%+v name=%+v but got version for namespace=%+v name=%+v",
|
||||
return versionAndCreated{}, fmt.Errorf("bug: expected to find version and created for namespace=%+v name=%+v but got for namespace=%+v name=%+v",
|
||||
namespace, name, namespaceFromDB, nameFromDB)
|
||||
}
|
||||
|
||||
if !active {
|
||||
createdAt = 0
|
||||
createdBy = ""
|
||||
}
|
||||
|
||||
return versionAndCreatedAt{
|
||||
return versionAndCreated{
|
||||
createdAt: createdAt,
|
||||
createdBy: createdBy,
|
||||
version: version,
|
||||
}, nil
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,12 +1,13 @@
|
||||
SELECT
|
||||
`created`,
|
||||
`created_by`,
|
||||
`version`,
|
||||
`active`,
|
||||
`namespace`,
|
||||
`name`
|
||||
FROM
|
||||
`secret_secure_value`
|
||||
WHERE
|
||||
WHERE
|
||||
`namespace` = 'ns' AND
|
||||
`name` = 'name'
|
||||
ORDER BY `version` DESC
|
||||
|
||||
+2
-1
@@ -1,12 +1,13 @@
|
||||
SELECT
|
||||
"created",
|
||||
"created_by",
|
||||
"version",
|
||||
"active",
|
||||
"namespace",
|
||||
"name"
|
||||
FROM
|
||||
"secret_secure_value"
|
||||
WHERE
|
||||
WHERE
|
||||
"namespace" = 'ns' AND
|
||||
"name" = 'name'
|
||||
ORDER BY "version" DESC
|
||||
|
||||
+2
-1
@@ -1,12 +1,13 @@
|
||||
SELECT
|
||||
"created",
|
||||
"created_by",
|
||||
"version",
|
||||
"active",
|
||||
"namespace",
|
||||
"name"
|
||||
FROM
|
||||
"secret_secure_value"
|
||||
WHERE
|
||||
WHERE
|
||||
"namespace" = 'ns' AND
|
||||
"name" = 'name'
|
||||
ORDER BY "version" DESC
|
||||
|
||||
Reference in New Issue
Block a user