Skip to content

Commit

Permalink
Fix error on MySQL backend when bsToken null
Browse files Browse the repository at this point in the history
This commit fixes an error when the bootstrap token for a device is NULL. Apple
specifies[0] the behaviour of `GetBootstrapToken` to return an empty body when
there is no bootstrap token for the device. This behaviour has been conformed
to.

[0]: https://developer.apple.com/documentation/devicemanagement/get_bootstrap_token
  • Loading branch information
Calvin Lee committed Jan 10, 2023
1 parent c90f186 commit be1171e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 7 additions & 3 deletions service/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ func CheckinRequest(svc Checkin, r *mdm.Request, bodyBytes []byte) ([]byte, erro
err = fmt.Errorf("setbootstraptoken service: %w", err)
}
case *mdm.GetBootstrapToken:
// https://developer.apple.com/documentation/devicemanagement/get_bootstrap_token
var bsToken *mdm.BootstrapToken
bsToken, err = svc.GetBootstrapToken(r, m)
if err != nil {
err = fmt.Errorf("getbootstraptoken service: %w", err)
break
}
respBytes, err = plist.Marshal(bsToken)
if err != nil {
err = fmt.Errorf("marshal bootstrap token: %w", err)
// If there is no bsToken, return an empty body
if bsToken != nil {
respBytes, err = plist.Marshal(bsToken)
if err != nil {
err = fmt.Errorf("marshal bootstrap token: %w", err)
}
}
case *mdm.DeclarativeManagement:
respBytes, err = svc.DeclarativeManagement(r, m)
Expand Down
8 changes: 5 additions & 3 deletions storage/mysql/bstoken.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mysql

import (
"database/sql"

"github.com/micromdm/nanomdm/mdm"
)

Expand All @@ -18,17 +20,17 @@ func (s *MySQLStorage) StoreBootstrapToken(r *mdm.Request, msg *mdm.SetBootstrap
}

func (s *MySQLStorage) RetrieveBootstrapToken(r *mdm.Request, _ *mdm.GetBootstrapToken) (*mdm.BootstrapToken, error) {
var tokenB64 string
var tokenB64 sql.NullString
err := s.db.QueryRowContext(
r.Context,
`SELECT bootstrap_token_b64 FROM devices WHERE id = ?;`,
r.ID,
).Scan(&tokenB64)
if err != nil {
if err != nil || !tokenB64.Valid {
return nil, err
}
bsToken := new(mdm.BootstrapToken)
err = bsToken.SetTokenString(tokenB64)
err = bsToken.SetTokenString(tokenB64.String)
if err == nil {
err = s.updateLastSeen(r)
}
Expand Down

0 comments on commit be1171e

Please sign in to comment.