Compare commits

..

2 commits

Author SHA1 Message Date
8f1897da97 Correctly return the created UUID on enrollment
All checks were successful
continuous-integration/drone/push Build is passing
2023-08-13 22:34:12 +02:00
016dc7fe5b Improve logging 2023-08-13 22:33:57 +02:00
2 changed files with 22 additions and 8 deletions

View file

@ -14,6 +14,10 @@ import (
const EnrollRoute = "/enroll" const EnrollRoute = "/enroll"
type newClientPayload struct {
ID string `json:"ID"`
}
type EnrollController struct { type EnrollController struct {
clientService *services.ClientHandlerService clientService *services.ClientHandlerService
l *logrus.Logger l *logrus.Logger
@ -26,30 +30,39 @@ func NewEnrollController(l *logrus.Logger, service *services.ClientHandlerServic
} }
} }
func (ec *EnrollController) enrollMachine(w http.ResponseWriter, r *http.Request) (int, error) { func (ec *EnrollController) enrollMachine(w http.ResponseWriter, r *http.Request) (int, []byte, error) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
return http.StatusMethodNotAllowed, nil return http.StatusMethodNotAllowed, nil, nil
} }
dat, err := io.ReadAll(r.Body) dat, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to read body: %w", err) return http.StatusInternalServerError, nil, fmt.Errorf("failed to read body: %w", err)
} }
var client bootoption.Client var client bootoption.Client
if err := json.Unmarshal(dat, &client); err != nil { if err := json.Unmarshal(dat, &client); err != nil {
return http.StatusInternalServerError, fmt.Errorf("failed to parse body: %w", err) return http.StatusInternalServerError, nil, fmt.Errorf("failed to parse body: %w", err)
}
cltID, err := ec.clientService.AddClient(&client)
if err != nil {
return http.StatusInternalServerError, nil, fmt.Errorf("failed to create client %w", err)
}
payload, err := json.Marshal(newClientPayload{ID: cltID.String()})
if err != nil {
return http.StatusInternalServerError, nil, fmt.Errorf("failed to serialize payload: %w", err)
} }
ec.clientService.AddClient(&client)
ec.l.Infof("Added client") ec.l.Infof("Added client")
return http.StatusAccepted, nil return http.StatusOK, payload, nil
} }
func (ec *EnrollController) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (ec *EnrollController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
returncode, err := ec.enrollMachine(w, r) returncode, content, err := ec.enrollMachine(w, r)
if err != nil { if err != nil {
ec.l.Errorf("Error handling client enrollement: %s", err.Error()) ec.l.Errorf("Error handling client enrollement: %s", err.Error())
} }
helpers.HandleResponse(w, r, returncode, nil, ec.l) helpers.HandleResponse(w, r, returncode, content, ec.l)
} }

View file

@ -120,6 +120,7 @@ func (l *UDPListener) listen() (*udpMessage, error) {
return nil, fmt.Errorf("UDP packet too big (%d/%d)", n, bufferLength) return nil, fmt.Errorf("UDP packet too big (%d/%d)", n, bufferLength)
} }
l.log.Debugf("Parsing UDP message %q", bytes.Trim(buffer, "\x00"))
parsedMsg, err := bootprotocol.MessageFromBytes(bytes.Trim(buffer, "\x00")) parsedMsg, err := bootprotocol.MessageFromBytes(bytes.Trim(buffer, "\x00"))
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse message: %w", err) return nil, fmt.Errorf("failed to parse message: %w", err)