policy_test.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "io"
  8. "net/http"
  9. "net/http/httptest"
  10. "strings"
  11. "testing"
  12. "github.com/stretchr/testify/assert"
  13. "google.golang.org/protobuf/encoding/protojson"
  14. "go.step.sm/linkedca"
  15. "github.com/smallstep/certificates/acme"
  16. "github.com/smallstep/certificates/authority"
  17. "github.com/smallstep/certificates/authority/admin"
  18. )
  19. type fakeLinkedCA struct {
  20. admin.MockDB
  21. }
  22. func (f *fakeLinkedCA) IsLinkedCA() bool {
  23. return true
  24. }
  25. // testAdminError is an error type that models the expected
  26. // error body returned.
  27. type testAdminError struct {
  28. Type string `json:"type"`
  29. Message string `json:"message"`
  30. Detail string `json:"detail"`
  31. }
  32. type testX509Policy struct {
  33. Allow *testX509Names `json:"allow,omitempty"`
  34. Deny *testX509Names `json:"deny,omitempty"`
  35. AllowWildcardNames bool `json:"allow_wildcard_names,omitempty"`
  36. }
  37. type testX509Names struct {
  38. CommonNames []string `json:"commonNames,omitempty"`
  39. DNSDomains []string `json:"dns,omitempty"`
  40. IPRanges []string `json:"ips,omitempty"`
  41. EmailAddresses []string `json:"emails,omitempty"`
  42. URIDomains []string `json:"uris,omitempty"`
  43. }
  44. type testSSHPolicy struct {
  45. User *testSSHUserPolicy `json:"user,omitempty"`
  46. Host *testSSHHostPolicy `json:"host,omitempty"`
  47. }
  48. type testSSHHostPolicy struct {
  49. Allow *testSSHHostNames `json:"allow,omitempty"`
  50. Deny *testSSHHostNames `json:"deny,omitempty"`
  51. }
  52. type testSSHHostNames struct {
  53. DNSDomains []string `json:"dns,omitempty"`
  54. IPRanges []string `json:"ips,omitempty"`
  55. Principals []string `json:"principals,omitempty"`
  56. }
  57. type testSSHUserPolicy struct {
  58. Allow *testSSHUserNames `json:"allow,omitempty"`
  59. Deny *testSSHUserNames `json:"deny,omitempty"`
  60. }
  61. type testSSHUserNames struct {
  62. EmailAddresses []string `json:"emails,omitempty"`
  63. Principals []string `json:"principals,omitempty"`
  64. }
  65. // testPolicyResponse models the Policy API JSON response
  66. type testPolicyResponse struct {
  67. X509 *testX509Policy `json:"x509,omitempty"`
  68. SSH *testSSHPolicy `json:"ssh,omitempty"`
  69. }
  70. func TestPolicyAdminResponder_GetAuthorityPolicy(t *testing.T) {
  71. type test struct {
  72. auth adminAuthority
  73. adminDB admin.DB
  74. ctx context.Context
  75. err *admin.Error
  76. response *testPolicyResponse
  77. statusCode int
  78. }
  79. var tests = map[string]func(t *testing.T) test{
  80. "fail/linkedca": func(t *testing.T) test {
  81. ctx := context.Background()
  82. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  83. err.Message = "policy operations not yet supported in linked deployments"
  84. return test{
  85. ctx: ctx,
  86. adminDB: &fakeLinkedCA{},
  87. err: err,
  88. statusCode: 501,
  89. }
  90. },
  91. "fail/auth.GetAuthorityPolicy-error": func(t *testing.T) test {
  92. ctx := context.Background()
  93. err := admin.WrapErrorISE(errors.New("force"), "error retrieving authority policy")
  94. err.Message = "error retrieving authority policy: force"
  95. return test{
  96. ctx: ctx,
  97. auth: &mockAdminAuthority{
  98. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  99. return nil, admin.NewError(admin.ErrorServerInternalType, "force")
  100. },
  101. },
  102. err: err,
  103. statusCode: 500,
  104. }
  105. },
  106. "fail/auth.GetAuthorityPolicy-not-found": func(t *testing.T) test {
  107. ctx := context.Background()
  108. err := admin.NewError(admin.ErrorNotFoundType, "authority policy does not exist")
  109. err.Message = "authority policy does not exist"
  110. return test{
  111. ctx: ctx,
  112. auth: &mockAdminAuthority{
  113. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  114. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  115. },
  116. },
  117. err: err,
  118. statusCode: 404,
  119. }
  120. },
  121. "ok": func(t *testing.T) test {
  122. ctx := context.Background()
  123. policy := &linkedca.Policy{
  124. X509: &linkedca.X509Policy{
  125. Allow: &linkedca.X509Names{
  126. Dns: []string{"*.local"},
  127. Ips: []string{"10.0.0.0/16"},
  128. Emails: []string{"@example.com"},
  129. Uris: []string{"example.com"},
  130. CommonNames: []string{"test"},
  131. },
  132. Deny: &linkedca.X509Names{
  133. Dns: []string{"bad.local"},
  134. Ips: []string{"10.0.0.30"},
  135. Emails: []string{"bad@example.com"},
  136. Uris: []string{"notexample.com"},
  137. CommonNames: []string{"bad"},
  138. },
  139. },
  140. Ssh: &linkedca.SSHPolicy{
  141. User: &linkedca.SSHUserPolicy{
  142. Allow: &linkedca.SSHUserNames{
  143. Emails: []string{"@example.com"},
  144. Principals: []string{"*"},
  145. },
  146. Deny: &linkedca.SSHUserNames{
  147. Emails: []string{"bad@example.com"},
  148. Principals: []string{"root"},
  149. },
  150. },
  151. Host: &linkedca.SSHHostPolicy{
  152. Allow: &linkedca.SSHHostNames{
  153. Dns: []string{"*.example.com"},
  154. Ips: []string{"10.10.0.0/16"},
  155. Principals: []string{"good"},
  156. },
  157. Deny: &linkedca.SSHHostNames{
  158. Dns: []string{"bad@example.com"},
  159. Ips: []string{"10.10.0.30"},
  160. Principals: []string{"bad"},
  161. },
  162. },
  163. },
  164. }
  165. return test{
  166. ctx: ctx,
  167. auth: &mockAdminAuthority{
  168. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  169. return policy, nil
  170. },
  171. },
  172. response: &testPolicyResponse{
  173. X509: &testX509Policy{
  174. Allow: &testX509Names{
  175. DNSDomains: []string{"*.local"},
  176. IPRanges: []string{"10.0.0.0/16"},
  177. EmailAddresses: []string{"@example.com"},
  178. URIDomains: []string{"example.com"},
  179. CommonNames: []string{"test"},
  180. },
  181. Deny: &testX509Names{
  182. DNSDomains: []string{"bad.local"},
  183. IPRanges: []string{"10.0.0.30"},
  184. EmailAddresses: []string{"bad@example.com"},
  185. URIDomains: []string{"notexample.com"},
  186. CommonNames: []string{"bad"},
  187. },
  188. },
  189. SSH: &testSSHPolicy{
  190. User: &testSSHUserPolicy{
  191. Allow: &testSSHUserNames{
  192. EmailAddresses: []string{"@example.com"},
  193. Principals: []string{"*"},
  194. },
  195. Deny: &testSSHUserNames{
  196. EmailAddresses: []string{"bad@example.com"},
  197. Principals: []string{"root"},
  198. },
  199. },
  200. Host: &testSSHHostPolicy{
  201. Allow: &testSSHHostNames{
  202. DNSDomains: []string{"*.example.com"},
  203. IPRanges: []string{"10.10.0.0/16"},
  204. Principals: []string{"good"},
  205. },
  206. Deny: &testSSHHostNames{
  207. DNSDomains: []string{"bad@example.com"},
  208. IPRanges: []string{"10.10.0.30"},
  209. Principals: []string{"bad"},
  210. },
  211. },
  212. },
  213. },
  214. statusCode: 200,
  215. }
  216. },
  217. }
  218. for name, prep := range tests {
  219. tc := prep(t)
  220. t.Run(name, func(t *testing.T) {
  221. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, nil)
  222. req := httptest.NewRequest("GET", "/foo", nil)
  223. req = req.WithContext(tc.ctx)
  224. w := httptest.NewRecorder()
  225. par.GetAuthorityPolicy(w, req)
  226. res := w.Result()
  227. assert.Equal(t, tc.statusCode, res.StatusCode)
  228. if res.StatusCode >= 400 {
  229. body, err := io.ReadAll(res.Body)
  230. res.Body.Close()
  231. assert.NoError(t, err)
  232. ae := testAdminError{}
  233. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  234. assert.Equal(t, tc.err.Type, ae.Type)
  235. assert.Equal(t, tc.err.Message, ae.Message)
  236. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  237. assert.Equal(t, tc.err.Detail, ae.Detail)
  238. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  239. return
  240. }
  241. p := &testPolicyResponse{}
  242. body, err := io.ReadAll(res.Body)
  243. assert.NoError(t, err)
  244. assert.NoError(t, json.Unmarshal(body, &p))
  245. assert.Equal(t, tc.response, p)
  246. })
  247. }
  248. }
  249. func TestPolicyAdminResponder_CreateAuthorityPolicy(t *testing.T) {
  250. type test struct {
  251. auth adminAuthority
  252. adminDB admin.DB
  253. body []byte
  254. ctx context.Context
  255. acmeDB acme.DB
  256. err *admin.Error
  257. response *testPolicyResponse
  258. statusCode int
  259. }
  260. var tests = map[string]func(t *testing.T) test{
  261. "fail/linkedca": func(t *testing.T) test {
  262. ctx := context.Background()
  263. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  264. err.Message = "policy operations not yet supported in linked deployments"
  265. return test{
  266. ctx: ctx,
  267. adminDB: &fakeLinkedCA{},
  268. err: err,
  269. statusCode: 501,
  270. }
  271. },
  272. "fail/auth.GetAuthorityPolicy-error": func(t *testing.T) test {
  273. ctx := context.Background()
  274. err := admin.WrapErrorISE(errors.New("force"), "error retrieving authority policy")
  275. err.Message = "error retrieving authority policy: force"
  276. return test{
  277. ctx: ctx,
  278. auth: &mockAdminAuthority{
  279. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  280. return nil, admin.NewError(admin.ErrorServerInternalType, "force")
  281. },
  282. },
  283. err: err,
  284. statusCode: 500,
  285. }
  286. },
  287. "fail/existing-policy": func(t *testing.T) test {
  288. ctx := context.Background()
  289. err := admin.NewError(admin.ErrorConflictType, "authority already has a policy")
  290. err.Message = "authority already has a policy"
  291. return test{
  292. ctx: ctx,
  293. auth: &mockAdminAuthority{
  294. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  295. return &linkedca.Policy{}, nil
  296. },
  297. },
  298. err: err,
  299. statusCode: 409,
  300. }
  301. },
  302. "fail/read.ProtoJSON": func(t *testing.T) test {
  303. ctx := context.Background()
  304. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  305. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  306. body := []byte("{?}")
  307. return test{
  308. ctx: ctx,
  309. auth: &mockAdminAuthority{
  310. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  311. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  312. },
  313. },
  314. body: body,
  315. err: adminErr,
  316. statusCode: 400,
  317. }
  318. },
  319. "fail/validatePolicy": func(t *testing.T) test {
  320. ctx := context.Background()
  321. adminErr := admin.NewError(admin.ErrorBadRequestType, "error validating authority policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)")
  322. adminErr.Message = "error validating authority policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)"
  323. body := []byte(`
  324. {
  325. "x509": {
  326. "allow": {
  327. "uris": [
  328. "https://example.com"
  329. ]
  330. }
  331. }
  332. }`)
  333. return test{
  334. ctx: ctx,
  335. auth: &mockAdminAuthority{
  336. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  337. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  338. },
  339. },
  340. body: body,
  341. err: adminErr,
  342. statusCode: 400,
  343. }
  344. },
  345. "fail/CreateAuthorityPolicy-policy-admin-lockout-error": func(t *testing.T) test {
  346. adm := &linkedca.Admin{
  347. Subject: "step",
  348. }
  349. ctx := context.Background()
  350. ctx = linkedca.NewContextWithAdmin(ctx, adm)
  351. adminErr := admin.NewError(admin.ErrorBadRequestType, "error storing authority policy")
  352. adminErr.Message = "error storing authority policy: admin lock out"
  353. policy := &linkedca.Policy{
  354. X509: &linkedca.X509Policy{
  355. Allow: &linkedca.X509Names{
  356. Dns: []string{"*.local"},
  357. },
  358. },
  359. }
  360. body, err := protojson.Marshal(policy)
  361. assert.NoError(t, err)
  362. return test{
  363. ctx: ctx,
  364. auth: &mockAdminAuthority{
  365. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  366. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  367. },
  368. MockCreateAuthorityPolicy: func(ctx context.Context, adm *linkedca.Admin, policy *linkedca.Policy) (*linkedca.Policy, error) {
  369. return nil, &authority.PolicyError{
  370. Typ: authority.AdminLockOut,
  371. Err: errors.New("admin lock out"),
  372. }
  373. },
  374. },
  375. adminDB: &admin.MockDB{
  376. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  377. return []*linkedca.Admin{
  378. adm,
  379. {
  380. Subject: "anotherAdmin",
  381. },
  382. }, nil
  383. },
  384. },
  385. body: body,
  386. err: adminErr,
  387. statusCode: 400,
  388. }
  389. },
  390. "fail/CreateAuthorityPolicy-error": func(t *testing.T) test {
  391. adm := &linkedca.Admin{
  392. Subject: "step",
  393. }
  394. ctx := context.Background()
  395. ctx = linkedca.NewContextWithAdmin(ctx, adm)
  396. adminErr := admin.NewError(admin.ErrorServerInternalType, "error storing authority policy: force")
  397. adminErr.Message = "error storing authority policy: force"
  398. policy := &linkedca.Policy{
  399. X509: &linkedca.X509Policy{
  400. Allow: &linkedca.X509Names{
  401. Dns: []string{"*.local"},
  402. },
  403. },
  404. }
  405. body, err := protojson.Marshal(policy)
  406. assert.NoError(t, err)
  407. return test{
  408. ctx: ctx,
  409. auth: &mockAdminAuthority{
  410. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  411. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  412. },
  413. MockCreateAuthorityPolicy: func(ctx context.Context, adm *linkedca.Admin, policy *linkedca.Policy) (*linkedca.Policy, error) {
  414. return nil, &authority.PolicyError{
  415. Typ: authority.StoreFailure,
  416. Err: errors.New("force"),
  417. }
  418. },
  419. },
  420. adminDB: &admin.MockDB{
  421. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  422. return []*linkedca.Admin{
  423. adm,
  424. {
  425. Subject: "anotherAdmin",
  426. },
  427. }, nil
  428. },
  429. },
  430. body: body,
  431. err: adminErr,
  432. statusCode: 500,
  433. }
  434. },
  435. "ok": func(t *testing.T) test {
  436. adm := &linkedca.Admin{
  437. Subject: "step",
  438. }
  439. ctx := context.Background()
  440. ctx = linkedca.NewContextWithAdmin(ctx, adm)
  441. policy := &linkedca.Policy{
  442. X509: &linkedca.X509Policy{
  443. Allow: &linkedca.X509Names{
  444. Dns: []string{"*.local"},
  445. },
  446. },
  447. }
  448. body, err := protojson.Marshal(policy)
  449. assert.NoError(t, err)
  450. return test{
  451. ctx: ctx,
  452. auth: &mockAdminAuthority{
  453. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  454. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  455. },
  456. MockCreateAuthorityPolicy: func(ctx context.Context, adm *linkedca.Admin, policy *linkedca.Policy) (*linkedca.Policy, error) {
  457. return policy, nil
  458. },
  459. },
  460. adminDB: &admin.MockDB{
  461. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  462. return []*linkedca.Admin{
  463. adm,
  464. {
  465. Subject: "anotherAdmin",
  466. },
  467. }, nil
  468. },
  469. },
  470. body: body,
  471. response: &testPolicyResponse{
  472. X509: &testX509Policy{
  473. Allow: &testX509Names{
  474. DNSDomains: []string{"*.local"},
  475. },
  476. },
  477. },
  478. statusCode: 201,
  479. }
  480. },
  481. }
  482. for name, prep := range tests {
  483. tc := prep(t)
  484. t.Run(name, func(t *testing.T) {
  485. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, tc.acmeDB)
  486. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  487. req = req.WithContext(tc.ctx)
  488. w := httptest.NewRecorder()
  489. par.CreateAuthorityPolicy(w, req)
  490. res := w.Result()
  491. assert.Equal(t, tc.statusCode, res.StatusCode)
  492. if res.StatusCode >= 400 {
  493. body, err := io.ReadAll(res.Body)
  494. res.Body.Close()
  495. assert.NoError(t, err)
  496. ae := testAdminError{}
  497. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  498. assert.Equal(t, tc.err.Type, ae.Type)
  499. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  500. assert.Equal(t, tc.err.Detail, ae.Detail)
  501. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  502. // when the error message starts with "proto", we expect it to have
  503. // a syntax error (in the tests). If the message doesn't start with "proto",
  504. // we expect a full string match.
  505. if strings.HasPrefix(tc.err.Message, "proto:") {
  506. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  507. } else {
  508. assert.Equal(t, tc.err.Message, ae.Message)
  509. }
  510. return
  511. }
  512. p := &testPolicyResponse{}
  513. body, err := io.ReadAll(res.Body)
  514. assert.NoError(t, err)
  515. assert.NoError(t, json.Unmarshal(body, &p))
  516. assert.Equal(t, tc.response, p)
  517. })
  518. }
  519. }
  520. func TestPolicyAdminResponder_UpdateAuthorityPolicy(t *testing.T) {
  521. type test struct {
  522. auth adminAuthority
  523. adminDB admin.DB
  524. body []byte
  525. ctx context.Context
  526. acmeDB acme.DB
  527. err *admin.Error
  528. response *testPolicyResponse
  529. statusCode int
  530. }
  531. var tests = map[string]func(t *testing.T) test{
  532. "fail/linkedca": func(t *testing.T) test {
  533. ctx := context.Background()
  534. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  535. err.Message = "policy operations not yet supported in linked deployments"
  536. return test{
  537. ctx: ctx,
  538. adminDB: &fakeLinkedCA{},
  539. err: err,
  540. statusCode: 501,
  541. }
  542. },
  543. "fail/auth.GetAuthorityPolicy-error": func(t *testing.T) test {
  544. ctx := context.Background()
  545. err := admin.WrapErrorISE(errors.New("force"), "error retrieving authority policy")
  546. err.Message = "error retrieving authority policy: force"
  547. return test{
  548. ctx: ctx,
  549. auth: &mockAdminAuthority{
  550. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  551. return nil, admin.NewError(admin.ErrorServerInternalType, "force")
  552. },
  553. },
  554. err: err,
  555. statusCode: 500,
  556. }
  557. },
  558. "fail/no-existing-policy": func(t *testing.T) test {
  559. ctx := context.Background()
  560. err := admin.NewError(admin.ErrorNotFoundType, "authority policy does not exist")
  561. err.Message = "authority policy does not exist"
  562. err.Status = http.StatusNotFound
  563. return test{
  564. ctx: ctx,
  565. auth: &mockAdminAuthority{
  566. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  567. return nil, nil
  568. },
  569. },
  570. err: err,
  571. statusCode: 404,
  572. }
  573. },
  574. "fail/read.ProtoJSON": func(t *testing.T) test {
  575. policy := &linkedca.Policy{
  576. X509: &linkedca.X509Policy{
  577. Allow: &linkedca.X509Names{
  578. Dns: []string{"*.local"},
  579. },
  580. },
  581. }
  582. ctx := context.Background()
  583. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  584. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  585. body := []byte("{?}")
  586. return test{
  587. ctx: ctx,
  588. auth: &mockAdminAuthority{
  589. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  590. return policy, nil
  591. },
  592. },
  593. body: body,
  594. err: adminErr,
  595. statusCode: 400,
  596. }
  597. },
  598. "fail/validatePolicy": func(t *testing.T) test {
  599. policy := &linkedca.Policy{
  600. X509: &linkedca.X509Policy{
  601. Allow: &linkedca.X509Names{
  602. Dns: []string{"*.local"},
  603. },
  604. },
  605. }
  606. ctx := context.Background()
  607. adminErr := admin.NewError(admin.ErrorBadRequestType, "error validating authority policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)")
  608. adminErr.Message = "error validating authority policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)"
  609. body := []byte(`
  610. {
  611. "x509": {
  612. "allow": {
  613. "uris": [
  614. "https://example.com"
  615. ]
  616. }
  617. }
  618. }`)
  619. return test{
  620. ctx: ctx,
  621. auth: &mockAdminAuthority{
  622. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  623. return policy, nil
  624. },
  625. },
  626. body: body,
  627. err: adminErr,
  628. statusCode: 400,
  629. }
  630. },
  631. "fail/UpdateAuthorityPolicy-policy-admin-lockout-error": func(t *testing.T) test {
  632. adm := &linkedca.Admin{
  633. Subject: "step",
  634. }
  635. ctx := context.Background()
  636. ctx = linkedca.NewContextWithAdmin(ctx, adm)
  637. adminErr := admin.NewError(admin.ErrorBadRequestType, "error updating authority policy: force")
  638. adminErr.Message = "error updating authority policy: admin lock out"
  639. policy := &linkedca.Policy{
  640. X509: &linkedca.X509Policy{
  641. Allow: &linkedca.X509Names{
  642. Dns: []string{"*.local"},
  643. },
  644. },
  645. }
  646. body, err := protojson.Marshal(policy)
  647. assert.NoError(t, err)
  648. return test{
  649. ctx: ctx,
  650. auth: &mockAdminAuthority{
  651. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  652. return policy, nil
  653. },
  654. MockUpdateAuthorityPolicy: func(ctx context.Context, adm *linkedca.Admin, policy *linkedca.Policy) (*linkedca.Policy, error) {
  655. return nil, &authority.PolicyError{
  656. Typ: authority.AdminLockOut,
  657. Err: errors.New("admin lock out"),
  658. }
  659. },
  660. },
  661. adminDB: &admin.MockDB{
  662. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  663. return []*linkedca.Admin{
  664. adm,
  665. {
  666. Subject: "anotherAdmin",
  667. },
  668. }, nil
  669. },
  670. },
  671. body: body,
  672. err: adminErr,
  673. statusCode: 400,
  674. }
  675. },
  676. "fail/UpdateAuthorityPolicy-error": func(t *testing.T) test {
  677. adm := &linkedca.Admin{
  678. Subject: "step",
  679. }
  680. ctx := context.Background()
  681. ctx = linkedca.NewContextWithAdmin(ctx, adm)
  682. adminErr := admin.NewError(admin.ErrorServerInternalType, "error updating authority policy: force")
  683. adminErr.Message = "error updating authority policy: force"
  684. policy := &linkedca.Policy{
  685. X509: &linkedca.X509Policy{
  686. Allow: &linkedca.X509Names{
  687. Dns: []string{"*.local"},
  688. },
  689. },
  690. }
  691. body, err := protojson.Marshal(policy)
  692. assert.NoError(t, err)
  693. return test{
  694. ctx: ctx,
  695. auth: &mockAdminAuthority{
  696. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  697. return policy, nil
  698. },
  699. MockUpdateAuthorityPolicy: func(ctx context.Context, adm *linkedca.Admin, policy *linkedca.Policy) (*linkedca.Policy, error) {
  700. return nil, &authority.PolicyError{
  701. Typ: authority.StoreFailure,
  702. Err: errors.New("force"),
  703. }
  704. },
  705. },
  706. adminDB: &admin.MockDB{
  707. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  708. return []*linkedca.Admin{
  709. adm,
  710. {
  711. Subject: "anotherAdmin",
  712. },
  713. }, nil
  714. },
  715. },
  716. body: body,
  717. err: adminErr,
  718. statusCode: 500,
  719. }
  720. },
  721. "ok": func(t *testing.T) test {
  722. adm := &linkedca.Admin{
  723. Subject: "step",
  724. }
  725. ctx := context.Background()
  726. ctx = linkedca.NewContextWithAdmin(ctx, adm)
  727. policy := &linkedca.Policy{
  728. X509: &linkedca.X509Policy{
  729. Allow: &linkedca.X509Names{
  730. Dns: []string{"*.local"},
  731. },
  732. },
  733. }
  734. body, err := protojson.Marshal(policy)
  735. assert.NoError(t, err)
  736. return test{
  737. ctx: ctx,
  738. auth: &mockAdminAuthority{
  739. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  740. return policy, nil
  741. },
  742. MockUpdateAuthorityPolicy: func(ctx context.Context, adm *linkedca.Admin, policy *linkedca.Policy) (*linkedca.Policy, error) {
  743. return policy, nil
  744. },
  745. },
  746. adminDB: &admin.MockDB{
  747. MockGetAdmins: func(ctx context.Context) ([]*linkedca.Admin, error) {
  748. return []*linkedca.Admin{
  749. adm,
  750. {
  751. Subject: "anotherAdmin",
  752. },
  753. }, nil
  754. },
  755. },
  756. body: body,
  757. response: &testPolicyResponse{
  758. X509: &testX509Policy{
  759. Allow: &testX509Names{
  760. DNSDomains: []string{"*.local"},
  761. },
  762. },
  763. },
  764. statusCode: 200,
  765. }
  766. },
  767. }
  768. for name, prep := range tests {
  769. tc := prep(t)
  770. t.Run(name, func(t *testing.T) {
  771. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, tc.acmeDB)
  772. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  773. req = req.WithContext(tc.ctx)
  774. w := httptest.NewRecorder()
  775. par.UpdateAuthorityPolicy(w, req)
  776. res := w.Result()
  777. assert.Equal(t, tc.statusCode, res.StatusCode)
  778. if res.StatusCode >= 400 {
  779. body, err := io.ReadAll(res.Body)
  780. res.Body.Close()
  781. assert.NoError(t, err)
  782. ae := testAdminError{}
  783. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  784. assert.Equal(t, tc.err.Type, ae.Type)
  785. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  786. assert.Equal(t, tc.err.Detail, ae.Detail)
  787. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  788. // when the error message starts with "proto", we expect it to have
  789. // a syntax error (in the tests). If the message doesn't start with "proto",
  790. // we expect a full string match.
  791. if strings.HasPrefix(tc.err.Message, "proto:") {
  792. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  793. } else {
  794. assert.Equal(t, tc.err.Message, ae.Message)
  795. }
  796. return
  797. }
  798. p := &testPolicyResponse{}
  799. body, err := io.ReadAll(res.Body)
  800. assert.NoError(t, err)
  801. assert.NoError(t, json.Unmarshal(body, &p))
  802. assert.Equal(t, tc.response, p)
  803. })
  804. }
  805. }
  806. func TestPolicyAdminResponder_DeleteAuthorityPolicy(t *testing.T) {
  807. type test struct {
  808. auth adminAuthority
  809. adminDB admin.DB
  810. body []byte
  811. ctx context.Context
  812. acmeDB acme.DB
  813. err *admin.Error
  814. statusCode int
  815. }
  816. var tests = map[string]func(t *testing.T) test{
  817. "fail/linkedca": func(t *testing.T) test {
  818. ctx := context.Background()
  819. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  820. err.Message = "policy operations not yet supported in linked deployments"
  821. return test{
  822. ctx: ctx,
  823. adminDB: &fakeLinkedCA{},
  824. err: err,
  825. statusCode: 501,
  826. }
  827. },
  828. "fail/auth.GetAuthorityPolicy-error": func(t *testing.T) test {
  829. ctx := context.Background()
  830. err := admin.WrapErrorISE(errors.New("force"), "error retrieving authority policy")
  831. err.Message = "error retrieving authority policy: force"
  832. return test{
  833. ctx: ctx,
  834. auth: &mockAdminAuthority{
  835. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  836. return nil, admin.NewError(admin.ErrorServerInternalType, "force")
  837. },
  838. },
  839. err: err,
  840. statusCode: 500,
  841. }
  842. },
  843. "fail/no-existing-policy": func(t *testing.T) test {
  844. ctx := context.Background()
  845. err := admin.NewError(admin.ErrorNotFoundType, "authority policy does not exist")
  846. err.Message = "authority policy does not exist"
  847. err.Status = http.StatusNotFound
  848. return test{
  849. ctx: ctx,
  850. auth: &mockAdminAuthority{
  851. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  852. return nil, nil
  853. },
  854. },
  855. err: err,
  856. statusCode: 404,
  857. }
  858. },
  859. "fail/auth.RemoveAuthorityPolicy-error": func(t *testing.T) test {
  860. policy := &linkedca.Policy{
  861. X509: &linkedca.X509Policy{
  862. Allow: &linkedca.X509Names{
  863. Dns: []string{"*.local"},
  864. },
  865. },
  866. }
  867. ctx := context.Background()
  868. err := admin.NewErrorISE("error deleting authority policy: force")
  869. err.Message = "error deleting authority policy: force"
  870. return test{
  871. ctx: ctx,
  872. auth: &mockAdminAuthority{
  873. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  874. return policy, nil
  875. },
  876. MockRemoveAuthorityPolicy: func(ctx context.Context) error {
  877. return errors.New("force")
  878. },
  879. },
  880. err: err,
  881. statusCode: 500,
  882. }
  883. },
  884. "ok": func(t *testing.T) test {
  885. policy := &linkedca.Policy{
  886. X509: &linkedca.X509Policy{
  887. Allow: &linkedca.X509Names{
  888. Dns: []string{"*.local"},
  889. },
  890. },
  891. }
  892. ctx := context.Background()
  893. return test{
  894. ctx: ctx,
  895. auth: &mockAdminAuthority{
  896. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  897. return policy, nil
  898. },
  899. MockRemoveAuthorityPolicy: func(ctx context.Context) error {
  900. return nil
  901. },
  902. },
  903. statusCode: 200,
  904. }
  905. },
  906. }
  907. for name, prep := range tests {
  908. tc := prep(t)
  909. t.Run(name, func(t *testing.T) {
  910. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, tc.acmeDB)
  911. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  912. req = req.WithContext(tc.ctx)
  913. w := httptest.NewRecorder()
  914. par.DeleteAuthorityPolicy(w, req)
  915. res := w.Result()
  916. assert.Equal(t, tc.statusCode, res.StatusCode)
  917. if res.StatusCode >= 400 {
  918. body, err := io.ReadAll(res.Body)
  919. res.Body.Close()
  920. assert.NoError(t, err)
  921. ae := testAdminError{}
  922. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  923. assert.Equal(t, tc.err.Type, ae.Type)
  924. assert.Equal(t, tc.err.Message, ae.Message)
  925. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  926. assert.Equal(t, tc.err.Detail, ae.Detail)
  927. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  928. return
  929. }
  930. body, err := io.ReadAll(res.Body)
  931. assert.NoError(t, err)
  932. res.Body.Close()
  933. response := DeleteResponse{}
  934. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  935. assert.Equal(t, "ok", response.Status)
  936. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  937. })
  938. }
  939. }
  940. func TestPolicyAdminResponder_GetProvisionerPolicy(t *testing.T) {
  941. type test struct {
  942. auth adminAuthority
  943. adminDB admin.DB
  944. ctx context.Context
  945. acmeDB acme.DB
  946. err *admin.Error
  947. response *testPolicyResponse
  948. statusCode int
  949. }
  950. var tests = map[string]func(t *testing.T) test{
  951. "fail/linkedca": func(t *testing.T) test {
  952. ctx := context.Background()
  953. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  954. err.Message = "policy operations not yet supported in linked deployments"
  955. return test{
  956. ctx: ctx,
  957. adminDB: &fakeLinkedCA{},
  958. err: err,
  959. statusCode: 501,
  960. }
  961. },
  962. "fail/prov-no-policy": func(t *testing.T) test {
  963. prov := &linkedca.Provisioner{}
  964. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  965. err := admin.NewError(admin.ErrorNotFoundType, "provisioner policy does not exist")
  966. err.Message = "provisioner policy does not exist"
  967. return test{
  968. ctx: ctx,
  969. err: err,
  970. statusCode: 404,
  971. }
  972. },
  973. "ok": func(t *testing.T) test {
  974. policy := &linkedca.Policy{
  975. X509: &linkedca.X509Policy{
  976. Allow: &linkedca.X509Names{
  977. Dns: []string{"*.local"},
  978. Ips: []string{"10.0.0.0/16"},
  979. Emails: []string{"@example.com"},
  980. Uris: []string{"example.com"},
  981. CommonNames: []string{"test"},
  982. },
  983. Deny: &linkedca.X509Names{
  984. Dns: []string{"bad.local"},
  985. Ips: []string{"10.0.0.30"},
  986. Emails: []string{"bad@example.com"},
  987. Uris: []string{"notexample.com"},
  988. CommonNames: []string{"bad"},
  989. },
  990. },
  991. Ssh: &linkedca.SSHPolicy{
  992. User: &linkedca.SSHUserPolicy{
  993. Allow: &linkedca.SSHUserNames{
  994. Emails: []string{"@example.com"},
  995. Principals: []string{"*"},
  996. },
  997. Deny: &linkedca.SSHUserNames{
  998. Emails: []string{"bad@example.com"},
  999. Principals: []string{"root"},
  1000. },
  1001. },
  1002. Host: &linkedca.SSHHostPolicy{
  1003. Allow: &linkedca.SSHHostNames{
  1004. Dns: []string{"*.example.com"},
  1005. Ips: []string{"10.10.0.0/16"},
  1006. Principals: []string{"good"},
  1007. },
  1008. Deny: &linkedca.SSHHostNames{
  1009. Dns: []string{"bad@example.com"},
  1010. Ips: []string{"10.10.0.30"},
  1011. Principals: []string{"bad"},
  1012. },
  1013. },
  1014. },
  1015. }
  1016. prov := &linkedca.Provisioner{
  1017. Policy: policy,
  1018. }
  1019. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1020. return test{
  1021. ctx: ctx,
  1022. response: &testPolicyResponse{
  1023. X509: &testX509Policy{
  1024. Allow: &testX509Names{
  1025. DNSDomains: []string{"*.local"},
  1026. IPRanges: []string{"10.0.0.0/16"},
  1027. EmailAddresses: []string{"@example.com"},
  1028. URIDomains: []string{"example.com"},
  1029. CommonNames: []string{"test"},
  1030. },
  1031. Deny: &testX509Names{
  1032. DNSDomains: []string{"bad.local"},
  1033. IPRanges: []string{"10.0.0.30"},
  1034. EmailAddresses: []string{"bad@example.com"},
  1035. URIDomains: []string{"notexample.com"},
  1036. CommonNames: []string{"bad"},
  1037. },
  1038. },
  1039. SSH: &testSSHPolicy{
  1040. User: &testSSHUserPolicy{
  1041. Allow: &testSSHUserNames{
  1042. EmailAddresses: []string{"@example.com"},
  1043. Principals: []string{"*"},
  1044. },
  1045. Deny: &testSSHUserNames{
  1046. EmailAddresses: []string{"bad@example.com"},
  1047. Principals: []string{"root"},
  1048. },
  1049. },
  1050. Host: &testSSHHostPolicy{
  1051. Allow: &testSSHHostNames{
  1052. DNSDomains: []string{"*.example.com"},
  1053. IPRanges: []string{"10.10.0.0/16"},
  1054. Principals: []string{"good"},
  1055. },
  1056. Deny: &testSSHHostNames{
  1057. DNSDomains: []string{"bad@example.com"},
  1058. IPRanges: []string{"10.10.0.30"},
  1059. Principals: []string{"bad"},
  1060. },
  1061. },
  1062. },
  1063. },
  1064. statusCode: 200,
  1065. }
  1066. },
  1067. }
  1068. for name, prep := range tests {
  1069. tc := prep(t)
  1070. t.Run(name, func(t *testing.T) {
  1071. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, tc.acmeDB)
  1072. req := httptest.NewRequest("GET", "/foo", nil)
  1073. req = req.WithContext(tc.ctx)
  1074. w := httptest.NewRecorder()
  1075. par.GetProvisionerPolicy(w, req)
  1076. res := w.Result()
  1077. assert.Equal(t, tc.statusCode, res.StatusCode)
  1078. if res.StatusCode >= 400 {
  1079. body, err := io.ReadAll(res.Body)
  1080. res.Body.Close()
  1081. assert.NoError(t, err)
  1082. ae := testAdminError{}
  1083. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1084. assert.Equal(t, tc.err.Type, ae.Type)
  1085. assert.Equal(t, tc.err.Message, ae.Message)
  1086. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  1087. assert.Equal(t, tc.err.Detail, ae.Detail)
  1088. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  1089. return
  1090. }
  1091. p := &testPolicyResponse{}
  1092. body, err := io.ReadAll(res.Body)
  1093. assert.NoError(t, err)
  1094. assert.NoError(t, json.Unmarshal(body, &p))
  1095. assert.Equal(t, tc.response, p)
  1096. })
  1097. }
  1098. }
  1099. func TestPolicyAdminResponder_CreateProvisionerPolicy(t *testing.T) {
  1100. type test struct {
  1101. auth adminAuthority
  1102. adminDB admin.DB
  1103. body []byte
  1104. ctx context.Context
  1105. err *admin.Error
  1106. response *testPolicyResponse
  1107. statusCode int
  1108. }
  1109. var tests = map[string]func(t *testing.T) test{
  1110. "fail/linkedca": func(t *testing.T) test {
  1111. ctx := context.Background()
  1112. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  1113. err.Message = "policy operations not yet supported in linked deployments"
  1114. return test{
  1115. ctx: ctx,
  1116. adminDB: &fakeLinkedCA{},
  1117. err: err,
  1118. statusCode: 501,
  1119. }
  1120. },
  1121. "fail/existing-policy": func(t *testing.T) test {
  1122. policy := &linkedca.Policy{
  1123. X509: &linkedca.X509Policy{
  1124. Allow: &linkedca.X509Names{
  1125. Dns: []string{"*.local"},
  1126. },
  1127. },
  1128. }
  1129. prov := &linkedca.Provisioner{
  1130. Name: "provName",
  1131. Policy: policy,
  1132. }
  1133. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1134. err := admin.NewError(admin.ErrorConflictType, "provisioner provName already has a policy")
  1135. err.Message = "provisioner provName already has a policy"
  1136. return test{
  1137. ctx: ctx,
  1138. err: err,
  1139. statusCode: 409,
  1140. }
  1141. },
  1142. "fail/read.ProtoJSON": func(t *testing.T) test {
  1143. prov := &linkedca.Provisioner{
  1144. Name: "provName",
  1145. }
  1146. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1147. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  1148. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  1149. body := []byte("{?}")
  1150. return test{
  1151. ctx: ctx,
  1152. body: body,
  1153. err: adminErr,
  1154. statusCode: 400,
  1155. }
  1156. },
  1157. "fail/validatePolicy": func(t *testing.T) test {
  1158. prov := &linkedca.Provisioner{
  1159. Name: "provName",
  1160. }
  1161. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1162. adminErr := admin.NewError(admin.ErrorBadRequestType, "error validating provisioner policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)")
  1163. adminErr.Message = "error validating provisioner policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)"
  1164. body := []byte(`
  1165. {
  1166. "x509": {
  1167. "allow": {
  1168. "uris": [
  1169. "https://example.com"
  1170. ]
  1171. }
  1172. }
  1173. }`)
  1174. return test{
  1175. ctx: ctx,
  1176. auth: &mockAdminAuthority{
  1177. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1178. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  1179. },
  1180. },
  1181. body: body,
  1182. err: adminErr,
  1183. statusCode: 400,
  1184. }
  1185. },
  1186. "fail/auth.UpdateProvisioner-policy-admin-lockout-error": func(t *testing.T) test {
  1187. adm := &linkedca.Admin{
  1188. Subject: "step",
  1189. }
  1190. prov := &linkedca.Provisioner{
  1191. Name: "provName",
  1192. }
  1193. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  1194. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  1195. adminErr := admin.NewError(admin.ErrorBadRequestType, "error creating provisioner policy")
  1196. adminErr.Message = "error creating provisioner policy: admin lock out"
  1197. policy := &linkedca.Policy{
  1198. X509: &linkedca.X509Policy{
  1199. Allow: &linkedca.X509Names{
  1200. Dns: []string{"*.local"},
  1201. },
  1202. },
  1203. }
  1204. body, err := protojson.Marshal(policy)
  1205. assert.NoError(t, err)
  1206. return test{
  1207. ctx: ctx,
  1208. auth: &mockAdminAuthority{
  1209. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1210. return &authority.PolicyError{
  1211. Typ: authority.AdminLockOut,
  1212. Err: errors.New("admin lock out"),
  1213. }
  1214. },
  1215. },
  1216. body: body,
  1217. err: adminErr,
  1218. statusCode: 400,
  1219. }
  1220. },
  1221. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  1222. adm := &linkedca.Admin{
  1223. Subject: "step",
  1224. }
  1225. prov := &linkedca.Provisioner{
  1226. Name: "provName",
  1227. }
  1228. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  1229. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  1230. adminErr := admin.NewError(admin.ErrorServerInternalType, "error creating provisioner policy: force")
  1231. adminErr.Message = "error creating provisioner policy: force"
  1232. policy := &linkedca.Policy{
  1233. X509: &linkedca.X509Policy{
  1234. Allow: &linkedca.X509Names{
  1235. Dns: []string{"*.local"},
  1236. },
  1237. },
  1238. }
  1239. body, err := protojson.Marshal(policy)
  1240. assert.NoError(t, err)
  1241. return test{
  1242. ctx: ctx,
  1243. auth: &mockAdminAuthority{
  1244. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1245. return &authority.PolicyError{
  1246. Typ: authority.StoreFailure,
  1247. Err: errors.New("force"),
  1248. }
  1249. },
  1250. },
  1251. body: body,
  1252. err: adminErr,
  1253. statusCode: 500,
  1254. }
  1255. },
  1256. "ok": func(t *testing.T) test {
  1257. adm := &linkedca.Admin{
  1258. Subject: "step",
  1259. }
  1260. prov := &linkedca.Provisioner{
  1261. Name: "provName",
  1262. }
  1263. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  1264. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  1265. policy := &linkedca.Policy{
  1266. X509: &linkedca.X509Policy{
  1267. Allow: &linkedca.X509Names{
  1268. Dns: []string{"*.local"},
  1269. },
  1270. },
  1271. }
  1272. body, err := protojson.Marshal(policy)
  1273. assert.NoError(t, err)
  1274. return test{
  1275. ctx: ctx,
  1276. auth: &mockAdminAuthority{
  1277. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1278. return nil
  1279. },
  1280. },
  1281. body: body,
  1282. response: &testPolicyResponse{
  1283. X509: &testX509Policy{
  1284. Allow: &testX509Names{
  1285. DNSDomains: []string{"*.local"},
  1286. },
  1287. },
  1288. },
  1289. statusCode: 201,
  1290. }
  1291. },
  1292. }
  1293. for name, prep := range tests {
  1294. tc := prep(t)
  1295. t.Run(name, func(t *testing.T) {
  1296. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, nil)
  1297. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  1298. req = req.WithContext(tc.ctx)
  1299. w := httptest.NewRecorder()
  1300. par.CreateProvisionerPolicy(w, req)
  1301. res := w.Result()
  1302. assert.Equal(t, tc.statusCode, res.StatusCode)
  1303. if res.StatusCode >= 400 {
  1304. body, err := io.ReadAll(res.Body)
  1305. res.Body.Close()
  1306. assert.NoError(t, err)
  1307. ae := testAdminError{}
  1308. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1309. assert.Equal(t, tc.err.Type, ae.Type)
  1310. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  1311. assert.Equal(t, tc.err.Detail, ae.Detail)
  1312. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  1313. // when the error message starts with "proto", we expect it to have
  1314. // a syntax error (in the tests). If the message doesn't start with "proto",
  1315. // we expect a full string match.
  1316. if strings.HasPrefix(tc.err.Message, "proto:") {
  1317. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  1318. } else {
  1319. assert.Equal(t, tc.err.Message, ae.Message)
  1320. }
  1321. return
  1322. }
  1323. p := &testPolicyResponse{}
  1324. body, err := io.ReadAll(res.Body)
  1325. assert.NoError(t, err)
  1326. assert.NoError(t, json.Unmarshal(body, &p))
  1327. assert.Equal(t, tc.response, p)
  1328. })
  1329. }
  1330. }
  1331. func TestPolicyAdminResponder_UpdateProvisionerPolicy(t *testing.T) {
  1332. type test struct {
  1333. auth adminAuthority
  1334. body []byte
  1335. adminDB admin.DB
  1336. ctx context.Context
  1337. err *admin.Error
  1338. response *testPolicyResponse
  1339. statusCode int
  1340. }
  1341. var tests = map[string]func(t *testing.T) test{
  1342. "fail/linkedca": func(t *testing.T) test {
  1343. ctx := context.Background()
  1344. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  1345. err.Message = "policy operations not yet supported in linked deployments"
  1346. return test{
  1347. ctx: ctx,
  1348. adminDB: &fakeLinkedCA{},
  1349. err: err,
  1350. statusCode: 501,
  1351. }
  1352. },
  1353. "fail/no-existing-policy": func(t *testing.T) test {
  1354. prov := &linkedca.Provisioner{
  1355. Name: "provName",
  1356. }
  1357. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1358. err := admin.NewError(admin.ErrorNotFoundType, "provisioner policy does not exist")
  1359. err.Message = "provisioner policy does not exist"
  1360. return test{
  1361. ctx: ctx,
  1362. err: err,
  1363. statusCode: 404,
  1364. }
  1365. },
  1366. "fail/read.ProtoJSON": func(t *testing.T) test {
  1367. policy := &linkedca.Policy{
  1368. X509: &linkedca.X509Policy{
  1369. Allow: &linkedca.X509Names{
  1370. Dns: []string{"*.local"},
  1371. },
  1372. },
  1373. }
  1374. prov := &linkedca.Provisioner{
  1375. Name: "provName",
  1376. Policy: policy,
  1377. }
  1378. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1379. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  1380. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  1381. body := []byte("{?}")
  1382. return test{
  1383. ctx: ctx,
  1384. body: body,
  1385. err: adminErr,
  1386. statusCode: 400,
  1387. }
  1388. },
  1389. "fail/validatePolicy": func(t *testing.T) test {
  1390. policy := &linkedca.Policy{
  1391. X509: &linkedca.X509Policy{
  1392. Allow: &linkedca.X509Names{
  1393. Dns: []string{"*.local"},
  1394. },
  1395. },
  1396. }
  1397. prov := &linkedca.Provisioner{
  1398. Name: "provName",
  1399. Policy: policy,
  1400. }
  1401. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1402. adminErr := admin.NewError(admin.ErrorBadRequestType, "error validating provisioner policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)")
  1403. adminErr.Message = "error validating provisioner policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)"
  1404. body := []byte(`
  1405. {
  1406. "x509": {
  1407. "allow": {
  1408. "uris": [
  1409. "https://example.com"
  1410. ]
  1411. }
  1412. }
  1413. }`)
  1414. return test{
  1415. ctx: ctx,
  1416. auth: &mockAdminAuthority{
  1417. MockGetAuthorityPolicy: func(ctx context.Context) (*linkedca.Policy, error) {
  1418. return nil, admin.NewError(admin.ErrorNotFoundType, "not found")
  1419. },
  1420. },
  1421. body: body,
  1422. err: adminErr,
  1423. statusCode: 400,
  1424. }
  1425. },
  1426. "fail/auth.UpdateProvisioner-policy-admin-lockout-error": func(t *testing.T) test {
  1427. adm := &linkedca.Admin{
  1428. Subject: "step",
  1429. }
  1430. policy := &linkedca.Policy{
  1431. X509: &linkedca.X509Policy{
  1432. Allow: &linkedca.X509Names{
  1433. Dns: []string{"*.local"},
  1434. },
  1435. },
  1436. }
  1437. prov := &linkedca.Provisioner{
  1438. Name: "provName",
  1439. Policy: policy,
  1440. }
  1441. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  1442. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  1443. adminErr := admin.NewError(admin.ErrorBadRequestType, "error updating provisioner policy")
  1444. adminErr.Message = "error updating provisioner policy: admin lock out"
  1445. body, err := protojson.Marshal(policy)
  1446. assert.NoError(t, err)
  1447. return test{
  1448. ctx: ctx,
  1449. auth: &mockAdminAuthority{
  1450. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1451. return &authority.PolicyError{
  1452. Typ: authority.AdminLockOut,
  1453. Err: errors.New("admin lock out"),
  1454. }
  1455. },
  1456. },
  1457. body: body,
  1458. err: adminErr,
  1459. statusCode: 400,
  1460. }
  1461. },
  1462. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  1463. adm := &linkedca.Admin{
  1464. Subject: "step",
  1465. }
  1466. policy := &linkedca.Policy{
  1467. X509: &linkedca.X509Policy{
  1468. Allow: &linkedca.X509Names{
  1469. Dns: []string{"*.local"},
  1470. },
  1471. },
  1472. }
  1473. prov := &linkedca.Provisioner{
  1474. Name: "provName",
  1475. Policy: policy,
  1476. }
  1477. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  1478. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  1479. adminErr := admin.NewError(admin.ErrorServerInternalType, "error updating provisioner policy: force")
  1480. adminErr.Message = "error updating provisioner policy: force"
  1481. body, err := protojson.Marshal(policy)
  1482. assert.NoError(t, err)
  1483. return test{
  1484. ctx: ctx,
  1485. auth: &mockAdminAuthority{
  1486. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1487. return &authority.PolicyError{
  1488. Typ: authority.StoreFailure,
  1489. Err: errors.New("force"),
  1490. }
  1491. },
  1492. },
  1493. body: body,
  1494. err: adminErr,
  1495. statusCode: 500,
  1496. }
  1497. },
  1498. "ok": func(t *testing.T) test {
  1499. adm := &linkedca.Admin{
  1500. Subject: "step",
  1501. }
  1502. policy := &linkedca.Policy{
  1503. X509: &linkedca.X509Policy{
  1504. Allow: &linkedca.X509Names{
  1505. Dns: []string{"*.local"},
  1506. },
  1507. },
  1508. }
  1509. prov := &linkedca.Provisioner{
  1510. Name: "provName",
  1511. Policy: policy,
  1512. }
  1513. ctx := linkedca.NewContextWithAdmin(context.Background(), adm)
  1514. ctx = linkedca.NewContextWithProvisioner(ctx, prov)
  1515. body, err := protojson.Marshal(policy)
  1516. assert.NoError(t, err)
  1517. return test{
  1518. ctx: ctx,
  1519. auth: &mockAdminAuthority{
  1520. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1521. return nil
  1522. },
  1523. },
  1524. body: body,
  1525. response: &testPolicyResponse{
  1526. X509: &testX509Policy{
  1527. Allow: &testX509Names{
  1528. DNSDomains: []string{"*.local"},
  1529. },
  1530. },
  1531. },
  1532. statusCode: 200,
  1533. }
  1534. },
  1535. }
  1536. for name, prep := range tests {
  1537. tc := prep(t)
  1538. t.Run(name, func(t *testing.T) {
  1539. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, nil)
  1540. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  1541. req = req.WithContext(tc.ctx)
  1542. w := httptest.NewRecorder()
  1543. par.UpdateProvisionerPolicy(w, req)
  1544. res := w.Result()
  1545. assert.Equal(t, tc.statusCode, res.StatusCode)
  1546. if res.StatusCode >= 400 {
  1547. body, err := io.ReadAll(res.Body)
  1548. res.Body.Close()
  1549. assert.NoError(t, err)
  1550. ae := testAdminError{}
  1551. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1552. assert.Equal(t, tc.err.Type, ae.Type)
  1553. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  1554. assert.Equal(t, tc.err.Detail, ae.Detail)
  1555. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  1556. // when the error message starts with "proto", we expect it to have
  1557. // a syntax error (in the tests). If the message doesn't start with "proto",
  1558. // we expect a full string match.
  1559. if strings.HasPrefix(tc.err.Message, "proto:") {
  1560. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  1561. } else {
  1562. assert.Equal(t, tc.err.Message, ae.Message)
  1563. }
  1564. return
  1565. }
  1566. p := &testPolicyResponse{}
  1567. body, err := io.ReadAll(res.Body)
  1568. assert.NoError(t, err)
  1569. assert.NoError(t, json.Unmarshal(body, &p))
  1570. assert.Equal(t, tc.response, p)
  1571. })
  1572. }
  1573. }
  1574. func TestPolicyAdminResponder_DeleteProvisionerPolicy(t *testing.T) {
  1575. type test struct {
  1576. auth adminAuthority
  1577. adminDB admin.DB
  1578. body []byte
  1579. ctx context.Context
  1580. acmeDB acme.DB
  1581. err *admin.Error
  1582. statusCode int
  1583. }
  1584. var tests = map[string]func(t *testing.T) test{
  1585. "fail/linkedca": func(t *testing.T) test {
  1586. ctx := context.Background()
  1587. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  1588. err.Message = "policy operations not yet supported in linked deployments"
  1589. return test{
  1590. ctx: ctx,
  1591. adminDB: &fakeLinkedCA{},
  1592. err: err,
  1593. statusCode: 501,
  1594. }
  1595. },
  1596. "fail/no-existing-policy": func(t *testing.T) test {
  1597. prov := &linkedca.Provisioner{
  1598. Name: "provName",
  1599. }
  1600. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1601. err := admin.NewError(admin.ErrorNotFoundType, "provisioner policy does not exist")
  1602. err.Message = "provisioner policy does not exist"
  1603. return test{
  1604. ctx: ctx,
  1605. err: err,
  1606. statusCode: 404,
  1607. }
  1608. },
  1609. "fail/auth.UpdateProvisioner-error": func(t *testing.T) test {
  1610. prov := &linkedca.Provisioner{
  1611. Name: "provName",
  1612. Policy: &linkedca.Policy{},
  1613. }
  1614. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1615. err := admin.NewErrorISE("error deleting provisioner policy: force")
  1616. err.Message = "error deleting provisioner policy: force"
  1617. return test{
  1618. ctx: ctx,
  1619. auth: &mockAdminAuthority{
  1620. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1621. return errors.New("force")
  1622. },
  1623. },
  1624. err: err,
  1625. statusCode: 500,
  1626. }
  1627. },
  1628. "ok": func(t *testing.T) test {
  1629. prov := &linkedca.Provisioner{
  1630. Name: "provName",
  1631. Policy: &linkedca.Policy{},
  1632. }
  1633. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1634. return test{
  1635. ctx: ctx,
  1636. auth: &mockAdminAuthority{
  1637. MockUpdateProvisioner: func(ctx context.Context, nu *linkedca.Provisioner) error {
  1638. return nil
  1639. },
  1640. },
  1641. statusCode: 200,
  1642. }
  1643. },
  1644. }
  1645. for name, prep := range tests {
  1646. tc := prep(t)
  1647. t.Run(name, func(t *testing.T) {
  1648. par := NewPolicyAdminResponder(tc.auth, tc.adminDB, tc.acmeDB)
  1649. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  1650. req = req.WithContext(tc.ctx)
  1651. w := httptest.NewRecorder()
  1652. par.DeleteProvisionerPolicy(w, req)
  1653. res := w.Result()
  1654. assert.Equal(t, tc.statusCode, res.StatusCode)
  1655. if res.StatusCode >= 400 {
  1656. body, err := io.ReadAll(res.Body)
  1657. res.Body.Close()
  1658. assert.NoError(t, err)
  1659. ae := testAdminError{}
  1660. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1661. assert.Equal(t, tc.err.Type, ae.Type)
  1662. assert.Equal(t, tc.err.Message, ae.Message)
  1663. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  1664. assert.Equal(t, tc.err.Detail, ae.Detail)
  1665. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  1666. return
  1667. }
  1668. body, err := io.ReadAll(res.Body)
  1669. assert.NoError(t, err)
  1670. res.Body.Close()
  1671. response := DeleteResponse{}
  1672. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  1673. assert.Equal(t, "ok", response.Status)
  1674. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  1675. })
  1676. }
  1677. }
  1678. func TestPolicyAdminResponder_GetACMEAccountPolicy(t *testing.T) {
  1679. type test struct {
  1680. ctx context.Context
  1681. acmeDB acme.DB
  1682. adminDB admin.DB
  1683. err *admin.Error
  1684. response *testPolicyResponse
  1685. statusCode int
  1686. }
  1687. var tests = map[string]func(t *testing.T) test{
  1688. "fail/linkedca": func(t *testing.T) test {
  1689. ctx := context.Background()
  1690. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  1691. err.Message = "policy operations not yet supported in linked deployments"
  1692. return test{
  1693. ctx: ctx,
  1694. adminDB: &fakeLinkedCA{},
  1695. err: err,
  1696. statusCode: 501,
  1697. }
  1698. },
  1699. "fail/no-policy": func(t *testing.T) test {
  1700. prov := &linkedca.Provisioner{
  1701. Name: "provName",
  1702. }
  1703. eak := &linkedca.EABKey{
  1704. Id: "eakID",
  1705. }
  1706. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1707. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1708. err := admin.NewError(admin.ErrorNotFoundType, "ACME EAK policy does not exist")
  1709. err.Message = "ACME EAK policy does not exist"
  1710. return test{
  1711. ctx: ctx,
  1712. err: err,
  1713. statusCode: 404,
  1714. }
  1715. },
  1716. "ok": func(t *testing.T) test {
  1717. policy := &linkedca.Policy{
  1718. X509: &linkedca.X509Policy{
  1719. Allow: &linkedca.X509Names{
  1720. Dns: []string{"*.local"},
  1721. Ips: []string{"10.0.0.0/16"},
  1722. Emails: []string{"@example.com"},
  1723. Uris: []string{"example.com"},
  1724. CommonNames: []string{"test"},
  1725. },
  1726. Deny: &linkedca.X509Names{
  1727. Dns: []string{"bad.local"},
  1728. Ips: []string{"10.0.0.30"},
  1729. Emails: []string{"bad@example.com"},
  1730. Uris: []string{"notexample.com"},
  1731. CommonNames: []string{"bad"},
  1732. },
  1733. },
  1734. Ssh: &linkedca.SSHPolicy{
  1735. User: &linkedca.SSHUserPolicy{
  1736. Allow: &linkedca.SSHUserNames{
  1737. Emails: []string{"@example.com"},
  1738. Principals: []string{"*"},
  1739. },
  1740. Deny: &linkedca.SSHUserNames{
  1741. Emails: []string{"bad@example.com"},
  1742. Principals: []string{"root"},
  1743. },
  1744. },
  1745. Host: &linkedca.SSHHostPolicy{
  1746. Allow: &linkedca.SSHHostNames{
  1747. Dns: []string{"*.example.com"},
  1748. Ips: []string{"10.10.0.0/16"},
  1749. Principals: []string{"good"},
  1750. },
  1751. Deny: &linkedca.SSHHostNames{
  1752. Dns: []string{"bad@example.com"},
  1753. Ips: []string{"10.10.0.30"},
  1754. Principals: []string{"bad"},
  1755. },
  1756. },
  1757. },
  1758. }
  1759. prov := &linkedca.Provisioner{
  1760. Name: "provName",
  1761. }
  1762. eak := &linkedca.EABKey{
  1763. Id: "eakID",
  1764. Policy: policy,
  1765. }
  1766. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1767. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1768. return test{
  1769. ctx: ctx,
  1770. response: &testPolicyResponse{
  1771. X509: &testX509Policy{
  1772. Allow: &testX509Names{
  1773. DNSDomains: []string{"*.local"},
  1774. IPRanges: []string{"10.0.0.0/16"},
  1775. EmailAddresses: []string{"@example.com"},
  1776. URIDomains: []string{"example.com"},
  1777. CommonNames: []string{"test"},
  1778. },
  1779. Deny: &testX509Names{
  1780. DNSDomains: []string{"bad.local"},
  1781. IPRanges: []string{"10.0.0.30"},
  1782. EmailAddresses: []string{"bad@example.com"},
  1783. URIDomains: []string{"notexample.com"},
  1784. CommonNames: []string{"bad"},
  1785. },
  1786. },
  1787. SSH: &testSSHPolicy{
  1788. User: &testSSHUserPolicy{
  1789. Allow: &testSSHUserNames{
  1790. EmailAddresses: []string{"@example.com"},
  1791. Principals: []string{"*"},
  1792. },
  1793. Deny: &testSSHUserNames{
  1794. EmailAddresses: []string{"bad@example.com"},
  1795. Principals: []string{"root"},
  1796. },
  1797. },
  1798. Host: &testSSHHostPolicy{
  1799. Allow: &testSSHHostNames{
  1800. DNSDomains: []string{"*.example.com"},
  1801. IPRanges: []string{"10.10.0.0/16"},
  1802. Principals: []string{"good"},
  1803. },
  1804. Deny: &testSSHHostNames{
  1805. DNSDomains: []string{"bad@example.com"},
  1806. IPRanges: []string{"10.10.0.30"},
  1807. Principals: []string{"bad"},
  1808. },
  1809. },
  1810. },
  1811. },
  1812. statusCode: 200,
  1813. }
  1814. },
  1815. }
  1816. for name, prep := range tests {
  1817. tc := prep(t)
  1818. t.Run(name, func(t *testing.T) {
  1819. par := NewPolicyAdminResponder(nil, tc.adminDB, tc.acmeDB)
  1820. req := httptest.NewRequest("GET", "/foo", nil)
  1821. req = req.WithContext(tc.ctx)
  1822. w := httptest.NewRecorder()
  1823. par.GetACMEAccountPolicy(w, req)
  1824. res := w.Result()
  1825. assert.Equal(t, tc.statusCode, res.StatusCode)
  1826. if res.StatusCode >= 400 {
  1827. body, err := io.ReadAll(res.Body)
  1828. res.Body.Close()
  1829. assert.NoError(t, err)
  1830. ae := testAdminError{}
  1831. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1832. assert.Equal(t, tc.err.Type, ae.Type)
  1833. assert.Equal(t, tc.err.Message, ae.Message)
  1834. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  1835. assert.Equal(t, tc.err.Detail, ae.Detail)
  1836. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  1837. return
  1838. }
  1839. p := &testPolicyResponse{}
  1840. body, err := io.ReadAll(res.Body)
  1841. assert.NoError(t, err)
  1842. assert.NoError(t, json.Unmarshal(body, &p))
  1843. assert.Equal(t, tc.response, p)
  1844. })
  1845. }
  1846. }
  1847. func TestPolicyAdminResponder_CreateACMEAccountPolicy(t *testing.T) {
  1848. type test struct {
  1849. acmeDB acme.DB
  1850. adminDB admin.DB
  1851. body []byte
  1852. ctx context.Context
  1853. err *admin.Error
  1854. response *testPolicyResponse
  1855. statusCode int
  1856. }
  1857. var tests = map[string]func(t *testing.T) test{
  1858. "fail/linkedca": func(t *testing.T) test {
  1859. ctx := context.Background()
  1860. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  1861. err.Message = "policy operations not yet supported in linked deployments"
  1862. return test{
  1863. ctx: ctx,
  1864. adminDB: &fakeLinkedCA{},
  1865. err: err,
  1866. statusCode: 501,
  1867. }
  1868. },
  1869. "fail/existing-policy": func(t *testing.T) test {
  1870. policy := &linkedca.Policy{
  1871. X509: &linkedca.X509Policy{
  1872. Allow: &linkedca.X509Names{
  1873. Dns: []string{"*.local"},
  1874. },
  1875. },
  1876. }
  1877. prov := &linkedca.Provisioner{
  1878. Name: "provName",
  1879. }
  1880. eak := &linkedca.EABKey{
  1881. Id: "eakID",
  1882. Policy: policy,
  1883. }
  1884. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1885. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1886. err := admin.NewError(admin.ErrorConflictType, "ACME EAK eakID already has a policy")
  1887. err.Message = "ACME EAK eakID already has a policy"
  1888. return test{
  1889. ctx: ctx,
  1890. err: err,
  1891. statusCode: 409,
  1892. }
  1893. },
  1894. "fail/read.ProtoJSON": func(t *testing.T) test {
  1895. prov := &linkedca.Provisioner{
  1896. Name: "provName",
  1897. }
  1898. eak := &linkedca.EABKey{
  1899. Id: "eakID",
  1900. }
  1901. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1902. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1903. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  1904. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  1905. body := []byte("{?}")
  1906. return test{
  1907. ctx: ctx,
  1908. body: body,
  1909. err: adminErr,
  1910. statusCode: 400,
  1911. }
  1912. },
  1913. "fail/validatePolicy": func(t *testing.T) test {
  1914. prov := &linkedca.Provisioner{
  1915. Name: "provName",
  1916. }
  1917. eak := &linkedca.EABKey{
  1918. Id: "eakID",
  1919. }
  1920. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1921. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1922. adminErr := admin.NewError(admin.ErrorBadRequestType, "error validating ACME EAK policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)")
  1923. adminErr.Message = "error validating ACME EAK policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)"
  1924. body := []byte(`
  1925. {
  1926. "x509": {
  1927. "allow": {
  1928. "uris": [
  1929. "https://example.com"
  1930. ]
  1931. }
  1932. }
  1933. }`)
  1934. return test{
  1935. ctx: ctx,
  1936. body: body,
  1937. err: adminErr,
  1938. statusCode: 400,
  1939. }
  1940. },
  1941. "fail/acmeDB.UpdateExternalAccountKey-error": func(t *testing.T) test {
  1942. prov := &linkedca.Provisioner{
  1943. Id: "provID",
  1944. Name: "provName",
  1945. }
  1946. eak := &linkedca.EABKey{
  1947. Id: "eakID",
  1948. }
  1949. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1950. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1951. adminErr := admin.NewError(admin.ErrorServerInternalType, "error creating ACME EAK policy")
  1952. adminErr.Message = "error creating ACME EAK policy: force"
  1953. policy := &linkedca.Policy{
  1954. X509: &linkedca.X509Policy{
  1955. Allow: &linkedca.X509Names{
  1956. Dns: []string{"*.local"},
  1957. },
  1958. },
  1959. }
  1960. body, err := protojson.Marshal(policy)
  1961. assert.NoError(t, err)
  1962. return test{
  1963. ctx: ctx,
  1964. acmeDB: &acme.MockDB{
  1965. MockUpdateExternalAccountKey: func(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
  1966. assert.Equal(t, "provID", provisionerID)
  1967. assert.Equal(t, "eakID", eak.ID)
  1968. return errors.New("force")
  1969. },
  1970. },
  1971. body: body,
  1972. err: adminErr,
  1973. statusCode: 500,
  1974. }
  1975. },
  1976. "ok": func(t *testing.T) test {
  1977. prov := &linkedca.Provisioner{
  1978. Id: "provID",
  1979. Name: "provName",
  1980. }
  1981. eak := &linkedca.EABKey{
  1982. Id: "eakID",
  1983. }
  1984. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  1985. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  1986. policy := &linkedca.Policy{
  1987. X509: &linkedca.X509Policy{
  1988. Allow: &linkedca.X509Names{
  1989. Dns: []string{"*.local"},
  1990. },
  1991. },
  1992. }
  1993. body, err := protojson.Marshal(policy)
  1994. assert.NoError(t, err)
  1995. return test{
  1996. ctx: ctx,
  1997. acmeDB: &acme.MockDB{
  1998. MockUpdateExternalAccountKey: func(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
  1999. assert.Equal(t, "provID", provisionerID)
  2000. assert.Equal(t, "eakID", eak.ID)
  2001. return nil
  2002. },
  2003. },
  2004. body: body,
  2005. response: &testPolicyResponse{
  2006. X509: &testX509Policy{
  2007. Allow: &testX509Names{
  2008. DNSDomains: []string{"*.local"},
  2009. },
  2010. },
  2011. },
  2012. statusCode: 201,
  2013. }
  2014. },
  2015. }
  2016. for name, prep := range tests {
  2017. tc := prep(t)
  2018. t.Run(name, func(t *testing.T) {
  2019. par := NewPolicyAdminResponder(nil, tc.adminDB, tc.acmeDB)
  2020. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  2021. req = req.WithContext(tc.ctx)
  2022. w := httptest.NewRecorder()
  2023. par.CreateACMEAccountPolicy(w, req)
  2024. res := w.Result()
  2025. assert.Equal(t, tc.statusCode, res.StatusCode)
  2026. if res.StatusCode >= 400 {
  2027. body, err := io.ReadAll(res.Body)
  2028. res.Body.Close()
  2029. assert.NoError(t, err)
  2030. ae := testAdminError{}
  2031. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  2032. assert.Equal(t, tc.err.Type, ae.Type)
  2033. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  2034. assert.Equal(t, tc.err.Detail, ae.Detail)
  2035. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  2036. // when the error message starts with "proto", we expect it to have
  2037. // a syntax error (in the tests). If the message doesn't start with "proto",
  2038. // we expect a full string match.
  2039. if strings.HasPrefix(tc.err.Message, "proto:") {
  2040. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  2041. } else {
  2042. assert.Equal(t, tc.err.Message, ae.Message)
  2043. }
  2044. return
  2045. }
  2046. p := &testPolicyResponse{}
  2047. body, err := io.ReadAll(res.Body)
  2048. assert.NoError(t, err)
  2049. assert.NoError(t, json.Unmarshal(body, &p))
  2050. assert.Equal(t, tc.response, p)
  2051. })
  2052. }
  2053. }
  2054. func TestPolicyAdminResponder_UpdateACMEAccountPolicy(t *testing.T) {
  2055. type test struct {
  2056. acmeDB acme.DB
  2057. adminDB admin.DB
  2058. body []byte
  2059. ctx context.Context
  2060. err *admin.Error
  2061. response *testPolicyResponse
  2062. statusCode int
  2063. }
  2064. var tests = map[string]func(t *testing.T) test{
  2065. "fail/linkedca": func(t *testing.T) test {
  2066. ctx := context.Background()
  2067. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  2068. err.Message = "policy operations not yet supported in linked deployments"
  2069. return test{
  2070. ctx: ctx,
  2071. adminDB: &fakeLinkedCA{},
  2072. err: err,
  2073. statusCode: 501,
  2074. }
  2075. },
  2076. "fail/no-existing-policy": func(t *testing.T) test {
  2077. prov := &linkedca.Provisioner{
  2078. Name: "provName",
  2079. }
  2080. eak := &linkedca.EABKey{
  2081. Id: "eakID",
  2082. }
  2083. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2084. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2085. err := admin.NewError(admin.ErrorNotFoundType, "ACME EAK policy does not exist")
  2086. err.Message = "ACME EAK policy does not exist"
  2087. return test{
  2088. ctx: ctx,
  2089. err: err,
  2090. statusCode: 404,
  2091. }
  2092. },
  2093. "fail/read.ProtoJSON": func(t *testing.T) test {
  2094. policy := &linkedca.Policy{
  2095. X509: &linkedca.X509Policy{
  2096. Allow: &linkedca.X509Names{
  2097. Dns: []string{"*.local"},
  2098. },
  2099. },
  2100. }
  2101. prov := &linkedca.Provisioner{
  2102. Name: "provName",
  2103. }
  2104. eak := &linkedca.EABKey{
  2105. Id: "eakID",
  2106. Policy: policy,
  2107. }
  2108. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2109. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2110. adminErr := admin.NewError(admin.ErrorBadRequestType, "proto: syntax error (line 1:2): invalid value ?")
  2111. adminErr.Message = "proto: syntax error (line 1:2): invalid value ?"
  2112. body := []byte("{?}")
  2113. return test{
  2114. ctx: ctx,
  2115. body: body,
  2116. err: adminErr,
  2117. statusCode: 400,
  2118. }
  2119. },
  2120. "fail/validatePolicy": func(t *testing.T) test {
  2121. policy := &linkedca.Policy{
  2122. X509: &linkedca.X509Policy{
  2123. Allow: &linkedca.X509Names{
  2124. Dns: []string{"*.local"},
  2125. },
  2126. },
  2127. }
  2128. prov := &linkedca.Provisioner{
  2129. Name: "provName",
  2130. }
  2131. eak := &linkedca.EABKey{
  2132. Id: "eakID",
  2133. Policy: policy,
  2134. }
  2135. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2136. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2137. adminErr := admin.NewError(admin.ErrorBadRequestType, "error validating ACME EAK policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)")
  2138. adminErr.Message = "error validating ACME EAK policy: cannot parse permitted URI domain constraint \"https://example.com\": URI domain constraint \"https://example.com\" contains scheme (not supported yet)"
  2139. body := []byte(`
  2140. {
  2141. "x509": {
  2142. "allow": {
  2143. "uris": [
  2144. "https://example.com"
  2145. ]
  2146. }
  2147. }
  2148. }`)
  2149. return test{
  2150. ctx: ctx,
  2151. body: body,
  2152. err: adminErr,
  2153. statusCode: 400,
  2154. }
  2155. },
  2156. "fail/acmeDB.UpdateExternalAccountKey-error": func(t *testing.T) test {
  2157. policy := &linkedca.Policy{
  2158. X509: &linkedca.X509Policy{
  2159. Allow: &linkedca.X509Names{
  2160. Dns: []string{"*.local"},
  2161. },
  2162. },
  2163. }
  2164. prov := &linkedca.Provisioner{
  2165. Name: "provName",
  2166. Id: "provID",
  2167. }
  2168. eak := &linkedca.EABKey{
  2169. Id: "eakID",
  2170. Policy: policy,
  2171. }
  2172. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2173. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2174. adminErr := admin.NewError(admin.ErrorServerInternalType, "error updating ACME EAK policy: force")
  2175. adminErr.Message = "error updating ACME EAK policy: force"
  2176. body, err := protojson.Marshal(policy)
  2177. assert.NoError(t, err)
  2178. return test{
  2179. ctx: ctx,
  2180. acmeDB: &acme.MockDB{
  2181. MockUpdateExternalAccountKey: func(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
  2182. assert.Equal(t, "provID", provisionerID)
  2183. assert.Equal(t, "eakID", eak.ID)
  2184. return errors.New("force")
  2185. },
  2186. },
  2187. body: body,
  2188. err: adminErr,
  2189. statusCode: 500,
  2190. }
  2191. },
  2192. "ok": func(t *testing.T) test {
  2193. policy := &linkedca.Policy{
  2194. X509: &linkedca.X509Policy{
  2195. Allow: &linkedca.X509Names{
  2196. Dns: []string{"*.local"},
  2197. },
  2198. },
  2199. }
  2200. prov := &linkedca.Provisioner{
  2201. Name: "provName",
  2202. Id: "provID",
  2203. }
  2204. eak := &linkedca.EABKey{
  2205. Id: "eakID",
  2206. Policy: policy,
  2207. }
  2208. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2209. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2210. body, err := protojson.Marshal(policy)
  2211. assert.NoError(t, err)
  2212. return test{
  2213. ctx: ctx,
  2214. acmeDB: &acme.MockDB{
  2215. MockUpdateExternalAccountKey: func(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
  2216. assert.Equal(t, "provID", provisionerID)
  2217. assert.Equal(t, "eakID", eak.ID)
  2218. return nil
  2219. },
  2220. },
  2221. body: body,
  2222. response: &testPolicyResponse{
  2223. X509: &testX509Policy{
  2224. Allow: &testX509Names{
  2225. DNSDomains: []string{"*.local"},
  2226. },
  2227. },
  2228. },
  2229. statusCode: 200,
  2230. }
  2231. },
  2232. }
  2233. for name, prep := range tests {
  2234. tc := prep(t)
  2235. t.Run(name, func(t *testing.T) {
  2236. par := NewPolicyAdminResponder(nil, tc.adminDB, tc.acmeDB)
  2237. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  2238. req = req.WithContext(tc.ctx)
  2239. w := httptest.NewRecorder()
  2240. par.UpdateACMEAccountPolicy(w, req)
  2241. res := w.Result()
  2242. assert.Equal(t, tc.statusCode, res.StatusCode)
  2243. if res.StatusCode >= 400 {
  2244. body, err := io.ReadAll(res.Body)
  2245. res.Body.Close()
  2246. assert.NoError(t, err)
  2247. ae := testAdminError{}
  2248. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  2249. assert.Equal(t, tc.err.Type, ae.Type)
  2250. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  2251. assert.Equal(t, tc.err.Detail, ae.Detail)
  2252. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  2253. // when the error message starts with "proto", we expect it to have
  2254. // a syntax error (in the tests). If the message doesn't start with "proto",
  2255. // we expect a full string match.
  2256. if strings.HasPrefix(tc.err.Message, "proto:") {
  2257. assert.True(t, strings.Contains(ae.Message, "syntax error"))
  2258. } else {
  2259. assert.Equal(t, tc.err.Message, ae.Message)
  2260. }
  2261. return
  2262. }
  2263. p := &testPolicyResponse{}
  2264. body, err := io.ReadAll(res.Body)
  2265. assert.NoError(t, err)
  2266. assert.NoError(t, json.Unmarshal(body, &p))
  2267. assert.Equal(t, tc.response, p)
  2268. })
  2269. }
  2270. }
  2271. func TestPolicyAdminResponder_DeleteACMEAccountPolicy(t *testing.T) {
  2272. type test struct {
  2273. body []byte
  2274. adminDB admin.DB
  2275. ctx context.Context
  2276. acmeDB acme.DB
  2277. err *admin.Error
  2278. statusCode int
  2279. }
  2280. var tests = map[string]func(t *testing.T) test{
  2281. "fail/linkedca": func(t *testing.T) test {
  2282. ctx := context.Background()
  2283. err := admin.NewError(admin.ErrorNotImplementedType, "policy operations not yet supported in linked deployments")
  2284. err.Message = "policy operations not yet supported in linked deployments"
  2285. return test{
  2286. ctx: ctx,
  2287. adminDB: &fakeLinkedCA{},
  2288. err: err,
  2289. statusCode: 501,
  2290. }
  2291. },
  2292. "fail/no-existing-policy": func(t *testing.T) test {
  2293. prov := &linkedca.Provisioner{
  2294. Name: "provName",
  2295. }
  2296. eak := &linkedca.EABKey{
  2297. Id: "eakID",
  2298. }
  2299. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2300. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2301. err := admin.NewError(admin.ErrorNotFoundType, "ACME EAK policy does not exist")
  2302. err.Message = "ACME EAK policy does not exist"
  2303. return test{
  2304. ctx: ctx,
  2305. err: err,
  2306. statusCode: 404,
  2307. }
  2308. },
  2309. "fail/acmeDB.UpdateExternalAccountKey-error": func(t *testing.T) test {
  2310. policy := &linkedca.Policy{
  2311. X509: &linkedca.X509Policy{
  2312. Allow: &linkedca.X509Names{
  2313. Dns: []string{"*.local"},
  2314. },
  2315. },
  2316. }
  2317. prov := &linkedca.Provisioner{
  2318. Name: "provName",
  2319. Id: "provID",
  2320. }
  2321. eak := &linkedca.EABKey{
  2322. Id: "eakID",
  2323. Policy: policy,
  2324. }
  2325. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2326. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2327. err := admin.NewErrorISE("error deleting ACME EAK policy: force")
  2328. err.Message = "error deleting ACME EAK policy: force"
  2329. return test{
  2330. ctx: ctx,
  2331. acmeDB: &acme.MockDB{
  2332. MockUpdateExternalAccountKey: func(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
  2333. assert.Equal(t, "provID", provisionerID)
  2334. assert.Equal(t, "eakID", eak.ID)
  2335. return errors.New("force")
  2336. },
  2337. },
  2338. err: err,
  2339. statusCode: 500,
  2340. }
  2341. },
  2342. "ok": func(t *testing.T) test {
  2343. policy := &linkedca.Policy{
  2344. X509: &linkedca.X509Policy{
  2345. Allow: &linkedca.X509Names{
  2346. Dns: []string{"*.local"},
  2347. },
  2348. },
  2349. }
  2350. prov := &linkedca.Provisioner{
  2351. Name: "provName",
  2352. Id: "provID",
  2353. }
  2354. eak := &linkedca.EABKey{
  2355. Id: "eakID",
  2356. Policy: policy,
  2357. }
  2358. ctx := linkedca.NewContextWithProvisioner(context.Background(), prov)
  2359. ctx = linkedca.NewContextWithExternalAccountKey(ctx, eak)
  2360. return test{
  2361. ctx: ctx,
  2362. acmeDB: &acme.MockDB{
  2363. MockUpdateExternalAccountKey: func(ctx context.Context, provisionerID string, eak *acme.ExternalAccountKey) error {
  2364. assert.Equal(t, "provID", provisionerID)
  2365. assert.Equal(t, "eakID", eak.ID)
  2366. return nil
  2367. },
  2368. },
  2369. statusCode: 200,
  2370. }
  2371. },
  2372. }
  2373. for name, prep := range tests {
  2374. tc := prep(t)
  2375. t.Run(name, func(t *testing.T) {
  2376. par := NewPolicyAdminResponder(nil, tc.adminDB, tc.acmeDB)
  2377. req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body)))
  2378. req = req.WithContext(tc.ctx)
  2379. w := httptest.NewRecorder()
  2380. par.DeleteACMEAccountPolicy(w, req)
  2381. res := w.Result()
  2382. assert.Equal(t, tc.statusCode, res.StatusCode)
  2383. if res.StatusCode >= 400 {
  2384. body, err := io.ReadAll(res.Body)
  2385. res.Body.Close()
  2386. assert.NoError(t, err)
  2387. ae := testAdminError{}
  2388. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  2389. assert.Equal(t, tc.err.Type, ae.Type)
  2390. assert.Equal(t, tc.err.Message, ae.Message)
  2391. assert.Equal(t, tc.err.StatusCode(), res.StatusCode)
  2392. assert.Equal(t, tc.err.Detail, ae.Detail)
  2393. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  2394. return
  2395. }
  2396. body, err := io.ReadAll(res.Body)
  2397. assert.NoError(t, err)
  2398. res.Body.Close()
  2399. response := DeleteResponse{}
  2400. assert.NoError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
  2401. assert.Equal(t, "ok", response.Status)
  2402. assert.Equal(t, []string{"application/json"}, res.Header["Content-Type"])
  2403. })
  2404. }
  2405. }
  2406. func Test_isBadRequest(t *testing.T) {
  2407. tests := []struct {
  2408. name string
  2409. err error
  2410. want bool
  2411. }{
  2412. {
  2413. name: "nil",
  2414. err: nil,
  2415. want: false,
  2416. },
  2417. {
  2418. name: "no-policy-error",
  2419. err: errors.New("some error"),
  2420. want: false,
  2421. },
  2422. {
  2423. name: "no-bad-request",
  2424. err: &authority.PolicyError{
  2425. Typ: authority.InternalFailure,
  2426. Err: errors.New("error"),
  2427. },
  2428. want: false,
  2429. },
  2430. {
  2431. name: "bad-request",
  2432. err: &authority.PolicyError{
  2433. Typ: authority.AdminLockOut,
  2434. Err: errors.New("admin lock out"),
  2435. },
  2436. want: true,
  2437. },
  2438. }
  2439. for _, tt := range tests {
  2440. t.Run(tt.name, func(t *testing.T) {
  2441. if got := isBadRequest(tt.err); got != tt.want {
  2442. t.Errorf("isBadRequest() = %v, want %v", got, tt.want)
  2443. }
  2444. })
  2445. }
  2446. }
  2447. func Test_validatePolicy(t *testing.T) {
  2448. type args struct {
  2449. p *linkedca.Policy
  2450. }
  2451. tests := []struct {
  2452. name string
  2453. args args
  2454. wantErr bool
  2455. }{
  2456. {
  2457. name: "nil",
  2458. args: args{
  2459. p: nil,
  2460. },
  2461. wantErr: false,
  2462. },
  2463. {
  2464. name: "x509",
  2465. args: args{
  2466. p: &linkedca.Policy{
  2467. X509: &linkedca.X509Policy{
  2468. Allow: &linkedca.X509Names{
  2469. Dns: []string{"**.local"},
  2470. },
  2471. },
  2472. },
  2473. },
  2474. wantErr: true,
  2475. },
  2476. {
  2477. name: "ssh user",
  2478. args: args{
  2479. p: &linkedca.Policy{
  2480. Ssh: &linkedca.SSHPolicy{
  2481. User: &linkedca.SSHUserPolicy{
  2482. Allow: &linkedca.SSHUserNames{
  2483. Emails: []string{"@@example.com"},
  2484. },
  2485. },
  2486. },
  2487. },
  2488. },
  2489. wantErr: true,
  2490. },
  2491. {
  2492. name: "ssh host",
  2493. args: args{
  2494. p: &linkedca.Policy{
  2495. Ssh: &linkedca.SSHPolicy{
  2496. Host: &linkedca.SSHHostPolicy{
  2497. Allow: &linkedca.SSHHostNames{
  2498. Dns: []string{"**.local"},
  2499. },
  2500. },
  2501. },
  2502. },
  2503. },
  2504. wantErr: true,
  2505. },
  2506. {
  2507. name: "ok",
  2508. args: args{
  2509. p: &linkedca.Policy{
  2510. X509: &linkedca.X509Policy{
  2511. Allow: &linkedca.X509Names{
  2512. Dns: []string{"*.local"},
  2513. },
  2514. },
  2515. Ssh: &linkedca.SSHPolicy{
  2516. User: &linkedca.SSHUserPolicy{
  2517. Allow: &linkedca.SSHUserNames{
  2518. Emails: []string{"@example.com"},
  2519. },
  2520. },
  2521. Host: &linkedca.SSHHostPolicy{
  2522. Allow: &linkedca.SSHHostNames{
  2523. Dns: []string{"*.local"},
  2524. },
  2525. },
  2526. },
  2527. },
  2528. },
  2529. wantErr: false,
  2530. },
  2531. }
  2532. for _, tt := range tests {
  2533. t.Run(tt.name, func(t *testing.T) {
  2534. if err := validatePolicy(tt.args.p); (err != nil) != tt.wantErr {
  2535. t.Errorf("validatePolicy() error = %v, wantErr %v", err, tt.wantErr)
  2536. }
  2537. })
  2538. }
  2539. }