generate user key for new cluster and add it to peers on bootstrap machine

This commit is contained in:
Pavel Sviderski
2024-08-29 15:38:13 +10:00
parent afe9c13283
commit eed2c8809c
7 changed files with 104 additions and 22 deletions
+2 -1
View File
@@ -48,7 +48,7 @@ func ConfigPath(dataDir string) string {
}
// NewBootstrapConfig returns a new machine configuration that should be applied to the first machine in a cluster.
func NewBootstrapConfig(name string, subnet netip.Prefix) (*Config, error) {
func NewBootstrapConfig(name string, subnet netip.Prefix, peers ...network.PeerConfig) (*Config, error) {
mid, err := secret.NewID()
if err != nil {
return nil, fmt.Errorf("generate machine ID: %w", err)
@@ -75,6 +75,7 @@ func NewBootstrapConfig(name string, subnet netip.Prefix) (*Config, error) {
Subnet: subnet,
PrivateKey: privKey,
PublicKey: pubKey,
Peers: peers,
},
}, nil
}
+9 -8
View File
@@ -19,13 +19,13 @@ type Config struct {
Subnet netip.Prefix
PrivateKey secret.Secret
PublicKey secret.Secret
Peers []PeerConfig
Peers []PeerConfig `json:",omitempty"`
}
type PeerConfig struct {
Subnet netip.Prefix
Endpoint netip.AddrPort
AllEndpoints []netip.AddrPort
Endpoint *netip.AddrPort `json:",omitempty"`
AllEndpoints []netip.AddrPort `json:",omitempty"`
PublicKey secret.Secret
}
@@ -43,17 +43,18 @@ func (c Config) toDeviceConfig() (wgtypes.Config, error) {
if kErr != nil {
return wgtypes.Config{}, fmt.Errorf("parse peer public key: %w", kErr)
}
endpoint := &net.UDPAddr{
IP: peerConfig.Endpoint.Addr().AsSlice(),
Port: int(peerConfig.Endpoint.Port()),
}
wgPeerConfigs[i] = wgtypes.PeerConfig{
PublicKey: peerPublicKey,
Endpoint: endpoint,
ReplaceAllowedIPs: true,
AllowedIPs: []net.IPNet{prefixToIPNet(peerConfig.Subnet)},
PersistentKeepaliveInterval: &persistentKeepalive,
}
if peerConfig.Endpoint != nil {
wgPeerConfigs[i].Endpoint = &net.UDPAddr{
IP: peerConfig.Endpoint.Addr().AsSlice(),
Port: int(peerConfig.Endpoint.Port()),
}
}
}
return wgtypes.Config{