substitutes.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  4. ;;; Copyright © 2018 Kyle Meyer <kyle@kyleam.com>
  5. ;;; Copyright © 2020 Christopher Baines <mail@cbaines.net>
  6. ;;;
  7. ;;; This file is part of GNU Guix.
  8. ;;;
  9. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; GNU Guix is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  21. (define-module (guix substitutes)
  22. #:use-module (guix narinfo)
  23. #:use-module (guix store)
  24. #:use-module (guix utils)
  25. #:use-module (guix combinators)
  26. #:use-module (guix config)
  27. #:use-module (guix records)
  28. #:use-module (guix diagnostics)
  29. #:use-module (guix i18n)
  30. #:use-module (gcrypt hash)
  31. #:use-module (guix base32)
  32. #:use-module (guix base64)
  33. #:use-module (guix cache)
  34. #:use-module (gcrypt pk-crypto)
  35. #:use-module (guix pki)
  36. #:use-module ((guix build utils) #:select (mkdir-p dump-port))
  37. #:use-module ((guix build download)
  38. #:select ((open-connection-for-uri
  39. . guix:open-connection-for-uri)))
  40. #:use-module (guix progress)
  41. #:use-module (ice-9 rdelim)
  42. #:use-module (ice-9 regex)
  43. #:use-module (ice-9 match)
  44. #:use-module (ice-9 format)
  45. #:use-module (ice-9 ftw)
  46. #:use-module (ice-9 binary-ports)
  47. #:use-module (ice-9 vlist)
  48. #:use-module (rnrs bytevectors)
  49. #:use-module (srfi srfi-1)
  50. #:use-module (srfi srfi-11)
  51. #:use-module (srfi srfi-19)
  52. #:use-module (srfi srfi-26)
  53. #:use-module (srfi srfi-34)
  54. #:use-module (srfi srfi-35)
  55. #:use-module (web uri)
  56. #:use-module (web request)
  57. #:use-module (web response)
  58. #:use-module (guix http-client)
  59. #:export (%narinfo-cache-directory
  60. call-with-connection-error-handling
  61. lookup-narinfos
  62. lookup-narinfos/diverse))
  63. (define %narinfo-ttl
  64. ;; Number of seconds during which cached narinfo lookups are considered
  65. ;; valid for substitute servers that do not advertise a TTL via the
  66. ;; 'Cache-Control' response header.
  67. (* 36 3600))
  68. (define %narinfo-negative-ttl
  69. ;; Likewise, but for negative lookups---i.e., cached lookup failures (404).
  70. (* 1 3600))
  71. (define %narinfo-transient-error-ttl
  72. ;; Likewise, but for transient errors such as 504 ("Gateway timeout").
  73. (* 10 60))
  74. (define %narinfo-cache-directory
  75. ;; A local cache of narinfos, to avoid going to the network. Most of the
  76. ;; time, 'guix substitute' is called by guix-daemon as root and stores its
  77. ;; cached data in /var/guix/…. However, when invoked from 'guix challenge'
  78. ;; as a user, it stores its cache in ~/.cache.
  79. (if (zero? (getuid))
  80. (or (and=> (getenv "XDG_CACHE_HOME")
  81. (cut string-append <> "/guix/substitute"))
  82. (string-append %state-directory "/substitute/cache"))
  83. (string-append (cache-directory #:ensure? #f) "/substitute")))
  84. (define (narinfo-cache-file cache-url path)
  85. "Return the name of the local file that contains an entry for PATH. The
  86. entry is stored in a sub-directory specific to CACHE-URL."
  87. ;; The daemon does not sanitize its input, so PATH could be something like
  88. ;; "/gnu/store/foo". Gracefully handle that.
  89. (match (store-path-hash-part path)
  90. (#f
  91. (leave (G_ "'~a' does not name a store item~%") path))
  92. ((? string? hash-part)
  93. (string-append %narinfo-cache-directory "/"
  94. (bytevector->base32-string (sha256 (string->utf8 cache-url)))
  95. "/" hash-part))))
  96. (define (cache-narinfo! cache-url path narinfo ttl)
  97. "Cache locally NARNIFO for PATH, which originates from CACHE-URL, with the
  98. given TTL (a number of seconds or #f). NARINFO may be #f, in which case it
  99. indicates that PATH is unavailable at CACHE-URL."
  100. (define now
  101. (current-time time-monotonic))
  102. (define (cache-entry cache-uri narinfo)
  103. `(narinfo (version 2)
  104. (cache-uri ,cache-uri)
  105. (date ,(time-second now))
  106. (ttl ,(or ttl
  107. (if narinfo %narinfo-ttl %narinfo-negative-ttl)))
  108. (value ,(and=> narinfo narinfo->string))))
  109. (let ((file (narinfo-cache-file cache-url path)))
  110. (mkdir-p (dirname file))
  111. (with-atomic-file-output file
  112. (lambda (out)
  113. (write (cache-entry cache-url narinfo) out))))
  114. narinfo)
  115. (define %unreachable-hosts
  116. ;; Set of names of unreachable hosts.
  117. (make-hash-table))
  118. (define* (call-with-connection-error-handling uri proc)
  119. "Call PROC, and catch if a connection fails, print a warning and return #f."
  120. (define host
  121. (uri-host uri))
  122. (catch #t
  123. proc
  124. (match-lambda*
  125. (('getaddrinfo-error error)
  126. (unless (hash-ref %unreachable-hosts host)
  127. (hash-set! %unreachable-hosts host #t) ;warn only once
  128. (warning (G_ "~a: host not found: ~a~%")
  129. host (gai-strerror error)))
  130. #f)
  131. (('system-error . args)
  132. (unless (hash-ref %unreachable-hosts host)
  133. (hash-set! %unreachable-hosts host #t)
  134. (warning (G_ "~a: connection failed: ~a~%") host
  135. (strerror
  136. (system-error-errno `(system-error ,@args)))))
  137. #f)
  138. (args
  139. (apply throw args)))))
  140. (define (narinfo-request cache-url path)
  141. "Return an HTTP request for the narinfo of PATH at CACHE-URL."
  142. (let ((url (string-append cache-url "/" (store-path-hash-part path)
  143. ".narinfo"))
  144. (headers '((User-Agent . "GNU Guile"))))
  145. (build-request (string->uri url) #:method 'GET #:headers headers)))
  146. (define (narinfo-from-file file url)
  147. "Attempt to read a narinfo from FILE, using URL as the cache URL. Return #f
  148. if file doesn't exist, and the narinfo otherwise."
  149. (catch 'system-error
  150. (lambda ()
  151. (call-with-input-file file
  152. (cut read-narinfo <> url)))
  153. (lambda args
  154. (if (= ENOENT (system-error-errno args))
  155. #f
  156. (apply throw args)))))
  157. (define* (fetch-narinfos url paths
  158. #:key
  159. (open-connection guix:open-connection-for-uri)
  160. (make-progress-reporter
  161. (const progress-reporter/silent)))
  162. "Retrieve all the narinfos for PATHS from the cache at URL and return them."
  163. (define progress-reporter
  164. (make-progress-reporter (length paths)
  165. #:url url))
  166. (define hash-part->path
  167. (let ((mapping (fold (lambda (path result)
  168. (vhash-cons (store-path-hash-part path) path
  169. result))
  170. vlist-null
  171. paths)))
  172. (lambda (hash)
  173. (match (vhash-assoc hash mapping)
  174. (#f #f)
  175. ((_ . path) path)))))
  176. (define (read-to-eof port)
  177. "Read from PORT until EOF is reached. The data are discarded."
  178. (dump-port port (%make-void-port "w")))
  179. (define (handle-narinfo-response request response port result)
  180. (let* ((code (response-code response))
  181. (len (response-content-length response))
  182. (cache (response-cache-control response))
  183. (ttl (and cache (assoc-ref cache 'max-age))))
  184. (progress-reporter-report! progress-reporter)
  185. ;; Make sure to read no more than LEN bytes since subsequent bytes may
  186. ;; belong to the next response.
  187. (if (= code 200) ; hit
  188. (let ((narinfo (read-narinfo port url #:size len)))
  189. (if (string=? (dirname (narinfo-path narinfo))
  190. (%store-prefix))
  191. (begin
  192. (cache-narinfo! url (narinfo-path narinfo) narinfo ttl)
  193. (cons narinfo result))
  194. result))
  195. (let* ((path (uri-path (request-uri request)))
  196. (hash-part (basename
  197. (string-drop-right path 8)))) ;drop ".narinfo"
  198. (if len
  199. (get-bytevector-n port len)
  200. (read-to-eof port))
  201. (cache-narinfo! url (hash-part->path hash-part) #f
  202. (if (or (= 404 code) (= 202 code))
  203. ttl
  204. %narinfo-transient-error-ttl))
  205. result))))
  206. (define (do-fetch uri)
  207. (case (and=> uri uri-scheme)
  208. ((http https)
  209. ;; Note: Do not check HTTPS server certificates to avoid depending
  210. ;; on the X.509 PKI. We can do it because we authenticate
  211. ;; narinfos, which provides a much stronger guarantee.
  212. (let* ((requests (map (cut narinfo-request url <>) paths))
  213. (result (begin
  214. (start-progress-reporter! progress-reporter)
  215. (call-with-connection-error-handling
  216. uri
  217. (lambda ()
  218. (http-multiple-get uri
  219. handle-narinfo-response '()
  220. requests
  221. #:open-connection open-connection
  222. #:verify-certificate? #f))))))
  223. (stop-progress-reporter! progress-reporter)
  224. result))
  225. ((file #f)
  226. (let* ((base (string-append (uri-path uri) "/"))
  227. (files (map (compose (cut string-append base <> ".narinfo")
  228. store-path-hash-part)
  229. paths)))
  230. (filter-map (cut narinfo-from-file <> url) files)))
  231. (else
  232. (leave (G_ "~s: unsupported server URI scheme~%")
  233. (if uri (uri-scheme uri) url)))))
  234. (do-fetch (string->uri url)))
  235. (define (cached-narinfo cache-url path)
  236. "Check locally if we have valid info about PATH coming from CACHE-URL.
  237. Return two values: a Boolean indicating whether we have valid cached info, and
  238. that info, which may be either #f (when PATH is unavailable) or the narinfo
  239. for PATH."
  240. (define now
  241. (current-time time-monotonic))
  242. (define cache-file
  243. (narinfo-cache-file cache-url path))
  244. (catch 'system-error
  245. (lambda ()
  246. (call-with-input-file cache-file
  247. (lambda (p)
  248. (match (read p)
  249. (('narinfo ('version 2)
  250. ('cache-uri cache-uri)
  251. ('date date) ('ttl ttl) ('value #f))
  252. ;; A cached negative lookup.
  253. (if (obsolete? date now ttl)
  254. (values #f #f)
  255. (values #t #f)))
  256. (('narinfo ('version 2)
  257. ('cache-uri cache-uri)
  258. ('date date) ('ttl ttl) ('value value))
  259. ;; A cached positive lookup
  260. (if (obsolete? date now ttl)
  261. (values #f #f)
  262. (values #t (string->narinfo value cache-uri))))
  263. (('narinfo ('version v) _ ...)
  264. (values #f #f))))))
  265. (lambda _
  266. (values #f #f))))
  267. (define* (lookup-narinfos cache paths
  268. #:key (open-connection guix:open-connection-for-uri)
  269. (make-progress-reporter
  270. (const progress-reporter/silent)))
  271. "Return the narinfos for PATHS, invoking the server at CACHE when no
  272. information is available locally."
  273. (let-values (((cached missing)
  274. (fold2 (lambda (path cached missing)
  275. (let-values (((valid? value)
  276. (cached-narinfo cache path)))
  277. (if valid?
  278. (if value
  279. (values (cons value cached) missing)
  280. (values cached missing))
  281. (values cached (cons path missing)))))
  282. '()
  283. '()
  284. paths)))
  285. (values (if (null? missing)
  286. cached
  287. (let ((missing (fetch-narinfos cache missing
  288. #:open-connection open-connection
  289. #:make-progress-reporter
  290. make-progress-reporter)))
  291. (append cached (or missing '()))))
  292. (length missing))))
  293. (define* (lookup-narinfos/diverse caches paths authorized?
  294. #:key (open-connection
  295. guix:open-connection-for-uri)
  296. (make-progress-reporter
  297. (const progress-reporter/silent)))
  298. "Look up narinfos for PATHS on all of CACHES, a list of URLS, in that order.
  299. That is, when a cache lacks an AUTHORIZED? narinfo, look it up in the next
  300. cache, and so on.
  301. Return a list of narinfos for PATHS or a subset thereof. The returned
  302. narinfos are either AUTHORIZED?, or they claim a hash that matches an
  303. AUTHORIZED? narinfo."
  304. (define (select-hit result)
  305. (lambda (path)
  306. (match (vhash-fold* cons '() path result)
  307. ((one)
  308. one)
  309. ((several ..1)
  310. (let ((authorized (find authorized? (reverse several))))
  311. (and authorized
  312. (find (cut equivalent-narinfo? <> authorized)
  313. several)))))))
  314. (let loop ((caches caches)
  315. (paths paths)
  316. (result vlist-null) ;path->narinfo vhash
  317. (hits '())) ;paths
  318. (match paths
  319. (() ;we're done
  320. ;; Now iterate on all the HITS, and return exactly one match for each
  321. ;; hit: the first narinfo that is authorized, or that has the same hash
  322. ;; as an authorized narinfo, in the order of CACHES.
  323. (filter-map (select-hit result) hits))
  324. (_
  325. (match caches
  326. ((cache rest ...)
  327. (let* ((narinfos (lookup-narinfos cache paths
  328. #:open-connection open-connection
  329. #:make-progress-reporter
  330. make-progress-reporter))
  331. (definite (map narinfo-path (filter authorized? narinfos)))
  332. (missing (lset-difference string=? paths definite))) ;XXX: perf
  333. (loop rest missing
  334. (fold vhash-cons result
  335. (map narinfo-path narinfos) narinfos)
  336. (append definite hits))))
  337. (() ;that's it
  338. (filter-map (select-hit result) hits)))))))
  339. ;;; substitutes.scm ends here