nebula_test.go 33 KB

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