sign_test.go 12 KB

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