http-boot-config/config/remote/enroll.go

103 lines
3 KiB
Go
Raw Normal View History

package remote
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
2023-08-22 20:39:23 +00:00
"net"
"net/http"
"git.faercol.me/faercol/http-boot-config/config/logger"
"git.faercol.me/faercol/http-boot-config/config/prober"
"github.com/google/uuid"
)
const enrollURL = "/enroll"
type enrollEFIOption struct {
2023-09-01 16:45:22 +00:00
Name string `json:"name"`
DevicePath string `json:"device_path"`
}
type enrollPayload struct {
Name string `json:"name"`
Options map[string]enrollEFIOption `json:"options"`
SelectedOption string `json:"selected_option"`
}
2023-08-22 20:39:23 +00:00
type enrollRespPayloadJSON struct {
ID string `json:"ID"`
MulticastGroup string `json:"multicast_group"`
MulticastPort int `json:"multicast_port"`
}
2023-08-22 20:39:23 +00:00
type EnrollConfig struct {
ID uuid.UUID
MulticastGroup net.IP
MulticastPort int
}
func enrollToServer(ctx context.Context, host string, name string, apps []prober.EfiApp) (EnrollConfig, error) {
payload := enrollPayload{
Name: name,
Options: make(map[string]enrollEFIOption),
}
for _, a := range apps {
appID := uuid.New()
2023-09-01 16:45:22 +00:00
payload.Options[appID.String()] = enrollEFIOption{Name: a.Name, DevicePath: a.DevicePath}
if a.Active {
payload.SelectedOption = appID.String()
}
}
dat, err := json.Marshal(payload)
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to serialize payload: %w", err)
}
subCtx, cancel := context.WithTimeout(ctx, reqTimeout)
defer cancel()
req, err := http.NewRequestWithContext(subCtx, http.MethodPost, host+enrollURL, bytes.NewBuffer(dat))
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to initialize request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to do query to remote boot server: %w", err)
}
if resp.StatusCode != http.StatusOK {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("unexpected returncode %d", resp.StatusCode)
}
2023-08-22 20:39:23 +00:00
var respPayload enrollRespPayloadJSON
respDat, err := io.ReadAll(resp.Body)
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to read response from server: %w", err)
}
if err := json.Unmarshal(respDat, &respPayload); err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to deserialize data from server: %w", err)
}
newID, err := uuid.Parse(respPayload.ID)
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("invalid UUID %q from server: %w", respPayload.ID, err)
}
2023-08-22 20:39:23 +00:00
return EnrollConfig{ID: newID, MulticastGroup: net.ParseIP(respPayload.MulticastGroup), MulticastPort: respPayload.MulticastPort}, nil
}
2023-08-22 20:39:23 +00:00
func Enroll(ctx context.Context, host, name string, l *logger.SimpleLogger) (EnrollConfig, error) {
apps, err := prober.GetEFIApps()
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to get list of available EFI applications: %w", err)
}
2023-08-22 20:39:23 +00:00
newConf, err := enrollToServer(ctx, host, name, apps)
if err != nil {
2023-08-22 20:39:23 +00:00
return EnrollConfig{}, fmt.Errorf("failed to enroll client to remote host: %w", err)
}
2023-08-22 20:39:23 +00:00
l.Infof("Successfully enrolled new client, ID is %q", newConf.ID.String())
return newConf, nil
}