engine_test.go 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846
  1. package policy
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "net"
  6. "net/url"
  7. "testing"
  8. "github.com/google/go-cmp/cmp"
  9. "golang.org/x/crypto/ssh"
  10. "github.com/smallstep/assert"
  11. )
  12. // TODO(hs): the functionality in the policy engine is a nice candidate for trying fuzzing on
  13. // TODO(hs): more complex test use cases that combine multiple names and permitted/excluded entries?
  14. func TestNamePolicyEngine_matchDomainConstraint(t *testing.T) {
  15. tests := []struct {
  16. name string
  17. allowLiteralWildcardNames bool
  18. domain string
  19. constraint string
  20. want bool
  21. wantErr bool
  22. }{
  23. {
  24. name: "fail/wildcard",
  25. domain: "host.local",
  26. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  27. want: false,
  28. wantErr: false,
  29. },
  30. {
  31. name: "fail/wildcard-literal",
  32. domain: "*.example.com",
  33. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  34. want: false,
  35. wantErr: false,
  36. },
  37. {
  38. name: "fail/specific-domain",
  39. domain: "www.example.com",
  40. constraint: "host.example.com",
  41. want: false,
  42. wantErr: false,
  43. },
  44. {
  45. name: "fail/single-whitespace-domain",
  46. domain: " ",
  47. constraint: "host.example.com",
  48. want: false,
  49. wantErr: false,
  50. },
  51. {
  52. name: "fail/period-domain",
  53. domain: ".host.example.com",
  54. constraint: ".example.com",
  55. want: false,
  56. wantErr: false,
  57. },
  58. {
  59. name: "fail/wrong-asterisk-prefix",
  60. domain: "*Xexample.com",
  61. constraint: ".example.com",
  62. want: false,
  63. wantErr: false,
  64. },
  65. {
  66. name: "fail/asterisk-in-domain",
  67. domain: "e*ample.com",
  68. constraint: ".com",
  69. want: false,
  70. wantErr: false,
  71. },
  72. {
  73. name: "fail/asterisk-label",
  74. domain: "example.*.local",
  75. constraint: ".local",
  76. want: false,
  77. wantErr: false,
  78. },
  79. {
  80. name: "fail/multiple-periods",
  81. domain: "example.local",
  82. constraint: "..local",
  83. want: false,
  84. wantErr: false,
  85. },
  86. {
  87. name: "fail/error-parsing-domain",
  88. domain: string(byte(0)),
  89. constraint: ".local",
  90. want: false,
  91. wantErr: true,
  92. },
  93. {
  94. name: "fail/error-parsing-constraint",
  95. domain: "example.local",
  96. constraint: string(byte(0)),
  97. want: false,
  98. wantErr: true,
  99. },
  100. {
  101. name: "fail/no-subdomain",
  102. domain: "local",
  103. constraint: ".local",
  104. want: false,
  105. wantErr: false,
  106. },
  107. {
  108. name: "fail/too-many-subdomains",
  109. domain: "www.example.local",
  110. constraint: ".local",
  111. want: false,
  112. wantErr: false,
  113. },
  114. {
  115. name: "fail/wrong-domain",
  116. domain: "example.notlocal",
  117. constraint: ".local",
  118. want: false,
  119. wantErr: false,
  120. },
  121. {
  122. name: "false/idna-internationalized-domain-name",
  123. domain: "JP納豆.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  124. constraint: ".例.jp",
  125. want: false,
  126. wantErr: true,
  127. },
  128. {
  129. name: "false/idna-internationalized-domain-name-constraint",
  130. domain: "xn--jp-cd2fp15c.xn--fsq.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  131. constraint: ".例.jp",
  132. want: false,
  133. wantErr: true,
  134. },
  135. {
  136. name: "ok/empty-constraint",
  137. domain: "www.example.com",
  138. constraint: "",
  139. want: true,
  140. wantErr: false,
  141. },
  142. {
  143. name: "ok/wildcard",
  144. domain: "www.example.com",
  145. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  146. want: true,
  147. wantErr: false,
  148. },
  149. {
  150. name: "ok/wildcard-literal",
  151. allowLiteralWildcardNames: true,
  152. domain: "*.example.com", // specifically allowed using an option on the NamePolicyEngine
  153. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  154. want: true,
  155. wantErr: false,
  156. },
  157. {
  158. name: "ok/specific-domain",
  159. domain: "www.example.com",
  160. constraint: "www.example.com",
  161. want: true,
  162. wantErr: false,
  163. },
  164. {
  165. name: "ok/different-case",
  166. domain: "WWW.EXAMPLE.com",
  167. constraint: "www.example.com",
  168. want: true,
  169. wantErr: false,
  170. },
  171. {
  172. name: "ok/idna-internationalized-domain-name-punycode",
  173. domain: "xn--jp-cd2fp15c.xn--fsq.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  174. constraint: ".xn--fsq.jp",
  175. want: true,
  176. wantErr: false,
  177. },
  178. }
  179. for _, tt := range tests {
  180. t.Run(tt.name, func(t *testing.T) {
  181. engine := NamePolicyEngine{
  182. allowLiteralWildcardNames: tt.allowLiteralWildcardNames,
  183. }
  184. got, err := engine.matchDomainConstraint(tt.domain, tt.constraint)
  185. if (err != nil) != tt.wantErr {
  186. t.Errorf("NamePolicyEngine.matchDomainConstraint() error = %v, wantErr %v", err, tt.wantErr)
  187. return
  188. }
  189. if got != tt.want {
  190. t.Errorf("NamePolicyEngine.matchDomainConstraint() = %v, want %v", got, tt.want)
  191. }
  192. })
  193. }
  194. }
  195. func Test_matchIPConstraint(t *testing.T) {
  196. nat64IP, nat64Net, err := net.ParseCIDR("64:ff9b::/96")
  197. assert.FatalError(t, err)
  198. tests := []struct {
  199. name string
  200. ip net.IP
  201. constraint *net.IPNet
  202. want bool
  203. wantErr bool
  204. }{
  205. {
  206. name: "false/ipv4-in-ipv6-nat64",
  207. ip: net.ParseIP("192.0.2.128"),
  208. constraint: nat64Net,
  209. want: false,
  210. wantErr: false,
  211. },
  212. {
  213. name: "ok/ipv4",
  214. ip: net.ParseIP("127.0.0.1"),
  215. constraint: &net.IPNet{
  216. IP: net.ParseIP("127.0.0.0"),
  217. Mask: net.IPv4Mask(255, 255, 255, 0),
  218. },
  219. want: true,
  220. wantErr: false,
  221. },
  222. {
  223. name: "ok/ipv6",
  224. ip: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"),
  225. constraint: &net.IPNet{
  226. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  227. Mask: net.CIDRMask(120, 128),
  228. },
  229. want: true,
  230. wantErr: false,
  231. },
  232. {
  233. name: "ok/ipv4-in-ipv6", // ipv4 in ipv6 addresses are considered the same in the current implementation, because Go parses them as IPv4
  234. ip: net.ParseIP("::ffff:192.0.2.128"),
  235. constraint: &net.IPNet{
  236. IP: net.ParseIP("192.0.2.0"),
  237. Mask: net.IPv4Mask(255, 255, 255, 0),
  238. },
  239. want: true,
  240. wantErr: false,
  241. },
  242. {
  243. name: "ok/ipv4-in-ipv6-nat64-fixed-ip",
  244. ip: nat64IP,
  245. constraint: nat64Net,
  246. want: true,
  247. wantErr: false,
  248. },
  249. {
  250. name: "ok/ipv4-in-ipv6-nat64",
  251. ip: net.ParseIP("64:ff9b::192.0.2.129"),
  252. constraint: nat64Net,
  253. want: true,
  254. wantErr: false,
  255. },
  256. }
  257. for _, tt := range tests {
  258. t.Run(tt.name, func(t *testing.T) {
  259. got, err := matchIPConstraint(tt.ip, tt.constraint)
  260. if (err != nil) != tt.wantErr {
  261. t.Errorf("matchIPConstraint() error = %v, wantErr %v", err, tt.wantErr)
  262. return
  263. }
  264. if got != tt.want {
  265. t.Errorf("matchIPConstraint() = %v, want %v", got, tt.want)
  266. }
  267. })
  268. }
  269. }
  270. func TestNamePolicyEngine_matchEmailConstraint(t *testing.T) {
  271. tests := []struct {
  272. name string
  273. engine *NamePolicyEngine
  274. mailbox rfc2821Mailbox
  275. constraint string
  276. want bool
  277. wantErr bool
  278. }{
  279. {
  280. name: "fail/asterisk-prefix",
  281. engine: &NamePolicyEngine{},
  282. mailbox: rfc2821Mailbox{
  283. local: "mail",
  284. domain: "local",
  285. },
  286. constraint: "*@example.com",
  287. want: false,
  288. wantErr: true,
  289. },
  290. {
  291. name: "fail/asterisk-label",
  292. engine: &NamePolicyEngine{},
  293. mailbox: rfc2821Mailbox{
  294. local: "mail",
  295. domain: "local",
  296. },
  297. constraint: "@host.*.example.com",
  298. want: false,
  299. wantErr: true,
  300. },
  301. {
  302. name: "fail/asterisk-inside-local",
  303. engine: &NamePolicyEngine{},
  304. mailbox: rfc2821Mailbox{
  305. local: "mail",
  306. domain: "local",
  307. },
  308. constraint: "m*il@local",
  309. want: false,
  310. wantErr: true,
  311. },
  312. {
  313. name: "fail/asterisk-inside-domain",
  314. engine: &NamePolicyEngine{},
  315. mailbox: rfc2821Mailbox{
  316. local: "mail",
  317. domain: "local",
  318. },
  319. constraint: "@h*st.example.com",
  320. want: false,
  321. wantErr: true,
  322. },
  323. {
  324. name: "fail/parse-email",
  325. engine: &NamePolicyEngine{},
  326. mailbox: rfc2821Mailbox{
  327. local: "mail",
  328. domain: "local",
  329. },
  330. constraint: "@example.com",
  331. want: false,
  332. wantErr: true,
  333. },
  334. {
  335. name: "fail/wildcard",
  336. engine: &NamePolicyEngine{},
  337. mailbox: rfc2821Mailbox{
  338. local: "mail",
  339. domain: "local",
  340. },
  341. constraint: "example.com",
  342. want: false,
  343. wantErr: false,
  344. },
  345. {
  346. name: "fail/wildcard-x509-period",
  347. engine: &NamePolicyEngine{},
  348. mailbox: rfc2821Mailbox{
  349. local: "mail",
  350. domain: "local",
  351. },
  352. constraint: ".local", // "wildcard" for the local domain; requires exactly 1 subdomain
  353. want: false,
  354. wantErr: false,
  355. },
  356. {
  357. name: "fail/specific-mail-wrong-domain",
  358. engine: &NamePolicyEngine{},
  359. mailbox: rfc2821Mailbox{
  360. local: "mail",
  361. domain: "local",
  362. },
  363. constraint: "mail@example.com",
  364. want: false,
  365. wantErr: false,
  366. },
  367. {
  368. name: "fail/specific-mail-wrong-local",
  369. engine: &NamePolicyEngine{},
  370. mailbox: rfc2821Mailbox{
  371. local: "root",
  372. domain: "example.com",
  373. },
  374. constraint: "mail@example.com",
  375. want: false,
  376. wantErr: false,
  377. },
  378. {
  379. name: "ok/wildcard",
  380. engine: &NamePolicyEngine{},
  381. mailbox: rfc2821Mailbox{
  382. local: "mail",
  383. domain: "local",
  384. },
  385. constraint: "local", // "wildcard" for the local domain
  386. want: true,
  387. wantErr: false,
  388. },
  389. {
  390. name: "ok/wildcard-x509-period",
  391. engine: &NamePolicyEngine{},
  392. mailbox: rfc2821Mailbox{
  393. local: "mail",
  394. domain: "example.local",
  395. },
  396. constraint: ".local", // "wildcard" for the local domain; requires exactly 1 subdomain
  397. want: true,
  398. wantErr: false,
  399. },
  400. {
  401. name: "ok/specific-mail",
  402. engine: &NamePolicyEngine{},
  403. mailbox: rfc2821Mailbox{
  404. local: "mail",
  405. domain: "local",
  406. },
  407. constraint: "mail@local",
  408. want: true,
  409. wantErr: false,
  410. },
  411. {
  412. name: "ok/wildcard-tld",
  413. engine: &NamePolicyEngine{},
  414. mailbox: rfc2821Mailbox{
  415. local: "mail",
  416. domain: "example.com",
  417. },
  418. constraint: "example.com", // "wildcard" for 'example.com'
  419. want: true,
  420. wantErr: false,
  421. },
  422. {
  423. name: "ok/different-case",
  424. engine: &NamePolicyEngine{},
  425. mailbox: rfc2821Mailbox{
  426. local: "mail",
  427. domain: "EXAMPLE.com",
  428. },
  429. constraint: "example.com", // "wildcard" for 'example.com'
  430. want: true,
  431. wantErr: false,
  432. },
  433. }
  434. for _, tt := range tests {
  435. t.Run(tt.name, func(t *testing.T) {
  436. got, err := tt.engine.matchEmailConstraint(tt.mailbox, tt.constraint)
  437. if (err != nil) != tt.wantErr {
  438. t.Errorf("NamePolicyEngine.matchEmailConstraint() error = %v, wantErr %v", err, tt.wantErr)
  439. return
  440. }
  441. if got != tt.want {
  442. t.Errorf("NamePolicyEngine.matchEmailConstraint() = %v, want %v", got, tt.want)
  443. }
  444. })
  445. }
  446. }
  447. func TestNamePolicyEngine_matchURIConstraint(t *testing.T) {
  448. tests := []struct {
  449. name string
  450. engine *NamePolicyEngine
  451. uri *url.URL
  452. constraint string
  453. want bool
  454. wantErr bool
  455. }{
  456. {
  457. name: "fail/empty-host",
  458. engine: &NamePolicyEngine{},
  459. uri: &url.URL{
  460. Scheme: "https",
  461. Host: "",
  462. },
  463. constraint: ".local",
  464. want: false,
  465. wantErr: true,
  466. },
  467. {
  468. name: "fail/host-with-asterisk-prefix",
  469. engine: &NamePolicyEngine{},
  470. uri: &url.URL{
  471. Scheme: "https",
  472. Host: "*.local",
  473. },
  474. constraint: ".local",
  475. want: false,
  476. wantErr: true,
  477. },
  478. {
  479. name: "fail/host-with-asterisk-label",
  480. engine: &NamePolicyEngine{},
  481. uri: &url.URL{
  482. Scheme: "https",
  483. Host: "host.*.local",
  484. },
  485. constraint: ".local",
  486. want: false,
  487. wantErr: true,
  488. },
  489. {
  490. name: "fail/host-with-asterisk-inside",
  491. engine: &NamePolicyEngine{},
  492. uri: &url.URL{
  493. Scheme: "https",
  494. Host: "h*st.local",
  495. },
  496. constraint: ".local",
  497. want: false,
  498. wantErr: true,
  499. },
  500. {
  501. name: "fail/wildcard",
  502. engine: &NamePolicyEngine{},
  503. uri: &url.URL{
  504. Scheme: "https",
  505. Host: "www.example.notlocal",
  506. },
  507. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  508. want: false,
  509. wantErr: false,
  510. },
  511. {
  512. name: "fail/wildcard-subdomains-too-deep",
  513. engine: &NamePolicyEngine{},
  514. uri: &url.URL{
  515. Scheme: "https",
  516. Host: "www.sub.example.local",
  517. },
  518. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  519. want: false,
  520. wantErr: false,
  521. },
  522. {
  523. name: "fail/host-with-port-split-error",
  524. engine: &NamePolicyEngine{},
  525. uri: &url.URL{
  526. Scheme: "https",
  527. Host: "www.example.local::8080",
  528. },
  529. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  530. want: false,
  531. wantErr: true,
  532. },
  533. {
  534. name: "fail/host-with-ipv4",
  535. engine: &NamePolicyEngine{},
  536. uri: &url.URL{
  537. Scheme: "https",
  538. Host: "127.0.0.1",
  539. },
  540. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  541. want: false,
  542. wantErr: true,
  543. },
  544. {
  545. name: "fail/host-with-ipv6",
  546. engine: &NamePolicyEngine{},
  547. uri: &url.URL{
  548. Scheme: "https",
  549. Host: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
  550. },
  551. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  552. want: false,
  553. wantErr: true,
  554. },
  555. {
  556. name: "ok/wildcard",
  557. engine: &NamePolicyEngine{},
  558. uri: &url.URL{
  559. Scheme: "https",
  560. Host: "www.example.local",
  561. },
  562. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  563. want: true,
  564. wantErr: false,
  565. },
  566. {
  567. name: "ok/host-with-port",
  568. engine: &NamePolicyEngine{},
  569. uri: &url.URL{
  570. Scheme: "https",
  571. Host: "www.example.local:8080",
  572. },
  573. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  574. want: true,
  575. wantErr: false,
  576. },
  577. {
  578. name: "ok/different-case",
  579. engine: &NamePolicyEngine{},
  580. uri: &url.URL{
  581. Scheme: "https",
  582. Host: "www.EXAMPLE.local",
  583. },
  584. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  585. want: true,
  586. wantErr: false,
  587. },
  588. }
  589. for _, tt := range tests {
  590. t.Run(tt.name, func(t *testing.T) {
  591. got, err := tt.engine.matchURIConstraint(tt.uri, tt.constraint)
  592. if (err != nil) != tt.wantErr {
  593. t.Errorf("NamePolicyEngine.matchURIConstraint() error = %v, wantErr %v", err, tt.wantErr)
  594. return
  595. }
  596. if got != tt.want {
  597. t.Errorf("NamePolicyEngine.matchURIConstraint() = %v, want %v", got, tt.want)
  598. }
  599. })
  600. }
  601. }
  602. func extractSANs(cert *x509.Certificate, includeSubject bool) []string {
  603. sans := []string{}
  604. sans = append(sans, cert.DNSNames...)
  605. for _, ip := range cert.IPAddresses {
  606. sans = append(sans, ip.String())
  607. }
  608. sans = append(sans, cert.EmailAddresses...)
  609. for _, uri := range cert.URIs {
  610. sans = append(sans, uri.String())
  611. }
  612. if includeSubject && cert.Subject.CommonName != "" {
  613. sans = append(sans, cert.Subject.CommonName)
  614. }
  615. return sans
  616. }
  617. func TestNamePolicyEngine_X509_AllAllowed(t *testing.T) {
  618. tests := []struct {
  619. name string
  620. options []NamePolicyOption
  621. cert *x509.Certificate
  622. want bool
  623. wantErr bool
  624. }{
  625. // SINGLE SAN TYPE PERMITTED FAILURE TESTS
  626. {
  627. name: "fail/dns-permitted",
  628. options: []NamePolicyOption{
  629. WithPermittedDNSDomains("*.local"),
  630. },
  631. cert: &x509.Certificate{
  632. DNSNames: []string{"www.example.com"},
  633. },
  634. want: false,
  635. wantErr: true,
  636. },
  637. {
  638. name: "fail/dns-permitted-wildcard-literal-x509",
  639. options: []NamePolicyOption{
  640. WithPermittedDNSDomains("*.x509local"),
  641. },
  642. cert: &x509.Certificate{
  643. DNSNames: []string{
  644. "*.x509local",
  645. },
  646. },
  647. want: false,
  648. wantErr: true,
  649. },
  650. {
  651. name: "fail/dns-permitted-single-host",
  652. options: []NamePolicyOption{
  653. WithPermittedDNSDomains("host.local"),
  654. },
  655. cert: &x509.Certificate{
  656. DNSNames: []string{"differenthost.local"},
  657. },
  658. want: false,
  659. wantErr: true,
  660. },
  661. {
  662. name: "fail/dns-permitted-no-label",
  663. options: []NamePolicyOption{
  664. WithPermittedDNSDomains("*.local"),
  665. },
  666. cert: &x509.Certificate{
  667. DNSNames: []string{"local"},
  668. },
  669. want: false,
  670. wantErr: true,
  671. },
  672. {
  673. name: "fail/dns-permitted-empty-label",
  674. options: []NamePolicyOption{
  675. WithPermittedDNSDomains("*.local"),
  676. },
  677. cert: &x509.Certificate{
  678. DNSNames: []string{"www..local"},
  679. },
  680. want: false,
  681. wantErr: true,
  682. },
  683. {
  684. name: "fail/dns-permitted-dot-domain",
  685. options: []NamePolicyOption{
  686. WithPermittedDNSDomains("*.local"),
  687. },
  688. cert: &x509.Certificate{
  689. DNSNames: []string{
  690. ".local",
  691. },
  692. },
  693. want: false,
  694. wantErr: true,
  695. },
  696. {
  697. name: "fail/dns-permitted-wildcard-multiple-subdomains",
  698. options: []NamePolicyOption{
  699. WithPermittedDNSDomains("*.local"),
  700. },
  701. cert: &x509.Certificate{
  702. DNSNames: []string{
  703. "sub.example.local",
  704. },
  705. },
  706. want: false,
  707. wantErr: true,
  708. },
  709. {
  710. name: "fail/dns-permitted-wildcard-literal",
  711. options: []NamePolicyOption{
  712. WithPermittedDNSDomains("*.local"),
  713. },
  714. cert: &x509.Certificate{
  715. DNSNames: []string{
  716. "*.local",
  717. },
  718. },
  719. want: false,
  720. wantErr: true,
  721. },
  722. {
  723. name: "fail/dns-permitted-idna-internationalized-domain",
  724. options: []NamePolicyOption{
  725. WithPermittedDNSDomains("*.豆.jp"),
  726. },
  727. cert: &x509.Certificate{
  728. DNSNames: []string{
  729. string(byte(0)) + ".例.jp",
  730. },
  731. },
  732. want: false,
  733. wantErr: true,
  734. },
  735. {
  736. name: "fail/ipv4-permitted",
  737. options: []NamePolicyOption{
  738. WithPermittedIPRanges(
  739. &net.IPNet{
  740. IP: net.ParseIP("127.0.0.1"),
  741. Mask: net.IPv4Mask(255, 255, 255, 0),
  742. },
  743. ),
  744. },
  745. cert: &x509.Certificate{
  746. IPAddresses: []net.IP{net.ParseIP("1.1.1.1")},
  747. },
  748. want: false,
  749. wantErr: true,
  750. },
  751. {
  752. name: "fail/ipv6-permitted",
  753. options: []NamePolicyOption{
  754. WithPermittedIPRanges(
  755. &net.IPNet{
  756. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  757. Mask: net.CIDRMask(120, 128),
  758. },
  759. ),
  760. },
  761. cert: &x509.Certificate{
  762. IPAddresses: []net.IP{net.ParseIP("3001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  763. },
  764. want: false,
  765. wantErr: true,
  766. },
  767. {
  768. name: "fail/mail-permitted-wildcard",
  769. options: []NamePolicyOption{
  770. WithPermittedEmailAddresses("@example.com"),
  771. },
  772. cert: &x509.Certificate{
  773. EmailAddresses: []string{
  774. "test@local.com",
  775. },
  776. },
  777. want: false,
  778. wantErr: true,
  779. },
  780. {
  781. name: "fail/mail-permitted-wildcard-x509",
  782. options: []NamePolicyOption{
  783. WithPermittedEmailAddresses("example.com"),
  784. },
  785. cert: &x509.Certificate{
  786. EmailAddresses: []string{
  787. "test@local.com",
  788. },
  789. },
  790. want: false,
  791. wantErr: true,
  792. },
  793. {
  794. name: "fail/mail-permitted-specific-mailbox",
  795. options: []NamePolicyOption{
  796. WithPermittedEmailAddresses("test@local.com"),
  797. },
  798. cert: &x509.Certificate{
  799. EmailAddresses: []string{
  800. "root@local.com",
  801. },
  802. },
  803. want: false,
  804. wantErr: true,
  805. },
  806. {
  807. name: "fail/mail-permitted-wildcard-subdomain",
  808. options: []NamePolicyOption{
  809. WithPermittedEmailAddresses("@example.com"),
  810. },
  811. cert: &x509.Certificate{
  812. EmailAddresses: []string{
  813. "test@sub.example.com",
  814. },
  815. },
  816. want: false,
  817. wantErr: true,
  818. },
  819. {
  820. name: "fail/mail-permitted-idna-internationalized-domain",
  821. options: []NamePolicyOption{
  822. WithPermittedEmailAddresses("@例.jp"),
  823. },
  824. cert: &x509.Certificate{
  825. EmailAddresses: []string{"bücher@例.jp"},
  826. },
  827. want: false,
  828. wantErr: true,
  829. },
  830. {
  831. name: "fail/mail-permitted-idna-internationalized-domain-rfc822",
  832. options: []NamePolicyOption{
  833. WithPermittedEmailAddresses("@例.jp"),
  834. },
  835. cert: &x509.Certificate{
  836. EmailAddresses: []string{"bücher@例.jp" + string(byte(0))},
  837. },
  838. want: false,
  839. wantErr: true,
  840. },
  841. {
  842. name: "fail/mail-permitted-idna-internationalized-domain-ascii",
  843. options: []NamePolicyOption{
  844. WithPermittedEmailAddresses("@例.jp"),
  845. },
  846. cert: &x509.Certificate{
  847. EmailAddresses: []string{"mail@xn---bla.jp"},
  848. },
  849. want: false,
  850. wantErr: true,
  851. },
  852. {
  853. name: "fail/uri-permitted-domain-wildcard",
  854. options: []NamePolicyOption{
  855. WithPermittedURIDomains("*.local"),
  856. },
  857. cert: &x509.Certificate{
  858. URIs: []*url.URL{
  859. {
  860. Scheme: "https",
  861. Host: "example.com",
  862. },
  863. },
  864. },
  865. want: false,
  866. wantErr: true,
  867. },
  868. {
  869. name: "fail/uri-permitted",
  870. options: []NamePolicyOption{
  871. WithPermittedURIDomains("test.local"),
  872. },
  873. cert: &x509.Certificate{
  874. URIs: []*url.URL{
  875. {
  876. Scheme: "https",
  877. Host: "bad.local",
  878. },
  879. },
  880. },
  881. want: false,
  882. wantErr: true,
  883. },
  884. {
  885. name: "fail/uri-permitted-with-literal-wildcard", // don't allow literal wildcard in URI, e.g. xxxx://*.domain.tld
  886. options: []NamePolicyOption{
  887. WithPermittedURIDomains("*.local"),
  888. },
  889. cert: &x509.Certificate{
  890. URIs: []*url.URL{
  891. {
  892. Scheme: "https",
  893. Host: "*.local",
  894. },
  895. },
  896. },
  897. want: false,
  898. wantErr: true,
  899. },
  900. {
  901. name: "fail/uri-permitted-idna-internationalized-domain",
  902. options: []NamePolicyOption{
  903. WithPermittedURIDomains("*.bücher.example.com"),
  904. },
  905. cert: &x509.Certificate{
  906. URIs: []*url.URL{
  907. {
  908. Scheme: "https",
  909. Host: "abc.bücher.example.com",
  910. },
  911. },
  912. },
  913. want: false,
  914. wantErr: true,
  915. },
  916. // SINGLE SAN TYPE EXCLUDED FAILURE TESTS
  917. {
  918. name: "fail/dns-excluded",
  919. options: []NamePolicyOption{
  920. WithExcludedDNSDomains("*.example.com"),
  921. },
  922. cert: &x509.Certificate{
  923. DNSNames: []string{"www.example.com"},
  924. },
  925. want: false,
  926. wantErr: true,
  927. },
  928. {
  929. name: "fail/dns-excluded-single-host",
  930. options: []NamePolicyOption{
  931. WithExcludedDNSDomains("host.example.com"),
  932. },
  933. cert: &x509.Certificate{
  934. DNSNames: []string{"host.example.com"},
  935. },
  936. want: false,
  937. wantErr: true,
  938. },
  939. {
  940. name: "fail/ipv4-excluded",
  941. options: []NamePolicyOption{
  942. WithExcludedIPRanges(
  943. &net.IPNet{
  944. IP: net.ParseIP("127.0.0.1"),
  945. Mask: net.IPv4Mask(255, 255, 255, 0),
  946. },
  947. ),
  948. },
  949. cert: &x509.Certificate{
  950. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  951. },
  952. want: false,
  953. wantErr: true,
  954. },
  955. {
  956. name: "fail/ipv6-excluded",
  957. options: []NamePolicyOption{
  958. WithExcludedIPRanges(
  959. &net.IPNet{
  960. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  961. Mask: net.CIDRMask(120, 128),
  962. },
  963. ),
  964. },
  965. cert: &x509.Certificate{
  966. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  967. },
  968. want: false,
  969. wantErr: true,
  970. },
  971. {
  972. name: "fail/mail-excluded",
  973. options: []NamePolicyOption{
  974. WithExcludedEmailAddresses("@example.com"),
  975. },
  976. cert: &x509.Certificate{
  977. EmailAddresses: []string{"mail@example.com"},
  978. },
  979. want: false,
  980. wantErr: true,
  981. },
  982. {
  983. name: "fail/uri-excluded",
  984. options: []NamePolicyOption{
  985. WithExcludedURIDomains("*.example.com"),
  986. },
  987. cert: &x509.Certificate{
  988. URIs: []*url.URL{
  989. {
  990. Scheme: "https",
  991. Host: "www.example.com",
  992. },
  993. },
  994. },
  995. want: false,
  996. wantErr: true,
  997. },
  998. {
  999. name: "fail/uri-excluded-with-literal-wildcard", // don't allow literal wildcard in URI, e.g. xxxx://*.domain.tld
  1000. options: []NamePolicyOption{
  1001. WithExcludedURIDomains("*.local"),
  1002. },
  1003. cert: &x509.Certificate{
  1004. URIs: []*url.URL{
  1005. {
  1006. Scheme: "https",
  1007. Host: "*.local",
  1008. },
  1009. },
  1010. },
  1011. want: false,
  1012. wantErr: true,
  1013. },
  1014. // SUBJECT FAILURE TESTS
  1015. {
  1016. name: "fail/subject-dns-permitted",
  1017. options: []NamePolicyOption{
  1018. WithSubjectCommonNameVerification(),
  1019. WithPermittedDNSDomains("*.local"),
  1020. },
  1021. cert: &x509.Certificate{
  1022. Subject: pkix.Name{
  1023. CommonName: "example.notlocal",
  1024. },
  1025. },
  1026. want: false,
  1027. wantErr: true,
  1028. },
  1029. {
  1030. name: "fail/subject-dns-excluded",
  1031. options: []NamePolicyOption{
  1032. WithSubjectCommonNameVerification(),
  1033. WithExcludedDNSDomains("*.local"),
  1034. },
  1035. cert: &x509.Certificate{
  1036. Subject: pkix.Name{
  1037. CommonName: "example.local",
  1038. },
  1039. },
  1040. want: false,
  1041. wantErr: true,
  1042. },
  1043. {
  1044. name: "fail/subject-ipv4-permitted",
  1045. options: []NamePolicyOption{
  1046. WithSubjectCommonNameVerification(),
  1047. WithPermittedIPRanges(
  1048. &net.IPNet{
  1049. IP: net.ParseIP("127.0.0.1"),
  1050. Mask: net.IPv4Mask(255, 255, 255, 0),
  1051. },
  1052. ),
  1053. },
  1054. cert: &x509.Certificate{
  1055. Subject: pkix.Name{
  1056. CommonName: "10.10.10.10",
  1057. },
  1058. },
  1059. want: false,
  1060. wantErr: true,
  1061. },
  1062. {
  1063. name: "fail/subject-ipv4-excluded",
  1064. options: []NamePolicyOption{
  1065. WithSubjectCommonNameVerification(),
  1066. WithExcludedIPRanges(
  1067. &net.IPNet{
  1068. IP: net.ParseIP("127.0.0.1"),
  1069. Mask: net.IPv4Mask(255, 255, 255, 0),
  1070. },
  1071. ),
  1072. },
  1073. cert: &x509.Certificate{
  1074. Subject: pkix.Name{
  1075. CommonName: "127.0.0.30",
  1076. },
  1077. },
  1078. want: false,
  1079. wantErr: true,
  1080. },
  1081. {
  1082. name: "fail/subject-ipv6-permitted",
  1083. options: []NamePolicyOption{
  1084. WithSubjectCommonNameVerification(),
  1085. WithPermittedIPRanges(
  1086. &net.IPNet{
  1087. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1088. Mask: net.CIDRMask(120, 128),
  1089. },
  1090. ),
  1091. },
  1092. cert: &x509.Certificate{
  1093. Subject: pkix.Name{
  1094. CommonName: "2002:0db8:85a3:0000:0000:8a2e:0370:7339",
  1095. },
  1096. },
  1097. want: false,
  1098. wantErr: true,
  1099. },
  1100. {
  1101. name: "fail/subject-ipv6-excluded",
  1102. options: []NamePolicyOption{
  1103. WithSubjectCommonNameVerification(),
  1104. WithExcludedIPRanges(
  1105. &net.IPNet{
  1106. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1107. Mask: net.CIDRMask(120, 128),
  1108. },
  1109. ),
  1110. },
  1111. cert: &x509.Certificate{
  1112. Subject: pkix.Name{
  1113. CommonName: "2001:0db8:85a3:0000:0000:8a2e:0370:7339",
  1114. },
  1115. },
  1116. want: false,
  1117. wantErr: true,
  1118. },
  1119. {
  1120. name: "fail/subject-email-permitted",
  1121. options: []NamePolicyOption{
  1122. WithSubjectCommonNameVerification(),
  1123. WithPermittedEmailAddresses("@example.local"),
  1124. },
  1125. cert: &x509.Certificate{
  1126. Subject: pkix.Name{
  1127. CommonName: "mail@smallstep.com",
  1128. },
  1129. },
  1130. want: false,
  1131. wantErr: true,
  1132. },
  1133. {
  1134. name: "fail/subject-email-excluded",
  1135. options: []NamePolicyOption{
  1136. WithSubjectCommonNameVerification(),
  1137. WithExcludedEmailAddresses("@example.local"),
  1138. },
  1139. cert: &x509.Certificate{
  1140. Subject: pkix.Name{
  1141. CommonName: "mail@example.local",
  1142. },
  1143. },
  1144. want: false,
  1145. wantErr: true,
  1146. },
  1147. {
  1148. name: "fail/subject-uri-permitted",
  1149. options: []NamePolicyOption{
  1150. WithSubjectCommonNameVerification(),
  1151. WithPermittedURIDomains("*.example.com"),
  1152. },
  1153. cert: &x509.Certificate{
  1154. Subject: pkix.Name{
  1155. CommonName: "https://www.google.com",
  1156. },
  1157. },
  1158. want: false,
  1159. wantErr: true,
  1160. },
  1161. {
  1162. name: "fail/subject-uri-excluded",
  1163. options: []NamePolicyOption{
  1164. WithSubjectCommonNameVerification(),
  1165. WithExcludedURIDomains("*.example.com"),
  1166. },
  1167. cert: &x509.Certificate{
  1168. Subject: pkix.Name{
  1169. CommonName: "https://www.example.com",
  1170. },
  1171. },
  1172. want: false,
  1173. wantErr: true,
  1174. },
  1175. // DIFFERENT SAN PERMITTED FAILURE TESTS
  1176. {
  1177. name: "fail/dns-permitted-with-ip-name", // when only DNS is permitted, IPs are not allowed.
  1178. options: []NamePolicyOption{
  1179. WithPermittedDNSDomains("*.local"),
  1180. },
  1181. cert: &x509.Certificate{
  1182. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  1183. },
  1184. want: false,
  1185. wantErr: true,
  1186. },
  1187. {
  1188. name: "fail/dns-permitted-with-mail", // when only DNS is permitted, mails are not allowed.
  1189. options: []NamePolicyOption{
  1190. WithPermittedDNSDomains("*.local"),
  1191. },
  1192. cert: &x509.Certificate{
  1193. EmailAddresses: []string{"mail@smallstep.com"},
  1194. },
  1195. want: false,
  1196. wantErr: true,
  1197. },
  1198. {
  1199. name: "fail/dns-permitted-with-uri", // when only DNS is permitted, URIs are not allowed.
  1200. options: []NamePolicyOption{
  1201. WithPermittedDNSDomains("*.local"),
  1202. },
  1203. cert: &x509.Certificate{
  1204. URIs: []*url.URL{
  1205. {
  1206. Scheme: "https",
  1207. Host: "www.example.com",
  1208. },
  1209. },
  1210. },
  1211. want: false,
  1212. wantErr: true,
  1213. },
  1214. {
  1215. name: "fail/ip-permitted-with-dns-name", // when only IP is permitted, DNS names are not allowed.
  1216. options: []NamePolicyOption{
  1217. WithPermittedIPRanges(
  1218. &net.IPNet{
  1219. IP: net.ParseIP("127.0.0.1"),
  1220. Mask: net.IPv4Mask(255, 255, 255, 0),
  1221. },
  1222. ),
  1223. },
  1224. cert: &x509.Certificate{
  1225. DNSNames: []string{"www.example.com"},
  1226. },
  1227. want: false,
  1228. wantErr: true,
  1229. },
  1230. {
  1231. name: "fail/ip-permitted-with-mail", // when only IP is permitted, mails are not allowed.
  1232. options: []NamePolicyOption{
  1233. WithPermittedIPRanges(
  1234. &net.IPNet{
  1235. IP: net.ParseIP("127.0.0.1"),
  1236. Mask: net.IPv4Mask(255, 255, 255, 0),
  1237. },
  1238. ),
  1239. },
  1240. cert: &x509.Certificate{
  1241. EmailAddresses: []string{"mail@smallstep.com"},
  1242. },
  1243. want: false,
  1244. wantErr: true,
  1245. },
  1246. {
  1247. name: "fail/ip-permitted-with-uri", // when only IP is permitted, URIs are not allowed.
  1248. options: []NamePolicyOption{
  1249. WithPermittedIPRanges(
  1250. &net.IPNet{
  1251. IP: net.ParseIP("127.0.0.1"),
  1252. Mask: net.IPv4Mask(255, 255, 255, 0),
  1253. },
  1254. ),
  1255. },
  1256. cert: &x509.Certificate{
  1257. URIs: []*url.URL{
  1258. {
  1259. Scheme: "https",
  1260. Host: "www.example.com",
  1261. },
  1262. },
  1263. },
  1264. want: false,
  1265. wantErr: true,
  1266. },
  1267. {
  1268. name: "fail/mail-permitted-with-dns-name", // when only mail is permitted, DNS names are not allowed.
  1269. options: []NamePolicyOption{
  1270. WithPermittedEmailAddresses("@example.com"),
  1271. },
  1272. cert: &x509.Certificate{
  1273. DNSNames: []string{"www.example.com"},
  1274. },
  1275. want: false,
  1276. wantErr: true,
  1277. },
  1278. {
  1279. name: "fail/mail-permitted-with-ip", // when only mail is permitted, IPs are not allowed.
  1280. options: []NamePolicyOption{
  1281. WithPermittedEmailAddresses("@example.com"),
  1282. },
  1283. cert: &x509.Certificate{
  1284. IPAddresses: []net.IP{
  1285. net.ParseIP("127.0.0.1"),
  1286. },
  1287. },
  1288. want: false,
  1289. wantErr: true,
  1290. },
  1291. {
  1292. name: "fail/mail-permitted-with-uri", // when only mail is permitted, URIs are not allowed.
  1293. options: []NamePolicyOption{
  1294. WithPermittedEmailAddresses("@example.com"),
  1295. },
  1296. cert: &x509.Certificate{
  1297. URIs: []*url.URL{
  1298. {
  1299. Scheme: "https",
  1300. Host: "www.example.com",
  1301. },
  1302. },
  1303. },
  1304. want: false,
  1305. wantErr: true,
  1306. },
  1307. {
  1308. name: "fail/uri-permitted-with-dns-name", // when only URI is permitted, DNS names are not allowed.
  1309. options: []NamePolicyOption{
  1310. WithPermittedURIDomains("*.local"),
  1311. },
  1312. cert: &x509.Certificate{
  1313. DNSNames: []string{"host.local"},
  1314. },
  1315. want: false,
  1316. wantErr: true,
  1317. },
  1318. {
  1319. name: "fail/uri-permitted-with-ip-name", // when only URI is permitted, IPs are not allowed.
  1320. options: []NamePolicyOption{
  1321. WithPermittedURIDomains("*.local"),
  1322. },
  1323. cert: &x509.Certificate{
  1324. IPAddresses: []net.IP{
  1325. net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1326. },
  1327. },
  1328. want: false,
  1329. wantErr: true,
  1330. },
  1331. {
  1332. name: "fail/uri-permitted-with-ip-name", // when only URI is permitted, mails are not allowed.
  1333. options: []NamePolicyOption{
  1334. WithPermittedURIDomains("*.local"),
  1335. },
  1336. cert: &x509.Certificate{
  1337. EmailAddresses: []string{"mail@smallstep.com"},
  1338. },
  1339. want: false,
  1340. wantErr: true,
  1341. },
  1342. // COMBINED FAILURE TESTS
  1343. {
  1344. name: "fail/combined-simple-all-badhost.local",
  1345. options: []NamePolicyOption{
  1346. WithSubjectCommonNameVerification(),
  1347. WithPermittedDNSDomains("*.local"),
  1348. WithPermittedCIDRs("127.0.0.1/24"),
  1349. WithPermittedEmailAddresses("@example.local"),
  1350. WithPermittedURIDomains("*.example.local"),
  1351. WithExcludedDNSDomains("badhost.local"),
  1352. WithExcludedCIDRs("127.0.0.128/25"),
  1353. WithExcludedEmailAddresses("badmail@example.local"),
  1354. WithExcludedURIDomains("badwww.example.local"),
  1355. },
  1356. cert: &x509.Certificate{
  1357. Subject: pkix.Name{
  1358. CommonName: "badhost.local",
  1359. },
  1360. DNSNames: []string{"example.local"},
  1361. IPAddresses: []net.IP{net.ParseIP("127.0.0.40")},
  1362. EmailAddresses: []string{"mail@example.local"},
  1363. URIs: []*url.URL{
  1364. {
  1365. Scheme: "https",
  1366. Host: "www.example.local",
  1367. },
  1368. },
  1369. },
  1370. want: false,
  1371. wantErr: true,
  1372. },
  1373. // NO CONSTRAINT SUCCESS TESTS
  1374. {
  1375. name: "ok/dns-no-constraints",
  1376. options: []NamePolicyOption{},
  1377. cert: &x509.Certificate{
  1378. DNSNames: []string{"www.example.com"},
  1379. },
  1380. want: true,
  1381. wantErr: false,
  1382. },
  1383. {
  1384. name: "ok/ipv4-no-constraints",
  1385. options: []NamePolicyOption{},
  1386. cert: &x509.Certificate{
  1387. IPAddresses: []net.IP{
  1388. net.ParseIP("127.0.0.1"),
  1389. },
  1390. },
  1391. want: true,
  1392. wantErr: false,
  1393. },
  1394. {
  1395. name: "ok/ipv6-no-constraints",
  1396. options: []NamePolicyOption{},
  1397. cert: &x509.Certificate{
  1398. IPAddresses: []net.IP{
  1399. net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1400. },
  1401. },
  1402. want: true,
  1403. wantErr: false,
  1404. },
  1405. {
  1406. name: "ok/mail-no-constraints",
  1407. options: []NamePolicyOption{},
  1408. cert: &x509.Certificate{
  1409. EmailAddresses: []string{"mail@smallstep.com"},
  1410. },
  1411. want: true,
  1412. wantErr: false,
  1413. },
  1414. {
  1415. name: "ok/uri-no-constraints",
  1416. options: []NamePolicyOption{},
  1417. cert: &x509.Certificate{
  1418. URIs: []*url.URL{
  1419. {
  1420. Scheme: "https",
  1421. Host: "www.example.com",
  1422. },
  1423. },
  1424. },
  1425. want: true,
  1426. wantErr: false,
  1427. },
  1428. {
  1429. name: "ok/subject-no-constraints",
  1430. options: []NamePolicyOption{
  1431. WithSubjectCommonNameVerification(),
  1432. },
  1433. cert: &x509.Certificate{
  1434. Subject: pkix.Name{
  1435. CommonName: "www.example.com",
  1436. },
  1437. },
  1438. want: true,
  1439. wantErr: false,
  1440. },
  1441. {
  1442. name: "ok/subject-empty-no-constraints",
  1443. options: []NamePolicyOption{
  1444. WithSubjectCommonNameVerification(),
  1445. },
  1446. cert: &x509.Certificate{
  1447. Subject: pkix.Name{
  1448. CommonName: "",
  1449. },
  1450. },
  1451. want: true,
  1452. wantErr: false,
  1453. },
  1454. // SINGLE SAN TYPE PERMITTED SUCCESS TESTS
  1455. {
  1456. name: "ok/dns-permitted",
  1457. options: []NamePolicyOption{
  1458. WithPermittedDNSDomains("*.local"),
  1459. },
  1460. cert: &x509.Certificate{
  1461. DNSNames: []string{"example.local"},
  1462. },
  1463. want: true,
  1464. wantErr: false,
  1465. },
  1466. {
  1467. name: "ok/dns-permitted-wildcard",
  1468. options: []NamePolicyOption{
  1469. WithPermittedDNSDomains("*.local", "*.x509local"),
  1470. WithAllowLiteralWildcardNames(),
  1471. },
  1472. cert: &x509.Certificate{
  1473. DNSNames: []string{
  1474. "host.local",
  1475. "test.x509local",
  1476. },
  1477. },
  1478. want: true,
  1479. wantErr: false,
  1480. },
  1481. {
  1482. name: "ok/dns-permitted-wildcard-literal",
  1483. options: []NamePolicyOption{
  1484. WithPermittedDNSDomains("*.local", "*.x509local"),
  1485. WithAllowLiteralWildcardNames(),
  1486. },
  1487. cert: &x509.Certificate{
  1488. DNSNames: []string{
  1489. "*.local",
  1490. "*.x509local",
  1491. },
  1492. },
  1493. want: true,
  1494. wantErr: false,
  1495. },
  1496. {
  1497. name: "ok/dns-permitted-combined",
  1498. options: []NamePolicyOption{
  1499. WithPermittedDNSDomains("*.local", "*.x509local", "host.example.com"),
  1500. },
  1501. cert: &x509.Certificate{
  1502. DNSNames: []string{
  1503. "example.local",
  1504. "example.x509local",
  1505. "host.example.com",
  1506. },
  1507. },
  1508. want: true,
  1509. wantErr: false,
  1510. },
  1511. {
  1512. name: "ok/dns-permitted-idna-internationalized-domain",
  1513. options: []NamePolicyOption{
  1514. WithPermittedDNSDomains("*.例.jp"),
  1515. },
  1516. cert: &x509.Certificate{
  1517. DNSNames: []string{
  1518. "JP納豆.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  1519. },
  1520. },
  1521. want: true,
  1522. wantErr: false,
  1523. },
  1524. {
  1525. name: "ok/ipv4-permitted",
  1526. options: []NamePolicyOption{
  1527. WithPermittedCIDRs("127.0.0.1/24"),
  1528. },
  1529. cert: &x509.Certificate{
  1530. IPAddresses: []net.IP{net.ParseIP("127.0.0.20")},
  1531. },
  1532. want: true,
  1533. wantErr: false,
  1534. },
  1535. {
  1536. name: "ok/ipv6-permitted",
  1537. options: []NamePolicyOption{
  1538. WithPermittedCIDRs("2001:0db8:85a3:0000:0000:8a2e:0370:7334/120"),
  1539. },
  1540. cert: &x509.Certificate{
  1541. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7339")},
  1542. },
  1543. want: true,
  1544. wantErr: false,
  1545. },
  1546. {
  1547. name: "ok/mail-permitted-wildcard",
  1548. options: []NamePolicyOption{
  1549. WithPermittedEmailAddresses("@example.com"),
  1550. },
  1551. cert: &x509.Certificate{
  1552. EmailAddresses: []string{
  1553. "test@example.com",
  1554. },
  1555. },
  1556. want: true,
  1557. wantErr: false,
  1558. },
  1559. {
  1560. name: "ok/mail-permitted-plain-domain",
  1561. options: []NamePolicyOption{
  1562. WithPermittedEmailAddresses("example.com"),
  1563. },
  1564. cert: &x509.Certificate{
  1565. EmailAddresses: []string{
  1566. "test@example.com",
  1567. },
  1568. },
  1569. want: true,
  1570. wantErr: false,
  1571. },
  1572. {
  1573. name: "ok/mail-permitted-specific-mailbox",
  1574. options: []NamePolicyOption{
  1575. WithPermittedEmailAddresses("test@local.com"),
  1576. },
  1577. cert: &x509.Certificate{
  1578. EmailAddresses: []string{
  1579. "test@local.com",
  1580. },
  1581. },
  1582. want: true,
  1583. wantErr: false,
  1584. },
  1585. {
  1586. name: "ok/mail-permitted-idna-internationalized-domain",
  1587. options: []NamePolicyOption{
  1588. WithPermittedEmailAddresses("@例.jp"),
  1589. },
  1590. cert: &x509.Certificate{
  1591. EmailAddresses: []string{},
  1592. },
  1593. want: true,
  1594. wantErr: false,
  1595. },
  1596. {
  1597. name: "ok/uri-permitted-domain-wildcard",
  1598. options: []NamePolicyOption{
  1599. WithPermittedURIDomains("*.local"),
  1600. },
  1601. cert: &x509.Certificate{
  1602. URIs: []*url.URL{
  1603. {
  1604. Scheme: "https",
  1605. Host: "example.local",
  1606. },
  1607. },
  1608. },
  1609. want: true,
  1610. wantErr: false,
  1611. },
  1612. {
  1613. name: "ok/uri-permitted-specific-uri",
  1614. options: []NamePolicyOption{
  1615. WithPermittedURIDomains("test.local"),
  1616. },
  1617. cert: &x509.Certificate{
  1618. URIs: []*url.URL{
  1619. {
  1620. Scheme: "https",
  1621. Host: "test.local",
  1622. },
  1623. },
  1624. },
  1625. want: true,
  1626. wantErr: false,
  1627. },
  1628. {
  1629. name: "ok/uri-permitted-with-port",
  1630. options: []NamePolicyOption{
  1631. WithPermittedURIDomains("*.example.com"),
  1632. },
  1633. cert: &x509.Certificate{
  1634. URIs: []*url.URL{
  1635. {
  1636. Scheme: "https",
  1637. Host: "www.example.com:8080",
  1638. },
  1639. },
  1640. },
  1641. want: true,
  1642. wantErr: false,
  1643. },
  1644. {
  1645. name: "ok/uri-permitted-idna-internationalized-domain",
  1646. options: []NamePolicyOption{
  1647. WithPermittedURIDomains("*.bücher.example.com"),
  1648. },
  1649. cert: &x509.Certificate{
  1650. URIs: []*url.URL{
  1651. {
  1652. Scheme: "https",
  1653. Host: "abc.xn--bcher-kva.example.com",
  1654. },
  1655. },
  1656. },
  1657. want: true,
  1658. wantErr: false,
  1659. },
  1660. {
  1661. name: "ok/uri-permitted-idna-internationalized-domain",
  1662. options: []NamePolicyOption{
  1663. WithPermittedURIDomains("bücher.example.com"),
  1664. },
  1665. cert: &x509.Certificate{
  1666. URIs: []*url.URL{
  1667. {
  1668. Scheme: "https",
  1669. Host: "xn--bcher-kva.example.com",
  1670. },
  1671. },
  1672. },
  1673. want: true,
  1674. wantErr: false,
  1675. },
  1676. // SINGLE SAN TYPE EXCLUDED SUCCESS TESTS
  1677. {
  1678. name: "ok/dns-excluded",
  1679. options: []NamePolicyOption{
  1680. WithExcludedDNSDomains("*.notlocal"),
  1681. },
  1682. cert: &x509.Certificate{
  1683. DNSNames: []string{"example.local"},
  1684. },
  1685. want: true,
  1686. wantErr: false,
  1687. },
  1688. {
  1689. name: "ok/ipv4-excluded",
  1690. options: []NamePolicyOption{
  1691. WithExcludedIPRanges(
  1692. &net.IPNet{
  1693. IP: net.ParseIP("127.0.0.1"),
  1694. Mask: net.IPv4Mask(255, 255, 255, 0),
  1695. },
  1696. ),
  1697. },
  1698. cert: &x509.Certificate{
  1699. IPAddresses: []net.IP{net.ParseIP("10.10.10.10")},
  1700. },
  1701. want: true,
  1702. wantErr: false,
  1703. },
  1704. {
  1705. name: "ok/ipv6-excluded",
  1706. options: []NamePolicyOption{
  1707. WithExcludedCIDRs("2001:0db8:85a3:0000:0000:8a2e:0370:7334/120"),
  1708. },
  1709. cert: &x509.Certificate{
  1710. IPAddresses: []net.IP{net.ParseIP("2003:0db8:85a3:0000:0000:8a2e:0370:7334")},
  1711. },
  1712. want: true,
  1713. wantErr: false,
  1714. },
  1715. {
  1716. name: "ok/mail-excluded",
  1717. options: []NamePolicyOption{
  1718. WithExcludedEmailAddresses("@notlocal"),
  1719. },
  1720. cert: &x509.Certificate{
  1721. EmailAddresses: []string{"mail@local"},
  1722. },
  1723. want: true,
  1724. wantErr: false,
  1725. },
  1726. {
  1727. name: "ok/mail-excluded-with-subdomain",
  1728. options: []NamePolicyOption{
  1729. WithExcludedEmailAddresses("@local"),
  1730. },
  1731. cert: &x509.Certificate{
  1732. EmailAddresses: []string{"mail@example.local"},
  1733. },
  1734. want: true,
  1735. wantErr: false,
  1736. },
  1737. {
  1738. name: "ok/uri-excluded",
  1739. options: []NamePolicyOption{
  1740. WithExcludedURIDomains("*.google.com"),
  1741. },
  1742. cert: &x509.Certificate{
  1743. URIs: []*url.URL{
  1744. {
  1745. Scheme: "https",
  1746. Host: "www.example.com",
  1747. },
  1748. },
  1749. },
  1750. want: true,
  1751. wantErr: false,
  1752. },
  1753. // SUBJECT SUCCESS TESTS
  1754. {
  1755. name: "ok/subject-empty",
  1756. options: []NamePolicyOption{
  1757. WithSubjectCommonNameVerification(),
  1758. WithPermittedDNSDomains("*.local"),
  1759. },
  1760. cert: &x509.Certificate{
  1761. Subject: pkix.Name{
  1762. CommonName: "",
  1763. },
  1764. DNSNames: []string{"example.local"},
  1765. },
  1766. want: true,
  1767. wantErr: false,
  1768. },
  1769. {
  1770. name: "ok/subject-dns-permitted",
  1771. options: []NamePolicyOption{
  1772. WithSubjectCommonNameVerification(),
  1773. WithPermittedDNSDomains("*.local"),
  1774. },
  1775. cert: &x509.Certificate{
  1776. Subject: pkix.Name{
  1777. CommonName: "example.local",
  1778. },
  1779. },
  1780. want: true,
  1781. wantErr: false,
  1782. },
  1783. {
  1784. name: "ok/subject-dns-excluded",
  1785. options: []NamePolicyOption{
  1786. WithSubjectCommonNameVerification(),
  1787. WithExcludedDNSDomains("*.notlocal"),
  1788. },
  1789. cert: &x509.Certificate{
  1790. Subject: pkix.Name{
  1791. CommonName: "example.local",
  1792. },
  1793. },
  1794. want: true,
  1795. wantErr: false,
  1796. },
  1797. {
  1798. name: "ok/subject-ipv4-permitted",
  1799. options: []NamePolicyOption{
  1800. WithSubjectCommonNameVerification(),
  1801. WithPermittedIPRanges(
  1802. &net.IPNet{
  1803. IP: net.ParseIP("127.0.0.1"),
  1804. Mask: net.IPv4Mask(255, 255, 255, 0),
  1805. },
  1806. ),
  1807. },
  1808. cert: &x509.Certificate{
  1809. Subject: pkix.Name{
  1810. CommonName: "127.0.0.20",
  1811. },
  1812. },
  1813. want: true,
  1814. wantErr: false,
  1815. },
  1816. {
  1817. name: "ok/subject-ipv4-excluded",
  1818. options: []NamePolicyOption{
  1819. WithSubjectCommonNameVerification(),
  1820. WithExcludedIPRanges(
  1821. &net.IPNet{
  1822. IP: net.ParseIP("128.0.0.1"),
  1823. Mask: net.IPv4Mask(255, 255, 255, 0),
  1824. },
  1825. ),
  1826. },
  1827. cert: &x509.Certificate{
  1828. Subject: pkix.Name{
  1829. CommonName: "127.0.0.1",
  1830. },
  1831. },
  1832. want: true,
  1833. wantErr: false,
  1834. },
  1835. {
  1836. name: "ok/subject-ipv6-permitted",
  1837. options: []NamePolicyOption{
  1838. WithSubjectCommonNameVerification(),
  1839. WithPermittedIPRanges(
  1840. &net.IPNet{
  1841. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1842. Mask: net.CIDRMask(120, 128),
  1843. },
  1844. ),
  1845. },
  1846. cert: &x509.Certificate{
  1847. Subject: pkix.Name{
  1848. CommonName: "2001:0db8:85a3:0000:0000:8a2e:0370:7339",
  1849. },
  1850. },
  1851. want: true,
  1852. wantErr: false,
  1853. },
  1854. {
  1855. name: "ok/subject-ipv6-excluded",
  1856. options: []NamePolicyOption{
  1857. WithSubjectCommonNameVerification(),
  1858. WithExcludedIPRanges(
  1859. &net.IPNet{
  1860. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1861. Mask: net.CIDRMask(120, 128),
  1862. },
  1863. ),
  1864. },
  1865. cert: &x509.Certificate{
  1866. Subject: pkix.Name{
  1867. CommonName: "2009:0db8:85a3:0000:0000:8a2e:0370:7339",
  1868. },
  1869. },
  1870. want: true,
  1871. wantErr: false,
  1872. },
  1873. {
  1874. name: "ok/subject-email-permitted",
  1875. options: []NamePolicyOption{
  1876. WithSubjectCommonNameVerification(),
  1877. WithPermittedEmailAddresses("@example.local"),
  1878. },
  1879. cert: &x509.Certificate{
  1880. Subject: pkix.Name{
  1881. CommonName: "mail@example.local",
  1882. },
  1883. },
  1884. want: true,
  1885. wantErr: false,
  1886. },
  1887. {
  1888. name: "ok/subject-email-excluded",
  1889. options: []NamePolicyOption{
  1890. WithSubjectCommonNameVerification(),
  1891. WithExcludedEmailAddresses("@example.notlocal"),
  1892. },
  1893. cert: &x509.Certificate{
  1894. Subject: pkix.Name{
  1895. CommonName: "mail@example.local",
  1896. },
  1897. },
  1898. want: true,
  1899. wantErr: false,
  1900. },
  1901. {
  1902. name: "ok/subject-uri-permitted",
  1903. options: []NamePolicyOption{
  1904. WithSubjectCommonNameVerification(),
  1905. WithPermittedURIDomains("*.example.com"),
  1906. },
  1907. cert: &x509.Certificate{
  1908. Subject: pkix.Name{
  1909. CommonName: "https://www.example.com",
  1910. },
  1911. },
  1912. want: true,
  1913. wantErr: false,
  1914. },
  1915. {
  1916. name: "ok/subject-uri-excluded",
  1917. options: []NamePolicyOption{
  1918. WithSubjectCommonNameVerification(),
  1919. WithExcludedURIDomains("*.smallstep.com"),
  1920. },
  1921. cert: &x509.Certificate{
  1922. Subject: pkix.Name{
  1923. CommonName: "https://www.example.com",
  1924. },
  1925. },
  1926. want: true,
  1927. wantErr: false,
  1928. },
  1929. // DIFFERENT SAN TYPE EXCLUDED SUCCESS TESTS
  1930. {
  1931. name: "ok/dns-excluded-with-ip-name", // when only DNS is exluded, we allow anything else
  1932. options: []NamePolicyOption{
  1933. WithExcludedDNSDomains("*.local"),
  1934. },
  1935. cert: &x509.Certificate{
  1936. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  1937. },
  1938. want: true,
  1939. wantErr: false,
  1940. },
  1941. {
  1942. name: "ok/dns-excluded-with-mail", // when only DNS is exluded, we allow anything else
  1943. options: []NamePolicyOption{
  1944. WithExcludedDNSDomains("*.local"),
  1945. },
  1946. cert: &x509.Certificate{
  1947. EmailAddresses: []string{"mail@example.com"},
  1948. },
  1949. want: true,
  1950. wantErr: false,
  1951. },
  1952. {
  1953. name: "ok/dns-excluded-with-mail", // when only DNS is exluded, we allow anything else
  1954. options: []NamePolicyOption{
  1955. WithExcludedDNSDomains("*.local"),
  1956. },
  1957. cert: &x509.Certificate{
  1958. URIs: []*url.URL{
  1959. {
  1960. Scheme: "https",
  1961. Host: "www.example.com",
  1962. },
  1963. },
  1964. },
  1965. want: true,
  1966. wantErr: false,
  1967. },
  1968. {
  1969. name: "ok/ip-excluded-with-dns", // when only IP is exluded, we allow anything else
  1970. options: []NamePolicyOption{
  1971. WithExcludedCIDRs("127.0.0.1/24"),
  1972. },
  1973. cert: &x509.Certificate{
  1974. DNSNames: []string{"test.local"},
  1975. },
  1976. want: true,
  1977. wantErr: false,
  1978. },
  1979. {
  1980. name: "ok/ip-excluded-with-mail", // when only IP is exluded, we allow anything else
  1981. options: []NamePolicyOption{
  1982. WithExcludedCIDRs("127.0.0.1/24"),
  1983. },
  1984. cert: &x509.Certificate{
  1985. EmailAddresses: []string{"mail@example.com"},
  1986. },
  1987. want: true,
  1988. wantErr: false,
  1989. },
  1990. {
  1991. name: "ok/ip-excluded-with-mail", // when only IP is exluded, we allow anything else
  1992. options: []NamePolicyOption{
  1993. WithExcludedCIDRs("127.0.0.1/24"),
  1994. },
  1995. cert: &x509.Certificate{
  1996. URIs: []*url.URL{
  1997. {
  1998. Scheme: "https",
  1999. Host: "www.example.com",
  2000. },
  2001. },
  2002. },
  2003. want: true,
  2004. wantErr: false,
  2005. },
  2006. {
  2007. name: "ok/mail-excluded-with-dns", // when only mail is exluded, we allow anything else
  2008. options: []NamePolicyOption{
  2009. WithExcludedEmailAddresses("@example.com"),
  2010. },
  2011. cert: &x509.Certificate{
  2012. DNSNames: []string{"test.local"},
  2013. },
  2014. want: true,
  2015. wantErr: false,
  2016. },
  2017. {
  2018. name: "ok/mail-excluded-with-ip", // when only mail is exluded, we allow anything else
  2019. options: []NamePolicyOption{
  2020. WithExcludedEmailAddresses("@example.com"),
  2021. },
  2022. cert: &x509.Certificate{
  2023. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2024. },
  2025. want: true,
  2026. wantErr: false,
  2027. },
  2028. {
  2029. name: "ok/mail-excluded-with-uri", // when only mail is exluded, we allow anything else
  2030. options: []NamePolicyOption{
  2031. WithExcludedEmailAddresses("@example.com"),
  2032. },
  2033. cert: &x509.Certificate{
  2034. URIs: []*url.URL{
  2035. {
  2036. Scheme: "https",
  2037. Host: "www.example.com",
  2038. },
  2039. },
  2040. },
  2041. want: true,
  2042. wantErr: false,
  2043. },
  2044. {
  2045. name: "ok/uri-excluded-with-dns", // when only URI is exluded, we allow anything else
  2046. options: []NamePolicyOption{
  2047. WithExcludedURIDomains("*.example.local"),
  2048. },
  2049. cert: &x509.Certificate{
  2050. DNSNames: []string{"test.example.local"},
  2051. },
  2052. want: true,
  2053. wantErr: false,
  2054. },
  2055. {
  2056. name: "ok/uri-excluded-with-dns", // when only URI is exluded, we allow anything else
  2057. options: []NamePolicyOption{
  2058. WithExcludedURIDomains("*.example.local"),
  2059. },
  2060. cert: &x509.Certificate{
  2061. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2062. },
  2063. want: true,
  2064. wantErr: false,
  2065. },
  2066. {
  2067. name: "ok/uri-excluded-with-mail", // when only URI is exluded, we allow anything else
  2068. options: []NamePolicyOption{
  2069. WithExcludedURIDomains("*.example.local"),
  2070. },
  2071. cert: &x509.Certificate{
  2072. EmailAddresses: []string{"mail@example.local"},
  2073. },
  2074. want: true,
  2075. wantErr: false,
  2076. },
  2077. {
  2078. name: "ok/dns-excluded-with-subject-ip-name", // when only DNS is exluded, we allow anything else
  2079. options: []NamePolicyOption{
  2080. WithSubjectCommonNameVerification(),
  2081. WithExcludedDNSDomains("*.local"),
  2082. },
  2083. cert: &x509.Certificate{
  2084. Subject: pkix.Name{
  2085. CommonName: "127.0.0.1",
  2086. },
  2087. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2088. },
  2089. want: true,
  2090. wantErr: false,
  2091. },
  2092. // COMBINED SUCCESS TESTS
  2093. {
  2094. name: "ok/combined-simple-permitted",
  2095. options: []NamePolicyOption{
  2096. WithSubjectCommonNameVerification(),
  2097. WithPermittedDNSDomains("*.local"),
  2098. WithPermittedCIDRs("127.0.0.1/24"),
  2099. WithPermittedEmailAddresses("@example.local"),
  2100. WithPermittedURIDomains("*.example.local"),
  2101. },
  2102. cert: &x509.Certificate{
  2103. Subject: pkix.Name{
  2104. CommonName: "somehost.local",
  2105. },
  2106. DNSNames: []string{"example.local"},
  2107. IPAddresses: []net.IP{net.ParseIP("127.0.0.15")},
  2108. EmailAddresses: []string{"mail@example.local"},
  2109. URIs: []*url.URL{
  2110. {
  2111. Scheme: "https",
  2112. Host: "www.example.local",
  2113. },
  2114. },
  2115. },
  2116. want: true,
  2117. wantErr: false,
  2118. },
  2119. {
  2120. name: "ok/combined-simple-permitted-without-subject-verification",
  2121. options: []NamePolicyOption{
  2122. WithPermittedDNSDomains("*.local"),
  2123. WithPermittedCIDRs("127.0.0.1/24"),
  2124. WithPermittedEmailAddresses("@example.local"),
  2125. WithPermittedURIDomains("*.example.local"),
  2126. },
  2127. cert: &x509.Certificate{
  2128. Subject: pkix.Name{
  2129. CommonName: "forbidden-but-non-verified-domain.example.com",
  2130. },
  2131. DNSNames: []string{"example.local"},
  2132. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2133. EmailAddresses: []string{"mail@example.local"},
  2134. URIs: []*url.URL{
  2135. {
  2136. Scheme: "https",
  2137. Host: "www.example.local",
  2138. },
  2139. },
  2140. },
  2141. want: true,
  2142. wantErr: false,
  2143. },
  2144. {
  2145. name: "ok/combined-simple-all",
  2146. options: []NamePolicyOption{
  2147. WithSubjectCommonNameVerification(),
  2148. WithPermittedDNSDomains("*.local"),
  2149. WithPermittedCIDRs("127.0.0.1/24"),
  2150. WithPermittedEmailAddresses("@example.local"),
  2151. WithPermittedURIDomains("*.example.local"),
  2152. WithExcludedDNSDomains("badhost.local"),
  2153. WithExcludedCIDRs("127.0.0.128/25"),
  2154. WithExcludedEmailAddresses("badmail@example.local"),
  2155. WithExcludedURIDomains("badwww.example.local"),
  2156. },
  2157. cert: &x509.Certificate{
  2158. Subject: pkix.Name{
  2159. CommonName: "somehost.local",
  2160. },
  2161. DNSNames: []string{"example.local"},
  2162. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2163. EmailAddresses: []string{"mail@example.local"},
  2164. URIs: []*url.URL{
  2165. {
  2166. Scheme: "https",
  2167. Host: "www.example.local",
  2168. },
  2169. },
  2170. },
  2171. want: true,
  2172. wantErr: false,
  2173. },
  2174. }
  2175. for _, tt := range tests {
  2176. t.Run(tt.name, func(t *testing.T) {
  2177. engine, err := New(tt.options...)
  2178. assert.FatalError(t, err)
  2179. got, err := engine.IsX509CertificateAllowed(tt.cert)
  2180. if (err != nil) != tt.wantErr {
  2181. t.Errorf("NamePolicyEngine.IsX509CertificateAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2182. return
  2183. }
  2184. if err != nil {
  2185. assert.NotEquals(t, "", err.Error()) // TODO(hs): implement a more specific error comparison?
  2186. }
  2187. if got != tt.want {
  2188. t.Errorf("NamePolicyEngine.IsX509CertificateAllowed() = %v, want %v", got, tt.want)
  2189. }
  2190. // Perform the same tests for a CSR, which are similar to Certificates
  2191. csr := &x509.CertificateRequest{
  2192. Subject: tt.cert.Subject,
  2193. DNSNames: tt.cert.DNSNames,
  2194. EmailAddresses: tt.cert.EmailAddresses,
  2195. IPAddresses: tt.cert.IPAddresses,
  2196. URIs: tt.cert.URIs,
  2197. }
  2198. got, err = engine.IsX509CertificateRequestAllowed(csr)
  2199. if (err != nil) != tt.wantErr {
  2200. t.Errorf("NamePolicyEngine.AreCSRNamesAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2201. return
  2202. }
  2203. if err != nil {
  2204. assert.NotEquals(t, "", err.Error())
  2205. }
  2206. if got != tt.want {
  2207. t.Errorf("NamePolicyEngine.AreCSRNamesAllowed() = %v, want %v", got, tt.want)
  2208. }
  2209. // Perform the same tests for a slice of SANs
  2210. includeSubject := engine.verifySubjectCommonName // copy behavior of the engine when Subject has to be included as a SAN
  2211. sans := extractSANs(tt.cert, includeSubject)
  2212. got, err = engine.AreSANsAllowed(sans)
  2213. if (err != nil) != tt.wantErr {
  2214. t.Errorf("NamePolicyEngine.AreSANsAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2215. return
  2216. }
  2217. if err != nil {
  2218. assert.NotEquals(t, "", err.Error())
  2219. }
  2220. if got != tt.want {
  2221. t.Errorf("NamePolicyEngine.AreSANsAllowed() = %v, want %v", got, tt.want)
  2222. }
  2223. })
  2224. }
  2225. }
  2226. func TestNamePolicyEngine_SSH_ArePrincipalsAllowed(t *testing.T) {
  2227. tests := []struct {
  2228. name string
  2229. options []NamePolicyOption
  2230. cert *ssh.Certificate
  2231. want bool
  2232. wantErr bool
  2233. }{
  2234. {
  2235. name: "fail/host-with-permitted-dns-domain",
  2236. options: []NamePolicyOption{
  2237. WithPermittedDNSDomains("*.local"),
  2238. },
  2239. cert: &ssh.Certificate{
  2240. CertType: ssh.HostCert,
  2241. ValidPrincipals: []string{
  2242. "host.example.com",
  2243. },
  2244. },
  2245. want: false,
  2246. wantErr: true,
  2247. },
  2248. {
  2249. name: "fail/host-with-excluded-dns-domain",
  2250. options: []NamePolicyOption{
  2251. WithExcludedDNSDomains("*.local"),
  2252. },
  2253. cert: &ssh.Certificate{
  2254. CertType: ssh.HostCert,
  2255. ValidPrincipals: []string{
  2256. "host.local",
  2257. },
  2258. },
  2259. want: false,
  2260. wantErr: true,
  2261. },
  2262. {
  2263. name: "fail/host-with-permitted-cidr",
  2264. options: []NamePolicyOption{
  2265. WithPermittedCIDRs("127.0.0.1/24"),
  2266. },
  2267. cert: &ssh.Certificate{
  2268. CertType: ssh.HostCert,
  2269. ValidPrincipals: []string{
  2270. "192.168.0.22",
  2271. },
  2272. },
  2273. want: false,
  2274. wantErr: true,
  2275. },
  2276. {
  2277. name: "fail/host-with-excluded-cidr",
  2278. options: []NamePolicyOption{
  2279. WithExcludedCIDRs("127.0.0.1/24"),
  2280. },
  2281. cert: &ssh.Certificate{
  2282. CertType: ssh.HostCert,
  2283. ValidPrincipals: []string{
  2284. "127.0.0.0",
  2285. },
  2286. },
  2287. want: false,
  2288. wantErr: true,
  2289. },
  2290. {
  2291. name: "fail/user-with-permitted-email",
  2292. options: []NamePolicyOption{
  2293. WithPermittedEmailAddresses("@example.com"),
  2294. },
  2295. cert: &ssh.Certificate{
  2296. CertType: ssh.UserCert,
  2297. ValidPrincipals: []string{
  2298. "mail@local",
  2299. },
  2300. },
  2301. want: false,
  2302. wantErr: true,
  2303. },
  2304. {
  2305. name: "fail/user-with-excluded-email",
  2306. options: []NamePolicyOption{
  2307. WithExcludedEmailAddresses("@example.com"),
  2308. },
  2309. cert: &ssh.Certificate{
  2310. CertType: ssh.UserCert,
  2311. ValidPrincipals: []string{
  2312. "mail@example.com",
  2313. },
  2314. },
  2315. want: false,
  2316. wantErr: true,
  2317. },
  2318. {
  2319. name: "fail/host-with-permitted-principals",
  2320. options: []NamePolicyOption{
  2321. WithPermittedPrincipals("localhost"),
  2322. },
  2323. cert: &ssh.Certificate{
  2324. CertType: ssh.HostCert,
  2325. ValidPrincipals: []string{
  2326. "host",
  2327. },
  2328. },
  2329. want: false,
  2330. wantErr: true,
  2331. },
  2332. {
  2333. name: "fail/host-with-excluded-principals",
  2334. options: []NamePolicyOption{
  2335. WithExcludedPrincipals("localhost"),
  2336. },
  2337. cert: &ssh.Certificate{
  2338. ValidPrincipals: []string{
  2339. "localhost",
  2340. },
  2341. },
  2342. want: false,
  2343. wantErr: true,
  2344. },
  2345. {
  2346. name: "fail/user-with-permitted-principals",
  2347. options: []NamePolicyOption{
  2348. WithPermittedPrincipals("user"),
  2349. },
  2350. cert: &ssh.Certificate{
  2351. CertType: ssh.UserCert,
  2352. ValidPrincipals: []string{
  2353. "root",
  2354. },
  2355. },
  2356. want: false,
  2357. wantErr: true,
  2358. },
  2359. {
  2360. name: "fail/user-with-excluded-principals",
  2361. options: []NamePolicyOption{
  2362. WithExcludedPrincipals("user"),
  2363. },
  2364. cert: &ssh.Certificate{
  2365. CertType: ssh.UserCert,
  2366. ValidPrincipals: []string{
  2367. "user",
  2368. },
  2369. },
  2370. want: false,
  2371. wantErr: true,
  2372. },
  2373. {
  2374. name: "fail/user-with-permitted-principal-as-mail",
  2375. options: []NamePolicyOption{
  2376. WithPermittedPrincipals("ops"),
  2377. },
  2378. cert: &ssh.Certificate{
  2379. CertType: ssh.UserCert,
  2380. ValidPrincipals: []string{
  2381. "ops@work", // this is (currently) parsed as an email-like principal; not allowed with just "ops" as the permitted principal
  2382. },
  2383. },
  2384. want: false,
  2385. wantErr: true,
  2386. },
  2387. {
  2388. name: "fail/host-principal-with-permitted-dns-domain", // when only DNS is permitted, username principals are not allowed.
  2389. options: []NamePolicyOption{
  2390. WithPermittedDNSDomains("*.local"),
  2391. },
  2392. cert: &ssh.Certificate{
  2393. CertType: ssh.HostCert,
  2394. ValidPrincipals: []string{
  2395. "user",
  2396. },
  2397. },
  2398. want: false,
  2399. wantErr: true,
  2400. },
  2401. {
  2402. name: "fail/host-principal-with-permitted-ip-range", // when only IPs are permitted, username principals are not allowed.
  2403. options: []NamePolicyOption{
  2404. WithPermittedCIDRs("127.0.0.1/24"),
  2405. },
  2406. cert: &ssh.Certificate{
  2407. CertType: ssh.HostCert,
  2408. ValidPrincipals: []string{
  2409. "user",
  2410. },
  2411. },
  2412. want: false,
  2413. wantErr: true,
  2414. },
  2415. {
  2416. name: "fail/user-principal-with-permitted-email", // when only emails are permitted, username principals are not allowed.
  2417. options: []NamePolicyOption{
  2418. WithPermittedEmailAddresses("@example.com"),
  2419. },
  2420. cert: &ssh.Certificate{
  2421. CertType: ssh.UserCert,
  2422. ValidPrincipals: []string{
  2423. "user",
  2424. },
  2425. },
  2426. want: false,
  2427. wantErr: true,
  2428. },
  2429. {
  2430. name: "fail/combined-user",
  2431. options: []NamePolicyOption{
  2432. WithPermittedEmailAddresses("@smallstep.com"),
  2433. WithExcludedEmailAddresses("root@smallstep.com"),
  2434. },
  2435. cert: &ssh.Certificate{
  2436. CertType: ssh.UserCert,
  2437. ValidPrincipals: []string{
  2438. "someone@smallstep.com",
  2439. "someone",
  2440. },
  2441. },
  2442. want: false,
  2443. wantErr: true,
  2444. },
  2445. {
  2446. name: "fail/combined-user-with-excluded-user-principal",
  2447. options: []NamePolicyOption{
  2448. WithPermittedEmailAddresses("@smallstep.com"),
  2449. WithExcludedPrincipals("root"),
  2450. },
  2451. cert: &ssh.Certificate{
  2452. CertType: ssh.UserCert,
  2453. ValidPrincipals: []string{
  2454. "someone@smallstep.com",
  2455. "root",
  2456. },
  2457. },
  2458. want: false,
  2459. wantErr: true,
  2460. },
  2461. {
  2462. name: "ok/host-with-permitted-user-principals",
  2463. options: []NamePolicyOption{
  2464. WithPermittedEmailAddresses("@work"),
  2465. },
  2466. cert: &ssh.Certificate{
  2467. CertType: ssh.HostCert,
  2468. ValidPrincipals: []string{
  2469. "example.work",
  2470. },
  2471. },
  2472. want: false,
  2473. wantErr: true,
  2474. },
  2475. {
  2476. name: "ok/user-with-permitted-user-principals",
  2477. options: []NamePolicyOption{
  2478. WithPermittedDNSDomains("*.local"),
  2479. },
  2480. cert: &ssh.Certificate{
  2481. CertType: ssh.UserCert,
  2482. ValidPrincipals: []string{
  2483. "herman@work",
  2484. },
  2485. },
  2486. want: false,
  2487. wantErr: true,
  2488. },
  2489. {
  2490. name: "ok/host-with-permitted-dns-domain",
  2491. options: []NamePolicyOption{
  2492. WithPermittedDNSDomains("*.local"),
  2493. },
  2494. cert: &ssh.Certificate{
  2495. CertType: ssh.HostCert,
  2496. ValidPrincipals: []string{
  2497. "host.local",
  2498. },
  2499. },
  2500. want: true,
  2501. wantErr: false,
  2502. },
  2503. {
  2504. name: "ok/host-with-excluded-dns-domain",
  2505. options: []NamePolicyOption{
  2506. WithExcludedDNSDomains("*.example.com"),
  2507. },
  2508. cert: &ssh.Certificate{
  2509. CertType: ssh.HostCert,
  2510. ValidPrincipals: []string{
  2511. "host.local",
  2512. },
  2513. },
  2514. want: true,
  2515. wantErr: false,
  2516. },
  2517. {
  2518. name: "ok/host-with-permitted-ip",
  2519. options: []NamePolicyOption{
  2520. WithPermittedCIDRs("127.0.0.1/24"),
  2521. },
  2522. cert: &ssh.Certificate{
  2523. CertType: ssh.HostCert,
  2524. ValidPrincipals: []string{
  2525. "127.0.0.33",
  2526. },
  2527. },
  2528. want: true,
  2529. wantErr: false,
  2530. },
  2531. {
  2532. name: "ok/host-with-excluded-ip",
  2533. options: []NamePolicyOption{
  2534. WithExcludedCIDRs("127.0.0.1/24"),
  2535. },
  2536. cert: &ssh.Certificate{
  2537. CertType: ssh.HostCert,
  2538. ValidPrincipals: []string{
  2539. "192.168.0.35",
  2540. },
  2541. },
  2542. want: true,
  2543. wantErr: false,
  2544. },
  2545. {
  2546. name: "ok/user-with-permitted-email",
  2547. options: []NamePolicyOption{
  2548. WithPermittedEmailAddresses("@example.com"),
  2549. },
  2550. cert: &ssh.Certificate{
  2551. CertType: ssh.UserCert,
  2552. ValidPrincipals: []string{
  2553. "mail@example.com",
  2554. },
  2555. },
  2556. want: true,
  2557. wantErr: false,
  2558. },
  2559. {
  2560. name: "ok/user-with-excluded-email",
  2561. options: []NamePolicyOption{
  2562. WithExcludedEmailAddresses("@example.com"),
  2563. },
  2564. cert: &ssh.Certificate{
  2565. CertType: ssh.UserCert,
  2566. ValidPrincipals: []string{
  2567. "mail@local",
  2568. },
  2569. },
  2570. want: true,
  2571. wantErr: false,
  2572. },
  2573. {
  2574. name: "ok/user-with-permitted-principals",
  2575. options: []NamePolicyOption{
  2576. WithPermittedPrincipals("*"),
  2577. },
  2578. cert: &ssh.Certificate{
  2579. CertType: ssh.UserCert,
  2580. ValidPrincipals: []string{
  2581. "user",
  2582. },
  2583. },
  2584. want: true,
  2585. wantErr: false,
  2586. },
  2587. {
  2588. name: "ok/user-with-excluded-principals",
  2589. options: []NamePolicyOption{
  2590. WithExcludedPrincipals("user"),
  2591. },
  2592. cert: &ssh.Certificate{
  2593. CertType: ssh.UserCert,
  2594. ValidPrincipals: []string{
  2595. "root",
  2596. },
  2597. },
  2598. want: true,
  2599. wantErr: false,
  2600. },
  2601. {
  2602. name: "ok/combined-user",
  2603. options: []NamePolicyOption{
  2604. WithPermittedEmailAddresses("@smallstep.com"),
  2605. WithPermittedPrincipals("*"), // without specifying the wildcard, "someone" would not be allowed.
  2606. WithExcludedEmailAddresses("root@smallstep.com"),
  2607. },
  2608. cert: &ssh.Certificate{
  2609. CertType: ssh.UserCert,
  2610. ValidPrincipals: []string{
  2611. "someone@smallstep.com",
  2612. "someone",
  2613. },
  2614. },
  2615. want: true,
  2616. wantErr: false,
  2617. },
  2618. {
  2619. name: "ok/combined-user-with-excluded-user-principal",
  2620. options: []NamePolicyOption{
  2621. WithPermittedEmailAddresses("@smallstep.com"),
  2622. WithExcludedEmailAddresses("root@smallstep.com"),
  2623. WithExcludedPrincipals("root"), // unlike the previous test, this implicitly allows any other username principal
  2624. },
  2625. cert: &ssh.Certificate{
  2626. CertType: ssh.UserCert,
  2627. ValidPrincipals: []string{
  2628. "someone@smallstep.com",
  2629. "someone",
  2630. },
  2631. },
  2632. want: true,
  2633. wantErr: false,
  2634. },
  2635. {
  2636. name: "ok/combined-host",
  2637. options: []NamePolicyOption{
  2638. WithPermittedDNSDomains("*.local"),
  2639. WithPermittedCIDRs("127.0.0.1/24"),
  2640. WithExcludedDNSDomains("badhost.local"),
  2641. WithExcludedCIDRs("127.0.0.128/25"),
  2642. },
  2643. cert: &ssh.Certificate{
  2644. CertType: ssh.HostCert,
  2645. ValidPrincipals: []string{
  2646. "example.local",
  2647. "127.0.0.31",
  2648. },
  2649. },
  2650. want: true,
  2651. wantErr: false,
  2652. },
  2653. }
  2654. for _, tt := range tests {
  2655. t.Run(tt.name, func(t *testing.T) {
  2656. engine, err := New(tt.options...)
  2657. assert.FatalError(t, err)
  2658. got, err := engine.IsSSHCertificateAllowed(tt.cert)
  2659. if (err != nil) != tt.wantErr {
  2660. t.Errorf("NamePolicyEngine.IsSSHCertificateAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2661. return
  2662. }
  2663. if got != tt.want {
  2664. t.Errorf("NamePolicyEngine.IsSSHCertificateAllowed() = %v, want %v", got, tt.want)
  2665. }
  2666. })
  2667. }
  2668. }
  2669. type result struct {
  2670. wantDNSNames []string
  2671. wantIps []net.IP
  2672. wantEmails []string
  2673. wantUsernames []string
  2674. }
  2675. func emptyResult() result {
  2676. return result{
  2677. wantDNSNames: []string{},
  2678. wantIps: []net.IP{},
  2679. wantEmails: []string{},
  2680. wantUsernames: []string{},
  2681. }
  2682. }
  2683. func Test_splitSSHPrincipals(t *testing.T) {
  2684. type test struct {
  2685. cert *ssh.Certificate
  2686. r result
  2687. wantErr bool
  2688. }
  2689. var tests = map[string]func(t *testing.T) test{
  2690. "fail/unexpected-cert-type": func(t *testing.T) test {
  2691. r := emptyResult()
  2692. return test{
  2693. cert: &ssh.Certificate{
  2694. CertType: uint32(0),
  2695. },
  2696. r: r,
  2697. wantErr: true,
  2698. }
  2699. },
  2700. "fail/user-ip": func(t *testing.T) test {
  2701. r := emptyResult()
  2702. r.wantIps = []net.IP{net.ParseIP("127.0.0.1")}
  2703. return test{
  2704. cert: &ssh.Certificate{
  2705. CertType: ssh.UserCert,
  2706. ValidPrincipals: []string{"127.0.0.1"},
  2707. },
  2708. r: r,
  2709. wantErr: true,
  2710. }
  2711. },
  2712. "fail/user-uri": func(t *testing.T) test {
  2713. r := emptyResult()
  2714. return test{
  2715. cert: &ssh.Certificate{
  2716. CertType: ssh.UserCert,
  2717. ValidPrincipals: []string{"https://host.local/"},
  2718. },
  2719. r: r,
  2720. wantErr: true,
  2721. }
  2722. },
  2723. "fail/host-uri": func(t *testing.T) test {
  2724. r := emptyResult()
  2725. return test{
  2726. cert: &ssh.Certificate{
  2727. CertType: ssh.HostCert,
  2728. ValidPrincipals: []string{"https://host.local/"},
  2729. },
  2730. r: r,
  2731. wantErr: true,
  2732. }
  2733. },
  2734. "ok/host-dns": func(t *testing.T) test {
  2735. r := emptyResult()
  2736. r.wantDNSNames = []string{"host.example.com"}
  2737. return test{
  2738. cert: &ssh.Certificate{
  2739. CertType: ssh.HostCert,
  2740. ValidPrincipals: []string{"host.example.com"},
  2741. },
  2742. r: r,
  2743. wantErr: false,
  2744. }
  2745. },
  2746. "ok/host-ip": func(t *testing.T) test {
  2747. r := emptyResult()
  2748. r.wantIps = []net.IP{net.ParseIP("127.0.0.1")}
  2749. return test{
  2750. cert: &ssh.Certificate{
  2751. CertType: ssh.HostCert,
  2752. ValidPrincipals: []string{"127.0.0.1"},
  2753. },
  2754. r: r,
  2755. wantErr: false,
  2756. }
  2757. },
  2758. "ok/host-email": func(t *testing.T) test {
  2759. r := emptyResult()
  2760. r.wantEmails = []string{"ops@work"}
  2761. return test{
  2762. cert: &ssh.Certificate{
  2763. CertType: ssh.HostCert,
  2764. ValidPrincipals: []string{"ops@work"},
  2765. },
  2766. r: r,
  2767. wantErr: false,
  2768. }
  2769. },
  2770. "ok/user-localhost": func(t *testing.T) test {
  2771. r := emptyResult()
  2772. r.wantUsernames = []string{"localhost"} // when type is User cert, this is considered a username; not a DNS
  2773. return test{
  2774. cert: &ssh.Certificate{
  2775. CertType: ssh.UserCert,
  2776. ValidPrincipals: []string{"localhost"},
  2777. },
  2778. r: r,
  2779. wantErr: false,
  2780. }
  2781. },
  2782. "ok/user-username-with-period": func(t *testing.T) test {
  2783. r := emptyResult()
  2784. r.wantUsernames = []string{"x.joe"}
  2785. return test{
  2786. cert: &ssh.Certificate{
  2787. CertType: ssh.UserCert,
  2788. ValidPrincipals: []string{"x.joe"},
  2789. },
  2790. r: r,
  2791. wantErr: false,
  2792. }
  2793. },
  2794. "ok/user-maillike": func(t *testing.T) test {
  2795. r := emptyResult()
  2796. r.wantEmails = []string{"ops@work"}
  2797. return test{
  2798. cert: &ssh.Certificate{
  2799. CertType: ssh.UserCert,
  2800. ValidPrincipals: []string{"ops@work"},
  2801. },
  2802. r: r,
  2803. wantErr: false,
  2804. }
  2805. },
  2806. }
  2807. for name, prep := range tests {
  2808. tt := prep(t)
  2809. t.Run(name, func(t *testing.T) {
  2810. gotDNSNames, gotIps, gotEmails, gotUsernames, err := splitSSHPrincipals(tt.cert)
  2811. if (err != nil) != tt.wantErr {
  2812. t.Errorf("splitSSHPrincipals() error = %v, wantErr %v", err, tt.wantErr)
  2813. return
  2814. }
  2815. if !cmp.Equal(tt.r.wantDNSNames, gotDNSNames) {
  2816. t.Errorf("splitSSHPrincipals() DNS names diff =\n%s", cmp.Diff(tt.r.wantDNSNames, gotDNSNames))
  2817. }
  2818. if !cmp.Equal(tt.r.wantIps, gotIps) {
  2819. t.Errorf("splitSSHPrincipals() IPs diff =\n%s", cmp.Diff(tt.r.wantIps, gotIps))
  2820. }
  2821. if !cmp.Equal(tt.r.wantEmails, gotEmails) {
  2822. t.Errorf("splitSSHPrincipals() Emails diff =\n%s", cmp.Diff(tt.r.wantEmails, gotEmails))
  2823. }
  2824. if !cmp.Equal(tt.r.wantUsernames, gotUsernames) {
  2825. t.Errorf("splitSSHPrincipals() Usernames diff =\n%s", cmp.Diff(tt.r.wantUsernames, gotUsernames))
  2826. }
  2827. })
  2828. }
  2829. }