substitutes.scm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. (let* ((base (string->uri cache-url))
  144. (ref (build-relative-ref
  145. #:path (string-append (store-path-hash-part path) ".narinfo")))
  146. (url (resolve-uri-reference ref base))
  147. (headers '((User-Agent . "GNU Guile"))))
  148. (build-request url #:method 'GET #:headers headers)))
  149. (define (narinfo-from-file file url)
  150. "Attempt to read a narinfo from FILE, using URL as the cache URL. Return #f
  151. if file doesn't exist, and the narinfo otherwise."
  152. (catch 'system-error
  153. (lambda ()
  154. (call-with-input-file file
  155. (cut read-narinfo <> url)))
  156. (lambda args
  157. (if (= ENOENT (system-error-errno args))
  158. #f
  159. (apply throw args)))))
  160. (define* (fetch-narinfos url paths
  161. #:key
  162. (open-connection guix:open-connection-for-uri)
  163. (make-progress-reporter
  164. (const progress-reporter/silent)))
  165. "Retrieve all the narinfos for PATHS from the cache at URL and return them."
  166. (define progress-reporter
  167. (make-progress-reporter (length paths)
  168. #:url url))
  169. (define hash-part->path
  170. (let ((mapping (fold (lambda (path result)
  171. (vhash-cons (store-path-hash-part path) path
  172. result))
  173. vlist-null
  174. paths)))
  175. (lambda (hash)
  176. (match (vhash-assoc hash mapping)
  177. (#f #f)
  178. ((_ . path) path)))))
  179. (define (read-to-eof port)
  180. "Read from PORT until EOF is reached. The data are discarded."
  181. (dump-port port (%make-void-port "w")))
  182. (define (handle-narinfo-response request response port result)
  183. (let* ((code (response-code response))
  184. (len (response-content-length response))
  185. (cache (response-cache-control response))
  186. (ttl (and cache (assoc-ref cache 'max-age))))
  187. (progress-reporter-report! progress-reporter)
  188. ;; Make sure to read no more than LEN bytes since subsequent bytes may
  189. ;; belong to the next response.
  190. (if (= code 200) ; hit
  191. (let ((narinfo (read-narinfo port url #:size len)))
  192. (if (string=? (dirname (narinfo-path narinfo))
  193. (%store-prefix))
  194. (begin
  195. (cache-narinfo! url (narinfo-path narinfo) narinfo ttl)
  196. (cons narinfo result))
  197. result))
  198. (let* ((path (uri-path (request-uri request)))
  199. (hash-part (basename
  200. (string-drop-right path 8)))) ;drop ".narinfo"
  201. (if len
  202. (get-bytevector-n port len)
  203. (read-to-eof port))
  204. (cache-narinfo! url (hash-part->path hash-part) #f
  205. (if (or (= 404 code) (= 202 code))
  206. ttl
  207. %narinfo-transient-error-ttl))
  208. result))))
  209. (define (do-fetch uri)
  210. (case (and=> uri uri-scheme)
  211. ((http https)
  212. ;; Note: Do not check HTTPS server certificates to avoid depending
  213. ;; on the X.509 PKI. We can do it because we authenticate
  214. ;; narinfos, which provides a much stronger guarantee.
  215. (let* ((requests (map (cut narinfo-request url <>) paths))
  216. (result (begin
  217. (start-progress-reporter! progress-reporter)
  218. (call-with-connection-error-handling
  219. uri
  220. (lambda ()
  221. (http-multiple-get uri
  222. handle-narinfo-response '()
  223. requests
  224. #:open-connection open-connection
  225. #:verify-certificate? #f))))))
  226. (stop-progress-reporter! progress-reporter)
  227. result))
  228. ((file #f)
  229. (let* ((base (string-append (uri-path uri) "/"))
  230. (files (map (compose (cut string-append base <> ".narinfo")
  231. store-path-hash-part)
  232. paths)))
  233. (filter-map (cut narinfo-from-file <> url) files)))
  234. (else
  235. (leave (G_ "~s: unsupported server URI scheme~%")
  236. (if uri (uri-scheme uri) url)))))
  237. (do-fetch (string->uri url)))
  238. (define (cached-narinfo cache-url path)
  239. "Check locally if we have valid info about PATH coming from CACHE-URL.
  240. Return two values: a Boolean indicating whether we have valid cached info, and
  241. that info, which may be either #f (when PATH is unavailable) or the narinfo
  242. for PATH."
  243. (define now
  244. (current-time time-monotonic))
  245. (define cache-file
  246. (narinfo-cache-file cache-url path))
  247. (catch 'system-error
  248. (lambda ()
  249. (call-with-input-file cache-file
  250. (lambda (p)
  251. (match (read p)
  252. (('narinfo ('version 2)
  253. ('cache-uri cache-uri)
  254. ('date date) ('ttl ttl) ('value #f))
  255. ;; A cached negative lookup.
  256. (if (obsolete? date now ttl)
  257. (values #f #f)
  258. (values #t #f)))
  259. (('narinfo ('version 2)
  260. ('cache-uri cache-uri)
  261. ('date date) ('ttl ttl) ('value value))
  262. ;; A cached positive lookup
  263. (if (obsolete? date now ttl)
  264. (values #f #f)
  265. (values #t (string->narinfo value cache-uri))))
  266. (('narinfo ('version v) _ ...)
  267. (values #f #f))))))
  268. (lambda _
  269. (values #f #f))))
  270. (define* (lookup-narinfos cache paths
  271. #:key (open-connection guix:open-connection-for-uri)
  272. (make-progress-reporter
  273. (const progress-reporter/silent)))
  274. "Return the narinfos for PATHS, invoking the server at CACHE when no
  275. information is available locally."
  276. (let-values (((cached missing)
  277. (fold2 (lambda (path cached missing)
  278. (let-values (((valid? value)
  279. (cached-narinfo cache path)))
  280. (if valid?
  281. (if value
  282. (values (cons value cached) missing)
  283. (values cached missing))
  284. (values cached (cons path missing)))))
  285. '()
  286. '()
  287. paths)))
  288. (values (if (null? missing)
  289. cached
  290. (let ((missing (fetch-narinfos cache missing
  291. #:open-connection open-connection
  292. #:make-progress-reporter
  293. make-progress-reporter)))
  294. (append cached (or missing '()))))
  295. (length missing))))
  296. (define* (lookup-narinfos/diverse caches paths authorized?
  297. #:key (open-connection
  298. guix:open-connection-for-uri)
  299. (make-progress-reporter
  300. (const progress-reporter/silent)))
  301. "Look up narinfos for PATHS on all of CACHES, a list of URLS, in that order.
  302. That is, when a cache lacks an AUTHORIZED? narinfo, look it up in the next
  303. cache, and so on.
  304. Return a list of narinfos for PATHS or a subset thereof. The returned
  305. narinfos are either AUTHORIZED?, or they claim a hash that matches an
  306. AUTHORIZED? narinfo."
  307. (define (select-hit result)
  308. (lambda (path)
  309. (match (vhash-fold* cons '() path result)
  310. ((one)
  311. one)
  312. ((several ..1)
  313. (let ((authorized (find authorized? (reverse several))))
  314. (and authorized
  315. (find (cut equivalent-narinfo? <> authorized)
  316. several)))))))
  317. (let loop ((caches caches)
  318. (paths paths)
  319. (result vlist-null) ;path->narinfo vhash
  320. (hits '())) ;paths
  321. (match paths
  322. (() ;we're done
  323. ;; Now iterate on all the HITS, and return exactly one match for each
  324. ;; hit: the first narinfo that is authorized, or that has the same hash
  325. ;; as an authorized narinfo, in the order of CACHES.
  326. (filter-map (select-hit result) hits))
  327. (_
  328. (match caches
  329. ((cache rest ...)
  330. (let* ((narinfos (lookup-narinfos cache paths
  331. #:open-connection open-connection
  332. #:make-progress-reporter
  333. make-progress-reporter))
  334. (definite (map narinfo-path (filter authorized? narinfos)))
  335. (missing (lset-difference string=? paths definite))) ;XXX: perf
  336. (loop rest missing
  337. (fold vhash-cons result
  338. (map narinfo-path narinfos) narinfos)
  339. (append definite hits))))
  340. (() ;that's it
  341. (filter-map (select-hit result) hits)))))))
  342. ;;; substitutes.scm ends here