policy_test.go 79 KB

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