Source file sts.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
open! Values
open! Core
open Ppx_yojson_conv_lib.Yojson_conv.Primitives
open! Async

module Statement = struct
  type effect_ =
    | Accept
    | Deny
  [@@deriving yojson]

  type action = string list [@@deriving yojson]
  type resource = string list [@@deriving yojson]

  let effect_to_string = function
    | Accept -> "Allow"
    | Deny -> "Deny"
  ;;

  let default_effect = Accept
  let default_sid = "solvuu"

  (* TODO what is this ? *)
  type t =
    { sid : string
    ; effect_ : effect_
    ; action : action
    ; resource : resource
    }
  [@@deriving yojson]

  let create ?(effect_ = default_effect) ?(sid = default_sid) ~action ~resource () =
    { sid; effect_; action; resource }
  ;;

  (* FIXME: Below comment says we're avoiding a dependency on yojson. However, the mli
     refers to Yojson so we're not.

     Manually compile json string to temporarily avoid yojson dependency *)
  let to_json t =
    `Assoc
      [ "Sid", `String t.sid
      ; "Effect", `String (effect_to_string t.effect_)
      ; "Action", `List (List.map ~f:(fun s -> `String s) t.action)
      ; "Resource", `List (List.map ~f:(fun s -> `String s) t.resource)
      ]
  ;;

  let to_json_all l = `List (List.map ~f:to_json l)
end

module Policy = struct
  type t =
    { version : string
    ; statement : Statement.t list
    }
  [@@deriving yojson]

  let to_json t =
    `Assoc
      [ "Version", `String t.version; "Statement", Statement.to_json_all t.statement ]
  ;;

  let create ?(version = "2012-10-17") statement =
    let t = { version; statement } in
    Log.Global.debug "policy as json: %s" (to_json t |> Yojson.Safe.to_string);
    t
  ;;
end

let assume_role
      ?(policy : Policy.t option)
      ?retry_delay
      ?retry_cnt
      ?duration_sec
      ~session_name
      ~role
      cfg
  =
  Awso_async.Import.with_retries ?retry_delay ?retry_cnt (fun () ->
    match%bind
      Io.assume_role
        ~cfg
        (AssumeRoleRequest.make
           ~roleArn:(ArnType.make role)
           ~roleSessionName:(RoleSessionNameType.make session_name)
           ?durationSeconds:
             (Option.map duration_sec ~f:RoleDurationSecondsType.make)
           ?policy:
             (Option.map
                ~f:(fun x ->
                  Policy.to_json x
                  |> Yojson.Safe.to_string
                  |> SessionPolicyDocumentType.make)
                policy)
           ())
    with
    | Ok v -> return v
    | Error _ -> failwithf "sts.assume_role" ())
;;

let assume_role_with_saml
      ?(policy : Policy.t option)
      ?retry_delay
      ?retry_cnt
      ?duration_sec
      ~principal_arn
      ~saml_assertion
      ~role
      cfg
  =
  Awso_async.Import.with_retries ?retry_delay ?retry_cnt (fun () ->
    match%bind
      Io.assume_role_with_s_a_m_l
        ~cfg
        (AssumeRoleWithSAMLRequest.make
           ~principalArn:principal_arn
           ~sAMLAssertion:saml_assertion
           ~roleArn:(ArnType.make role)
           ?durationSeconds:
             (Option.map duration_sec ~f:RoleDurationSecondsType.make)
           ?policy:
             (Option.map
                ~f:(fun x ->
                  Policy.to_json x
                  |> Yojson.Safe.to_string
                  |> SessionPolicyDocumentType.make)
                policy)
           ())
    with
    | Ok v -> return v
    | Error _ -> failwithf "sts.assume_role" ())
;;