Files
postgresql-tenant-operator/internal/openbao/credentials_test.go
T
panxiao81 a4d835e612
E2E Tests / Run on Ubuntu (pull_request) Failing after 59s
Tests / Run on Ubuntu (pull_request) Successful in 3m55s
Lint / Run on Ubuntu (pull_request) Successful in 4m18s
refactor: assemble and reuse Instance dependencies
2026-09-11 15:54:05 +00:00

126 lines
3.5 KiB
Go

/*
Copyright 2026.
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 openbao
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
api "git.ddupan.top/panxiao81/postgresql-tenant-operator/api/v1alpha1"
bao "github.com/openbao/openbao/api/v2"
)
const testToken = "test-token"
const testAdmin = "admin"
func TestSharedIdentityReauthenticatesOnExpiryAndRevocation(t *testing.T) {
var logins atomic.Int32
var revoked atomic.Bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/secret/data/admin" {
http.NotFound(w, r)
return
}
if revoked.Swap(false) {
w.WriteHeader(http.StatusForbidden)
return
}
if r.Header.Get("X-Vault-Token") != testToken {
w.WriteHeader(http.StatusForbidden)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"data": map[string]any{"username": testAdmin, "password": "test-password"},
"metadata": map[string]any{"version": 1},
},
})
}))
defer server.Close()
config := bao.NewConfig()
config.Address = server.URL
config.MaxRetries = 0
client, err := bao.NewClient(config)
if err != nil {
t.Fatal(err)
}
source := NewCredentials(client, "secret", "kubernetes", "controller")
source.authenticate = func(context.Context) (*bao.Secret, error) {
logins.Add(1)
return &bao.Secret{Auth: &bao.SecretAuth{ClientToken: testToken, LeaseDuration: 300}}, nil
}
reference := api.OpenBaoSecretReference{Path: testAdmin}
var wg sync.WaitGroup
for range 8 {
wg.Go(func() {
if _, err := source.Read(context.Background(), reference); err != nil {
t.Error(err)
}
})
}
wg.Wait()
if logins.Load() != 1 {
t.Fatal("concurrent reads repeatedly authenticated")
}
source.expires = time.Time{}
if _, err := source.Read(context.Background(), reference); err != nil {
t.Fatal(err)
}
if logins.Load() != 2 {
t.Fatal("expired identity was not refreshed")
}
revoked.Store(true)
if _, err := source.Read(context.Background(), reference); err != nil {
t.Fatal(err)
}
if logins.Load() != 3 {
t.Fatal("revoked identity was not refreshed")
}
}
func TestDeniedPolicyRetriesAuthenticationOnlyOnce(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusForbidden)
}))
defer server.Close()
config := bao.NewConfig()
config.Address = server.URL
config.MaxRetries = 0
client, err := bao.NewClient(config)
if err != nil {
t.Fatal(err)
}
source := NewCredentials(client, "secret", "kubernetes", "controller")
logins := 0
source.authenticate = func(context.Context) (*bao.Secret, error) {
logins++
return &bao.Secret{Auth: &bao.SecretAuth{ClientToken: testToken, LeaseDuration: 300}}, nil
}
if _, err := source.Read(context.Background(), api.OpenBaoSecretReference{Path: testAdmin}); err == nil {
t.Fatal("denied policy was accepted")
}
if logins != 2 {
t.Fatal("authentication did not stop after one retry")
}
}