nebula_test.go 34 KB

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