about summary refs log tree commit diff
path: root/genenetwork/services/container-ssh.scm
blob: 1553bc2dc2b00864d306a206b239777c9b20fca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
;;; genenetwork-machines --- Guix configuration for genenetwork machines
;;;
;;; Shared SSH support for the minimal system containers run by the
;;; run-guix-container daemon (bnw-container.scm, cgit-container.scm,
;;; ...).  It gives a container password-less root login with a pinned
;;; client key, a pinned SSH host key so every rebuild presents the
;;; same key (no known_hosts complaints), and the directories sshd
;;; needs in a minimal container.

(define-module (genenetwork services container-ssh)
  #:use-module (gnu)
  #:use-module (guix gexp)
  #:use-module (gnu services ssh)
  #:export (container-ssh-service))

;; The pinned keys live outside the repository, directly in $HOME
;; (see the Keys section of genenetwork/containers/cgit-container.scm)
;; and are fetched from there at build time:
#!
     ssh-keygen -t ecdsa   -f ~/ssh-container-key   -N ""
     ssh-keygen -t ed25519 -f ~/hello-container-key -N ""
!#
;; ssh-container-key is the pinned SSH *host* key the containers
;; present; hello-container-key authorizes password-less root logins.
(define %key-directory (getenv "HOME"))

(define* (container-ssh-service #:key
                                (port-number 2222)
                                (host-key (local-file
                                           (string-append %key-directory
                                                          "/ssh-container-key")
                                           "ssh_host_ecdsa_key"))
                                (host-key.pub (local-file
                                               (string-append %key-directory
                                                              "/ssh-container-key.pub")
                                               "ssh_host_ecdsa_key.pub"))
                                (authorized-key (local-file
                                                 (string-append %key-directory
                                                                "/hello-container-key.pub")
                                                 "authorized_keys")))
  "Return the services that give a minimal Guix system container SSH
on PORT-NUMBER: an openssh service with password-less root login for
AUTHORIZED-KEY, the pinned ECDSA HOST-KEY installed into /etc/ssh
(generate-host-keys? is #f, so every rebuild presents the same host
key), and the /var/empty and /var/run/sshd directories sshd needs when
the container has no system accounts.

This is a service PROCEDURE (returning a list of services) rather than
a service-type on purpose: openssh-service-type has no extension point
that merges configurations, so a wrapping service-type would have to
re-implement sshd's shepherd, activation and account services."
  (list
   ;; Password-less root login with the client key.  Host keys are
   ;; pinned, not generated at boot.
   (service openssh-service-type
            (openssh-configuration
             (port-number port-number)
             (permit-root-login #t)
             (password-authentication? #f)
             (generate-host-keys? #f)
             (authorized-keys `(("root" ,authorized-key)))))

   ;; Install the pinned ECDSA host key into /etc/ssh where sshd
   ;; looks for it by default.
   (simple-service 'container-ssh-host-key
                   activation-service-type
                   (with-imported-modules '((guix build utils))
                     #~(begin
                         (use-modules (guix build utils))
                         (mkdir-p "/etc/ssh")
                         (copy-file #$host-key
                                    "/etc/ssh/ssh_host_ecdsa_key")
                         (chmod "/etc/ssh/ssh_host_ecdsa_key" #o600)
                         (copy-file #$host-key.pub
                                    "/etc/ssh/ssh_host_ecdsa_key.pub")
                         (chmod "/etc/ssh/ssh_host_ecdsa_key.pub" #o644))))

   ;; sshd needs the privilege separation directory /var/empty; on
   ;; a full system it is created as the home directory of some
   ;; system account, but a minimal container has none.  The sshd
   ;; privsep user's home is /var/run/sshd and is likewise missing
   ;; in a minimal container.
   ;;
   ;; /var/empty must end up root-owned and read-only: services
   ;; whose system users have it as the home directory (e.g. the
   ;; stock nginx-service-type) make guix's activation chown it to
   ;; that user -- correct on a full system, where /var/empty is
   ;; shared by several accounts and activation hands it back to
   ;; root, but fatal for sshd on a minimal container where the
   ;; service user is the only one ("must be owned by root and not
   ;; group or world-writable").  This snippet runs after the
   ;; account activation, so resetting the ownership here holds.
   (simple-service 'container-var-empty
                   activation-service-type
                   (with-imported-modules '((guix build utils))
                     #~(begin
                         (use-modules (guix build utils))
                         (mkdir-p "/var/empty")
                         (mkdir-p "/var/run/sshd")
                         (chown "/var/empty" 0 0)
                         (chmod "/var/empty" #o555))))))