2022-03-22 11:03:15 -04:00
|
|
|
package storewrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-03-30 08:28:14 -04:00
|
|
|
"net/netip"
|
2022-03-22 11:03:15 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/peer"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/state"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/store"
|
|
|
|
kubeadm "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
|
|
|
|
)
|
|
|
|
|
2022-03-30 08:28:14 -04:00
|
|
|
// variables which will be used as a store-prefix start with prefix[...].
|
|
|
|
// variables which will be used as a store-key start with key[...].
|
2022-03-22 11:03:15 -04:00
|
|
|
const (
|
2022-03-30 08:28:14 -04:00
|
|
|
keyHighestAvailableCoordinatorIP = "highestAvailableCoordinatorIP"
|
|
|
|
keyHighestAvailableNodeIP = "highestAvailableNodeIP"
|
|
|
|
keyKubernetesJoinCommand = "kubeJoin"
|
|
|
|
keyPeersResourceVersion = "peersResourceVersion"
|
|
|
|
keyMasterSecret = "masterSecret"
|
|
|
|
keyKubeConfig = "kubeConfig"
|
|
|
|
keyClusterID = "clusterID"
|
|
|
|
keyVPNPubKey = "vpnKey"
|
|
|
|
keyKEKID = "kekID"
|
|
|
|
prefixFreeCoordinatorIPs = "freeCoordinatorVPNIPs"
|
|
|
|
prefixAdminLocation = "externalAdminsData"
|
|
|
|
prefixPeerLocation = "peerPrefix"
|
|
|
|
prefixFreeNodeIPs = "freeNodeVPNIPs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
coordinatorIPRangeStart = netip.AddrFrom4([4]byte{10, 118, 0, 1})
|
|
|
|
coordinatorIPRangeEnd = netip.AddrFrom4([4]byte{10, 118, 0, 10})
|
|
|
|
nodeIPRangeStart = netip.AddrFrom4([4]byte{10, 118, 0, 11})
|
|
|
|
nodeIPRangeEnd = netip.AddrFrom4([4]byte{10, 118, 255, 254})
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// StoreWrapper is a wrapper for the store interface.
|
|
|
|
type StoreWrapper struct {
|
|
|
|
Store interface {
|
|
|
|
Get(string) ([]byte, error)
|
|
|
|
Put(string, []byte) error
|
|
|
|
Delete(string) error
|
|
|
|
Iterator(string) (store.Iterator, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetState returns the state from store.
|
|
|
|
func (s StoreWrapper) GetState() (state.State, error) {
|
|
|
|
rawState, err := s.Store.Get("state")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
currState, err := strconv.Atoi(string(rawState))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.State(currState), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutState saves the state to store.
|
|
|
|
func (s StoreWrapper) PutState(currState state.State) error {
|
|
|
|
rawState := []byte(strconv.Itoa(int(currState)))
|
|
|
|
return s.Store.Put("state", rawState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVPNKey returns the VPN pubKey from Store.
|
|
|
|
func (s StoreWrapper) GetVPNKey() ([]byte, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Get(keyVPNPubKey)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutVPNKey saves the VPN pubKey to store.
|
|
|
|
func (s StoreWrapper) PutVPNKey(key []byte) error {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyVPNPubKey, key)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutPeer puts a single peer in the store, with a unique key derived form the VPNIP.
|
|
|
|
func (s StoreWrapper) PutPeer(peer peer.Peer) error {
|
|
|
|
if len(peer.VPNIP) == 0 {
|
2022-03-30 08:28:14 -04:00
|
|
|
return errors.New("unique ID of peer not set")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
jsonPeer, err := json.Marshal(peer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(prefixPeerLocation+peer.VPNIP, jsonPeer)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemovePeer removes a peer from the store.
|
|
|
|
func (s StoreWrapper) RemovePeer(peer peer.Peer) error {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Delete(prefixPeerLocation + peer.VPNIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeer returns a peer requested by the given VPN IP address.
|
|
|
|
func (s StoreWrapper) GetPeer(vpnIP string) (peer.Peer, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
bytePeer, err := s.Store.Get(prefixPeerLocation + vpnIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return peer.Peer{}, err
|
|
|
|
}
|
|
|
|
var peer peer.Peer
|
|
|
|
err = json.Unmarshal(bytePeer, &peer)
|
|
|
|
return peer, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeers returns all peers in the store.
|
|
|
|
func (s StoreWrapper) GetPeers() ([]peer.Peer, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.getPeersByPrefix(prefixPeerLocation)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IncrementPeersResourceVersion increments the version of the stored peers.
|
|
|
|
// Should be called in a transaction together with Add/Remove operation(s).
|
|
|
|
func (s StoreWrapper) IncrementPeersResourceVersion() error {
|
|
|
|
val, err := s.GetPeersResourceVersion()
|
2022-03-25 07:49:22 -04:00
|
|
|
var unsetErr *store.ValueUnsetError
|
2022-03-22 11:03:15 -04:00
|
|
|
if errors.As(err, &unsetErr) {
|
|
|
|
val = 0
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyPeersResourceVersion, []byte(strconv.Itoa(val+1)))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPeersResourceVersion returns the current version of the stored peers.
|
|
|
|
func (s StoreWrapper) GetPeersResourceVersion() (int, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
raw, err := s.Store.Get(keyPeersResourceVersion)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
val, err := strconv.Atoi(string(raw))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatePeers synchronizes the stored peers with the passed peers, returning added and removed peers.
|
|
|
|
func (s StoreWrapper) UpdatePeers(peers []peer.Peer) (added, removed []peer.Peer, err error) {
|
|
|
|
// convert to map for easier lookup
|
|
|
|
updatedPeers := make(map[string]peer.Peer)
|
|
|
|
for _, p := range peers {
|
|
|
|
updatedPeers[p.VPNIP] = p
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:28:14 -04:00
|
|
|
it, err := s.Store.Iterator(prefixPeerLocation)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect peers that need to be added or removed
|
|
|
|
for it.HasNext() {
|
|
|
|
key, err := it.GetNext()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
val, err := s.Store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
var storedPeer peer.Peer
|
|
|
|
if err := json.Unmarshal(val, &storedPeer); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if updPeer, ok := updatedPeers[storedPeer.VPNIP]; ok {
|
|
|
|
if updPeer.PublicEndpoint != storedPeer.PublicEndpoint || !bytes.Equal(updPeer.VPNPubKey, storedPeer.VPNPubKey) {
|
|
|
|
// stored peer must be updated, so mark for addition AND removal
|
|
|
|
added = append(added, updPeer)
|
|
|
|
removed = append(removed, storedPeer)
|
|
|
|
}
|
|
|
|
delete(updatedPeers, updPeer.VPNIP)
|
|
|
|
} else {
|
|
|
|
// stored peer is not contained in the updated peers, so mark for removal
|
|
|
|
removed = append(removed, storedPeer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remaining updated peers were not in the store, so mark for addition
|
|
|
|
for _, p := range updatedPeers {
|
|
|
|
added = append(added, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform remove and add
|
|
|
|
for _, p := range removed {
|
2022-03-30 08:28:14 -04:00
|
|
|
if err := s.Store.Delete(prefixPeerLocation + p.VPNIP); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, p := range added {
|
|
|
|
data, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
if err := s.Store.Put(prefixPeerLocation+p.VPNIP, data); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return added, removed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutAdmin puts a single admin in the store, with a unique key derived form the VPNIP.
|
|
|
|
func (s StoreWrapper) PutAdmin(peer peer.AdminData) error {
|
|
|
|
jsonPeer, err := json.Marshal(peer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(prefixAdminLocation+peer.VPNIP, jsonPeer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAdmin gets a single admin from the store.
|
|
|
|
// TODO: extend if we want to have multiple admins.
|
|
|
|
func (s StoreWrapper) GetAdmin() (peer.AdminData, error) {
|
|
|
|
iter, err := s.Store.Iterator(prefixAdminLocation)
|
|
|
|
if err != nil {
|
|
|
|
return peer.AdminData{}, err
|
|
|
|
}
|
|
|
|
key, err := iter.GetNext()
|
|
|
|
if err != nil {
|
|
|
|
return peer.AdminData{}, err
|
|
|
|
}
|
|
|
|
value, err := s.Store.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return peer.AdminData{}, err
|
|
|
|
}
|
|
|
|
var adminData peer.AdminData
|
|
|
|
if err := json.Unmarshal(value, &adminData); err != nil {
|
|
|
|
return peer.AdminData{}, err
|
|
|
|
}
|
|
|
|
return adminData, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s StoreWrapper) getPeersByPrefix(prefix string) ([]peer.Peer, error) {
|
|
|
|
peerKeys, err := s.Store.Iterator(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var peers []peer.Peer
|
|
|
|
for peerKeys.HasNext() {
|
|
|
|
storeKey, err := peerKeys.GetNext()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
marshalPeer, err := s.Store.Get(storeKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var peer peer.Peer
|
|
|
|
if err := json.Unmarshal(marshalPeer, &peer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peers = append(peers, peer)
|
|
|
|
|
|
|
|
}
|
|
|
|
return peers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKubernetesJoinArgs returns the Kubernetes join command from store.
|
|
|
|
func (s StoreWrapper) GetKubernetesJoinArgs() (*kubeadm.BootstrapTokenDiscovery, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
rawJoinCommand, err := s.Store.Get(keyKubernetesJoinCommand)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
joinCommand := kubeadm.BootstrapTokenDiscovery{}
|
|
|
|
if err := json.Unmarshal(rawJoinCommand, &joinCommand); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &joinCommand, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutKubernetesJoinArgs saves the Kubernetes join command to store.
|
|
|
|
func (s StoreWrapper) PutKubernetesJoinArgs(args *kubeadm.BootstrapTokenDiscovery) error {
|
|
|
|
j, err := json.Marshal(args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyKubernetesJoinCommand, j)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetKubernetesConfig returns the Kubernetes kubeconfig file to authenticate with the kubernetes API.
|
|
|
|
func (s StoreWrapper) GetKubernetesConfig() ([]byte, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Get(keyKubeConfig)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutKubernetesConfig saves the Kubernetes kubeconfig file command to store.
|
|
|
|
func (s StoreWrapper) PutKubernetesConfig(kubeConfig []byte) error {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyKubeConfig, kubeConfig)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetMasterSecret returns the Constellation master secret from store.
|
|
|
|
func (s StoreWrapper) GetMasterSecret() ([]byte, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Get(keyMasterSecret)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutMasterSecret saves the Constellation master secret to store.
|
|
|
|
func (s StoreWrapper) PutMasterSecret(masterSecret []byte) error {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyMasterSecret, masterSecret)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetKEKID returns the key encryption key ID from store.
|
|
|
|
func (s StoreWrapper) GetKEKID() (string, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
kekID, err := s.Store.Get(keyKEKID)
|
2022-03-22 11:03:15 -04:00
|
|
|
return string(kekID), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutKEKID saves the key encryption key ID to store.
|
|
|
|
func (s StoreWrapper) PutKEKID(kekID string) error {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyKEKID, []byte(kekID))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClusterID returns the unique identifier of the cluster from store.
|
|
|
|
func (s StoreWrapper) GetClusterID() ([]byte, error) {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Get(keyClusterID)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutClusterID saves the unique identifier of the cluster to store.
|
|
|
|
func (s StoreWrapper) PutClusterID(clusterID []byte) error {
|
2022-03-30 08:28:14 -04:00
|
|
|
return s.Store.Put(keyClusterID, clusterID)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:28:14 -04:00
|
|
|
func (s StoreWrapper) InitializeStoreIPs() error {
|
|
|
|
if err := s.PutNextNodeIP(nodeIPRangeStart); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.PutNextCoordinatorIP(coordinatorIPRangeStart)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:28:14 -04:00
|
|
|
// PutNextCoordinatorIP puts the last used ip into the store.
|
|
|
|
func (s StoreWrapper) PutNextCoordinatorIP(ip netip.Addr) error {
|
|
|
|
return s.Store.Put(keyHighestAvailableCoordinatorIP, ip.AsSlice())
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:28:14 -04:00
|
|
|
// getNextCoordinatorIP generates addresses from a /16 subnet.
|
|
|
|
func (s StoreWrapper) getNextCoordinatorIP() (netip.Addr, error) {
|
|
|
|
byteIP, err := s.Store.Get(keyHighestAvailableCoordinatorIP)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-03-30 08:28:14 -04:00
|
|
|
return netip.Addr{}, errors.New("could not obtain IP from store")
|
|
|
|
}
|
|
|
|
ip, ok := netip.AddrFromSlice(byteIP)
|
|
|
|
if !ok {
|
|
|
|
return netip.Addr{}, fmt.Errorf("ip addr malformed %v", byteIP)
|
|
|
|
}
|
|
|
|
if !ip.IsValid() || ip.Compare(coordinatorIPRangeEnd) == 1 {
|
|
|
|
return netip.Addr{}, errors.New("no ips left to assign")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
nextIP := ip.Next()
|
|
|
|
if err := s.PutNextCoordinatorIP(nextIP); err != nil {
|
|
|
|
return netip.Addr{}, errors.New("could not put IP to store")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
return ip, nil
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-03-30 08:28:14 -04:00
|
|
|
// PopNextFreeCoordinatorIP return the next free IP, these could be a old one from a removed peer
|
|
|
|
// or a newly generated IP.
|
|
|
|
func (s StoreWrapper) PopNextFreeCoordinatorIP() (netip.Addr, error) {
|
|
|
|
vpnIP, err := s.getFreedVPNIP(prefixFreeCoordinatorIPs)
|
|
|
|
var noElementsError *store.NoElementsLeftError
|
|
|
|
if errors.As(err, &noElementsError) {
|
|
|
|
return s.getNextCoordinatorIP()
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-03-30 08:28:14 -04:00
|
|
|
return netip.Addr{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
return vpnIP, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutFreedCoordinatorVPNIP puts a already generated VPNIP (IP < highestAvailableCoordinatorIP ),
|
|
|
|
// which is currently unused, into the store.
|
|
|
|
// The IP is saved at a specific prefix and will be used with priority when we
|
|
|
|
// request a new Coordinator IP.
|
|
|
|
func (s StoreWrapper) PutFreedCoordinatorVPNIP(vpnIP string) error {
|
|
|
|
return s.Store.Put(prefixFreeCoordinatorIPs+vpnIP, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutNextNodeIP puts the last used ip into the store.
|
|
|
|
func (s StoreWrapper) PutNextNodeIP(ip netip.Addr) error {
|
|
|
|
return s.Store.Put(keyHighestAvailableNodeIP, ip.AsSlice())
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNextNodeIP generates addresses from a /16 subnet.
|
|
|
|
func (s StoreWrapper) getNextNodeIP() (netip.Addr, error) {
|
|
|
|
byteIP, err := s.Store.Get(keyHighestAvailableNodeIP)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, errors.New("could not obtain IP from store")
|
|
|
|
}
|
|
|
|
ip, ok := netip.AddrFromSlice(byteIP)
|
|
|
|
if !ok {
|
|
|
|
return netip.Addr{}, fmt.Errorf("ip addr malformed %v", byteIP)
|
|
|
|
}
|
|
|
|
if !ip.IsValid() || ip.Compare(nodeIPRangeEnd) == 1 {
|
|
|
|
return netip.Addr{}, errors.New("no ips left to assign")
|
|
|
|
}
|
|
|
|
nextIP := ip.Next()
|
|
|
|
|
|
|
|
if err := s.PutNextNodeIP(nextIP); err != nil {
|
|
|
|
return netip.Addr{}, errors.New("could not put IP to store")
|
|
|
|
}
|
|
|
|
return ip, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PopNextFreeNodeIP return the next free IP, these could be a old one from a removed peer
|
|
|
|
// or a newly generated IP.
|
2022-03-30 08:28:14 -04:00
|
|
|
func (s StoreWrapper) PopNextFreeNodeIP() (netip.Addr, error) {
|
|
|
|
vpnIP, err := s.getFreedVPNIP(prefixFreeNodeIPs)
|
|
|
|
var noElementsError *store.NoElementsLeftError
|
|
|
|
if errors.As(err, &noElementsError) {
|
|
|
|
return s.getNextNodeIP()
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
return vpnIP, nil
|
|
|
|
}
|
2022-03-30 08:28:14 -04:00
|
|
|
|
|
|
|
// PutFreedNodeVPNIP puts a already generated VPNIP (IP < highestAvailableNodeIP ),
|
|
|
|
// which is currently unused, into the store.
|
|
|
|
// The IP is saved at a specific prefix and will be used with priority when we
|
|
|
|
// request a new Node IP.
|
|
|
|
func (s StoreWrapper) PutFreedNodeVPNIP(vpnIP string) error {
|
|
|
|
return s.Store.Put(prefixFreeNodeIPs+vpnIP, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getFreedVPNIP reclaims a VPNIP from the store and removes it from there.
|
|
|
|
func (s StoreWrapper) getFreedVPNIP(prefix string) (netip.Addr, error) {
|
|
|
|
iter, err := s.Store.Iterator(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
}
|
|
|
|
vpnIPWithPrefix, err := iter.GetNext()
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, err
|
|
|
|
}
|
|
|
|
stringVPNIP := strings.TrimPrefix(vpnIPWithPrefix, prefix)
|
|
|
|
vpnIP, err := netip.ParseAddr(stringVPNIP)
|
|
|
|
if err != nil {
|
|
|
|
return netip.Addr{}, fmt.Errorf("ip addr malformed %v, %w", stringVPNIP, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vpnIP, s.Store.Delete(vpnIPWithPrefix)
|
|
|
|
}
|