package krb

  1. Overview
  2. Docs
A library for using Kerberos for both Rpc and Tcp communication

Install

Dune Dependency

Authors

Maintainers

Sources

krb-v0.16.0.tar.gz
sha256=353675621e4c5a888f2483dc1bb7281bd17ce4ed7dfd2f40142257f98db7c77d

doc/src/krb.public/kerberized_rpc_over_protocol.ml.html

Source file kerberized_rpc_over_protocol.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
open! Core
open! Async
open! Import

let collect_errors writer_monitor ~f =
  choose
    [ choice (Monitor.detach_and_get_next_error writer_monitor) (fun e -> Error e)
    ; choice
        (try_with
           ~run:`Schedule
           ~name:"Rpc.Connection.collect_errors"
           f)
        Fn.id
    ]
;;

let create_connection_collect_errors_and_close_exn
      ~implementations
      ~connection_state
      ~description
      ?handshake_timeout
      ?heartbeat_config
      (transport : Rpc.Transport.t)
  =
  let writer_monitor = Rpc.Transport.Writer.monitor transport.writer in
  collect_errors writer_monitor ~f:(fun () ->
    Async_rpc_kernel.Rpc.Connection.create
      ~implementations
      ~connection_state
      ~description
      ?handshake_timeout:
        (Option.map handshake_timeout ~f:Time_ns.Span.of_span_float_round_nearest)
      ?heartbeat_config
      transport
    >>= function
    | Error exn -> raise exn
    | Ok t -> Rpc.Connection.close_finished t)
  >>= fun result ->
  Rpc.Transport.close transport
  >>= fun () ->
  match result with
  | Error exn -> raise exn
  | Ok () -> return ()
;;

let handle_client
      (type conn)
      (module Connection : Protocol.Connection with type t = conn)
      ?heartbeat_config
      ?handshake_timeout
      initial_connection_state
      implementations
  =
  Staged.stage (fun client_address (transport : Rpc.Transport.t) conn ->
    let request_forwarded_creds rpc_connection () =
      Rpc.Rpc.dispatch Request_forwarded_creds_rpc.rpc rpc_connection ()
      >>| Or_error.join
      >>=? fun krb_cred -> Connection.read_krb_cred conn krb_cred
    in
    let client_principal = Connection.peer_principal conn in
    let description =
      let server_principal = Connection.my_principal conn in
      Info.create_s
        [%message
          "Kerberized RPC server"
            (client_address : Socket.Address.Inet.t)
            (client_principal : Principal.Name.t)
            (server_principal : Principal.Name.t)]
    in
    create_connection_collect_errors_and_close_exn
      ~implementations
      ~connection_state:(fun rpc_conn ->
        let client_identity =
          { Client_identity.client_principal
          ; cross_realm_client_principal = Connection.Cross_realm.peer_principal conn
          ; request_forwarded_creds = request_forwarded_creds rpc_conn
          }
        in
        initial_connection_state client_identity client_address rpc_conn)
      ?handshake_timeout
      ~description
      ?heartbeat_config
      transport)
;;

let handle_client_with_anon
      (type conn)
      (module Connection : Protocol_intf.Connection with type t = conn)
      ?heartbeat_config
      ?handshake_timeout
      initial_connection_state
      implementations
  =
  let handle_krb_client =
    let initial_connection_state client_identity =
      initial_connection_state (Some client_identity)
    in
    handle_client
      (module Connection)
      ?heartbeat_config
      ?handshake_timeout
      initial_connection_state
      implementations
    |> Staged.unstage
  in
  let handle_rpc_client client_address =
    let connection_state conn = initial_connection_state None client_address conn in
    let description =
      Info.create_s
        [%message
          "Anon connection in Krb.Rpc.Connection.serve_with_anon"
            (client_address : Socket.Address.Inet.t)]
    in
    create_connection_collect_errors_and_close_exn
      ~implementations
      ~connection_state
      ~description
      ?handshake_timeout
      ?heartbeat_config
  in
  Staged.stage (fun client_address transport conn ->
    match conn with
    | Some conn -> handle_krb_client client_address transport conn
    | None -> handle_rpc_client client_address transport)
;;

let client
      (type conn)
      (module Connection : Protocol_intf.Connection with type t = conn)
      ?heartbeat_config
      ?implementations
      ?description
      ?(on_credential_forwarding_request = Fn.const On_credential_forwarding_request.Deny)
      ~finish_handshake_by
      where_to_connect
      transport
      conn
  =
  let server_principal = Connection.peer_principal conn in
  let client_principal = Connection.my_principal conn in
  let description =
    match description with
    | Some i -> i
    | None ->
      let connected_to =
        (* We must guard this with [am_running_test] because the sexp_of function
           for [Tcp.Where_to_connect.inet] does not mask the port if it was
           constructed using [of_host_and_port] :/. *)
        match am_running_test with
        | true -> [%sexp "<omitted-in-test>"]
        | false -> [%sexp (where_to_connect : Tcp.Where_to_connect.inet)]
      in
      Info.create_s
        [%message
          "Kerberized RPC client"
            (connected_to : Sexp.t)
            (client_principal : Principal.Name.t)
            (server_principal : Principal.Name.t)]
  in
  let handshake_timeout = Time.diff finish_handshake_by (Time.now ()) in
  let implementations =
    Option.map implementations ~f:(fun f -> f { Server_principal.server_principal })
  in
  let forwarded_creds_implementation () =
    Option.some_if
      (Connection.can_forward_creds conn)
      (Rpc.Rpc.implement Request_forwarded_creds_rpc.rpc (fun _ () ->
         match on_credential_forwarding_request { Server_principal.server_principal } with
         | Allow_server_to_impersonate_me { forwardable_tkt } ->
           Connection.make_krb_cred conn ~forwardable:forwardable_tkt
         | Deny ->
           Deferred.Or_error.error_s
             [%message
               "Client denied request to forward credentials"
                 (client_principal : Principal.Name.t)
                 (server_principal : Principal.Name.t)]))
  in
  match (implementations : _ Rpc.Connection.Client_implementations.t option) with
  | None ->
    (match
       Rpc.Implementations.create
         ~implementations:(forwarded_creds_implementation () |> Option.to_list)
         ~on_unknown_rpc:`Continue
     with
     | Ok implementations ->
       Async_rpc_kernel.Rpc.Connection.create
         ~implementations
         ~connection_state:(const ())
         ~handshake_timeout:(Time_ns.Span.of_span_float_round_nearest handshake_timeout)
         ?heartbeat_config
         ~description
         transport
       >>| Or_error.of_exn_result
     | Error duplicate_impls ->
       Deferred.Or_error.error_s
         [%message
           "Unable to create client implementations"
             (duplicate_impls : [ `Duplicate_implementations of Rpc.Description.t list ])])
  | Some { implementations; connection_state } ->
    (match forwarded_creds_implementation () with
     | None -> Deferred.Or_error.return implementations
     | Some implementation ->
       Rpc.Implementations.add implementations implementation |> return)
    >>=? fun implementations ->
    Async_rpc_kernel.Rpc.Connection.create
      ~implementations
      ~connection_state
      ~handshake_timeout:(Time_ns.Span.of_span_float_round_nearest handshake_timeout)
      ?heartbeat_config
      ~description
      transport
    >>| Or_error.of_exn_result
;;

module For_testing = struct
  let test_async f =
    Thread_safe.block_on_async_exn (fun () ->
      Clock.with_timeout (sec 1.) (f ())
      >>| function
      | `Result x -> x
      | `Timeout -> failwith "Timeout.")
  ;;

  module Test_rpc = struct
    type query = { greeting : string } [@@deriving sexp, bin_io, compare]

    type response = Thank_you_very_much of Principal.Stable.Name.V1.t
    [@@deriving sexp, bin_io, compare]

    let test_query = { greeting = "hello, earthling" }
    let rpc = Rpc.Rpc.create ~name:"test-rpc" ~version:0 ~bin_query ~bin_response
  end

  let ensure_test_mode_works ~serve ~with_client =
    test_async (fun () ->
      let implementations =
        match
          Rpc.Implementations.create
            ~on_unknown_rpc:`Raise
            ~implementations:
              [ Rpc.Rpc.implement Test_rpc.rpc (fun principal query ->
                  [%test_result: Test_rpc.query] query ~expect:Test_rpc.test_query;
                  return (Test_rpc.Thank_you_very_much principal))
              ]
        with
        | Ok t -> t
        | Error (`Duplicate_implementations _) ->
          failwith "impossible: only 1 implementation"
      in
      let service = Principal.Name.Service { service = "bogus"; hostname = "test" } in
      let client = Principal.Name.User "me" in
      let host = "127.0.0.1" in
      serve
        ~implementations
        ~initial_connection_state:(fun { Client_identity.client_principal; _ } _ _ ->
          client_principal)
        ~where_to_listen:Tcp.Where_to_listen.of_port_chosen_by_os
        ~krb_mode:(Mode.Test_with_principal service)
        ~authorize:
          (Authorize.create (fun (`Inet (client_host, _client_port)) client_principal ->
             if [%compare.equal: Principal.Name.t] client client_principal
             && [%compare.equal: Unix.Inet_addr.t]
                  client_host
                  (Unix.Inet_addr.of_string host)
             then `Accept
             else `Reject))
      >>= fun server ->
      let test_with_client ~krb_mode ~expect =
        let port = Tcp.Server.listening_on (Or_error.ok_exn server) in
        with_client
          ~authorize:
            (Authorize.create (fun server_address server_principal ->
               if [%compare.equal: Principal.Name.t] service server_principal
               && [%compare.equal: Socket.Address.Inet.t]
                    server_address
                    (Socket.Address.Inet.create (Unix.Inet_addr.of_string host) ~port)
               then `Accept
               else `Reject))
          ~krb_mode
          (Tcp.Where_to_connect.of_host_and_port { host; port })
          (fun connection ->
             Rpc.Rpc.dispatch_exn Test_rpc.rpc connection Test_rpc.test_query
             >>| function
             | Thank_you_very_much principal ->
               [%test_result: Principal.Name.t] principal ~expect)
        >>| ok_exn
      in
      test_with_client ~krb_mode:(Mode.Test_with_principal client) ~expect:client)
  ;;
end
OCaml

Innovation. Community. Security.