blob: d7cd7218a627d58a9138ae3146d684e6cbc10c67 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Lorenz Brune306d782021-09-01 13:01:06 +02004// 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.
14package time
15
16import (
17 "context"
18 "fmt"
19 "os/exec"
20 "strconv"
21 "strings"
22
Jan Schär0f8ce4c2025-09-04 13:27:50 +020023 "source.monogon.dev/metropolis/node/allocs"
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020024 "source.monogon.dev/osbase/fileargs"
25 "source.monogon.dev/osbase/supervisor"
Lorenz Brune306d782021-09-01 13:01:06 +020026)
27
28// Service implements the time service. See package documentation for further
29// information.
30type Service struct{}
31
32func New() *Service {
33 return &Service{}
34}
35
36func (s *Service) Run(ctx context.Context) error {
37 // TODO(#72): Apply for a NTP pool vendor zone
38 config := strings.Join([]string{
Leopold Schabel44d6b832021-09-06 22:02:04 +020039 "pool ntp.monogon.dev iburst",
Lorenz Brune306d782021-09-01 13:01:06 +020040 "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ärbdbb9c22024-12-18 15:14:02 +010053 cmd := exec.CommandContext(ctx,
Lorenz Brune306d782021-09-01 13:01:06 +020054 "/time/chrony",
55 "-d",
Jan Schär0f8ce4c2025-09-04 13:27:50 +020056 "-i", strconv.Itoa(allocs.UidTime),
57 "-g", strconv.Itoa(allocs.UidTime),
Lorenz Brune306d782021-09-01 13:01:06 +020058 "-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}