sign_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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: ip 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 subnet 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. // bad subnet cidr
  139. ob.Reset()
  140. eb.Reset()
  141. 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"}
  142. assertHelpError(t, signCert(args, ob, eb), "invalid subnet definition: invalid CIDR address: a")
  143. assert.Empty(t, ob.String())
  144. assert.Empty(t, eb.String())
  145. // mismatched ca key
  146. _, caPriv2, _ := ed25519.GenerateKey(rand.Reader)
  147. caKeyF2, err := ioutil.TempFile("", "sign-cert-2.key")
  148. assert.Nil(t, err)
  149. defer os.Remove(caKeyF2.Name())
  150. caKeyF2.Write(cert.MarshalEd25519PrivateKey(caPriv2))
  151. ob.Reset()
  152. eb.Reset()
  153. 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"}
  154. assert.EqualError(t, signCert(args, ob, eb), "refusing to sign, root certificate does not match private key")
  155. assert.Empty(t, ob.String())
  156. assert.Empty(t, eb.String())
  157. // failed key write
  158. ob.Reset()
  159. eb.Reset()
  160. 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"}
  161. assert.EqualError(t, signCert(args, ob, eb), "error while writing out-key: open /do/not/write/pleasekey: "+NoSuchDirError)
  162. assert.Empty(t, ob.String())
  163. assert.Empty(t, eb.String())
  164. // create temp key file
  165. keyF, err := ioutil.TempFile("", "test.key")
  166. assert.Nil(t, err)
  167. os.Remove(keyF.Name())
  168. // failed cert write
  169. ob.Reset()
  170. eb.Reset()
  171. 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"}
  172. assert.EqualError(t, signCert(args, ob, eb), "error while writing out-crt: open /do/not/write/pleasecrt: "+NoSuchDirError)
  173. assert.Empty(t, ob.String())
  174. assert.Empty(t, eb.String())
  175. os.Remove(keyF.Name())
  176. // create temp cert file
  177. crtF, err := ioutil.TempFile("", "test.crt")
  178. assert.Nil(t, err)
  179. os.Remove(crtF.Name())
  180. // test proper cert with removed empty groups and subnets
  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", 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"}
  184. assert.Nil(t, signCert(args, ob, eb))
  185. assert.Empty(t, ob.String())
  186. assert.Empty(t, eb.String())
  187. // read cert and key files
  188. rb, _ := ioutil.ReadFile(keyF.Name())
  189. lKey, b, err := cert.UnmarshalX25519PrivateKey(rb)
  190. assert.Len(t, b, 0)
  191. assert.Nil(t, err)
  192. assert.Len(t, lKey, 32)
  193. rb, _ = ioutil.ReadFile(crtF.Name())
  194. lCrt, b, err := cert.UnmarshalNebulaCertificateFromPEM(rb)
  195. assert.Len(t, b, 0)
  196. assert.Nil(t, err)
  197. assert.Equal(t, "test", lCrt.Details.Name)
  198. assert.Equal(t, "1.1.1.1/24", lCrt.Details.Ips[0].String())
  199. assert.Len(t, lCrt.Details.Ips, 1)
  200. assert.False(t, lCrt.Details.IsCA)
  201. assert.Equal(t, []string{"1", "2", "3", "4", "5"}, lCrt.Details.Groups)
  202. assert.Len(t, lCrt.Details.Subnets, 3)
  203. assert.Len(t, lCrt.Details.PublicKey, 32)
  204. assert.Equal(t, time.Duration(time.Minute*100), lCrt.Details.NotAfter.Sub(lCrt.Details.NotBefore))
  205. sns := []string{}
  206. for _, sn := range lCrt.Details.Subnets {
  207. sns = append(sns, sn.String())
  208. }
  209. assert.Equal(t, []string{"10.1.1.1/32", "10.2.2.2/32", "10.5.5.5/32"}, sns)
  210. issuer, _ := ca.Sha256Sum()
  211. assert.Equal(t, issuer, lCrt.Details.Issuer)
  212. assert.True(t, lCrt.CheckSignature(caPub))
  213. // test proper cert with in-pub
  214. os.Remove(keyF.Name())
  215. os.Remove(crtF.Name())
  216. ob.Reset()
  217. eb.Reset()
  218. 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"}
  219. assert.Nil(t, signCert(args, ob, eb))
  220. assert.Empty(t, ob.String())
  221. assert.Empty(t, eb.String())
  222. // read cert file and check pub key matches in-pub
  223. rb, _ = ioutil.ReadFile(crtF.Name())
  224. lCrt, b, err = cert.UnmarshalNebulaCertificateFromPEM(rb)
  225. assert.Len(t, b, 0)
  226. assert.Nil(t, err)
  227. assert.Equal(t, lCrt.Details.PublicKey, inPub)
  228. // test refuse to sign cert with duration beyond root
  229. ob.Reset()
  230. eb.Reset()
  231. 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"}
  232. assert.EqualError(t, signCert(args, ob, eb), "refusing to sign, root certificate constraints violated: certificate expires after signing certificate")
  233. assert.Empty(t, ob.String())
  234. assert.Empty(t, eb.String())
  235. // create valid cert/key for overwrite tests
  236. os.Remove(keyF.Name())
  237. os.Remove(crtF.Name())
  238. 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"}
  239. assert.Nil(t, signCert(args, ob, eb))
  240. // test that we won't overwrite existing key file
  241. os.Remove(crtF.Name())
  242. ob.Reset()
  243. eb.Reset()
  244. 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"}
  245. assert.EqualError(t, signCert(args, ob, eb), "refusing to overwrite existing key: "+keyF.Name())
  246. assert.Empty(t, ob.String())
  247. assert.Empty(t, eb.String())
  248. // create valid cert/key for overwrite tests
  249. os.Remove(keyF.Name())
  250. os.Remove(crtF.Name())
  251. 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"}
  252. assert.Nil(t, signCert(args, ob, eb))
  253. // test that we won't overwrite existing certificate file
  254. os.Remove(keyF.Name())
  255. ob.Reset()
  256. eb.Reset()
  257. 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"}
  258. assert.EqualError(t, signCert(args, ob, eb), "refusing to overwrite existing cert: "+crtF.Name())
  259. assert.Empty(t, ob.String())
  260. assert.Empty(t, eb.String())
  261. }