Add --check flag to mdmproxy (#21094)

This can be used to check the migration status for a given UDID. See the
README updates for more.
This commit is contained in:
Zach Wasserman
2024-08-07 10:18:39 -07:00
committed by GitHub
parent 3b34bbf1ef
commit ae211a3966
2 changed files with 50 additions and 24 deletions
+15 -1
View File
@@ -25,4 +25,18 @@ Usage of ./mdmproxy:
### Example invocation
```
mdmproxy --migrate-udids '' --auth-token foo --existing-url https://3.14.233.249 --existing-hostname micromdm.example.com --fleet-url https://example.cloud.fleetdm.com --migrate-percentage 0
```
```
### Check migration status
To check the migration status for a given UDID, provide the `--migrate-udids` and
`--migrate-percentage` flags with the `--check` flag:
```
$ go run . --migrate-percentage=50 --check E5C6DBBA-D5CC-4DB6-9560-995F17FB7A59
E5C6DBBA-D5CC-4DB6-9560-995F17FB7A59 IS NOT migrated
$ go run . --migrate-percentage=50 --check 575424CB-09D7-4CAD-8A7A-D3511FE8A7E2
575424CB-09D7-4CAD-8A7A-D3511FE8A7E2 IS migrated
```
When the `--check` flag is used, the program prints the migration status and exits. The server is not started.
+35 -23
View File
@@ -12,6 +12,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"
"strings"
"sync"
@@ -321,35 +322,13 @@ func main() {
serverAddr := flag.String("server-address", ":8080", "Address for server to listen on")
debug := flag.Bool("debug", false, "Enable debug logging")
logSkipped := flag.Bool("log-skipped", false, "Log skipped requests (usually from web scanners)")
check := flag.String("check", "", "Print whether the specified UDID is migrated with the current configuration, then exit")
flag.Parse()
// Check required flags
if *existingURL == "" {
log.Fatal("--existing-url must be set")
}
if *existingHostname == "" {
log.Fatal("--existing-hostname must be set")
}
if *fleetURL == "" {
log.Fatal("--fleet-url must be set")
}
udids, err := processUDIDs(bytes.NewBufferString(*migrateUDIDs))
if err != nil {
panic(err)
}
log.Printf("--migrate-udids set: %v", udids)
log.Printf("--migrate-percentage set: %d", *migratePercentage)
log.Printf("--existing-url set: %s", *existingURL)
log.Printf("--existing-hostname set: %s", *existingHostname)
log.Printf("--fleet-url set: %s", *fleetURL)
log.Printf("--debug set: %v", *debug)
log.Printf("--log-skipped set: %v", *logSkipped)
if *authToken != "" {
log.Printf("--auth-token set. Remote configuration enabled.")
} else {
log.Printf("--auth-token is empty. Remote configuration disabled.")
}
proxy := mdmProxy{
token: *authToken,
@@ -364,6 +343,39 @@ func main() {
logSkipped: *logSkipped,
}
if len(*check) > 0 {
if proxy.isUDIDMigrated(*check) {
fmt.Printf("%s IS migrated\n", *check)
} else {
fmt.Printf("%s IS NOT migrated\n", *check)
}
os.Exit(0)
}
// Check required flags
if *existingURL == "" {
log.Fatal("--existing-url must be set")
}
if *existingHostname == "" {
log.Fatal("--existing-hostname must be set")
}
if *fleetURL == "" {
log.Fatal("--fleet-url must be set")
}
log.Printf("--migrate-udids set: %v", udids)
log.Printf("--migrate-percentage set: %d", *migratePercentage)
log.Printf("--existing-url set: %s", *existingURL)
log.Printf("--existing-hostname set: %s", *existingHostname)
log.Printf("--fleet-url set: %s", *fleetURL)
log.Printf("--debug set: %v", *debug)
log.Printf("--log-skipped set: %v", *logSkipped)
if *authToken != "" {
log.Printf("--auth-token set. Remote configuration enabled.")
} else {
log.Printf("--auth-token is empty. Remote configuration disabled.")
}
mux := http.NewServeMux()
// Health check endpoint used for load balancers
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {