engine_test.go 80 KB

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