-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added enrollment migration (nano2nano)
- Loading branch information
1 parent
b725d03
commit f99f83a
Showing
11 changed files
with
338 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/nanomdm-* | ||
/nano2nano-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
stdlog "log" | ||
"net/http" | ||
|
||
"github.com/jessepeterson/nanomdm/cmd/cli" | ||
"github.com/jessepeterson/nanomdm/log/stdlogfmt" | ||
"github.com/jessepeterson/nanomdm/mdm" | ||
) | ||
|
||
// overridden by -ldflags -X | ||
var version = "unknown" | ||
|
||
func main() { | ||
cliStorage := cli.NewStorage() | ||
flag.Var(&cliStorage.Storage, "storage", "name of storage system") | ||
flag.Var(&cliStorage.DSN, "dsn", "data source name (e.g. connection string or path)") | ||
var ( | ||
flVersion = flag.Bool("version", false, "print version") | ||
flDebug = flag.Bool("debug", false, "log debug messages") | ||
flURL = flag.String("url", "", "NanoMDM migration URL") | ||
flAPIKey = flag.String("key", "", "NanoMDM API Key") | ||
) | ||
flag.Parse() | ||
|
||
if *flVersion { | ||
fmt.Println(version) | ||
return | ||
} | ||
|
||
logger := stdlogfmt.New(stdlog.Default(), *flDebug) | ||
|
||
var skipServer bool | ||
if *flURL == "" || *flAPIKey == "" { | ||
logger.Info("msg", "URL or API key not set; not sending server requests") | ||
skipServer = true | ||
} | ||
client := http.DefaultClient | ||
|
||
mdmStorage, err := cliStorage.Parse(logger) | ||
if err != nil { | ||
stdlog.Fatal(err) | ||
} | ||
|
||
checkins := make(chan interface{}) | ||
ctx := context.Background() | ||
go func() { | ||
// dispatch to our storage backend to start sending the checkins | ||
// channel our MDM check-in messages. | ||
if err := mdmStorage.RetrieveMigrationCheckins(ctx, checkins); err != nil { | ||
logger.Info( | ||
"msg", "retrieving migration checkins", | ||
"err", err, | ||
) | ||
} | ||
close(checkins) | ||
}() | ||
|
||
// because order matters (a lot) we are purposefully single threaded for now. | ||
for checkin := range checkins { | ||
switch v := checkin.(type) { | ||
case *mdm.Authenticate: | ||
logger.Info(logsFromEnrollment("Authenticate", &v.Enrollment)...) | ||
if !skipServer { | ||
if err := httpPut(client, *flURL, *flAPIKey, v.Raw); err != nil { | ||
logger.Info("msg", "sending to migration endpoint", "err", err) | ||
} | ||
} | ||
case *mdm.TokenUpdate: | ||
logger.Info(logsFromEnrollment("TokenUpdate", &v.Enrollment)...) | ||
if !skipServer { | ||
if err := httpPut(client, *flURL, *flAPIKey, v.Raw); err != nil { | ||
logger.Info("msg", "sending to migration endpoint", "err", err) | ||
} | ||
} | ||
case error: | ||
logger.Info("msg", "receiving checkin", "err", v) | ||
default: | ||
logger.Info("msg", "invalid type provided") | ||
} | ||
} | ||
} | ||
|
||
func logsFromEnrollment(checkin string, e *mdm.Enrollment) []interface{} { | ||
r := e.Resolved() | ||
logs := []interface{}{ | ||
"checkin", checkin, | ||
"device_id", r.DeviceChannelID, | ||
} | ||
if r.UserChannelID != "" { | ||
logs = append(logs, "user_id", r.UserChannelID) | ||
} | ||
if e.UserShortName != "" { | ||
logs = append(logs, "user_short_name", e.UserShortName) | ||
} | ||
logs = append(logs, "type", r.Type.String()) | ||
return logs | ||
} | ||
|
||
func httpPut(client *http.Client, url string, key string, sendBytes []byte) error { | ||
if url == "" || key == "" { | ||
return errors.New("no URL or API key") | ||
} | ||
req, err := http.NewRequest("PUT", url, bytes.NewReader(sendBytes)) | ||
if err != nil { | ||
return err | ||
} | ||
req.SetBasicAuth("nanomdm", key) | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
_, err = ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return err | ||
} | ||
if res.StatusCode != 200 { | ||
return fmt.Errorf("Check-in Request failed with HTTP status: %d", res.StatusCode) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ type AllStorage interface { | |
PushCertStore | ||
CommandEnqueuer | ||
CertAuthStore | ||
StoreMigrator | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package allmulti | ||
|
||
import "context" | ||
|
||
func (ms *MultiAllStorage) RetrieveMigrationCheckins(ctx context.Context, c chan<- interface{}) error { | ||
ms.logger.Info("msg", "only using first store for migration") | ||
return ms.stores[0].RetrieveMigrationCheckins(ctx, c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package file | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/jessepeterson/nanomdm/mdm" | ||
) | ||
|
||
func sendCheckinMessage(e *enrollment, filename string, c chan<- interface{}) { | ||
msgBytes, err := e.readFile(filename) | ||
if err != nil { | ||
c <- err | ||
return | ||
} | ||
msg, err := mdm.DecodeCheckin(msgBytes) | ||
if err != nil { | ||
c <- err | ||
return | ||
} | ||
c <- msg | ||
} | ||
|
||
func (s *FileStorage) RetrieveMigrationCheckins(_ context.Context, c chan<- interface{}) error { | ||
for _, userLoop := range []bool{false, true} { | ||
entries, err := os.ReadDir(s.path) | ||
if err != nil { | ||
return err | ||
} | ||
for _, entry := range entries { | ||
if !entry.IsDir() { | ||
continue | ||
} | ||
e := s.newEnrollment(entry.Name()) | ||
authExists, err := e.fileExists(AuthenticateFilename) | ||
if err != nil { | ||
c <- err | ||
} | ||
// if an Authenticate doesn't exist then this is a | ||
// user-channel enrollment. skip it for this loop | ||
if !userLoop && !authExists { | ||
continue | ||
} | ||
if !userLoop { | ||
sendCheckinMessage(e, AuthenticateFilename, c) | ||
} | ||
tokExists, err := e.fileExists(TokenUpdateFilename) | ||
if err != nil { | ||
c <- err | ||
} | ||
// if neither an authenticate nor tokenupdate exists then | ||
// this is an invalid enrollment and we should skip it | ||
if !tokExists && !authExists { | ||
continue | ||
} | ||
// TODO: if we have an UnlockToken for a device we | ||
// should synthesize it into a TokenUpdate message because | ||
// they are saved out-of-band. | ||
sendCheckinMessage(e, TokenUpdateFilename, c) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package mysql | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/jessepeterson/nanomdm/mdm" | ||
) | ||
|
||
func (s *MySQLStorage) RetrieveMigrationCheckins(ctx context.Context, c chan<- interface{}) error { | ||
// TODO: if a TokenUpdate does not include the latest UnlockToken | ||
// then we should synthesize a TokenUpdate to transfer it over. | ||
deviceRows, err := s.db.QueryContext( | ||
ctx, | ||
`SELECT authenticate, token_update FROM devices;`, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer deviceRows.Close() | ||
for deviceRows.Next() { | ||
var authBytes, tokenBytes []byte | ||
if err := deviceRows.Scan(&authBytes, &tokenBytes); err != nil { | ||
return err | ||
} | ||
for _, msgBytes := range [][]byte{authBytes, tokenBytes} { | ||
msg, err := mdm.DecodeCheckin(msgBytes) | ||
if err != nil { | ||
c <- err | ||
} else { | ||
c <- msg | ||
} | ||
} | ||
} | ||
if err = deviceRows.Err(); err != nil { | ||
return err | ||
} | ||
userRows, err := s.db.QueryContext( | ||
ctx, | ||
`SELECT token_update FROM users;`, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer userRows.Close() | ||
for userRows.Next() { | ||
var msgBytes []byte | ||
if err := userRows.Scan(&msgBytes); err != nil { | ||
return err | ||
} | ||
msg, err := mdm.DecodeCheckin(msgBytes) | ||
if err != nil { | ||
c <- err | ||
} else { | ||
c <- msg | ||
} | ||
} | ||
if err = userRows.Err(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.