Skip to content

Commit

Permalink
generate token all in one function, use unix socket for etcd server c…
Browse files Browse the repository at this point in the history
…luster
  • Loading branch information
NHAS committed Jan 24, 2024
1 parent 5183efe commit 32e92f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 8 additions & 3 deletions internal/data/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ func Load(path string) error {

}

part, err := generateRandomBytes(10)
if err != nil {
return err
}
etcdUnixSocket := "unix:///tmp/wag.etcd." + part

cfg := embed.NewConfig()
cfg.Name = config.Values.Clustering.Name
cfg.InitialClusterToken = "wag-test"
cfg.LogLevel = config.Values.Clustering.ETCDLogLevel
cfg.ListenPeerUrls = parseUrls(config.Values.Clustering.ListenAddresses...)
cfg.ListenClientUrls = parseUrls("http://127.0.0.1:2480")
cfg.ListenClientUrls = parseUrls(etcdUnixSocket)
cfg.AdvertisePeerUrls = cfg.ListenPeerUrls

if _, ok := config.Values.Clustering.Peers[cfg.Name]; ok {
Expand Down Expand Up @@ -115,8 +121,7 @@ func Load(path string) error {
}

etcd, err = clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2480"},
DialTimeout: 5 * time.Second,
Endpoints: []string{etcdUnixSocket},
})
if err != nil {
return err
Expand Down
10 changes: 4 additions & 6 deletions internal/data/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,12 @@ func FinaliseRegistration(token string) error {

// Randomly generate a token for a specific username
func GenerateToken(username, overwrite string, groups []string, uses int) (token string, err error) {
tokenBytes, err := generateRandomBytes(32)
token, err = generateRandomBytes(32)
if err != nil {
return "", err
}

token = hex.EncodeToString(tokenBytes)
err = AddRegistrationToken(token, username, overwrite, groups, uses)

return
}

Expand Down Expand Up @@ -159,12 +157,12 @@ func AddRegistrationToken(token, username, overwrite string, groups []string, us
return err
}

func generateRandomBytes(n uint32) ([]byte, error) {
func generateRandomBytes(n uint32) (string, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
return "", err
}

return b, nil
return hex.EncodeToString(b), nil
}

0 comments on commit 32e92f7

Please sign in to comment.