Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.26.4
require (
// TODO(#236): keiailab-commons v1.0.0 안정 릴리스 시 업그레이드 예정.
// 현재 v0.10.0 — API 변경 가능성 잔존.
github.com/keiailab/keiailab-commons v0.11.0
github.com/keiailab/keiailab-commons v0.13.0
github.com/onsi/ginkgo/v2 v2.29.0
github.com/onsi/gomega v1.41.0
github.com/prometheus/client_golang v1.23.2
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/keiailab/keiailab-commons v0.11.0 h1:aydaheRHz3OjqgWU6C6dpssYA87ba9wZsd3G2kTAxCY=
github.com/keiailab/keiailab-commons v0.11.0/go.mod h1:Lfj6BDoP6OaBxSDAtBuG0iHp9ADo5T8b/Bl54w8n74o=
github.com/keiailab/keiailab-commons v0.12.1-0.20260630004504-13e0976fc111 h1:9eZ33N5WdGza4aoJm13PVFC3UAsJ3EyCh9k8/W+5DJs=
github.com/keiailab/keiailab-commons v0.12.1-0.20260630004504-13e0976fc111/go.mod h1:Lfj6BDoP6OaBxSDAtBuG0iHp9ADo5T8b/Bl54w8n74o=
github.com/keiailab/keiailab-commons v0.12.1-0.20260630011743-a54c4a826028 h1:lFNY/8inscTA9bzp3MRjmZNKeA9LDGCChu6UE4vGAo0=
github.com/keiailab/keiailab-commons v0.12.1-0.20260630011743-a54c4a826028/go.mod h1:Lfj6BDoP6OaBxSDAtBuG0iHp9ADo5T8b/Bl54w8n74o=
github.com/keiailab/keiailab-commons v0.13.0 h1:GfJEIs/HeOu5CGMUJQkTHoeyA3BOX3tHMqqACldo6DI=
github.com/keiailab/keiailab-commons v0.13.0/go.mod h1:Lfj6BDoP6OaBxSDAtBuG0iHp9ADo5T8b/Bl54w8n74o=
github.com/klauspost/compress v1.18.6 h1:2jupLlAwFm95+YDR+NwD2MEfFO9d4z4Prjl1XXDjuao=
github.com/klauspost/compress v1.18.6/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
15 changes: 7 additions & 8 deletions internal/controller/password_rotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package controller

import (
"context"
"crypto/sha256"
"fmt"

commonssecrethash "github.com/keiailab/keiailab-commons/pkg/secrethash"
commonsstatus "github.com/keiailab/keiailab-commons/pkg/status"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -62,12 +61,12 @@ func (r *MongoDBReconciler) reconcilePasswordRotation(ctx context.Context, mdb *
}

func hashSecretData(secret *corev1.Secret) string {
h := sha256.New()
for k, v := range secret.Data {
h.Write([]byte(k))
h.Write(v)
}
return fmt.Sprintf("%x", h.Sum(nil))[:16]
// 결정적 정렬-누적은 keiailab-commons/pkg/secrethash.Hash 에 위임 — 기존
// `for k, v := range secret.Data` map 순회의 *비결정성 버그*(동일 Secret 이
// reconcile 마다 다른 해시 → 불필요한 rotation 신호) 를 구조적 교정. 16자
// truncation 은 annotation 형식 보존을 위해 유지 (determinism 만 교정 →
// adopt 시 1회 해시 변화 후 안정).
return commonssecrethash.Hash(secret.Data)[:16]
}

func getAnnotation(mdb *mongodbv1alpha1.MongoDB, key string) string {
Expand Down
37 changes: 37 additions & 0 deletions internal/controller/password_rotation_hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controller

import (
"testing"

corev1 "k8s.io/api/core/v1"
)

// TestHashSecretData_Deterministic 은 commons secrethash 위임으로 기존 map-순회
// 비결정성 버그가 교정됐음을 보증한다 — 동일 Secret 은 항상 동일 해시.
func TestHashSecretData_Deterministic(t *testing.T) {
t.Parallel()
s := &corev1.Secret{Data: map[string][]byte{
"username": []byte("admin"),
"password": []byte("p@ss"),
"keyfile": []byte("xyzkeyfilecontents"),
}}
first := hashSecretData(s)
for range 200 {
if got := hashSecretData(s); got != first {
t.Fatalf("hashSecretData 비결정적: %s != %s", got, first)
}
}
if len(first) != 16 {
t.Fatalf("해시 길이 %d, want 16 (annotation 형식 truncation 보존)", len(first))
}
}

// 값 변경이 해시에 반영돼야 rotation 신호가 동작한다.
func TestHashSecretData_ValueChangeChangesHash(t *testing.T) {
t.Parallel()
a := hashSecretData(&corev1.Secret{Data: map[string][]byte{"password": []byte("OLD")}})
b := hashSecretData(&corev1.Secret{Data: map[string][]byte{"password": []byte("NEW")}})
if a == b {
t.Fatal("값 변경이 해시에 반영되지 않음")
}
}
Loading
Loading