88 "strconv"
99
1010 "github.com/pinterest/knox"
11+ "github.com/pinterest/knox/log"
1112 "github.com/pinterest/knox/server/auth"
1213)
1314
@@ -211,9 +212,15 @@ func getKeyHandler(m KeyManager, principal knox.Principal, parameters map[string
211212 }
212213
213214 // Authorize access to data
214- if ! principal .CanAccess (key .ACL , knox .Read ) {
215+ authorized , authzErr := authorizeRequest (key , principal , knox .Read )
216+ if authzErr != nil {
217+ return nil , errF (knox .InternalServerErrorCode , authzErr .Error ())
218+ }
219+
220+ if ! authorized {
215221 return nil , errF (knox .UnauthorizedCode , fmt .Sprintf ("Principal %s not authorized to read %s" , principal .GetID (), keyID ))
216222 }
223+
217224 // Zero ACL for key response, in order to avoid caching unnecessarily
218225 key .ACL = knox.ACL {}
219226 return key , nil
@@ -234,7 +241,12 @@ func deleteKeyHandler(m KeyManager, principal knox.Principal, parameters map[str
234241 }
235242
236243 // Authorize
237- if ! principal .CanAccess (key .ACL , knox .Admin ) {
244+ authorized , authzErr := authorizeRequest (key , principal , knox .Admin )
245+ if authzErr != nil {
246+ return nil , errF (knox .InternalServerErrorCode , authzErr .Error ())
247+ }
248+
249+ if ! authorized {
238250 return nil , errF (knox .UnauthorizedCode , fmt .Sprintf ("Principal %s not authorized to delete %s" , principal .GetID (), keyID ))
239251 }
240252
@@ -314,7 +326,12 @@ func putAccessHandler(m KeyManager, principal knox.Principal, parameters map[str
314326 }
315327
316328 // Authorize
317- if ! principal .CanAccess (key .ACL , knox .Admin ) {
329+ authorized , authzErr := authorizeRequest (key , principal , knox .Admin )
330+ if authzErr != nil {
331+ return nil , errF (knox .InternalServerErrorCode , authzErr .Error ())
332+ }
333+
334+ if ! authorized {
318335 return nil , errF (knox .UnauthorizedCode , fmt .Sprintf ("Principal %s not authorized to update access for %s" , principal .GetID (), keyID ))
319336 }
320337
@@ -371,7 +388,12 @@ func postVersionHandler(m KeyManager, principal knox.Principal, parameters map[s
371388 }
372389
373390 // Authorize
374- if ! principal .CanAccess (key .ACL , knox .Write ) {
391+ authorized , authzErr := authorizeRequest (key , principal , knox .Write )
392+ if authzErr != nil {
393+ return nil , errF (knox .InternalServerErrorCode , authzErr .Error ())
394+ }
395+
396+ if ! authorized {
375397 return nil , errF (knox .UnauthorizedCode , fmt .Sprintf ("Principal %s not authorized to write %s" , principal .GetID (), keyID ))
376398 }
377399
@@ -428,7 +450,12 @@ func putVersionsHandler(m KeyManager, principal knox.Principal, parameters map[s
428450 }
429451
430452 // Authorize
431- if ! principal .CanAccess (key .ACL , knox .Write ) {
453+ authorized , authzErr := authorizeRequest (key , principal , knox .Write )
454+ if authzErr != nil {
455+ return nil , errF (knox .InternalServerErrorCode , authzErr .Error ())
456+ }
457+
458+ if ! authorized {
432459 return nil , errF (knox .UnauthorizedCode , fmt .Sprintf ("Principal %s not authorized to write %s" , principal .GetID (), keyID ))
433460 }
434461
@@ -445,3 +472,25 @@ func putVersionsHandler(m KeyManager, principal knox.Principal, parameters map[s
445472 return nil , errF (knox .InternalServerErrorCode , err .Error ())
446473 }
447474}
475+
476+ func authorizeRequest (key * knox.Key , principal knox.Principal , access knox.AccessType ) (allow bool , err error ) {
477+ defer func () {
478+ if r := recover (); r != nil {
479+ log .Printf ("Recovered from panic in access callback: %v" , r )
480+
481+ err = fmt .Errorf ("Recovered from panic in access callback: %v" , r )
482+ }
483+ }()
484+
485+ allow = principal .CanAccess (key .ACL , access )
486+
487+ if ! allow && accessCallback != nil {
488+ allow , err = accessCallback (knox.AccessCallbackInput {
489+ Key : * key ,
490+ Principals : principal .Raw (),
491+ AccessType : access ,
492+ })
493+ }
494+
495+ return
496+ }
0 commit comments