replace static status definition by const
This commit is contained in:
+14
-13
@@ -9,6 +9,7 @@ import (
|
||||
"mediarr/cluster"
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
"mediarr/identity"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -42,20 +43,20 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
||||
|
||||
switch {
|
||||
case err == nil:
|
||||
identity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false)
|
||||
clusterIdentity, 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) {
|
||||
if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
|
||||
return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base")
|
||||
}
|
||||
|
||||
n.cluster = identity
|
||||
n.cluster = clusterIdentity
|
||||
return nil
|
||||
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
identity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
|
||||
clusterIdentity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
|
||||
if createErr != nil {
|
||||
return createErr
|
||||
}
|
||||
@@ -69,21 +70,21 @@ func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
||||
ID: clusterID,
|
||||
Kind: "cluster",
|
||||
Name: "cluster",
|
||||
PublicKey: identity.PublicKeyBytes(),
|
||||
Status: "active",
|
||||
PublicKey: clusterIdentity.PublicKeyBytes(),
|
||||
Status: identity.StatusActive,
|
||||
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
|
||||
if getErr == nil && bytes.Equal(existing.PublicKey, clusterIdentity.PublicKey) {
|
||||
n.cluster = clusterIdentity
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cluster : création du principal : %w", createErr)
|
||||
}
|
||||
|
||||
n.cluster = identity
|
||||
n.cluster = clusterIdentity
|
||||
return nil
|
||||
|
||||
default:
|
||||
@@ -99,28 +100,28 @@ func requiredNodePrincipals(now int64) []database.CreatePrincipalParams {
|
||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000001"),
|
||||
Kind: "system",
|
||||
Name: "system",
|
||||
Status: "active",
|
||||
Status: identity.StatusActive,
|
||||
CreatedAt: now,
|
||||
},
|
||||
{
|
||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000002"),
|
||||
Kind: "group",
|
||||
Name: "admins",
|
||||
Status: "active",
|
||||
Status: identity.StatusActive,
|
||||
CreatedAt: now,
|
||||
},
|
||||
{
|
||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000003"),
|
||||
Kind: "group",
|
||||
Name: "members",
|
||||
Status: "active",
|
||||
Status: identity.StatusActive,
|
||||
CreatedAt: now,
|
||||
},
|
||||
{
|
||||
ID: ident.MustParse("00000000-0000-7000-8000-000000000004"),
|
||||
Kind: "group",
|
||||
Name: "guests",
|
||||
Status: "active",
|
||||
Status: identity.StatusActive,
|
||||
CreatedAt: now,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"mediarr/cluster"
|
||||
"mediarr/identity"
|
||||
)
|
||||
|
||||
func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
||||
@@ -25,9 +26,9 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
||||
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))
|
||||
clusterIdentity := node.ClusterIdentity()
|
||||
if len(clusterIdentity.PublicKey) != 32 {
|
||||
t.Fatalf("clé publique de %d octets, attendu 32", len(clusterIdentity.PublicKey))
|
||||
}
|
||||
|
||||
principal, err := node.db.Queries.GetClusterPrincipal(context.Background())
|
||||
@@ -40,10 +41,10 @@ func TestUpCreatesAndKeepsClusterIdentity(t *testing.T) {
|
||||
if principal.Name != "cluster" {
|
||||
t.Fatalf("name = %q, attendu cluster", principal.Name)
|
||||
}
|
||||
if principal.Status != "active" {
|
||||
if principal.Status != identity.StatusActive {
|
||||
t.Fatalf("status = %q, attendu active", principal.Status)
|
||||
}
|
||||
if !bytes.Equal(principal.PublicKey, identity.PublicKey) {
|
||||
if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
|
||||
t.Fatal("la clé publique en base ne correspond pas à la clé chargée")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user