substitute.scm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  3. ;;; Copyright © 2014, 2015, 2017, 2018 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 build utils)
  31. #:select (mkdir-p delete-file-recursively))
  32. #:use-module (guix tests http)
  33. #:use-module (rnrs bytevectors)
  34. #:use-module (rnrs io ports)
  35. #:use-module (web uri)
  36. #:use-module (ice-9 regex)
  37. #:use-module (srfi srfi-26)
  38. #:use-module (srfi srfi-34)
  39. #:use-module (srfi srfi-35)
  40. #:use-module ((srfi srfi-64) #:hide (test-error)))
  41. (define-syntax-rule (test-quit name error-rx exp)
  42. "Emit a test that passes when EXP throws to 'quit' with value 1, and when
  43. it writes to GUIX-WARNING-PORT a messages that matches ERROR-RX."
  44. (test-equal name
  45. '(1 #t)
  46. (let ((error-output (open-output-string)))
  47. (parameterize ((guix-warning-port error-output))
  48. (catch 'quit
  49. (lambda ()
  50. exp
  51. #f)
  52. (lambda (key value)
  53. (list value
  54. (let ((message (get-output-string error-output)))
  55. (->bool (string-match error-rx message))))))))))
  56. (define %public-key
  57. ;; This key is known to be in the ACL by default.
  58. (call-with-input-file (string-append %config-directory "/signing-key.pub")
  59. (compose string->canonical-sexp get-string-all)))
  60. (define %private-key
  61. (call-with-input-file (string-append %config-directory "/signing-key.sec")
  62. (compose string->canonical-sexp get-string-all)))
  63. (define* (signature-body bv #:key (public-key %public-key))
  64. "Return the signature of BV as the base64-encoded body of a narinfo's
  65. 'Signature' field."
  66. (base64-encode
  67. (string->utf8
  68. (canonical-sexp->string
  69. (signature-sexp (bytevector->hash-data (sha256 bv)
  70. #:key-type 'rsa)
  71. %private-key
  72. public-key)))))
  73. (define %wrong-public-key
  74. (string->canonical-sexp "(public-key
  75. (rsa
  76. (n #00E05873AC2B168760343145918E954EE9AB73C026355693B192E01EE835261AA689E9EF46642E895BCD65C648524059FC450E4BA77A68F4C52D0E39EF0CC9359709AB6AAB153B63782201871325B0FDA19CB401CD99FD0C31A91CA9000AA90A77E82B89E036FB63BC1D3961207469B3B12468977148D376F8012BB12A4B11A8F1#)
  77. (e #010001#)
  78. )
  79. )"))
  80. (define* (signature-field bv-or-str
  81. #:key (version "1") (public-key %public-key))
  82. "Return the 'Signature' field value of bytevector/string BV-OR-STR, using
  83. PUBLIC-KEY as the signature's principal, and using VERSION as the signature
  84. version identifier.."
  85. (string-append version ";example.gnu.org;"
  86. (signature-body (if (string? bv-or-str)
  87. (string->utf8 bv-or-str)
  88. bv-or-str)
  89. #:public-key public-key)))
  90. (test-begin "substitute")
  91. (test-quit "not a number"
  92. "signature version"
  93. (narinfo-signature->canonical-sexp
  94. (signature-field "foo" #:version "not a number")))
  95. (test-quit "wrong version number"
  96. "unsupported.*version"
  97. (narinfo-signature->canonical-sexp
  98. (signature-field "foo" #:version "2")))
  99. (test-assert "valid narinfo-signature->canonical-sexp"
  100. (canonical-sexp? (narinfo-signature->canonical-sexp (signature-field "foo"))))
  101. (define %main-substitute-directory
  102. ;; The place where 'call-with-narinfo' stores its data by default.
  103. (uri-path (string->uri (getenv "GUIX_BINARY_SUBSTITUTE_URL"))))
  104. (define %alternate-substitute-directory
  105. ;; Another place.
  106. (string-append (dirname %main-substitute-directory)
  107. "/substituter-alt-data"))
  108. (define %narinfo
  109. ;; Skeleton of the narinfo used below.
  110. (string-append "StorePath: " (%store-prefix)
  111. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo
  112. URL: example.nar
  113. Compression: none
  114. NarHash: sha256:" (bytevector->nix-base32-string
  115. (sha256 (string->utf8 "Substitutable data."))) "
  116. NarSize: 42
  117. References: bar baz
  118. Deriver: " (%store-prefix) "/foo.drv
  119. System: mips64el-linux\n"))
  120. (define* (call-with-narinfo narinfo thunk
  121. #:optional
  122. (narinfo-directory %main-substitute-directory))
  123. "Call THUNK in a context where the directory at URL is populated with
  124. a file for NARINFO."
  125. (mkdir-p narinfo-directory)
  126. (let ((cache-directory (string-append (getenv "XDG_CACHE_HOME")
  127. "/guix/substitute/")))
  128. (dynamic-wind
  129. (lambda ()
  130. (when (file-exists? cache-directory)
  131. (delete-file-recursively cache-directory))
  132. (call-with-output-file (string-append narinfo-directory
  133. "/nix-cache-info")
  134. (lambda (port)
  135. (format port "StoreDir: ~a\nWantMassQuery: 0\n"
  136. (%store-prefix))))
  137. (call-with-output-file (string-append narinfo-directory "/"
  138. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  139. ".narinfo")
  140. (cut display narinfo <>))
  141. ;; Prepare the nar.
  142. (call-with-output-file
  143. (string-append narinfo-directory "/example.out")
  144. (cut display "Substitutable data." <>))
  145. (call-with-output-file
  146. (string-append narinfo-directory "/example.nar")
  147. (cute write-file
  148. (string-append narinfo-directory "/example.out") <>))
  149. (set! (@@ (guix scripts substitute)
  150. %allow-unauthenticated-substitutes?)
  151. #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-end "substitute")
  419. ;;; Local Variables:
  420. ;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
  421. ;;; eval: (put 'with-narinfo* 'scheme-indent-function 2)
  422. ;;; eval: (put 'test-quit 'scheme-indent-function 2)
  423. ;;; End: