Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
287 lines
6.8 KiB
Go
287 lines
6.8 KiB
Go
package store_test
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func registrationProof(
|
|
t *testing.T,
|
|
fqdn string,
|
|
token string,
|
|
) (string, string, string) {
|
|
t.Helper()
|
|
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sum := sha256.Sum256(publicKey)
|
|
return "sha256:" + hex.EncodeToString(sum[:]),
|
|
base64.RawStdEncoding.EncodeToString(publicKey),
|
|
base64.RawStdEncoding.EncodeToString(
|
|
ed25519.Sign(privateKey, []byte(fqdn+"\n"+token)),
|
|
)
|
|
}
|
|
|
|
func TestCampusTokenIsHashedAndFingerprintChangeIsPending(t *testing.T) {
|
|
st := newTopologyStore(t)
|
|
if err := st.CreateCampus(
|
|
"federated",
|
|
"Federated Campus",
|
|
"en",
|
|
"controller",
|
|
"federated.example.test",
|
|
true,
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var campus store.Campus
|
|
campuses, err := st.ListCampuses()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, candidate := range campuses {
|
|
if candidate.Name == "federated" {
|
|
campus = candidate
|
|
}
|
|
}
|
|
token, err := st.IssueCampusToken(campus.UUID, "node enrollment")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var storedHash string
|
|
if err := st.DB.QueryRow(`
|
|
SELECT token_hash FROM campus_tokens WHERE campus_uuid = ?
|
|
`, campus.UUID).Scan(&storedHash); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if storedHash == token || strings.Contains(storedHash, token) {
|
|
t.Fatal("plain enrollment token was persisted")
|
|
}
|
|
|
|
firstFingerprint, firstPublicKey, firstSignature :=
|
|
registrationProof(t, "node.example.test", token)
|
|
_, first, err := st.RegisterFederationInstance(
|
|
token,
|
|
"node.example.test",
|
|
firstFingerprint,
|
|
firstPublicKey,
|
|
firstSignature,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.SetFederationInstanceStatus(first.ID, "approved"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
replacementFingerprint, replacementPublicKey, replacementSignature :=
|
|
registrationProof(t, "node.example.test", token)
|
|
_, replacement, err := st.RegisterFederationInstance(
|
|
token,
|
|
"node.example.test",
|
|
replacementFingerprint,
|
|
replacementPublicKey,
|
|
replacementSignature,
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if replacement.Status != "pending" {
|
|
t.Fatalf("replacement status = %q", replacement.Status)
|
|
}
|
|
if replacement.ID == first.ID {
|
|
t.Fatal("changed fingerprint reused the approved instance identity")
|
|
}
|
|
tokens, err := st.ListCampusTokens()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(tokens) != 1 {
|
|
t.Fatalf("token count = %d", len(tokens))
|
|
}
|
|
if err := st.RevokeCampusToken(tokens[0].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
revokedFingerprint, revokedPublicKey, revokedSignature :=
|
|
registrationProof(t, "revoked.example.test", token)
|
|
if _, _, err := st.RegisterFederationInstance(
|
|
token,
|
|
"revoked.example.test",
|
|
revokedFingerprint,
|
|
revokedPublicKey,
|
|
revokedSignature,
|
|
); err == nil {
|
|
t.Fatal("revoked token registered an instance")
|
|
}
|
|
}
|
|
|
|
func TestDeletingControllerCampusRemovesFederationCredentials(t *testing.T) {
|
|
st := newTopologyStore(t)
|
|
if err := st.CreateCampus(
|
|
"controller-delete",
|
|
"Controller to delete",
|
|
"en",
|
|
"controller",
|
|
"delete.example.test",
|
|
true,
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
campuses, err := st.ListCampuses()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var controller store.Campus
|
|
for _, campus := range campuses {
|
|
if campus.Name == "controller-delete" {
|
|
controller = campus
|
|
break
|
|
}
|
|
}
|
|
token, err := st.IssueCampusToken(controller.UUID, "delete test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fingerprint, publicKey, signature :=
|
|
registrationProof(t, "node.delete.example.test", token)
|
|
if _, _, err := st.RegisterFederationInstance(
|
|
token,
|
|
"node.delete.example.test",
|
|
fingerprint,
|
|
publicKey,
|
|
signature,
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := st.DeleteCampus(controller.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var tokenCount, instanceCount int
|
|
if err := st.DB.QueryRow(`
|
|
SELECT count(*) FROM campus_tokens WHERE campus_uuid = ?
|
|
`, controller.UUID).Scan(&tokenCount); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.DB.QueryRow(`
|
|
SELECT count(*) FROM federation_instances WHERE campus_uuid = ?
|
|
`, controller.UUID).Scan(&instanceCount); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tokenCount != 0 || instanceCount != 0 {
|
|
t.Fatalf(
|
|
"federation records remain: tokens=%d instances=%d",
|
|
tokenCount,
|
|
instanceCount,
|
|
)
|
|
}
|
|
if _, _, err := st.RegisterFederationInstance(
|
|
token,
|
|
"node.delete.example.test",
|
|
fingerprint,
|
|
publicKey,
|
|
signature,
|
|
); err == nil || !strings.Contains(err.Error(), "invalid or revoked") {
|
|
t.Fatalf("deleted Campus token error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestManagedCampusSnapshotIsInheritedAndReadOnly(t *testing.T) {
|
|
st := newTopologyStore(t)
|
|
campus := store.Campus{
|
|
UUID: "managed-campus-uuid",
|
|
Name: "external-campus",
|
|
Title: "External Campus",
|
|
DefaultLang: "ja",
|
|
FQDN: "campus.example.test",
|
|
}
|
|
domains := []store.Domain{
|
|
{
|
|
UUID: "managed-local-domain",
|
|
CampusUUID: campus.UUID,
|
|
Name: "managed-local",
|
|
Title: "Managed Local",
|
|
Domain: "local",
|
|
Enabled: true,
|
|
InstanceFQDN: "node.example.test",
|
|
InstanceFingerprint: "sha256:node",
|
|
},
|
|
{
|
|
UUID: "managed-remote-domain",
|
|
CampusUUID: campus.UUID,
|
|
Name: "managed-remote",
|
|
Title: "Managed Remote",
|
|
Domain: "remote",
|
|
Enabled: true,
|
|
InstanceFQDN: "other.example.test",
|
|
InstanceFingerprint: "sha256:other",
|
|
},
|
|
}
|
|
if err := st.ApplyManagedCampusSnapshot(
|
|
campus,
|
|
domains,
|
|
"node.example.test",
|
|
"https",
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
inherited, err := st.GetCampusByUUID(campus.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if inherited.AuthorityMode != "managed" ||
|
|
inherited.Title != campus.Title ||
|
|
inherited.DefaultLang != "ja" {
|
|
t.Fatalf("inherited Campus = %#v", inherited)
|
|
}
|
|
local, err := st.GetDomainByUUID("managed-local-domain")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
remote, err := st.GetDomainByUUID("managed-remote-domain")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if local.Placement != "local" || remote.Placement != "remote" {
|
|
t.Fatalf(
|
|
"managed placements = %q, %q",
|
|
local.Placement,
|
|
remote.Placement,
|
|
)
|
|
}
|
|
if err := st.UpdateCampus(
|
|
inherited.ID,
|
|
"changed",
|
|
"Changed",
|
|
"en",
|
|
"managed",
|
|
inherited.FQDN,
|
|
true,
|
|
); err == nil {
|
|
t.Fatal("managed Campus was edited locally")
|
|
}
|
|
if err := st.RecordCampusContact(campus.UUID, "connection refused"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
contact, err := st.GetCampusByUUID(campus.UUID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if contact.LastContactError != "connection refused" {
|
|
t.Fatalf("contact error = %q", contact.LastContactError)
|
|
}
|
|
if err := st.DeleteCampus(inherited.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.GetCampusByUUID(campus.UUID); err == nil {
|
|
t.Fatal("managed Campus was not deleted")
|
|
}
|
|
}
|