sign_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //go:build !windows
  2. // +build !windows
  3. package main
  4. import (
  5. "bytes"
  6. "crypto/rand"
  7. "io/ioutil"
  8. "os"
  9. "testing"
  10. "time"
  11. "github.com/slackhq/nebula/cert"
  12. "github.com/stretchr/testify/assert"
  13. "golang.org/x/crypto/ed25519"
  14. )
  15. //TODO: test file permissions
  16. func Test_signSummary(t *testing.T) {
  17. assert.Equal(t, "sign <flags>: create and sign a certificate", signSummary())
  18. }
  19. func Test_signHelp(t *testing.T) {
  20. ob := &bytes.Buffer{}
  21. signHelp(ob)
  22. assert.Equal(
  23. t,
  24. "Usage of "+os.Args[0]+" sign <flags>: create and sign a certificate\n"+
  25. " -ca-crt string\n"+
  26. " \tOptional: path to the signing CA cert (default \"ca.crt\")\n"+
  27. " -ca-key string\n"+
  28. " \tOptional: path to the signing CA key (default \"ca.key\")\n"+
  29. " -duration duration\n"+
  30. " \tOptional: how long the cert should be valid for. The default is 1 second before the signing cert expires. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"\n"+
  31. " -groups string\n"+
  32. " \tOptional: comma separated list of groups\n"+
  33. " -in-pub string\n"+
  34. " \tOptional (if out-key not set): path to read a previously generated public key\n"+
  35. " -ip string\n"+
  36. " \tRequired: ipv4 address and network in CIDR notation to assign the cert\n"+
  37. " -name string\n"+
  38. " \tRequired: name of the cert, usually a hostname\n"+
  39. " -out-crt string\n"+
  40. " \tOptional: path to write the certificate to\n"+
  41. " -out-key string\n"+
  42. " \tOptional (if in-pub not set): path to write the private key to\n"+
  43. " -out-qr string\n"+
  44. " \tOptional: output a qr code image (png) of the certificate\n"+
  45. " -subnets string\n"+
  46. " \tOptional: comma separated list of ipv4 address and network in CIDR notation. Subnets this cert can serve for\n",
  47. ob.String(),
  48. )
  49. }
  50. func Test_signCert(t *testing.T) {
  51. ob := &bytes.Buffer{}
  52. eb := &bytes.Buffer{}
  53. // required args
  54. assertHelpError(t, signCert([]string{"-ca-crt", "./nope", "-ca-key", "./nope", "-ip", "1.1.1.1/24", "-out-key", "nope", "-out-crt", "nope"}, ob, eb), "-name is required")
  55. assert.Empty(t, ob.String())
  56. assert.Empty(t, eb.String())
  57. assertHelpError(t, signCert([]string{"-ca-crt", "./nope", "-ca-key", "./nope", "-name", "test", "-out-key", "nope", "-out-crt", "nope"}, ob, eb), "-ip is required")
  58. assert.Empty(t, ob.String())
  59. assert.Empty(t, eb.String())
  60. // cannot set -in-pub and -out-key
  61. assertHelpError(t, signCert([]string{"-ca-crt", "./nope", "-ca-key", "./nope", "-name", "test", "-in-pub", "nope", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope"}, ob, eb), "cannot set both -in-pub and -out-key")
  62. assert.Empty(t, ob.String())
  63. assert.Empty(t, eb.String())
  64. // failed to read key
  65. ob.Reset()
  66. eb.Reset()
  67. args := []string{"-ca-crt", "./nope", "-ca-key", "./nope", "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m"}
  68. assert.EqualError(t, signCert(args, ob, eb), "error while reading ca-key: open ./nope: "+NoSuchFileError)
  69. // failed to unmarshal key
  70. ob.Reset()
  71. eb.Reset()
  72. caKeyF, err := ioutil.TempFile("", "sign-cert.key")
  73. assert.Nil(t, err)
  74. defer os.Remove(caKeyF.Name())
  75. args = []string{"-ca-crt", "./nope", "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m"}
  76. assert.EqualError(t, signCert(args, ob, eb), "error while parsing ca-key: input did not contain a valid PEM encoded block")
  77. assert.Empty(t, ob.String())
  78. assert.Empty(t, eb.String())
  79. // Write a proper ca key for later
  80. ob.Reset()
  81. eb.Reset()
  82. caPub, caPriv, _ := ed25519.GenerateKey(rand.Reader)
  83. caKeyF.Write(cert.MarshalEd25519PrivateKey(caPriv))
  84. // failed to read cert
  85. args = []string{"-ca-crt", "./nope", "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m"}
  86. assert.EqualError(t, signCert(args, ob, eb), "error while reading ca-crt: open ./nope: "+NoSuchFileError)
  87. assert.Empty(t, ob.String())
  88. assert.Empty(t, eb.String())
  89. // failed to unmarshal cert
  90. ob.Reset()
  91. eb.Reset()
  92. caCrtF, err := ioutil.TempFile("", "sign-cert.crt")
  93. assert.Nil(t, err)
  94. defer os.Remove(caCrtF.Name())
  95. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m"}
  96. assert.EqualError(t, signCert(args, ob, eb), "error while parsing ca-crt: input did not contain a valid PEM encoded block")
  97. assert.Empty(t, ob.String())
  98. assert.Empty(t, eb.String())
  99. // write a proper ca cert for later
  100. ca := cert.NebulaCertificate{
  101. Details: cert.NebulaCertificateDetails{
  102. Name: "ca",
  103. NotBefore: time.Now(),
  104. NotAfter: time.Now().Add(time.Minute * 200),
  105. PublicKey: caPub,
  106. IsCA: true,
  107. },
  108. }
  109. b, _ := ca.MarshalToPEM()
  110. caCrtF.Write(b)
  111. // failed to read pub
  112. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-in-pub", "./nope", "-duration", "100m"}
  113. assert.EqualError(t, signCert(args, ob, eb), "error while reading in-pub: open ./nope: "+NoSuchFileError)
  114. assert.Empty(t, ob.String())
  115. assert.Empty(t, eb.String())
  116. // failed to unmarshal pub
  117. ob.Reset()
  118. eb.Reset()
  119. inPubF, err := ioutil.TempFile("", "in.pub")
  120. assert.Nil(t, err)
  121. defer os.Remove(inPubF.Name())
  122. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-in-pub", inPubF.Name(), "-duration", "100m"}
  123. assert.EqualError(t, signCert(args, ob, eb), "error while parsing in-pub: input did not contain a valid PEM encoded block")
  124. assert.Empty(t, ob.String())
  125. assert.Empty(t, eb.String())
  126. // write a proper pub for later
  127. ob.Reset()
  128. eb.Reset()
  129. inPub, _ := x25519Keypair()
  130. inPubF.Write(cert.MarshalX25519PublicKey(inPub))
  131. // bad ip cidr
  132. ob.Reset()
  133. eb.Reset()
  134. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "a1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m"}
  135. assertHelpError(t, signCert(args, ob, eb), "invalid ip definition: invalid CIDR address: a1.1.1.1/24")
  136. assert.Empty(t, ob.String())
  137. assert.Empty(t, eb.String())
  138. ob.Reset()
  139. eb.Reset()
  140. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "100::100/100", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m"}
  141. assertHelpError(t, signCert(args, ob, eb), "invalid ip definition: can only be ipv4, have 100::100/100")
  142. assert.Empty(t, ob.String())
  143. assert.Empty(t, eb.String())
  144. // bad subnet cidr
  145. ob.Reset()
  146. eb.Reset()
  147. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m", "-subnets", "a"}
  148. assertHelpError(t, signCert(args, ob, eb), "invalid subnet definition: invalid CIDR address: a")
  149. assert.Empty(t, ob.String())
  150. assert.Empty(t, eb.String())
  151. ob.Reset()
  152. eb.Reset()
  153. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m", "-subnets", "100::100/100"}
  154. assertHelpError(t, signCert(args, ob, eb), "invalid subnet definition: can only be ipv4, have 100::100/100")
  155. assert.Empty(t, ob.String())
  156. assert.Empty(t, eb.String())
  157. // mismatched ca key
  158. _, caPriv2, _ := ed25519.GenerateKey(rand.Reader)
  159. caKeyF2, err := ioutil.TempFile("", "sign-cert-2.key")
  160. assert.Nil(t, err)
  161. defer os.Remove(caKeyF2.Name())
  162. caKeyF2.Write(cert.MarshalEd25519PrivateKey(caPriv2))
  163. ob.Reset()
  164. eb.Reset()
  165. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF2.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "nope", "-out-key", "nope", "-duration", "100m", "-subnets", "a"}
  166. assert.EqualError(t, signCert(args, ob, eb), "refusing to sign, root certificate does not match private key")
  167. assert.Empty(t, ob.String())
  168. assert.Empty(t, eb.String())
  169. // failed key write
  170. ob.Reset()
  171. eb.Reset()
  172. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "/do/not/write/pleasecrt", "-out-key", "/do/not/write/pleasekey", "-duration", "100m", "-subnets", "10.1.1.1/32"}
  173. assert.EqualError(t, signCert(args, ob, eb), "error while writing out-key: open /do/not/write/pleasekey: "+NoSuchDirError)
  174. assert.Empty(t, ob.String())
  175. assert.Empty(t, eb.String())
  176. // create temp key file
  177. keyF, err := ioutil.TempFile("", "test.key")
  178. assert.Nil(t, err)
  179. os.Remove(keyF.Name())
  180. // failed cert write
  181. ob.Reset()
  182. eb.Reset()
  183. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", "/do/not/write/pleasecrt", "-out-key", keyF.Name(), "-duration", "100m", "-subnets", "10.1.1.1/32"}
  184. assert.EqualError(t, signCert(args, ob, eb), "error while writing out-crt: open /do/not/write/pleasecrt: "+NoSuchDirError)
  185. assert.Empty(t, ob.String())
  186. assert.Empty(t, eb.String())
  187. os.Remove(keyF.Name())
  188. // create temp cert file
  189. crtF, err := ioutil.TempFile("", "test.crt")
  190. assert.Nil(t, err)
  191. os.Remove(crtF.Name())
  192. // test proper cert with removed empty groups and subnets
  193. ob.Reset()
  194. eb.Reset()
  195. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-out-key", keyF.Name(), "-duration", "100m", "-subnets", "10.1.1.1/32, , 10.2.2.2/32 , , ,, 10.5.5.5/32", "-groups", "1,, 2 , ,,,3,4,5"}
  196. assert.Nil(t, signCert(args, ob, eb))
  197. assert.Empty(t, ob.String())
  198. assert.Empty(t, eb.String())
  199. // read cert and key files
  200. rb, _ := ioutil.ReadFile(keyF.Name())
  201. lKey, b, err := cert.UnmarshalX25519PrivateKey(rb)
  202. assert.Len(t, b, 0)
  203. assert.Nil(t, err)
  204. assert.Len(t, lKey, 32)
  205. rb, _ = ioutil.ReadFile(crtF.Name())
  206. lCrt, b, err := cert.UnmarshalNebulaCertificateFromPEM(rb)
  207. assert.Len(t, b, 0)
  208. assert.Nil(t, err)
  209. assert.Equal(t, "test", lCrt.Details.Name)
  210. assert.Equal(t, "1.1.1.1/24", lCrt.Details.Ips[0].String())
  211. assert.Len(t, lCrt.Details.Ips, 1)
  212. assert.False(t, lCrt.Details.IsCA)
  213. assert.Equal(t, []string{"1", "2", "3", "4", "5"}, lCrt.Details.Groups)
  214. assert.Len(t, lCrt.Details.Subnets, 3)
  215. assert.Len(t, lCrt.Details.PublicKey, 32)
  216. assert.Equal(t, time.Duration(time.Minute*100), lCrt.Details.NotAfter.Sub(lCrt.Details.NotBefore))
  217. sns := []string{}
  218. for _, sn := range lCrt.Details.Subnets {
  219. sns = append(sns, sn.String())
  220. }
  221. assert.Equal(t, []string{"10.1.1.1/32", "10.2.2.2/32", "10.5.5.5/32"}, sns)
  222. issuer, _ := ca.Sha256Sum()
  223. assert.Equal(t, issuer, lCrt.Details.Issuer)
  224. assert.True(t, lCrt.CheckSignature(caPub))
  225. // test proper cert with in-pub
  226. os.Remove(keyF.Name())
  227. os.Remove(crtF.Name())
  228. ob.Reset()
  229. eb.Reset()
  230. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-in-pub", inPubF.Name(), "-duration", "100m", "-groups", "1"}
  231. assert.Nil(t, signCert(args, ob, eb))
  232. assert.Empty(t, ob.String())
  233. assert.Empty(t, eb.String())
  234. // read cert file and check pub key matches in-pub
  235. rb, _ = ioutil.ReadFile(crtF.Name())
  236. lCrt, b, err = cert.UnmarshalNebulaCertificateFromPEM(rb)
  237. assert.Len(t, b, 0)
  238. assert.Nil(t, err)
  239. assert.Equal(t, lCrt.Details.PublicKey, inPub)
  240. // test refuse to sign cert with duration beyond root
  241. ob.Reset()
  242. eb.Reset()
  243. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-out-key", keyF.Name(), "-duration", "1000m", "-subnets", "10.1.1.1/32, , 10.2.2.2/32 , , ,, 10.5.5.5/32", "-groups", "1,, 2 , ,,,3,4,5"}
  244. assert.EqualError(t, signCert(args, ob, eb), "refusing to sign, root certificate constraints violated: certificate expires after signing certificate")
  245. assert.Empty(t, ob.String())
  246. assert.Empty(t, eb.String())
  247. // create valid cert/key for overwrite tests
  248. os.Remove(keyF.Name())
  249. os.Remove(crtF.Name())
  250. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-out-key", keyF.Name(), "-duration", "100m", "-subnets", "10.1.1.1/32, , 10.2.2.2/32 , , ,, 10.5.5.5/32", "-groups", "1,, 2 , ,,,3,4,5"}
  251. assert.Nil(t, signCert(args, ob, eb))
  252. // test that we won't overwrite existing key file
  253. os.Remove(crtF.Name())
  254. ob.Reset()
  255. eb.Reset()
  256. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-out-key", keyF.Name(), "-duration", "100m", "-subnets", "10.1.1.1/32, , 10.2.2.2/32 , , ,, 10.5.5.5/32", "-groups", "1,, 2 , ,,,3,4,5"}
  257. assert.EqualError(t, signCert(args, ob, eb), "refusing to overwrite existing key: "+keyF.Name())
  258. assert.Empty(t, ob.String())
  259. assert.Empty(t, eb.String())
  260. // create valid cert/key for overwrite tests
  261. os.Remove(keyF.Name())
  262. os.Remove(crtF.Name())
  263. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-out-key", keyF.Name(), "-duration", "100m", "-subnets", "10.1.1.1/32, , 10.2.2.2/32 , , ,, 10.5.5.5/32", "-groups", "1,, 2 , ,,,3,4,5"}
  264. assert.Nil(t, signCert(args, ob, eb))
  265. // test that we won't overwrite existing certificate file
  266. os.Remove(keyF.Name())
  267. ob.Reset()
  268. eb.Reset()
  269. args = []string{"-ca-crt", caCrtF.Name(), "-ca-key", caKeyF.Name(), "-name", "test", "-ip", "1.1.1.1/24", "-out-crt", crtF.Name(), "-out-key", keyF.Name(), "-duration", "100m", "-subnets", "10.1.1.1/32, , 10.2.2.2/32 , , ,, 10.5.5.5/32", "-groups", "1,, 2 , ,,,3,4,5"}
  270. assert.EqualError(t, signCert(args, ob, eb), "refusing to overwrite existing cert: "+crtF.Name())
  271. assert.Empty(t, ob.String())
  272. assert.Empty(t, eb.String())
  273. }