substitute.scm 21 KB

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