diff --git a/cmd/fleet/serve.go b/cmd/fleet/serve.go index 78279167ea..ad85bf7ad3 100644 --- a/cmd/fleet/serve.go +++ b/cmd/fleet/serve.go @@ -711,13 +711,7 @@ func runServeCmd(cmd *cobra.Command, configManager configpkg.Manager, debug, dev } if len(toInsert) > 0 { - if !config.MDM.IsAppleAPNsSet() { - initFatal(errors.New("Apple APNs MDM configuration must be provided when Apple SCEP is provided"), - "validate Apple MDM") - } else if !config.MDM.IsAppleSCEPSet() { - initFatal(errors.New("Apple SCEP MDM configuration must be provided when Apple APNs is provided"), - "validate Apple MDM") - } + config.MDM.ValidateAppleAPNSAndSCEPPair(initFatal) // parse the APNs and SCEP assets from the config _, apnsCertPEM, apnsKeyPEM, err := config.MDM.AppleAPNs() diff --git a/server/config/config.go b/server/config/config.go index 2ece59edf7..b5ece1c320 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -1045,6 +1045,20 @@ func (m *MDMConfig) IsAppleBMSet() bool { return pair.IsSet() || m.AppleBMServerToken != "" || m.AppleBMServerTokenBytes != "" } +// ValidateAppleAPNSAndSCEPPair enforces that Apple APNs and SCEP are +// configured together — neither half of the pair is usable on its own. +// Callers should gate this on a precondition that at least one side is set +// (the outer Apple-MDM init flow handles that today). +func (m *MDMConfig) ValidateAppleAPNSAndSCEPPair(initFatal func(err error, msg string)) { + if !m.IsAppleAPNsSet() { + initFatal(errors.New("Apple APNs MDM configuration must be provided when Apple SCEP is provided"), + "validate Apple MDM") + } else if !m.IsAppleSCEPSet() { + initFatal(errors.New("Apple SCEP MDM configuration must be provided when Apple APNs is provided"), + "validate Apple MDM") + } +} + // AppleAPNs returns the parsed TLS certificate for Apple APNs. func (m *MDMConfig) AppleAPNs() (cert *tls.Certificate, pemCert, pemKey []byte, err error) { if m.appleAPNs == nil { diff --git a/server/config/config_test.go b/server/config/config_test.go index d8036824a8..15115e29d7 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -1041,3 +1041,28 @@ func TestServerConfigURLPrefix(t *testing.T) { require.True(t, called) }) } + +func TestMDMConfigValidateAppleAPNSAndSCEPPair(t *testing.T) { + t.Parallel() + + t.Run("both APNs and SCEP set is valid", func(t *testing.T) { + cfg := MDMConfig{AppleAPNsCert: "apns.cert", AppleSCEPCert: "scep.cert"} + cfg.ValidateAppleAPNSAndSCEPPair(func(err error, msg string) { + t.Fatalf("unexpected error: %v", err) + }) + }) + + t.Run("SCEP set without APNs is rejected", func(t *testing.T) { + cfg := MDMConfig{AppleSCEPCert: "scep.cert"} + called := false + cfg.ValidateAppleAPNSAndSCEPPair(func(err error, msg string) { called = true }) + require.True(t, called) + }) + + t.Run("APNs set without SCEP is rejected", func(t *testing.T) { + cfg := MDMConfig{AppleAPNsCert: "apns.cert"} + called := false + cfg.ValidateAppleAPNSAndSCEPPair(func(err error, msg string) { called = true }) + require.True(t, called) + }) +}