nebula_test.go 34 KB

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