| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Lorenz Brun | e306d78 | 2021-09-01 13:01:06 +0200 | [diff] [blame] | 4 | // Package time implements a supervisor runnable which is responsible for |
| 5 | // keeping both the system clock and the RTC accurate. |
| 6 | // Metropolis nodes need accurate time both for themselves (for log |
| 7 | // timestamping, validating certain certificates, ...) as well as workloads |
| 8 | // running on top of it expecting accurate time. |
| 9 | // This initial implementation is very minimalistic, running just a stateless |
| 10 | // NTP client per node for the whole lifecycle of it. |
| 11 | // This implementation is simple, but is fairly unsafe as NTP by itself does |
| 12 | // not offer any cryptography, so it's easy to tamper with the responses. |
| 13 | // See #73 for further work in that direction. |
| 14 | package time |
| 15 | |
| 16 | import ( |
| 17 | "context" |
| 18 | "fmt" |
| 19 | "os/exec" |
| 20 | "strconv" |
| 21 | "strings" |
| 22 | |
| 23 | "source.monogon.dev/metropolis/node" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 24 | "source.monogon.dev/osbase/fileargs" |
| 25 | "source.monogon.dev/osbase/supervisor" |
| Lorenz Brun | e306d78 | 2021-09-01 13:01:06 +0200 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // Service implements the time service. See package documentation for further |
| 29 | // information. |
| 30 | type Service struct{} |
| 31 | |
| 32 | func New() *Service { |
| 33 | return &Service{} |
| 34 | } |
| 35 | |
| 36 | func (s *Service) Run(ctx context.Context) error { |
| 37 | // TODO(#72): Apply for a NTP pool vendor zone |
| 38 | config := strings.Join([]string{ |
| Leopold Schabel | 44d6b83 | 2021-09-06 22:02:04 +0200 | [diff] [blame] | 39 | "pool ntp.monogon.dev iburst", |
| Lorenz Brun | e306d78 | 2021-09-01 13:01:06 +0200 | [diff] [blame] | 40 | "bindcmdaddress /", |
| 41 | "stratumweight 0.01", |
| 42 | "leapsecmode slew", |
| 43 | "maxslewrate 10000", |
| 44 | "makestep 2.0 3", |
| 45 | "rtconutc", |
| 46 | "rtcsync", |
| 47 | }, "\n") |
| 48 | args, err := fileargs.New() |
| 49 | if err != nil { |
| 50 | return fmt.Errorf("cannot create fileargs: %w", err) |
| 51 | } |
| 52 | defer args.Close() |
| Jan Schär | bdbb9c2 | 2024-12-18 15:14:02 +0100 | [diff] [blame] | 53 | cmd := exec.CommandContext(ctx, |
| Lorenz Brun | e306d78 | 2021-09-01 13:01:06 +0200 | [diff] [blame] | 54 | "/time/chrony", |
| 55 | "-d", |
| 56 | "-i", strconv.Itoa(node.TimeUid), |
| 57 | "-g", strconv.Itoa(node.TimeUid), |
| 58 | "-f", args.ArgPath("chrony.conf", []byte(config)), |
| 59 | ) |
| 60 | cmd.Stdout = supervisor.RawLogger(ctx) |
| 61 | cmd.Stderr = supervisor.RawLogger(ctx) |
| 62 | return supervisor.RunCommand(ctx, cmd) |
| 63 | } |