aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/nar/controller.scm
blob: 783fd1203c0751f106e2faac22fffb875bb09862 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-data-service web nar controller)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 iconv)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:use-module (ice-9 binary-ports)
  #:use-module (rnrs bytevectors)
  #:use-module (gcrypt hash)
  #:use-module (gcrypt pk-crypto)
  #:use-module (web uri)
  #:use-module (web request)
  #:use-module (web response)
  #:use-module (guix pki)
  #:use-module (guix base32)
  #:use-module (guix base64)
  #:use-module (guix serialization)
  #:use-module (guix-data-service web render)
  #:use-module (guix-data-service model derivation)
  #:export (nar-controller

            %narinfo-signing-private-key
            %narinfo-signing-public-key))


(define %narinfo-signing-private-key
  (make-parameter #f))

(define %narinfo-signing-public-key
  (make-parameter #f))

(define (nar-controller request
                          method-and-path-components
                          mime-types
                          body
                          conn)
  (define (.narinfo-suffix s)
    (string-suffix? ".narinfo" s))

  (match method-and-path-components
    (('GET "nar" derivation)
     (render-nar request
                 mime-types
                 conn
                 (string-append "/gnu/store/" derivation)))
    (('GET (? .narinfo-suffix path))
     (let* ((hash (string-drop-right
                   path
                   (string-length ".narinfo")))
            (derivation (select-derivation-by-file-name-hash
                         conn
                         hash)))
       (if derivation
           (list (build-response
                  #:code 200
                  #:headers '((content-type . (application/x-narinfo))))
                 (let* ((derivation-file-name
                         (second derivation))
                        (derivation-text
                         (select-serialized-derivation-by-file-name
                          conn
                          derivation-file-name))
                        (derivation-bytevector
                         (string->bytevector derivation-text
                                             "ISO-8859-1"))
                        (derivation-references
                         (select-derivation-references-by-derivation-id
                          conn
                          (first derivation)))
                        (nar-bytevector
                         (call-with-values
                             (lambda ()
                               (open-bytevector-output-port))
                           (lambda (port get-bytevector)
                             (write-file-tree
                              derivation-file-name
                              port
                              #:file-type+size
                              (lambda (file)
                                (values 'regular
                                        (bytevector-length derivation-bytevector)))
                              #:file-port
                              (lambda (file)
                                (open-bytevector-input-port derivation-bytevector)))
                             (get-bytevector)))))
                 (lambda (port)
                   (display (narinfo-string derivation-file-name
                                            nar-bytevector
                                            derivation-references)
                            port))))
           (not-found (request-uri request)))))
    (_ #f)))

(define (render-nar request
                    mime-types
                    conn
                    derivation-file-name)
  (let ((derivation-text
         (select-serialized-derivation-by-file-name
          conn
          derivation-file-name)))
    (if derivation-text
        (let ((derivation-bytevector
               (string->bytevector derivation-text
                                   "ISO-8859-1")))
          (list (build-response
                 #:code 200
                 #:headers '((content-type . (application/x-nix-archive
                                              (charset . "ISO-8859-1")))))
                (lambda (port)
                  (write-file-tree
                   derivation-file-name
                   port
                   #:file-type+size
                   (lambda (file)
                     (values 'regular
                             (bytevector-length derivation-bytevector)))
                   #:file-port
                   (lambda (file)
                     (open-bytevector-input-port derivation-bytevector))))))
        (not-found (request-uri request)))))

(define* (narinfo-string store-item
                         nar-bytevector
                         references
                         #:key
                         (nar-path "nar"))
  (define (signed-string s)
    (let* ((public-key (%narinfo-signing-public-key))
           (hash (bytevector->hash-data (sha256 (string->utf8 s))
                                        #:key-type (key-type public-key))))
      (signature-sexp hash (%narinfo-signing-private-key) public-key)))

  (let* ((hash       (bytevector->nix-base32-string
                      (sha256 nar-bytevector)))
         (size       (bytevector-length nar-bytevector))
         (references (string-join
                      (map basename references)
                      " "))
         (info (format #f
                             "\
StorePath: ~a
URL: ~a
Compression: none
NarHash: sha256:~a
NarSize: ~d
References: ~a~%"
                             store-item
                             (encode-and-join-uri-path
                              (list nar-path
                                    (basename store-item)))
                             hash
                             size
                             references)))
    (if (%narinfo-signing-private-key)
        (format #f "~aSignature: 1;~a;~a~%"
                info
                (gethostname)
                (base64-encode
                 (string->utf8
                  (canonical-sexp->string (signed-string info)))))
        info)))