generate identity
This commit is contained in:
+23
-4
@@ -1,32 +1,51 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"mediarr/cluster"
|
||||
"mediarr/database"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
db database.DatabaseClient
|
||||
db database.DatabaseClient
|
||||
clusterKeyPath string
|
||||
cluster cluster.Identity
|
||||
}
|
||||
|
||||
type NewNodeParams struct {
|
||||
DatabaseName string
|
||||
DatabaseName string
|
||||
ClusterKeyPath string
|
||||
}
|
||||
|
||||
func NewNode(params NewNodeParams) (error, Node) {
|
||||
|
||||
var databaseName *string
|
||||
if params.DatabaseName != "" {
|
||||
databaseName = ¶ms.DatabaseName
|
||||
}
|
||||
|
||||
err, db := database.NewDatabase(database.NewDatabaseParams{
|
||||
DatabaseName: ¶ms.DatabaseName,
|
||||
DatabaseName: databaseName,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err, Node{}
|
||||
}
|
||||
|
||||
clusterKeyPath := params.ClusterKeyPath
|
||||
if clusterKeyPath == "" {
|
||||
clusterKeyPath = "cluster.key"
|
||||
}
|
||||
|
||||
return nil, Node{
|
||||
db: *db,
|
||||
db: *db,
|
||||
clusterKeyPath: clusterKeyPath,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) Close() error {
|
||||
return n.db.Close()
|
||||
}
|
||||
|
||||
func (n *Node) ClusterIdentity() cluster.Identity {
|
||||
return n.cluster
|
||||
}
|
||||
|
||||
+60
-1
@@ -1,9 +1,12 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mediarr/cluster"
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"time"
|
||||
@@ -29,7 +32,63 @@ func (n *Node) Up() error {
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return n.ensureClusterIdentity(ctx)
|
||||
|
||||
}
|
||||
|
||||
func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
||||
|
||||
principal, err := n.db.Queries.GetClusterPrincipal(ctx)
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
identity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false)
|
||||
if loadErr != nil {
|
||||
return fmt.Errorf("cluster : principal présent en base mais clé indisponible : %w", loadErr)
|
||||
}
|
||||
|
||||
if !bytes.Equal(principal.PublicKey, identity.PublicKey) {
|
||||
return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base")
|
||||
}
|
||||
|
||||
n.cluster = identity
|
||||
return nil
|
||||
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
identity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
|
||||
if createErr != nil {
|
||||
return createErr
|
||||
}
|
||||
|
||||
clusterID, idErr := ident.NewV7()
|
||||
if idErr != nil {
|
||||
return idErr
|
||||
}
|
||||
|
||||
_, createErr = n.db.WriteQueries.CreatePrincipal(ctx, database.CreatePrincipalParams{
|
||||
ID: clusterID,
|
||||
Kind: "cluster",
|
||||
Name: "cluster",
|
||||
PublicKey: identity.PublicKeyBytes(),
|
||||
Status: "active",
|
||||
CreatedAt: time.Now().UnixMilli(),
|
||||
})
|
||||
if createErr != nil {
|
||||
existing, getErr := n.db.Queries.GetClusterPrincipal(ctx)
|
||||
if getErr == nil && bytes.Equal(existing.PublicKey, identity.PublicKey) {
|
||||
n.cluster = identity
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cluster : création du principal : %w", createErr)
|
||||
}
|
||||
|
||||
n.cluster = identity
|
||||
return nil
|
||||
|
||||
default:
|
||||
return fmt.Errorf("cluster : lecture du principal : %w", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"mediarr/cluster"
|
||||
)
|
||||
|
||||
func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
keyPath := filepath.Join(t.TempDir(), "cluster.key")
|
||||
|
||||
err, node := NewNode(NewNodeParams{DatabaseName: "identity", ClusterKeyPath: keyPath})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode : %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = node.Close() })
|
||||
|
||||
if err := node.Up(); err != nil {
|
||||
t.Fatalf("Up : %v", err)
|
||||
}
|
||||
|
||||
identity := node.ClusterIdentity()
|
||||
if len(identity.PublicKey) != 32 {
|
||||
t.Fatalf("clé publique de %d octets, attendu 32", len(identity.PublicKey))
|
||||
}
|
||||
|
||||
principal, err := node.db.Queries.GetClusterPrincipal(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("GetClusterPrincipal : %v", err)
|
||||
}
|
||||
if principal.Kind != "cluster" {
|
||||
t.Fatalf("kind = %q, attendu cluster", principal.Kind)
|
||||
}
|
||||
if principal.Name != "cluster" {
|
||||
t.Fatalf("name = %q, attendu cluster", principal.Name)
|
||||
}
|
||||
if principal.Status != "active" {
|
||||
t.Fatalf("status = %q, attendu active", principal.Status)
|
||||
}
|
||||
if !bytes.Equal(principal.PublicKey, identity.PublicKey) {
|
||||
t.Fatal("la clé publique en base ne correspond pas à la clé chargée")
|
||||
}
|
||||
|
||||
if err := node.Up(); err != nil {
|
||||
t.Fatalf("second Up : %v", err)
|
||||
}
|
||||
|
||||
again, err := node.db.Queries.GetClusterPrincipal(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("GetClusterPrincipal après second Up : %v", err)
|
||||
}
|
||||
if again.ID != principal.ID {
|
||||
t.Fatal("le principal cluster a été recréé au lieu d'être réutilisé")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpReusesKeyWhenDatabaseRecreated(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
keyPath := filepath.Join(t.TempDir(), "cluster.key")
|
||||
|
||||
err, first := NewNode(NewNodeParams{DatabaseName: "recreate", ClusterKeyPath: keyPath})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode : %v", err)
|
||||
}
|
||||
|
||||
if err := first.Up(); err != nil {
|
||||
t.Fatalf("Up : %v", err)
|
||||
}
|
||||
|
||||
firstPublicKey := first.ClusterIdentity().PublicKeyBytes()
|
||||
|
||||
if err := first.Close(); err != nil {
|
||||
t.Fatalf("Close : %v", err)
|
||||
}
|
||||
|
||||
removeDatabaseFiles(t, "recreate")
|
||||
|
||||
err, second := NewNode(NewNodeParams{DatabaseName: "recreate", ClusterKeyPath: keyPath})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode après recréation : %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = second.Close() })
|
||||
|
||||
if err := second.Up(); err != nil {
|
||||
t.Fatalf("Up après recréation : %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(second.ClusterIdentity().PublicKeyBytes(), firstPublicKey) {
|
||||
t.Fatal("la clé du cluster a changé après recréation de la base")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpFailsWhenKeyMissing(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
keyPath := filepath.Join(t.TempDir(), "cluster.key")
|
||||
|
||||
err, node := NewNode(NewNodeParams{DatabaseName: "missingkey", ClusterKeyPath: keyPath})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode : %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = node.Close() })
|
||||
|
||||
if err := node.Up(); err != nil {
|
||||
t.Fatalf("Up : %v", err)
|
||||
}
|
||||
|
||||
if err := os.Remove(keyPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := node.Up(); err == nil {
|
||||
t.Fatal("Up aurait dû échouer sans la clé du cluster")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpFailsOnKeyMismatch(t *testing.T) {
|
||||
t.Chdir(t.TempDir())
|
||||
|
||||
keyPath := filepath.Join(t.TempDir(), "cluster.key")
|
||||
|
||||
err, node := NewNode(NewNodeParams{DatabaseName: "mismatch", ClusterKeyPath: keyPath})
|
||||
if err != nil {
|
||||
t.Fatalf("NewNode : %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = node.Close() })
|
||||
|
||||
if err := node.Up(); err != nil {
|
||||
t.Fatalf("Up : %v", err)
|
||||
}
|
||||
|
||||
if err := os.Remove(keyPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := cluster.LoadOrCreate(keyPath, true); err != nil {
|
||||
t.Fatalf("génération d'une nouvelle clé : %v", err)
|
||||
}
|
||||
|
||||
if err := node.Up(); err == nil {
|
||||
t.Fatal("Up aurait dû détecter un mismatch entre la clé et le principal")
|
||||
}
|
||||
}
|
||||
|
||||
func removeDatabaseFiles(t *testing.T, databaseName string) {
|
||||
t.Helper()
|
||||
|
||||
for _, suffix := range []string{"", "-wal", "-shm"} {
|
||||
path := databaseName + ".db" + suffix
|
||||
|
||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||
t.Fatalf("suppression de %s : %v", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user