Skip to content

Commit

Permalink
Use new wireguard-ips API for finding IP
Browse files Browse the repository at this point in the history
The original method we used can't work on large numbers of devices. This
change uses a new API from the server to more efficiently find a free IP
address.

Signed-off-by: Andy Doan <[email protected]>
  • Loading branch information
doanac committed May 27, 2021
1 parent 13105d6 commit f886034
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
17 changes: 17 additions & 0 deletions client/foundries.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,12 @@ type WaveStatus struct {
OtherGroups []RolloutGroupStatus `json:"other-groups"`
}

type WireGuardIp struct {
Name string `json:"name"`
Ip string `json:"ip"`
Enabled bool `json:"enabled"`
}

// This is an error returned in case if we've successfully received an HTTP response which contains
// an unexpected HTTP status code
type HttpError struct {
Expand Down Expand Up @@ -981,6 +987,17 @@ func (a *Api) GetFoundriesTargetsKey(factory string) (*AtsKey, error) {
return &key, err
}

func (a *Api) GetWireGuardIps(factory string) ([]WireGuardIp, error) {
url := a.serverUrl + "/ota/factories/" + factory + "/wireguard-ips/"
body, err := a.Get(url)
if err != nil {
return nil, err
}
var ips []WireGuardIp
err = json.Unmarshal(*body, &ips)
return ips, err
}

func (a *Api) tufRootGet(factory string, prod bool, version int) (*AtsTufRoot, error) {
url := a.serverUrl + "/ota/repo/" + factory + "/api/v1/user_repo/"
if version > 0 {
Expand Down
34 changes: 7 additions & 27 deletions subcommands/devices/config_wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,36 +88,16 @@ func ipToUint32(ipaddr string) (uint32, error) {
// Create a dictionary of device VPN addresses in the factory
func factoryIps(factory string) map[uint32]bool {
ips := make(map[uint32]bool)

var dl *client.DeviceList
for {
var err error
if dl == nil {
dl, err = api.DeviceList(true, "", factory, "", "", "", 1, 1000)
ipList, err := api.GetWireGuardIps(factory)
subcommands.DieNotNil(err)
for _, item := range ipList {
ip, err := ipToUint32(item.Ip)
if err != nil {
logrus.Errorf("Unable to compute VPN Address for %s - %s", item.Name, item.Ip)
} else {
if dl.Next != nil {
dl, err = api.DeviceListCont(*dl.Next)
} else {
break
}
}
subcommands.DieNotNil(err)
for _, device := range dl.Devices {
// TODO - we need to come up with a backend API that
// won't require an API call per device. Maybe:
// api.foundries.io/ota/device-configs/?file=wireguard-client
wcc := loadWireguardClientConfig(device.Name)
if len(wcc.Address) > 0 {
ip, err := ipToUint32(wcc.Address)
if err != nil {
logrus.Errorf("Unable to compute VPN Address for %s - %s", device.Name, wcc.Address)
} else {
ips[ip] = true
}
}
ips[ip] = true
}
}

return ips
}

Expand Down

0 comments on commit f886034

Please sign in to comment.