substitutes.scm 15 KB

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