substitute.scm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  3. ;;; Copyright © 2014, 2015, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (test-substitute)
  20. #:use-module (guix scripts substitute)
  21. #:use-module (guix base64)
  22. #:use-module (gcrypt hash)
  23. #:use-module (guix serialization)
  24. #:use-module (gcrypt pk-crypto)
  25. #:use-module (guix pki)
  26. #:use-module (guix config)
  27. #:use-module (guix base32)
  28. #:use-module ((guix store) #:select (%store-prefix))
  29. #:use-module ((guix ui) #:select (guix-warning-port))
  30. #:use-module ((guix utils) #:select (call-with-compressed-output-port))
  31. #:use-module ((guix lzlib) #:select (lzlib-available?))
  32. #:use-module ((guix build utils)
  33. #:select (mkdir-p delete-file-recursively dump-port))
  34. #:use-module (guix tests http)
  35. #:use-module (rnrs bytevectors)
  36. #:use-module (rnrs io ports)
  37. #:use-module (web uri)
  38. #:use-module (ice-9 regex)
  39. #:use-module (srfi srfi-26)
  40. #:use-module (srfi srfi-34)
  41. #:use-module (srfi srfi-35)
  42. #:use-module ((srfi srfi-64) #:hide (test-error)))
  43. (define-syntax-rule (test-quit name error-rx exp)
  44. "Emit a test that passes when EXP throws to 'quit' with value 1, and when
  45. it writes to GUIX-WARNING-PORT a messages that matches ERROR-RX."
  46. (test-equal name
  47. '(1 #t)
  48. (let ((error-output (open-output-string)))
  49. (parameterize ((guix-warning-port error-output))
  50. (catch 'quit
  51. (lambda ()
  52. exp
  53. #f)
  54. (lambda (key value)
  55. (list value
  56. (let ((message (get-output-string error-output)))
  57. (->bool (string-match error-rx message))))))))))
  58. (define %public-key
  59. ;; This key is known to be in the ACL by default.
  60. (call-with-input-file (string-append %config-directory "/signing-key.pub")
  61. (compose string->canonical-sexp get-string-all)))
  62. (define %private-key
  63. (call-with-input-file (string-append %config-directory "/signing-key.sec")
  64. (compose string->canonical-sexp get-string-all)))
  65. (define* (signature-body bv #:key (public-key %public-key))
  66. "Return the signature of BV as the base64-encoded body of a narinfo's
  67. 'Signature' field."
  68. (base64-encode
  69. (string->utf8
  70. (canonical-sexp->string
  71. (signature-sexp (bytevector->hash-data (sha256 bv)
  72. #:key-type 'rsa)
  73. %private-key
  74. public-key)))))
  75. (define %wrong-public-key
  76. (string->canonical-sexp "(public-key
  77. (rsa
  78. (n #00E05873AC2B168760343145918E954EE9AB73C026355693B192E01EE835261AA689E9EF46642E895BCD65C648524059FC450E4BA77A68F4C52D0E39EF0CC9359709AB6AAB153B63782201871325B0FDA19CB401CD99FD0C31A91CA9000AA90A77E82B89E036FB63BC1D3961207469B3B12468977148D376F8012BB12A4B11A8F1#)
  79. (e #010001#)
  80. )
  81. )"))
  82. (define* (signature-field bv-or-str
  83. #:key (version "1") (public-key %public-key))
  84. "Return the 'Signature' field value of bytevector/string BV-OR-STR, using
  85. PUBLIC-KEY as the signature's principal, and using VERSION as the signature
  86. version identifier.."
  87. (string-append version ";example.gnu.org;"
  88. (signature-body (if (string? bv-or-str)
  89. (string->utf8 bv-or-str)
  90. bv-or-str)
  91. #:public-key public-key)))
  92. (test-begin "substitute")
  93. (test-quit "not a number"
  94. "signature version"
  95. (narinfo-signature->canonical-sexp
  96. (signature-field "foo" #:version "not a number")))
  97. (test-quit "wrong version number"
  98. "unsupported.*version"
  99. (narinfo-signature->canonical-sexp
  100. (signature-field "foo" #:version "2")))
  101. (test-assert "valid narinfo-signature->canonical-sexp"
  102. (canonical-sexp? (narinfo-signature->canonical-sexp (signature-field "foo"))))
  103. (define %main-substitute-directory
  104. ;; The place where 'call-with-narinfo' stores its data by default.
  105. (uri-path (string->uri (getenv "GUIX_BINARY_SUBSTITUTE_URL"))))
  106. (define %alternate-substitute-directory
  107. ;; Another place.
  108. (string-append (dirname %main-substitute-directory)
  109. "/substituter-alt-data"))
  110. (define %narinfo
  111. ;; Skeleton of the narinfo used below.
  112. (string-append "StorePath: " (%store-prefix)
  113. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  114. URL: example.nar
  115. Compression: none
  116. NarHash: sha256:" (bytevector->nix-base32-string
  117. (sha256 (string->utf8 "Substitutable data."))) "
  118. NarSize: 42
  119. References: bar baz
  120. Deriver: " (%store-prefix) "/foo.drv
  121. System: mips64el-linux\n"))
  122. (define* (call-with-narinfo narinfo thunk
  123. #:optional
  124. (narinfo-directory %main-substitute-directory))
  125. "Call THUNK in a context where the directory at URL is populated with
  126. a file for NARINFO."
  127. (mkdir-p narinfo-directory)
  128. (let ((cache-directory (string-append (getenv "XDG_CACHE_HOME")
  129. "/guix/substitute/")))
  130. (dynamic-wind
  131. (lambda ()
  132. (when (file-exists? cache-directory)
  133. (delete-file-recursively cache-directory))
  134. (call-with-output-file (string-append narinfo-directory
  135. "/nix-cache-info")
  136. (lambda (port)
  137. (format port "StoreDir: ~a\nWantMassQuery: 0\n"
  138. (%store-prefix))))
  139. (call-with-output-file (string-append narinfo-directory "/"
  140. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  141. ".narinfo")
  142. (cut display narinfo <>))
  143. ;; Prepare the nar.
  144. (call-with-output-file
  145. (string-append narinfo-directory "/example.out")
  146. (cut display "Substitutable data." <>))
  147. (call-with-output-file
  148. (string-append narinfo-directory "/example.nar")
  149. (cute write-file
  150. (string-append narinfo-directory "/example.out") <>))
  151. (%allow-unauthenticated-substitutes? #f))
  152. thunk
  153. (lambda ()
  154. (when (file-exists? cache-directory)
  155. (delete-file-recursively cache-directory))))))
  156. (define-syntax-rule (with-narinfo narinfo body ...)
  157. (call-with-narinfo narinfo (lambda () body ...)))
  158. (define-syntax-rule (with-narinfo* narinfo directory body ...)
  159. (call-with-narinfo narinfo (lambda () body ...) directory))
  160. ;; Transmit these options to 'guix substitute'.
  161. (substitute-urls (list (getenv "GUIX_BINARY_SUBSTITUTE_URL")))
  162. (test-equal "query narinfo without signature"
  163. "" ; not substitutable
  164. (with-narinfo %narinfo
  165. (string-trim-both
  166. (with-output-to-string
  167. (lambda ()
  168. (with-input-from-string (string-append "have " (%store-prefix)
  169. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  170. (lambda ()
  171. (guix-substitute "--query"))))))))
  172. (test-equal "query narinfo with invalid hash"
  173. ;; The hash in the signature differs from the hash of %NARINFO.
  174. ""
  175. (with-narinfo (string-append %narinfo "Signature: "
  176. (signature-field "different body")
  177. "\n")
  178. (string-trim-both
  179. (with-output-to-string
  180. (lambda ()
  181. (with-input-from-string (string-append "have " (%store-prefix)
  182. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  183. (lambda ()
  184. (guix-substitute "--query"))))))))
  185. (test-equal "query narinfo with signature over nothing"
  186. ;; The signature is computed over the empty string, not over the important
  187. ;; parts, so the narinfo must be ignored.
  188. ""
  189. (with-narinfo (string-append "Signature: " (signature-field "") "\n"
  190. %narinfo "\n")
  191. (string-trim-both
  192. (with-output-to-string
  193. (lambda ()
  194. (with-input-from-string (string-append "have " (%store-prefix)
  195. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  196. (lambda ()
  197. (guix-substitute "--query"))))))))
  198. (test-equal "query narinfo with signature over irrelevant bits"
  199. ;; The signature is valid but it does not cover the
  200. ;; StorePath/NarHash/References tuple and is thus irrelevant; the narinfo
  201. ;; must be ignored.
  202. ""
  203. (let ((prefix (string-append "StorePath: " (%store-prefix)
  204. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  205. URL: example.nar
  206. Compression: none\n")))
  207. (with-narinfo (string-append prefix
  208. "Signature: " (signature-field prefix) "
  209. NarHash: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  210. NarSize: 42
  211. References: bar baz
  212. Deriver: " (%store-prefix) "/foo.drv
  213. System: mips64el-linux\n")
  214. (string-trim-both
  215. (with-output-to-string
  216. (lambda ()
  217. (with-input-from-string (string-append "have " (%store-prefix)
  218. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  219. (lambda ()
  220. (guix-substitute "--query")))))))))
  221. (test-equal "query narinfo signed with authorized key"
  222. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  223. (with-narinfo (string-append %narinfo "Signature: "
  224. (signature-field %narinfo)
  225. "\n")
  226. (string-trim-both
  227. (with-output-to-string
  228. (lambda ()
  229. (with-input-from-string (string-append "have " (%store-prefix)
  230. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  231. (lambda ()
  232. (guix-substitute "--query"))))))))
  233. (test-equal "query narinfo signed with unauthorized key"
  234. "" ; not substitutable
  235. (with-narinfo (string-append %narinfo "Signature: "
  236. (signature-field
  237. %narinfo
  238. #:public-key %wrong-public-key)
  239. "\n")
  240. (string-trim-both
  241. (with-output-to-string
  242. (lambda ()
  243. (with-input-from-string (string-append "have " (%store-prefix)
  244. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  245. (lambda ()
  246. (guix-substitute "--query"))))))))
  247. (test-quit "substitute, no signature"
  248. "no valid substitute"
  249. (with-narinfo %narinfo
  250. (guix-substitute "--substitute"
  251. (string-append (%store-prefix)
  252. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  253. "foo")))
  254. (test-quit "substitute, invalid hash"
  255. "no valid substitute"
  256. ;; The hash in the signature differs from the hash of %NARINFO.
  257. (with-narinfo (string-append %narinfo "Signature: "
  258. (signature-field "different body")
  259. "\n")
  260. (guix-substitute "--substitute"
  261. (string-append (%store-prefix)
  262. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  263. "foo")))
  264. (test-quit "substitute, unauthorized key"
  265. "no valid substitute"
  266. (with-narinfo (string-append %narinfo "Signature: "
  267. (signature-field
  268. %narinfo
  269. #:public-key %wrong-public-key)
  270. "\n")
  271. (guix-substitute "--substitute"
  272. (string-append (%store-prefix)
  273. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  274. "foo")))
  275. (test-equal "substitute, authorized key"
  276. "Substitutable data."
  277. (with-narinfo (string-append %narinfo "Signature: "
  278. (signature-field %narinfo))
  279. (dynamic-wind
  280. (const #t)
  281. (lambda ()
  282. (guix-substitute "--substitute"
  283. (string-append (%store-prefix)
  284. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  285. "substitute-retrieved")
  286. (call-with-input-file "substitute-retrieved" get-string-all))
  287. (lambda ()
  288. (false-if-exception (delete-file "substitute-retrieved"))))))
  289. (test-equal "substitute, unauthorized narinfo comes first"
  290. "Substitutable data."
  291. (with-narinfo*
  292. (string-append %narinfo "Signature: "
  293. (signature-field
  294. %narinfo
  295. #:public-key %wrong-public-key))
  296. %alternate-substitute-directory
  297. (with-narinfo* (string-append %narinfo "Signature: "
  298. (signature-field %narinfo))
  299. %main-substitute-directory
  300. (dynamic-wind
  301. (const #t)
  302. (lambda ()
  303. ;; Remove this file so that the substitute can only be retrieved
  304. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  305. (delete-file (string-append %main-substitute-directory
  306. "/example.nar"))
  307. (parameterize ((substitute-urls
  308. (map (cut string-append "file://" <>)
  309. (list %alternate-substitute-directory
  310. %main-substitute-directory))))
  311. (guix-substitute "--substitute"
  312. (string-append (%store-prefix)
  313. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  314. "substitute-retrieved"))
  315. (call-with-input-file "substitute-retrieved" get-string-all))
  316. (lambda ()
  317. (false-if-exception (delete-file "substitute-retrieved")))))))
  318. (test-equal "substitute, unsigned narinfo comes first"
  319. "Substitutable data."
  320. (with-narinfo* %narinfo ;not signed!
  321. %alternate-substitute-directory
  322. (with-narinfo* (string-append %narinfo "Signature: "
  323. (signature-field %narinfo))
  324. %main-substitute-directory
  325. (dynamic-wind
  326. (const #t)
  327. (lambda ()
  328. ;; Remove this file so that the substitute can only be retrieved
  329. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  330. (delete-file (string-append %main-substitute-directory
  331. "/example.nar"))
  332. (parameterize ((substitute-urls
  333. (map (cut string-append "file://" <>)
  334. (list %alternate-substitute-directory
  335. %main-substitute-directory))))
  336. (guix-substitute "--substitute"
  337. (string-append (%store-prefix)
  338. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  339. "substitute-retrieved"))
  340. (call-with-input-file "substitute-retrieved" get-string-all))
  341. (lambda ()
  342. (false-if-exception (delete-file "substitute-retrieved")))))))
  343. (test-equal "substitute, first narinfo is unsigned and has wrong hash"
  344. "Substitutable data."
  345. (with-narinfo* (regexp-substitute #f
  346. (string-match "NarHash: [[:graph:]]+"
  347. %narinfo)
  348. 'pre
  349. "NarHash: sha256:"
  350. (bytevector->nix-base32-string
  351. (make-bytevector 32))
  352. 'post)
  353. %alternate-substitute-directory
  354. (with-narinfo* (string-append %narinfo "Signature: "
  355. (signature-field %narinfo))
  356. %main-substitute-directory
  357. (dynamic-wind
  358. (const #t)
  359. (lambda ()
  360. ;; This time remove the file so that the substitute can only be
  361. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  362. (delete-file (string-append %alternate-substitute-directory
  363. "/example.nar"))
  364. (parameterize ((substitute-urls
  365. (map (cut string-append "file://" <>)
  366. (list %alternate-substitute-directory
  367. %main-substitute-directory))))
  368. (guix-substitute "--substitute"
  369. (string-append (%store-prefix)
  370. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  371. "substitute-retrieved"))
  372. (call-with-input-file "substitute-retrieved" get-string-all))
  373. (lambda ()
  374. (false-if-exception (delete-file "substitute-retrieved")))))))
  375. (test-equal "substitute, first narinfo is unsigned and has wrong refs"
  376. "Substitutable data."
  377. (with-narinfo* (regexp-substitute #f
  378. (string-match "References: ([^\n]+)\n"
  379. %narinfo)
  380. 'pre "References: " 1
  381. " wrong set of references\n"
  382. 'post)
  383. %alternate-substitute-directory
  384. (with-narinfo* (string-append %narinfo "Signature: "
  385. (signature-field %narinfo))
  386. %main-substitute-directory
  387. (dynamic-wind
  388. (const #t)
  389. (lambda ()
  390. ;; This time remove the file so that the substitute can only be
  391. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  392. (delete-file (string-append %alternate-substitute-directory
  393. "/example.nar"))
  394. (parameterize ((substitute-urls
  395. (map (cut string-append "file://" <>)
  396. (list %alternate-substitute-directory
  397. %main-substitute-directory))))
  398. (guix-substitute "--substitute"
  399. (string-append (%store-prefix)
  400. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  401. "substitute-retrieved"))
  402. (call-with-input-file "substitute-retrieved" get-string-all))
  403. (lambda ()
  404. (false-if-exception (delete-file "substitute-retrieved")))))))
  405. (test-quit "substitute, two invalid narinfos"
  406. "no valid substitute"
  407. (with-narinfo* %narinfo ;not signed
  408. %alternate-substitute-directory
  409. (with-narinfo* (string-append %narinfo "Signature: " ;unauthorized
  410. (signature-field
  411. %narinfo
  412. #:public-key %wrong-public-key))
  413. %main-substitute-directory
  414. (guix-substitute "--substitute"
  415. (string-append (%store-prefix)
  416. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  417. "substitute-retrieved"))))
  418. (test-equal "substitute, narinfo with several URLs"
  419. "Substitutable data."
  420. (let ((narinfo (string-append "StorePath: " (%store-prefix)
  421. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  422. URL: example.nar.gz
  423. Compression: gzip
  424. URL: example.nar.lz
  425. Compression: lzip
  426. URL: example.nar
  427. Compression: none
  428. NarHash: sha256:" (bytevector->nix-base32-string
  429. (sha256 (string->utf8 "Substitutable data."))) "
  430. NarSize: 42
  431. References: bar baz
  432. Deriver: " (%store-prefix) "/foo.drv
  433. System: mips64el-linux\n")))
  434. (with-narinfo (string-append narinfo "Signature: "
  435. (signature-field narinfo))
  436. (dynamic-wind
  437. (const #t)
  438. (lambda ()
  439. (define (compress input output compression)
  440. (call-with-output-file output
  441. (lambda (port)
  442. (call-with-compressed-output-port compression port
  443. (lambda (port)
  444. (call-with-input-file input
  445. (lambda (input)
  446. (dump-port input port))))))))
  447. (let ((nar (string-append %main-substitute-directory
  448. "/example.nar")))
  449. (compress nar (string-append nar ".gz") 'gzip)
  450. (when (lzlib-available?)
  451. (compress nar (string-append nar ".lz") 'lzip)))
  452. (parameterize ((substitute-urls
  453. (list (string-append "file://"
  454. %main-substitute-directory))))
  455. (guix-substitute "--substitute"
  456. (string-append (%store-prefix)
  457. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  458. "substitute-retrieved"))
  459. (call-with-input-file "substitute-retrieved" get-string-all))
  460. (lambda ()
  461. (false-if-exception (delete-file "substitute-retrieved")))))))
  462. (test-end "substitute")
  463. ;;; Local Variables:
  464. ;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
  465. ;;; eval: (put 'with-narinfo* 'scheme-indent-function 2)
  466. ;;; eval: (put 'test-quit 'scheme-indent-function 2)
  467. ;;; End: