forked from canonical/microcloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxd_join.go
65 lines (52 loc) · 1.93 KB
/
lxd_join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package service
import (
"context"
"encoding/pem"
"fmt"
"github.com/canonical/lxd/lxd/util"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/version"
)
func (s *LXDService) configFromToken(token string) (*api.ClusterPut, error) {
joinToken, err := shared.JoinTokenDecode(token)
if err != nil {
return nil, fmt.Errorf("Invalid cluster join token: %w", err)
}
config := &api.ClusterPut{
Cluster: api.Cluster{ServerName: s.name, Enabled: true},
ServerAddress: util.CanonicalNetworkAddress(s.address, s.port),
}
ok, err := s.HasExtension(context.Background(), s.Name(), s.Address(), nil, "explicit_trust_token")
if err != nil {
return nil, err
}
if ok {
config.ClusterToken = token
} else {
config.ClusterPassword = token
}
// Attempt to find a working cluster member to use for joining by retrieving the
// cluster certificate from each address in the join token until we succeed.
for _, clusterAddress := range joinToken.Addresses {
// Cluster URL
config.ClusterAddress = util.CanonicalNetworkAddress(clusterAddress, s.port)
// Cluster certificate
cert, err := shared.GetRemoteCertificate(fmt.Sprintf("https://%s", config.ClusterAddress), version.UserAgent)
if err != nil {
logger.Warnf("Error connecting to existing cluster member %q: %v\n", clusterAddress, err)
continue
}
certDigest := shared.CertFingerprint(cert)
if joinToken.Fingerprint != certDigest {
return nil, fmt.Errorf("Certificate fingerprint mismatch between join token and cluster member %q", clusterAddress)
}
config.ClusterCertificate = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}))
break // We've found a working cluster member.
}
if config.ClusterCertificate == "" {
return nil, fmt.Errorf("Unable to connect to any of the cluster members specified in join token")
}
return config, nil
}