engine_test.go 80 KB

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