Skip to content

Commit bf41a04

Browse files
authored
trust password and code cleanup (#226)
1 parent 6684788 commit bf41a04

File tree

2 files changed

+17
-44
lines changed

2 files changed

+17
-44
lines changed

lxd-initializer/lxd-initializer.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package main
22

33
import (
44
"context"
5-
"crypto/sha256"
6-
"encoding/hex"
75
"flag"
86
"fmt"
97
"log"
@@ -26,48 +24,6 @@ var lxdSocketPaths = []string{
2624
"/var/snap/lxd/common/lxd/unix.socket", // Snap path
2725
}
2826

29-
// normalizeToken makes a safe-ish token from a string
30-
func normalizeToken(s string) string {
31-
s = strings.ToLower(strings.TrimSpace(s))
32-
if s == "" {
33-
return s
34-
}
35-
var b strings.Builder
36-
prevDash := false
37-
for _, r := range s {
38-
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
39-
b.WriteRune(r)
40-
prevDash = false
41-
continue
42-
}
43-
if !prevDash {
44-
b.WriteByte('-')
45-
prevDash = true
46-
}
47-
}
48-
return strings.Trim(b.String(), "-")
49-
}
50-
51-
func envBool(key string, def bool) bool {
52-
v := strings.ToLower(strings.TrimSpace(os.Getenv(key)))
53-
if v == "true" || v == "1" || v == "yes" {
54-
return true
55-
}
56-
if v == "false" || v == "0" || v == "no" {
57-
return false
58-
}
59-
return def
60-
}
61-
62-
func deriveTrustPassword(seed string) string {
63-
sum := sha256.Sum256([]byte("lxd-trust:" + seed))
64-
val := hex.EncodeToString(sum[:])
65-
if len(val) > 32 {
66-
return val[:32]
67-
}
68-
return val
69-
}
70-
7127
// getKubernetesClient returns a Kubernetes client using in-cluster config
7228
func getKubernetesClient() (*kubernetes.Clientset, error) {
7329
config, err := rest.InClusterConfig()

pkg/util/trust/password.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package trust
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
)
7+
8+
// DeriveTrustPassword generates a deterministic trust password from a given seed.
9+
// The output is a hex string truncated to 32 characters for readability.
10+
func DeriveTrustPassword(seed string) string {
11+
sum := sha256.Sum256([]byte("lxd-trust:" + seed))
12+
s := hex.EncodeToString(sum[:])
13+
if len(s) > 32 {
14+
return s[:32]
15+
}
16+
return s
17+
}

0 commit comments

Comments
 (0)