nebula_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/ed25519"
  6. "crypto/rand"
  7. "crypto/x509"
  8. "net"
  9. "net/url"
  10. "reflect"
  11. "strings"
  12. "testing"
  13. "time"
  14. "github.com/google/go-cmp/cmp"
  15. "github.com/slackhq/nebula/cert"
  16. "go.step.sm/crypto/jose"
  17. "go.step.sm/crypto/randutil"
  18. "go.step.sm/crypto/x25519"
  19. "go.step.sm/crypto/x509util"
  20. "golang.org/x/crypto/ssh"
  21. )
  22. func mustNebulaIPNet(t *testing.T, s string) *net.IPNet {
  23. t.Helper()
  24. ip, ipNet, err := net.ParseCIDR(s)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. if ip = ip.To4(); ip == nil {
  29. t.Fatalf("nebula only supports ipv4, have %s", s)
  30. }
  31. ipNet.IP = ip
  32. return ipNet
  33. }
  34. func mustNebulaCA(t *testing.T) (*cert.NebulaCertificate, ed25519.PrivateKey) {
  35. t.Helper()
  36. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. nc := &cert.NebulaCertificate{
  41. Details: cert.NebulaCertificateDetails{
  42. Name: "TestCA",
  43. Groups: []string{"test"},
  44. Ips: []*net.IPNet{
  45. mustNebulaIPNet(t, "10.1.0.0/16"),
  46. },
  47. Subnets: []*net.IPNet{},
  48. NotBefore: time.Now(),
  49. NotAfter: time.Now().Add(10 * time.Minute),
  50. PublicKey: pub,
  51. IsCA: true,
  52. },
  53. }
  54. if err := nc.Sign(priv); err != nil {
  55. t.Fatal(err)
  56. }
  57. return nc, priv
  58. }
  59. func mustNebulaCert(t *testing.T, name string, ipNet *net.IPNet, groups []string, ca *cert.NebulaCertificate, signer ed25519.PrivateKey) (*cert.NebulaCertificate, crypto.Signer) {
  60. t.Helper()
  61. pub, priv, err := x25519.GenerateKey(rand.Reader)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. issuer, err := ca.Sha256Sum()
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. invertedGroups := make(map[string]struct{}, len(groups))
  70. for _, name := range groups {
  71. invertedGroups[name] = struct{}{}
  72. }
  73. t1 := time.Now().Truncate(time.Second)
  74. nc := &cert.NebulaCertificate{
  75. Details: cert.NebulaCertificateDetails{
  76. Name: name,
  77. Ips: []*net.IPNet{ipNet},
  78. Subnets: []*net.IPNet{},
  79. Groups: groups,
  80. NotBefore: t1,
  81. NotAfter: t1.Add(5 * time.Minute),
  82. PublicKey: pub,
  83. IsCA: false,
  84. Issuer: issuer,
  85. InvertedGroups: invertedGroups,
  86. },
  87. }
  88. if err := nc.Sign(signer); err != nil {
  89. t.Fatal(err)
  90. }
  91. return nc, priv
  92. }
  93. func mustNebulaProvisioner(t *testing.T) (*Nebula, *cert.NebulaCertificate, ed25519.PrivateKey) {
  94. t.Helper()
  95. nc, signer := mustNebulaCA(t)
  96. ncPem, err := nc.MarshalToPEM()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. bTrue := true
  101. p := &Nebula{
  102. Type: TypeNebula.String(),
  103. Name: "nebulous",
  104. Roots: ncPem,
  105. Claims: &Claims{
  106. EnableSSHCA: &bTrue,
  107. },
  108. }
  109. if err := p.Init(Config{
  110. Claims: globalProvisionerClaims,
  111. Audiences: testAudiences,
  112. }); err != nil {
  113. t.Fatal(err)
  114. }
  115. return p, nc, signer
  116. }
  117. func mustNebulaToken(t *testing.T, sub, iss, aud string, iat time.Time, sans []string, nc *cert.NebulaCertificate, key crypto.Signer) string {
  118. t.Helper()
  119. ncDer, err := nc.Marshal()
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. so := new(jose.SignerOptions)
  124. so.WithType("JWT")
  125. so.WithHeader(NebulaCertHeader, ncDer)
  126. sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.XEdDSA, Key: key}, so)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. id, err := randutil.ASCII(64)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. claims := struct {
  135. jose.Claims
  136. SANS []string `json:"sans"`
  137. }{
  138. Claims: jose.Claims{
  139. ID: id,
  140. Subject: sub,
  141. Issuer: iss,
  142. IssuedAt: jose.NewNumericDate(iat),
  143. NotBefore: jose.NewNumericDate(iat),
  144. Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
  145. Audience: []string{aud},
  146. },
  147. SANS: sans,
  148. }
  149. tok, err := jose.Signed(sig).Claims(claims).CompactSerialize()
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. return tok
  154. }
  155. func mustNebulaSSHToken(t *testing.T, sub, iss, aud string, iat time.Time, opts *SignSSHOptions, nc *cert.NebulaCertificate, key crypto.Signer) string {
  156. t.Helper()
  157. ncDer, err := nc.Marshal()
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. so := new(jose.SignerOptions)
  162. so.WithType("JWT")
  163. so.WithHeader(NebulaCertHeader, ncDer)
  164. sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.XEdDSA, Key: key}, so)
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. id, err := randutil.ASCII(64)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. claims := struct {
  173. jose.Claims
  174. Step *stepPayload `json:"step,omitempty"`
  175. }{
  176. Claims: jose.Claims{
  177. ID: id,
  178. Subject: sub,
  179. Issuer: iss,
  180. IssuedAt: jose.NewNumericDate(iat),
  181. NotBefore: jose.NewNumericDate(iat),
  182. Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
  183. Audience: []string{aud},
  184. },
  185. }
  186. if opts != nil {
  187. claims.Step = &stepPayload{
  188. SSH: opts,
  189. }
  190. }
  191. tok, err := jose.Signed(sig).Claims(claims).CompactSerialize()
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. return tok
  196. }
  197. func TestNebula_Init(t *testing.T) {
  198. nc, _ := mustNebulaCA(t)
  199. ncPem, err := nc.MarshalToPEM()
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. cfg := Config{
  204. Claims: globalProvisionerClaims,
  205. Audiences: testAudiences,
  206. }
  207. type fields struct {
  208. Type string
  209. Name string
  210. Roots []byte
  211. Claims *Claims
  212. Options *Options
  213. }
  214. type args struct {
  215. config Config
  216. }
  217. tests := []struct {
  218. name string
  219. fields fields
  220. args args
  221. wantErr bool
  222. }{
  223. {"ok", fields{"Nebula", "Nebulous", ncPem, nil, nil}, args{cfg}, false},
  224. {"ok with claims", fields{"Nebula", "Nebulous", ncPem, &Claims{DefaultTLSDur: &Duration{Duration: time.Hour}}, nil}, args{cfg}, false},
  225. {"ok with options", fields{"Nebula", "Nebulous", ncPem, nil, &Options{X509: &X509Options{Template: x509util.DefaultLeafTemplate}}}, args{cfg}, false},
  226. {"fail type", fields{"", "Nebulous", ncPem, nil, nil}, args{cfg}, true},
  227. {"fail name", fields{"Nebula", "", ncPem, nil, nil}, args{cfg}, true},
  228. {"fail root", fields{"Nebula", "Nebulous", nil, nil, nil}, args{cfg}, true},
  229. {"fail bad root", fields{"Nebula", "Nebulous", ncPem[:16], nil, nil}, args{cfg}, true},
  230. {"fail bad claims", fields{"Nebula", "Nebulous", ncPem, &Claims{
  231. MinTLSDur: &Duration{Duration: 0},
  232. }, nil}, args{cfg}, true},
  233. }
  234. for _, tt := range tests {
  235. t.Run(tt.name, func(t *testing.T) {
  236. p := &Nebula{
  237. Type: tt.fields.Type,
  238. Name: tt.fields.Name,
  239. Roots: tt.fields.Roots,
  240. Claims: tt.fields.Claims,
  241. Options: tt.fields.Options,
  242. }
  243. if err := p.Init(tt.args.config); (err != nil) != tt.wantErr {
  244. t.Errorf("Nebula.Init() error = %v, wantErr %v", err, tt.wantErr)
  245. }
  246. })
  247. }
  248. }
  249. func TestNebula_GetID(t *testing.T) {
  250. type fields struct {
  251. ID string
  252. Name string
  253. }
  254. tests := []struct {
  255. name string
  256. fields fields
  257. want string
  258. }{
  259. {"ok with id", fields{"1234", "nebulous"}, "1234"},
  260. {"ok with name", fields{"", "nebulous"}, "nebula/nebulous"},
  261. }
  262. for _, tt := range tests {
  263. t.Run(tt.name, func(t *testing.T) {
  264. p := &Nebula{
  265. ID: tt.fields.ID,
  266. Name: tt.fields.Name,
  267. }
  268. if got := p.GetID(); got != tt.want {
  269. t.Errorf("Nebula.GetID() = %v, want %v", got, tt.want)
  270. }
  271. })
  272. }
  273. }
  274. func TestNebula_GetIDForToken(t *testing.T) {
  275. type fields struct {
  276. Name string
  277. }
  278. tests := []struct {
  279. name string
  280. fields fields
  281. want string
  282. }{
  283. {"ok", fields{"nebulous"}, "nebula/nebulous"},
  284. }
  285. for _, tt := range tests {
  286. t.Run(tt.name, func(t *testing.T) {
  287. p := &Nebula{
  288. Name: tt.fields.Name,
  289. }
  290. if got := p.GetIDForToken(); got != tt.want {
  291. t.Errorf("Nebula.GetIDForToken() = %v, want %v", got, tt.want)
  292. }
  293. })
  294. }
  295. }
  296. func TestNebula_GetTokenID(t *testing.T) {
  297. p, ca, signer := mustNebulaProvisioner(t)
  298. c1, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"group"}, ca, signer)
  299. t1 := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], now(), []string{"test.lan", "10.1.0.1"}, c1, priv)
  300. _, claims, err := parseToken(t1)
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. type args struct {
  305. token string
  306. }
  307. tests := []struct {
  308. name string
  309. p *Nebula
  310. args args
  311. want string
  312. wantErr bool
  313. }{
  314. {"ok", p, args{t1}, claims.ID, false},
  315. {"fail parse", p, args{"token"}, "", true},
  316. {"fail claims", p, args{func() string {
  317. parts := strings.Split(t1, ".")
  318. return parts[0] + ".eyIifQ." + parts[1]
  319. }()}, "", true},
  320. }
  321. for _, tt := range tests {
  322. t.Run(tt.name, func(t *testing.T) {
  323. got, err := tt.p.GetTokenID(tt.args.token)
  324. if (err != nil) != tt.wantErr {
  325. t.Errorf("Nebula.GetTokenID() error = %v, wantErr %v", err, tt.wantErr)
  326. return
  327. }
  328. if got != tt.want {
  329. t.Errorf("Nebula.GetTokenID() = %v, want %v", got, tt.want)
  330. }
  331. })
  332. }
  333. }
  334. func TestNebula_GetName(t *testing.T) {
  335. type fields struct {
  336. Name string
  337. }
  338. tests := []struct {
  339. name string
  340. fields fields
  341. want string
  342. }{
  343. {"ok", fields{"nebulous"}, "nebulous"},
  344. }
  345. for _, tt := range tests {
  346. t.Run(tt.name, func(t *testing.T) {
  347. p := &Nebula{
  348. Name: tt.fields.Name,
  349. }
  350. if got := p.GetName(); got != tt.want {
  351. t.Errorf("Nebula.GetName() = %v, want %v", got, tt.want)
  352. }
  353. })
  354. }
  355. }
  356. func TestNebula_GetType(t *testing.T) {
  357. type fields struct {
  358. Type string
  359. }
  360. tests := []struct {
  361. name string
  362. fields fields
  363. want Type
  364. }{
  365. {"ok", fields{"Nebula"}, TypeNebula},
  366. }
  367. for _, tt := range tests {
  368. t.Run(tt.name, func(t *testing.T) {
  369. p := &Nebula{
  370. Type: tt.fields.Type,
  371. }
  372. if got := p.GetType(); got != tt.want {
  373. t.Errorf("Nebula.GetType() = %v, want %v", got, tt.want)
  374. }
  375. })
  376. }
  377. }
  378. func TestNebula_GetEncryptedKey(t *testing.T) {
  379. tests := []struct {
  380. name string
  381. p *Nebula
  382. wantKid string
  383. wantKey string
  384. wantOk bool
  385. }{
  386. {"ok", &Nebula{}, "", "", false},
  387. }
  388. for _, tt := range tests {
  389. t.Run(tt.name, func(t *testing.T) {
  390. gotKid, gotKey, gotOk := tt.p.GetEncryptedKey()
  391. if gotKid != tt.wantKid {
  392. t.Errorf("Nebula.GetEncryptedKey() gotKid = %v, want %v", gotKid, tt.wantKid)
  393. }
  394. if gotKey != tt.wantKey {
  395. t.Errorf("Nebula.GetEncryptedKey() gotKey = %v, want %v", gotKey, tt.wantKey)
  396. }
  397. if gotOk != tt.wantOk {
  398. t.Errorf("Nebula.GetEncryptedKey() gotOk = %v, want %v", gotOk, tt.wantOk)
  399. }
  400. })
  401. }
  402. }
  403. func TestNebula_AuthorizeSign(t *testing.T) {
  404. ctx := context.TODO()
  405. p, ca, signer := mustNebulaProvisioner(t)
  406. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  407. ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], now(), []string{"test.lan", "10.1.0.1"}, crt, priv)
  408. okNoSANs := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], now(), nil, crt, priv)
  409. pBadOptions, _, _ := mustNebulaProvisioner(t)
  410. pBadOptions.caPool = p.caPool
  411. pBadOptions.Options = &Options{
  412. X509: &X509Options{
  413. TemplateData: []byte(`{""}`),
  414. },
  415. }
  416. type args struct {
  417. ctx context.Context
  418. token string
  419. }
  420. tests := []struct {
  421. name string
  422. p *Nebula
  423. args args
  424. wantErr bool
  425. }{
  426. {"ok", p, args{ctx, ok}, false},
  427. {"ok no sans", p, args{ctx, okNoSANs}, false},
  428. {"fail token", p, args{ctx, "token"}, true},
  429. {"fail template", pBadOptions, args{ctx, ok}, true},
  430. }
  431. for _, tt := range tests {
  432. t.Run(tt.name, func(t *testing.T) {
  433. _, err := tt.p.AuthorizeSign(tt.args.ctx, tt.args.token)
  434. if (err != nil) != tt.wantErr {
  435. t.Errorf("Nebula.AuthorizeSign() error = %v, wantErr %v", err, tt.wantErr)
  436. return
  437. }
  438. })
  439. }
  440. }
  441. func TestNebula_AuthorizeSSHSign(t *testing.T) {
  442. ctx := context.TODO()
  443. // Ok provisioner
  444. p, ca, signer := mustNebulaProvisioner(t)
  445. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  446. ok := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], now(), &SignSSHOptions{
  447. CertType: "host",
  448. KeyID: "test.lan",
  449. Principals: []string{"test.lan", "10.1.0.1"},
  450. }, crt, priv)
  451. okNoOptions := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], now(), nil, crt, priv)
  452. okWithValidity := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], now(), &SignSSHOptions{
  453. ValidAfter: NewTimeDuration(now().Add(1 * time.Hour)),
  454. ValidBefore: NewTimeDuration(now().Add(10 * time.Hour)),
  455. }, crt, priv)
  456. failUserCert := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], now(), &SignSSHOptions{
  457. CertType: "user",
  458. }, crt, priv)
  459. failPrincipals := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], now(), &SignSSHOptions{
  460. CertType: "host",
  461. KeyID: "test.lan",
  462. Principals: []string{"test.lan", "10.1.0.1", "foo.bar"},
  463. }, crt, priv)
  464. // Provisioner with SSH disabled
  465. var bFalse bool
  466. pDisabled, _, _ := mustNebulaProvisioner(t)
  467. pDisabled.caPool = p.caPool
  468. pDisabled.Claims.EnableSSHCA = &bFalse
  469. // Provisioner with bad templates
  470. pBadOptions, _, _ := mustNebulaProvisioner(t)
  471. pBadOptions.caPool = p.caPool
  472. pBadOptions.Options = &Options{
  473. SSH: &SSHOptions{
  474. TemplateData: []byte(`{""}`),
  475. },
  476. }
  477. type args struct {
  478. ctx context.Context
  479. token string
  480. }
  481. tests := []struct {
  482. name string
  483. p *Nebula
  484. args args
  485. wantErr bool
  486. }{
  487. {"ok", p, args{ctx, ok}, false},
  488. {"ok no options", p, args{ctx, okNoOptions}, false},
  489. {"ok with validity", p, args{ctx, okWithValidity}, false},
  490. {"fail token", p, args{ctx, "token"}, true},
  491. {"fail user", p, args{ctx, failUserCert}, true},
  492. {"fail principals", p, args{ctx, failPrincipals}, true},
  493. {"fail disabled", pDisabled, args{ctx, ok}, true},
  494. {"fail template", pBadOptions, args{ctx, ok}, true},
  495. }
  496. for _, tt := range tests {
  497. t.Run(tt.name, func(t *testing.T) {
  498. _, err := tt.p.AuthorizeSSHSign(tt.args.ctx, tt.args.token)
  499. if (err != nil) != tt.wantErr {
  500. t.Errorf("Nebula.AuthorizeSSHSign() error = %v, wantErr %v", err, tt.wantErr)
  501. return
  502. }
  503. })
  504. }
  505. }
  506. func TestNebula_AuthorizeRenew(t *testing.T) {
  507. ctx := context.TODO()
  508. now := time.Now().Truncate(time.Second)
  509. // Ok provisioner
  510. p, _, _ := mustNebulaProvisioner(t)
  511. // Provisioner with renewal disabled
  512. bTrue := true
  513. pDisabled, _, _ := mustNebulaProvisioner(t)
  514. pDisabled.Claims.DisableRenewal = &bTrue
  515. type args struct {
  516. ctx context.Context
  517. crt *x509.Certificate
  518. }
  519. tests := []struct {
  520. name string
  521. p *Nebula
  522. args args
  523. wantErr bool
  524. }{
  525. {"ok", p, args{ctx, &x509.Certificate{
  526. NotBefore: now,
  527. NotAfter: now.Add(time.Hour),
  528. }}, false},
  529. {"fail disabled", pDisabled, args{ctx, &x509.Certificate{
  530. NotBefore: now,
  531. NotAfter: now.Add(time.Hour),
  532. }}, true},
  533. }
  534. for _, tt := range tests {
  535. t.Run(tt.name, func(t *testing.T) {
  536. if err := tt.p.AuthorizeRenew(tt.args.ctx, tt.args.crt); (err != nil) != tt.wantErr {
  537. t.Errorf("Nebula.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
  538. }
  539. })
  540. }
  541. }
  542. func TestNebula_AuthorizeRevoke(t *testing.T) {
  543. ctx := context.TODO()
  544. // Ok provisioner
  545. p, ca, signer := mustNebulaProvisioner(t)
  546. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  547. ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Revoke[0], now(), nil, crt, priv)
  548. // Fail different CA
  549. nc, signer := mustNebulaCA(t)
  550. crt, priv = mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, nc, signer)
  551. failToken := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Revoke[0], now(), nil, crt, priv)
  552. type args struct {
  553. ctx context.Context
  554. token string
  555. }
  556. tests := []struct {
  557. name string
  558. p *Nebula
  559. args args
  560. wantErr bool
  561. }{
  562. {"ok", p, args{ctx, ok}, false},
  563. {"fail token", p, args{ctx, failToken}, true},
  564. }
  565. for _, tt := range tests {
  566. t.Run(tt.name, func(t *testing.T) {
  567. if err := tt.p.AuthorizeRevoke(tt.args.ctx, tt.args.token); (err != nil) != tt.wantErr {
  568. t.Errorf("Nebula.AuthorizeRevoke() error = %v, wantErr %v", err, tt.wantErr)
  569. }
  570. })
  571. }
  572. }
  573. func TestNebula_AuthorizeSSHRevoke(t *testing.T) {
  574. ctx := context.TODO()
  575. // Ok provisioner
  576. p, ca, signer := mustNebulaProvisioner(t)
  577. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  578. ok := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRevoke[0], now(), nil, crt, priv)
  579. // Fail different CA
  580. nc, signer := mustNebulaCA(t)
  581. crt, priv = mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, nc, signer)
  582. failToken := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRevoke[0], now(), nil, crt, priv)
  583. // Provisioner with SSH disabled
  584. var bFalse bool
  585. pDisabled, _, _ := mustNebulaProvisioner(t)
  586. pDisabled.caPool = p.caPool
  587. pDisabled.Claims.EnableSSHCA = &bFalse
  588. type args struct {
  589. ctx context.Context
  590. token string
  591. }
  592. tests := []struct {
  593. name string
  594. p *Nebula
  595. args args
  596. wantErr bool
  597. }{
  598. {"ok", p, args{ctx, ok}, false},
  599. {"fail token", p, args{ctx, failToken}, true},
  600. {"fail disabled", pDisabled, args{ctx, ok}, true},
  601. }
  602. for _, tt := range tests {
  603. t.Run(tt.name, func(t *testing.T) {
  604. if err := tt.p.AuthorizeSSHRevoke(tt.args.ctx, tt.args.token); (err != nil) != tt.wantErr {
  605. t.Errorf("Nebula.AuthorizeSSHRevoke() error = %v, wantErr %v", err, tt.wantErr)
  606. }
  607. })
  608. }
  609. }
  610. func TestNebula_AuthorizeSSHRenew(t *testing.T) {
  611. p, ca, signer := mustNebulaProvisioner(t)
  612. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  613. t1 := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRenew[0], now(), nil, crt, priv)
  614. type args struct {
  615. ctx context.Context
  616. token string
  617. }
  618. tests := []struct {
  619. name string
  620. p *Nebula
  621. args args
  622. want *ssh.Certificate
  623. wantErr bool
  624. }{
  625. {"fail", p, args{context.TODO(), t1}, nil, true},
  626. }
  627. for _, tt := range tests {
  628. t.Run(tt.name, func(t *testing.T) {
  629. got, err := tt.p.AuthorizeSSHRenew(tt.args.ctx, tt.args.token)
  630. if (err != nil) != tt.wantErr {
  631. t.Errorf("Nebula.AuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
  632. return
  633. }
  634. if !reflect.DeepEqual(got, tt.want) {
  635. t.Errorf("Nebula.AuthorizeSSHRenew() = %v, want %v", got, tt.want)
  636. }
  637. })
  638. }
  639. }
  640. func TestNebula_AuthorizeSSHRekey(t *testing.T) {
  641. p, ca, signer := mustNebulaProvisioner(t)
  642. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  643. t1 := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRekey[0], now(), nil, crt, priv)
  644. type args struct {
  645. ctx context.Context
  646. token string
  647. }
  648. tests := []struct {
  649. name string
  650. p *Nebula
  651. args args
  652. want *ssh.Certificate
  653. want1 []SignOption
  654. wantErr bool
  655. }{
  656. {"fail", p, args{context.TODO(), t1}, nil, nil, true},
  657. }
  658. for _, tt := range tests {
  659. t.Run(tt.name, func(t *testing.T) {
  660. got, got1, err := tt.p.AuthorizeSSHRekey(tt.args.ctx, tt.args.token)
  661. if (err != nil) != tt.wantErr {
  662. t.Errorf("Nebula.AuthorizeSSHRekey() error = %v, wantErr %v", err, tt.wantErr)
  663. return
  664. }
  665. if !reflect.DeepEqual(got, tt.want) {
  666. t.Errorf("Nebula.AuthorizeSSHRekey() got = %v, want %v", got, tt.want)
  667. }
  668. if !reflect.DeepEqual(got1, tt.want1) {
  669. t.Errorf("Nebula.AuthorizeSSHRekey() got1 = %v, want %v", got1, tt.want1)
  670. }
  671. })
  672. }
  673. }
  674. func TestNebula_authorizeToken(t *testing.T) {
  675. t1 := now()
  676. p, ca, signer := mustNebulaProvisioner(t)
  677. crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
  678. ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1, []string{"10.1.0.1"}, crt, priv)
  679. okNoSANs := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1, nil, crt, priv)
  680. okSSH := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], t1, &SignSSHOptions{
  681. CertType: "host",
  682. KeyID: "test.lan",
  683. Principals: []string{"test.lan"},
  684. }, crt, priv)
  685. okSSHNoOptions := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], t1, nil, crt, priv)
  686. // Token with errors
  687. failNotBefore := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1.Add(1*time.Hour), []string{"10.1.0.1"}, crt, priv)
  688. failIssuer := mustNebulaToken(t, "test.lan", "foo", p.ctl.Audiences.Sign[0], t1, []string{"10.1.0.1"}, crt, priv)
  689. failAudience := mustNebulaToken(t, "test.lan", p.Name, "foo", t1, []string{"10.1.0.1"}, crt, priv)
  690. failSubject := mustNebulaToken(t, "", p.Name, p.ctl.Audiences.Sign[0], t1, []string{"10.1.0.1"}, crt, priv)
  691. // Not a nebula token
  692. jwk, err := generateJSONWebKey()
  693. if err != nil {
  694. t.Fatal(err)
  695. }
  696. simpleToken, err := generateSimpleToken("iss", "aud", jwk)
  697. if err != nil {
  698. t.Fatal(err)
  699. }
  700. // Provisioner with a different CA
  701. p2, _, _ := mustNebulaProvisioner(t)
  702. x509Claims := jose.Claims{
  703. ID: "[REPLACEME]",
  704. Subject: "test.lan",
  705. Issuer: p.Name,
  706. IssuedAt: jose.NewNumericDate(t1),
  707. NotBefore: jose.NewNumericDate(t1),
  708. Expiry: jose.NewNumericDate(t1.Add(5 * time.Minute)),
  709. Audience: []string{p.ctl.Audiences.Sign[0]},
  710. }
  711. sshClaims := jose.Claims{
  712. ID: "[REPLACEME]",
  713. Subject: "test.lan",
  714. Issuer: p.Name,
  715. IssuedAt: jose.NewNumericDate(t1),
  716. NotBefore: jose.NewNumericDate(t1),
  717. Expiry: jose.NewNumericDate(t1.Add(5 * time.Minute)),
  718. Audience: []string{p.ctl.Audiences.SSHSign[0]},
  719. }
  720. type args struct {
  721. token string
  722. audiences []string
  723. }
  724. tests := []struct {
  725. name string
  726. p *Nebula
  727. args args
  728. want *cert.NebulaCertificate
  729. want1 *jwtPayload
  730. wantErr bool
  731. }{
  732. {"ok x509", p, args{ok, p.ctl.Audiences.Sign}, crt, &jwtPayload{
  733. Claims: x509Claims,
  734. SANs: []string{"10.1.0.1"},
  735. }, false},
  736. {"ok x509 no sans", p, args{okNoSANs, p.ctl.Audiences.Sign}, crt, &jwtPayload{
  737. Claims: x509Claims,
  738. }, false},
  739. {"ok ssh", p, args{okSSH, p.ctl.Audiences.SSHSign}, crt, &jwtPayload{
  740. Claims: sshClaims,
  741. Step: &stepPayload{
  742. SSH: &SignSSHOptions{
  743. CertType: "host",
  744. KeyID: "test.lan",
  745. Principals: []string{"test.lan"},
  746. },
  747. },
  748. }, false},
  749. {"ok ssh no principals", p, args{okSSHNoOptions, p.ctl.Audiences.SSHSign}, crt, &jwtPayload{
  750. Claims: sshClaims,
  751. }, false},
  752. {"fail parse", p, args{"bad.token", p.ctl.Audiences.Sign}, nil, nil, true},
  753. {"fail header", p, args{simpleToken, p.ctl.Audiences.Sign}, nil, nil, true},
  754. {"fail verify", p2, args{ok, p.ctl.Audiences.Sign}, nil, nil, true},
  755. {"fail claims nbf", p, args{failNotBefore, p.ctl.Audiences.Sign}, nil, nil, true},
  756. {"fail claims iss", p, args{failIssuer, p.ctl.Audiences.Sign}, nil, nil, true},
  757. {"fail claims aud", p, args{failAudience, p.ctl.Audiences.Sign}, nil, nil, true},
  758. {"fail claims sub", p, args{failSubject, p.ctl.Audiences.Sign}, nil, nil, true},
  759. }
  760. for _, tt := range tests {
  761. t.Run(tt.name, func(t *testing.T) {
  762. got, got1, err := tt.p.authorizeToken(tt.args.token, tt.args.audiences)
  763. if (err != nil) != tt.wantErr {
  764. t.Errorf("Nebula.authorizeToken() error = %v, wantErr %v", err, tt.wantErr)
  765. return
  766. }
  767. if !reflect.DeepEqual(got, tt.want) {
  768. t.Errorf("Nebula.authorizeToken() got = %#v, want %#v", got, tt.want)
  769. t.Error(cmp.Equal(got, tt.want))
  770. }
  771. if got1 != nil && tt.want1 != nil {
  772. tt.want1.ID = got1.ID
  773. }
  774. if !reflect.DeepEqual(got1, tt.want1) {
  775. t.Errorf("Nebula.authorizeToken() got1 = %v, want %v", got1, tt.want1)
  776. }
  777. })
  778. }
  779. }
  780. func Test_nebulaSANsValidator_Valid(t *testing.T) {
  781. ipNet := mustNebulaIPNet(t, "10.1.2.3/16")
  782. type fields struct {
  783. Name string
  784. IPs []*net.IPNet
  785. }
  786. type args struct {
  787. req *x509.CertificateRequest
  788. }
  789. tests := []struct {
  790. name string
  791. fields fields
  792. args args
  793. wantErr bool
  794. }{
  795. {"ok", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  796. DNSNames: []string{"dns.name"},
  797. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  798. }}, false},
  799. {"ok name only", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  800. DNSNames: []string{"dns.name"},
  801. }}, false},
  802. {"ok ip only", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  803. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  804. }}, false},
  805. {"ok email name", fields{"jane@doe.org", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  806. EmailAddresses: []string{"jane@doe.org"},
  807. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  808. }}, false},
  809. {"ok uri name", fields{"urn:foobar", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  810. URIs: []*url.URL{{Scheme: "urn", Opaque: "foobar"}},
  811. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  812. }}, false},
  813. {"ok ip name", fields{"127.0.0.1", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  814. IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv4(10, 1, 2, 3)},
  815. }}, false},
  816. {"ok multiple ips", fields{"dns.name", []*net.IPNet{ipNet, mustNebulaIPNet(t, "10.2.2.3/8")}}, args{&x509.CertificateRequest{
  817. DNSNames: []string{"dns.name"},
  818. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3), net.IPv4(10, 2, 2, 3)},
  819. }}, false},
  820. {"fail dns", fields{"fail.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  821. DNSNames: []string{"dns.name"},
  822. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  823. }}, true},
  824. {"fail email", fields{"fail@doe.org", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  825. EmailAddresses: []string{"jane@doe.org"},
  826. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  827. }}, true},
  828. {"fail uri", fields{"urn:barfoo", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  829. URIs: []*url.URL{{Scheme: "urn", Opaque: "foobar"}},
  830. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
  831. }}, true},
  832. {"fail ip", fields{"127.0.0.1", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  833. IPAddresses: []net.IP{net.IPv4(10, 1, 2, 1), net.IPv4(10, 1, 2, 3)},
  834. }}, true},
  835. {"fail nebula ip", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
  836. DNSNames: []string{"dns.name"},
  837. IPAddresses: []net.IP{net.IPv4(10, 2, 2, 3)},
  838. }}, true},
  839. }
  840. for _, tt := range tests {
  841. t.Run(tt.name, func(t *testing.T) {
  842. v := nebulaSANsValidator{
  843. Name: tt.fields.Name,
  844. IPs: tt.fields.IPs,
  845. }
  846. if err := v.Valid(tt.args.req); (err != nil) != tt.wantErr {
  847. t.Errorf("nebulaSANsValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  848. }
  849. })
  850. }
  851. }
  852. func Test_nebulaPrincipalsValidator_Valid(t *testing.T) {
  853. ipNet := mustNebulaIPNet(t, "10.1.2.3/16")
  854. type fields struct {
  855. Name string
  856. IPs []*net.IPNet
  857. }
  858. type args struct {
  859. got SignSSHOptions
  860. }
  861. tests := []struct {
  862. name string
  863. fields fields
  864. args args
  865. wantErr bool
  866. }{
  867. {"ok", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
  868. Principals: []string{"dns.name", "10.1.2.3"},
  869. }}, false},
  870. {"ok name", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
  871. Principals: []string{"dns.name"},
  872. }}, false},
  873. {"ok ip", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
  874. Principals: []string{"10.1.2.3"},
  875. }}, false},
  876. {"fail name", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
  877. Principals: []string{"foo.name", "10.1.2.3"},
  878. }}, true},
  879. {"fail ip", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
  880. Principals: []string{"dns.name", "10.2.2.3"},
  881. }}, true},
  882. }
  883. for _, tt := range tests {
  884. t.Run(tt.name, func(t *testing.T) {
  885. v := nebulaPrincipalsValidator{
  886. Name: tt.fields.Name,
  887. IPs: tt.fields.IPs,
  888. }
  889. if err := v.Valid(tt.args.got); (err != nil) != tt.wantErr {
  890. t.Errorf("nebulaPrincipalsValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
  891. }
  892. })
  893. }
  894. }