PageRenderTime 26ms CodeModel.GetById 2ms RepoModel.GetById 0ms app.codeStats 0ms

/GeneralPDP/Scenario/DkalToXacmlEndPoint.fs

#
F# | 115 lines | 88 code | 14 blank | 13 comment | 10 complexity | 213557a1aec1a8eb2e98129b3e652fa4 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, LGPL-3.0, BSD-3-Clause
  1. namespace Microsoft.Research.GeneralPDP.Scenario
  2. open Microsoft.Research.GeneralPDP.Translations.ToXACML.DkalPolicyTranslator
  3. open Microsoft.Research.GeneralPDP.Translations.ToXACML.DkalRequestTranslator
  4. open Microsoft.Research.GeneralPDP.Translations.ToXACML.DkalResponseTranslator
  5. open Microsoft.Research.GeneralPDP.Translations.ToXACML.DkalTermTranslator
  6. open Microsoft.Research.GeneralPDP.Translations.ToDKAL.XacmlResponseTranslator
  7. open Microsoft.Research.GeneralPDP.DKAL.Engine.ParsingCtxFactory
  8. open Microsoft.Research.GeneralPDP.DKAL.Engine.Basics
  9. open Microsoft.Research.GeneralPDP.XACML.Ast
  10. open Microsoft.Research.DkalEngine
  11. open Microsoft.Research.DkalEngine.Ast
  12. open Basics
  13. open Message
  14. open EndPoint
  15. open EndPointImageFactory
  16. open Microsoft.Msagl.Drawing
  17. open System.Drawing
  18. open System.Collections.Generic
  19. module DkalToXacmlEndPoint =
  20. type DkalToXacmlEndPoint(id: EndPointId, xacmlId: EndPointId, dkalId: EndPointId,
  21. ?pctx: ParsingCtx, ?dkalPolicy: DkalPolicy) =
  22. inherit EndPoint(id)
  23. let dkalRequestTranslator = DkalRequestTranslator()
  24. let dkalResponseTranslator = DkalResponseTranslator()
  25. let pctx = match pctx with
  26. | None -> let pctx, _ = xacmlAwareParsingCtx(id)
  27. pctx
  28. | Some pctx -> pctx
  29. let ppalMe = pctx.LookupOrAddPrincipal(id)
  30. let xacmlResponseTranslator = XacmlResponseTranslator(pctx)
  31. let pendingResponses = new Queue<Infon * EndPointId * int>()
  32. override ep.Process (m: IMessage) =
  33. match m.Content with
  34. | XacmlResponseContent(resp) ->
  35. if pendingResponses.Count > 0 then
  36. let (reqInfon, reqSender, reqId) = pendingResponses.Dequeue()
  37. // forward definite answer
  38. if resp.Decision = Permit || resp.Decision = Deny then
  39. let dkalResponse = xacmlResponseTranslator.TranslateResponse reqId reqSender resp
  40. ep.Send({sender= id;
  41. receiver= reqSender;
  42. content= InfonContent(App(pctx.LookupFunction("said"), [Const(Principal(ppalMe)); dkalResponse]))})
  43. else
  44. // ask dkal if no definite answer from XACML
  45. // let signature = App(pctx.LookupFunction("Ev.signedBy"),
  46. // [Const(Principal(pctx.LookupOrAddPrincipal(id))); reqInfon; Const(Int(42))])
  47. // let justified = App(pctx.LookupFunction("justified"), [reqInfon; signature])
  48. ep.Send({sender= id;
  49. receiver= dkalId;
  50. content= InfonContent(reqInfon)})
  51. else
  52. ep.Fail "I received an XACML response but no one had asked a request"
  53. | InfonContent(infon) ->
  54. if m.Sender = dkalId then
  55. // infon comes from my dkal engine, it must be a response
  56. try
  57. let reqPpal, resp = dkalResponseTranslator.TranslateResponse(infon)
  58. let strippedResponseInfon = (DkalTermTranslator()).StripSignatures(infon)
  59. let impostedInfon = match strippedResponseInfon with
  60. | App(f, [_; d]) when f.name = "said" -> App(f, [Const(Principal(ppalMe)); d])
  61. | _ -> failwith "expecting 'said' in response infon"
  62. ep.Send({sender= id;
  63. receiver= reqPpal;
  64. content= InfonContent(impostedInfon)})
  65. with
  66. | DkalResponseTranslatorException(e) -> printfn "%O" e
  67. else
  68. // infon comes from somewhere else, it must be a request (or something else)
  69. try
  70. let reqId, pep, req, unusedInfons = dkalRequestTranslator.TranslateRequest(infon)
  71. pendingResponses.Enqueue((infon, m.Sender, reqId))
  72. ep.Send({sender= id;
  73. receiver= xacmlId;
  74. content= XacmlRequestContent(req)})
  75. // inform of unused infons
  76. for inf in unusedInfons do
  77. printfn "Unused infon when translating request: %O" (inf.ToSX())
  78. with
  79. | DkalRequestTranslatorException(e) -> printfn "%O" e
  80. | _ -> "I don't understand content " + m.Content.ToString() |> ep.Fail
  81. override ep.CleanUp() = ()
  82. override ep.StartUp() =
  83. match dkalPolicy with
  84. | None -> ()
  85. | Some pcy ->
  86. let tr = DkalPolicyTranslator()
  87. let xacmlPolicy, unusedAssertions = tr.TranslateCommRules(pcy)
  88. // send policy to xacml
  89. ep.Send({sender= id;
  90. receiver= xacmlId;
  91. content= XacmlPolicyContent(xacmlPolicy)})
  92. if not unusedAssertions.IsEmpty then
  93. printfn "there are unused assertions after translation"
  94. // send unused assertions (if any) to dkal
  95. (*ep.Send({sender= id;
  96. receiver= dkalId;
  97. content= DkalAssertionsContent(unusedAssertions)})*)
  98. override ep.Image = Some (image (ep :> IEndPoint).Color EmptyDrawing)
  99. override ep.ApplyStyle (n: Node) =
  100. n.LabelText <- ep.Description
  101. override ep.Description = ep.Id + ": DKAL->XACML"