substitute.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014 Nikita Karetnikov <nikita@karetnikov.org>
  3. ;;; Copyright © 2014, 2015, 2017 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 (guix hash)
  23. #:use-module (guix serialization)
  24. #:use-module (guix 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 signed with authorized key"
  186. (string-append (%store-prefix) "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  187. (with-narinfo (string-append %narinfo "Signature: "
  188. (signature-field %narinfo)
  189. "\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 signed with unauthorized key"
  198. "" ; not substitutable
  199. (with-narinfo (string-append %narinfo "Signature: "
  200. (signature-field
  201. %narinfo
  202. #:public-key %wrong-public-key)
  203. "\n")
  204. (string-trim-both
  205. (with-output-to-string
  206. (lambda ()
  207. (with-input-from-string (string-append "have " (%store-prefix)
  208. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  209. (lambda ()
  210. (guix-substitute "--query"))))))))
  211. (test-quit "substitute, no signature"
  212. "no valid substitute"
  213. (with-narinfo %narinfo
  214. (guix-substitute "--substitute"
  215. (string-append (%store-prefix)
  216. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  217. "foo")))
  218. (test-quit "substitute, invalid hash"
  219. "no valid substitute"
  220. ;; The hash in the signature differs from the hash of %NARINFO.
  221. (with-narinfo (string-append %narinfo "Signature: "
  222. (signature-field "different body")
  223. "\n")
  224. (guix-substitute "--substitute"
  225. (string-append (%store-prefix)
  226. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  227. "foo")))
  228. (test-quit "substitute, unauthorized key"
  229. "no valid substitute"
  230. (with-narinfo (string-append %narinfo "Signature: "
  231. (signature-field
  232. %narinfo
  233. #:public-key %wrong-public-key)
  234. "\n")
  235. (guix-substitute "--substitute"
  236. (string-append (%store-prefix)
  237. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  238. "foo")))
  239. (test-equal "substitute, authorized key"
  240. "Substitutable data."
  241. (with-narinfo (string-append %narinfo "Signature: "
  242. (signature-field %narinfo))
  243. (dynamic-wind
  244. (const #t)
  245. (lambda ()
  246. (guix-substitute "--substitute"
  247. (string-append (%store-prefix)
  248. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  249. "substitute-retrieved")
  250. (call-with-input-file "substitute-retrieved" get-string-all))
  251. (lambda ()
  252. (false-if-exception (delete-file "substitute-retrieved"))))))
  253. (test-equal "substitute, unauthorized narinfo comes first"
  254. "Substitutable data."
  255. (with-narinfo*
  256. (string-append %narinfo "Signature: "
  257. (signature-field
  258. %narinfo
  259. #:public-key %wrong-public-key))
  260. %alternate-substitute-directory
  261. (with-narinfo* (string-append %narinfo "Signature: "
  262. (signature-field %narinfo))
  263. %main-substitute-directory
  264. (dynamic-wind
  265. (const #t)
  266. (lambda ()
  267. ;; Remove this file so that the substitute can only be retrieved
  268. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  269. (delete-file (string-append %main-substitute-directory
  270. "/example.nar"))
  271. (parameterize ((substitute-urls
  272. (map (cut string-append "file://" <>)
  273. (list %alternate-substitute-directory
  274. %main-substitute-directory))))
  275. (guix-substitute "--substitute"
  276. (string-append (%store-prefix)
  277. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  278. "substitute-retrieved"))
  279. (call-with-input-file "substitute-retrieved" get-string-all))
  280. (lambda ()
  281. (false-if-exception (delete-file "substitute-retrieved")))))))
  282. (test-equal "substitute, unsigned narinfo comes first"
  283. "Substitutable data."
  284. (with-narinfo* %narinfo ;not signed!
  285. %alternate-substitute-directory
  286. (with-narinfo* (string-append %narinfo "Signature: "
  287. (signature-field %narinfo))
  288. %main-substitute-directory
  289. (dynamic-wind
  290. (const #t)
  291. (lambda ()
  292. ;; Remove this file so that the substitute can only be retrieved
  293. ;; from %ALTERNATE-SUBSTITUTE-DIRECTORY.
  294. (delete-file (string-append %main-substitute-directory
  295. "/example.nar"))
  296. (parameterize ((substitute-urls
  297. (map (cut string-append "file://" <>)
  298. (list %alternate-substitute-directory
  299. %main-substitute-directory))))
  300. (guix-substitute "--substitute"
  301. (string-append (%store-prefix)
  302. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  303. "substitute-retrieved"))
  304. (call-with-input-file "substitute-retrieved" get-string-all))
  305. (lambda ()
  306. (false-if-exception (delete-file "substitute-retrieved")))))))
  307. (test-equal "substitute, first narinfo is unsigned and has wrong hash"
  308. "Substitutable data."
  309. (with-narinfo* (regexp-substitute #f
  310. (string-match "NarHash: [[:graph:]]+"
  311. %narinfo)
  312. 'pre
  313. "NarHash: sha256:"
  314. (bytevector->nix-base32-string
  315. (make-bytevector 32))
  316. 'post)
  317. %alternate-substitute-directory
  318. (with-narinfo* (string-append %narinfo "Signature: "
  319. (signature-field %narinfo))
  320. %main-substitute-directory
  321. (dynamic-wind
  322. (const #t)
  323. (lambda ()
  324. ;; This time remove the file so that the substitute can only be
  325. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  326. (delete-file (string-append %alternate-substitute-directory
  327. "/example.nar"))
  328. (parameterize ((substitute-urls
  329. (map (cut string-append "file://" <>)
  330. (list %alternate-substitute-directory
  331. %main-substitute-directory))))
  332. (guix-substitute "--substitute"
  333. (string-append (%store-prefix)
  334. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  335. "substitute-retrieved"))
  336. (call-with-input-file "substitute-retrieved" get-string-all))
  337. (lambda ()
  338. (false-if-exception (delete-file "substitute-retrieved")))))))
  339. (test-equal "substitute, first narinfo is unsigned and has wrong refs"
  340. "Substitutable data."
  341. (with-narinfo* (regexp-substitute #f
  342. (string-match "References: ([^\n]+)\n"
  343. %narinfo)
  344. 'pre "References: " 1
  345. " wrong set of references\n"
  346. 'post)
  347. %alternate-substitute-directory
  348. (with-narinfo* (string-append %narinfo "Signature: "
  349. (signature-field %narinfo))
  350. %main-substitute-directory
  351. (dynamic-wind
  352. (const #t)
  353. (lambda ()
  354. ;; This time remove the file so that the substitute can only be
  355. ;; retrieved from %MAIN-SUBSTITUTE-DIRECTORY.
  356. (delete-file (string-append %alternate-substitute-directory
  357. "/example.nar"))
  358. (parameterize ((substitute-urls
  359. (map (cut string-append "file://" <>)
  360. (list %alternate-substitute-directory
  361. %main-substitute-directory))))
  362. (guix-substitute "--substitute"
  363. (string-append (%store-prefix)
  364. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  365. "substitute-retrieved"))
  366. (call-with-input-file "substitute-retrieved" get-string-all))
  367. (lambda ()
  368. (false-if-exception (delete-file "substitute-retrieved")))))))
  369. (test-quit "substitute, two invalid narinfos"
  370. "no valid substitute"
  371. (with-narinfo* %narinfo ;not signed
  372. %alternate-substitute-directory
  373. (with-narinfo* (string-append %narinfo "Signature: " ;unauthorized
  374. (signature-field
  375. %narinfo
  376. #:public-key %wrong-public-key))
  377. %main-substitute-directory
  378. (guix-substitute "--substitute"
  379. (string-append (%store-prefix)
  380. "/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-foo")
  381. "substitute-retrieved"))))
  382. (test-end "substitute")
  383. ;;; Local Variables:
  384. ;;; eval: (put 'with-narinfo 'scheme-indent-function 1)
  385. ;;; eval: (put 'with-narinfo* 'scheme-indent-function 2)
  386. ;;; eval: (put 'test-quit 'scheme-indent-function 2)
  387. ;;; End: