aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/nar/controller.scm
blob: e2ace7a1e12030f84d2922850e4175597a26d4d1 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
;;; 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 utils)
  #:use-module (guix-data-service database)
  #:use-module (guix-data-service web render)
  #:use-module (guix-data-service web controller)
  #:use-module (guix-data-service web nar html)
  #: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 %nix-cache-info
  `(("StoreDir" . "/gnu/store")
    ("WantMassQuery" . 0)
    ("Priority" . 100)))

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

  (match method-and-path-components
    (('GET "nix-cache-info")
     (render-text
      (string-concatenate
       (map (match-lambda
              ((key . value)
               (format #f "~a: ~a~%" key value)))
            %nix-cache-info))))
    (('GET "substitutes")
     (render-html
      #:sxml (view-substitutes (%narinfo-signing-public-key))))
    (('GET "nar" _)
     ;; These routes are a little special, as the extensions aren't used for
     ;; content negotiation, so just use the path from the request
     (let* ((path (uri-path (request-uri request)))
            (file-name
             (uri-decode (last (string-split path #\/)))))
       (render-nar request
                   mime-types
                   (string-append "/gnu/store/" file-name))))
    (('GET "nar" "lzip" _)
     ;; These routes are a little special, as the extensions aren't used for
     ;; content negotiation, so just use the path from the request
     (let* ((path (uri-path (request-uri request)))
            (file-name
             (uri-decode (last (string-split path #\/)))))
       (render-lzip-nar request
                        mime-types
                        (string-append "/gnu/store/" file-name))))
    (('GET (? .narinfo-suffix path))
     (render-narinfo request
                     (string-drop-right path
                                        (string-length ".narinfo"))))
    (_ #f)))

(define (render-nar request
                    mime-types
                    file-name)
  (or
   (and=> (with-resource-from-pool (reserved-connection-pool) conn
            (select-serialized-derivation-by-file-name conn
                                                       file-name))
          (lambda (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")))))
                    (call-with-output-bytevector
                     (lambda (port)
                       (write-file-tree
                        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 (render-lzip-nar request
                         mime-types
                         file-name)
  (or
   (and=> (with-resource-from-pool (reserved-connection-pool) conn
            (select-derivation-source-file-nar-data-by-file-name conn
                                                                 file-name))
          (lambda (data)
            (list (build-response
                   #:code 200
                   #:headers `((content-type . (application/x-nix-archive
                                                (charset . "ISO-8859-1")))
                               (content-length . ,(bytevector-length data))))
                  (lambda (port)
                    ;; TODO: This should probably be handled by guile-fibers
                    (set-port-encoding! port "ISO-8859-1")
                    (put-bytevector port data)))))
   (not-found (request-uri request))))

(define (render-narinfo request
                        hash)
  (or
   (and=> (with-resource-from-pool (reserved-connection-pool) conn
            (select-derivation-by-file-name-hash conn
                                                 hash))
          (lambda (derivation)
            (list (build-response
                   #:code 200
                   #:headers '((content-type . (application/x-narinfo))))
                  (let ((derivation-file-name (second derivation)))
                    (letpar&
                        ((derivation-text
                          (with-resource-from-pool (reserved-connection-pool) conn
                            (select-serialized-derivation-by-file-name
                             conn
                             derivation-file-name)))
                         (derivation-references
                          (with-resource-from-pool (reserved-connection-pool) conn
                            (select-derivation-references-by-derivation-id
                             conn
                             (first derivation)))))
                      (let* ((derivation-bytevector
                              (string->bytevector derivation-text
                                                  "ISO-8859-1"))
                             (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)))))
                        (narinfo-string derivation-file-name
                                        nar-bytevector
                                        derivation-references)))))))
   (and=> (with-resource-from-pool (reserved-connection-pool) conn
            (select-derivation-source-file-data-by-file-name-hash conn
                                                                  hash))
          (match-lambda
            ((store-path compression compressed-size
                         hash-algorithm hash uncompressed-size)
             (list (build-response
                    #:code 200
                    #:headers '((content-type . (application/x-narinfo))))
                   (derivation-source-file-narinfo-string store-path
                                                          compression
                                                          compressed-size
                                                          hash-algorithm
                                                          hash
                                                          uncompressed-size)))))
   (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)))

(define* (derivation-source-file-narinfo-string store-item
                                                compression
                                                compressed-size
                                                hash-algorithm
                                                hash
                                                uncompressed-size
                                                #: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* ((info (format #f
                             "\
StorePath: ~a
URL: ~a
Compression: ~a
FileSize: ~d
NarHash: ~a:~a
NarSize: ~d
References: ~%"
                             store-item
                             (encode-and-join-uri-path
                              (list nar-path
                                    compression
                                    (basename store-item)))
                             compression
                             compressed-size
                             hash-algorithm
                             hash
                             uncompressed-size)))
    (if (%narinfo-signing-private-key)
        (format #f "~aSignature: 1;~a;~a~%"
                info
                (gethostname)
                (base64-encode
                 (string->utf8
                  (canonical-sexp->string (signed-string info)))))
        info)))