Source file arn.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
open! Import
open Ppx_yojson_conv_lib.Yojson_conv.Primitives

module Error = struct
  type make =
    [ `Invalid_qualifier of string
    | `Invalid_account_id of string
    ]
  [@@deriving yojson]
end

module Default = struct
  let partition = "aws"
end

type resource_type =
  [ `Slash_delimited of string
  | `Colon_delimited of string
  | `None
  ]
[@@deriving yojson]

type qualifier =
  [ `Slash_delimited of string
  | `Colon_delimited of string
  | `None
  ]
[@@deriving yojson]

type t =
  { partition : string
  ; service : Service.t
  ; region : Region.t option
  ; account_id : string option
  ; resource : string
  ; resource_type : resource_type
  ; qualifier : qualifier
  }
[@@deriving yojson]

let make
      ?(partition = Default.partition)
      ?region
      ?account_id
      ?(resource_type = `None)
      ?(qualifier = `None)
      ~service
      ~resource
      ()
  =
  let make_qualifier x =
    match x with
    | `None -> `Ok `None
    | `Slash_delimited s | `Colon_delimited s ->
      if String.equal s "" then `Invalid_qualifier s else `Ok x
  in
  let make_account_id x =
    Option.value_map x ~default:(`Ok None) ~f:(fun x ->
      if String.equal x "" then `Invalid_account_id x else `Ok (Some x))
  in
  match make_qualifier qualifier with
  | `Invalid_qualifier _ as x -> x
  | `Ok qualifier -> (
    match make_account_id account_id with
    | `Invalid_account_id _ as x -> x
    | `Ok account_id ->
      `Ok { partition; service; region; account_id; resource; resource_type; qualifier })
;;

let make_exn
      ?partition
      ?region
      ?account_id
      ?resource_type
      ?qualifier
      ~service
      ~resource
      ()
  =
  match
    make ?partition ?region ?account_id ?resource_type ?qualifier ~service ~resource ()
  with
  | `Ok x -> x
  | #Error.make as e ->
    failwith
      ("Invalid ARN " ^ Yojson.Safe.to_string (Error.yojson_of_make (e :> Error.make)))
;;

let of_string s =
  let build partition service region account_id resource_type resource qualifier =
    let service = Service.of_string service in
    let region =
      match region with
      | "" -> None
      | _ -> Some (Region.of_string region)
    in
    let account_id =
      match account_id with
      | "" -> None
      | _ -> Some account_id
    in
    make_exn
      ~partition
      ~service
      ?region
      ?account_id
      ~resource
      ~resource_type
      ~qualifier
      ()
  in
  let split = String.split ~on:':' s in
  match split with
  (* arn:partition:service:region:account-id:resourcetype:resource:qualifier *)
  | [ "arn"; partition; service; region; account_id; resource_type; resource; qualifier ]
    ->
    build
      partition
      service
      region
      account_id
      (`Colon_delimited resource_type)
      resource
      (`Colon_delimited qualifier)
  | [ "arn"; partition; service; region; account_id; resource_part; last_part ] ->
    if String.contains resource_part '/'
    then (
      (* arn:partition:service:region:account-id:resourcetype/resource:qualifier *)
      let qualifier = last_part in
      let resource_type, resource = String.lsplit2_exn ~on:'/' resource_part in
      build
        partition
        service
        region
        account_id
        (`Slash_delimited resource_type)
        resource
        (`Colon_delimited qualifier))
    else (
      (* arn:partition:service:region:account-id:resourcetype:resource *)
      let resource = last_part in
      build
        partition
        service
        region
        account_id
        (`Colon_delimited resource_part)
        resource
        `None)
  (* arn:partition:service:region:account-id:resource
     arn:partition:service:region:account-id:resourcetype/resource
     arn:partition:service:region:account-id:resourcetype/resource/qualifier *)
  | [ "arn"
    ; partition
    ; service
    ; region
    ; account_id
    ; resource_type_slash_resource_slash_qualifier
    ] -> (
    match String.split ~on:'/' resource_type_slash_resource_slash_qualifier with
    | [ resource_type; resource; qualifier ] ->
      build
        partition
        service
        region
        account_id
        (`Slash_delimited resource_type)
        resource
        (`Slash_delimited qualifier)
    | [ resource_type; resource ] ->
      build
        partition
        service
        region
        account_id
        (`Slash_delimited resource_type)
        resource
        `None
    | [ resource ] -> build partition service region account_id `None resource `None
    | _ -> failwithf "'%s' has an invalid number of '/' delimiters." s ())
  | "arn" :: _ -> failwithf "'%s' has an invalid number of ':' delimiters." s ()
  | _ -> failwithf "'%s' is not an amazon resource name." s ()
;;

let%expect_test "of_string" =
  (* Strings expected to be valid ARNs. *)
  let good =
    [ (* Format arn:partition:service:region:account-id:resource *)
      [ "arn:partition:sqs:us-east-2:account-id:resource" ]
    ; (* Format arn:partition:service:region:account-id:resourcetype/resource *)
      [ "arn:partition:sqs:us-east-2:account-id:resourcetype/resource" ]
    ; (* Format
         arn:partition:service:region:account-id:resourcetype/resource/qualifier *)
      [ "arn:partition:sqs:us-east-2:account-id:resourcetype/resource/qualifier" ]
    ; (* Format
         arn:partition:service:region:account-id:resourcetype/resource:qualifier *)
      [ "arn:partition:sqs:us-east-2:account-id:resourcetype/resource:qualifier" ]
    ; (* Format arn:partition:service:region:account-id:resourcetype:resource *)
      [ (* Full *)
        "arn:partition:sqs:us-east-2:account-id:resourcetype:resource"
      ; (* Empty region *)
        "arn:partition:sqs::account-id:resourcetype:resource"
      ; (* Empty account *)
        "arn:partition:sqs:us-east-2::resourcetype:resource"
      ]
    ; (* Format
         arn:partition:service:region:account-id:resourcetype:resource:qualifier *)
      [ "arn:partition:sqs:us-east-2:account-id:resourcetype:resource:qualifier" ]
    ]
  in
  (* Strings expected to be invalid ARNs. *)
  let bad =
    [ (* Empty string. *)
      ""
    ; (* Invalid format. *)
      "x"
    ; (* Colon_delimited resource type with Slash_delimited qualifier. *)
      "arn:partition:sqs:us-east-2:account-id:resourcetype:resource/qualifier"
    ; (* Colon_delimited qualifier without resource type. *)
      "arn:partition:sqs:us-east-2:account-id::resource:qualifier"
    ; (* Slash_delimited qualifier without resource type. *)
      "arn:partition:sqs:us-east-2:account-id::resource/qualifier"
    ]
  in
  let test_bad input =
    match of_string input with
    | x ->
      Test.fail
        "Input unexpected to parse as ARN but did."
        [ "input", input; "arn", Yojson.Safe.to_string (yojson_of_t x) ]
    | exception e ->
      Test.pass
        "Correctly failed to parse input as an ARN."
        [ "input", input; "exception", Exn.to_string e ]
  in
  let test_good input =
    match of_string input with
    | x ->
      Test.pass
        "Input parsed as ARN."
        [ "input", input; "arn", Yojson.Safe.to_string (yojson_of_t x) ]
    | exception e ->
      Test.fail
        "Input failed to parse as ARN."
        [ "input", input; "exception", Exn.to_string e ]
  in
  List.iter (List.concat good) ~f:(fun x ->
    test_good x;
    printf "\n");
  List.iter bad ~f:(fun x ->
    test_bad x;
    printf "\n");
  [%expect
    {|
    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2:account-id:resource
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["None"],"qualifier":["None"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2:account-id:resourcetype/resource
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["Slash_delimited","resourcetype"],"qualifier":["None"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2:account-id:resourcetype/resource/qualifier
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["Slash_delimited","resourcetype"],"qualifier":["Slash_delimited","qualifier"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2:account-id:resourcetype/resource:qualifier
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["Slash_delimited","resourcetype"],"qualifier":["Colon_delimited","qualifier"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2:account-id:resourcetype:resource
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["Colon_delimited","resourcetype"],"qualifier":["None"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs::account-id:resourcetype:resource
    arn: {"partition":"partition","service":"sqs","region":null,"account_id":"account-id","resource":"resource","resource_type":["Colon_delimited","resourcetype"],"qualifier":["None"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2::resourcetype:resource
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":null,"resource":"resource","resource_type":["Colon_delimited","resourcetype"],"qualifier":["None"]}

    [OK] Input parsed as ARN.
    input: arn:partition:sqs:us-east-2:account-id:resourcetype:resource:qualifier
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["Colon_delimited","resourcetype"],"qualifier":["Colon_delimited","qualifier"]}

    [OK] Correctly failed to parse input as an ARN.
    input:
    exception: (Failure "'' is not an amazon resource name.")

    [OK] Correctly failed to parse input as an ARN.
    input: x
    exception: (Failure "'x' is not an amazon resource name.")

    [FIXME] Input unexpected to parse as ARN but did.
    input: arn:partition:sqs:us-east-2:account-id:resourcetype:resource/qualifier
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource/qualifier","resource_type":["Colon_delimited","resourcetype"],"qualifier":["None"]}

    [FIXME] Input unexpected to parse as ARN but did.
    input: arn:partition:sqs:us-east-2:account-id::resource:qualifier
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource","resource_type":["Colon_delimited",""],"qualifier":["Colon_delimited","qualifier"]}

    [FIXME] Input unexpected to parse as ARN but did.
    input: arn:partition:sqs:us-east-2:account-id::resource/qualifier
    arn: {"partition":"partition","service":"sqs","region":"us-east-2","account_id":"account-id","resource":"resource/qualifier","resource_type":["Colon_delimited",""],"qualifier":["None"]}
    |}]
;;

let to_string
      { partition; service; region; account_id; resource; resource_type; qualifier }
  =
  let account_id = Option.value ~default:"" account_id in
  let region = Option.value_map ~default:"" ~f:Region.to_string region in
  match resource_type, qualifier with
  (* arn:partition:service:region:account-id:resource *)
  | `None, `None ->
    sprintf
      "arn:%s:%s:%s:%s:%s"
      partition
      (Service.to_string service)
      region
      account_id
      resource
  (* arn:partition:service:region:account-id:resourcetype/resource *)
  | `Slash_delimited resource_type, `None ->
    sprintf
      "arn:%s:%s:%s:%s:%s/%s"
      partition
      (Service.to_string service)
      region
      account_id
      resource_type
      resource
  (* arn:partition:service:region:account-id:resourcetype/resource/qualifier *)
  | `Slash_delimited resource_type, `Slash_delimited qualifier ->
    sprintf
      "arn:%s:%s:%s:%s:%s/%s/%s"
      partition
      (Service.to_string service)
      region
      account_id
      resource_type
      resource
      qualifier
  (* arn:partition:service:region:account-id:resourcetype/resource:qualifier *)
  | `Slash_delimited resource_type, `Colon_delimited qualifier ->
    sprintf
      "arn:%s:%s:%s:%s:%s/%s:%s"
      partition
      (Service.to_string service)
      region
      account_id
      resource_type
      resource
      qualifier
  (* arn:partition:service:region:account-id:resourcetype:resource *)
  | `Colon_delimited resource_type, `None ->
    sprintf
      "arn:%s:%s:%s:%s:%s:%s"
      partition
      (Service.to_string service)
      region
      account_id
      resource_type
      resource
  (* arn:partition:service:region:account-id:resourcetype:resource:qualifier *)
  | `Colon_delimited resource_type, `Colon_delimited qualifier ->
    sprintf
      "arn:%s:%s:%s:%s:%s:%s:%s"
      partition
      (Service.to_string service)
      region
      account_id
      resource_type
      resource
      qualifier
  | `Colon_delimited _, `Slash_delimited _
  | `None, `Colon_delimited _
  | `None, `Slash_delimited _ -> failwith "cannot build such arn"
;;

let s3 ?(partition = Default.partition) ~bucket ~key () =
  (* FIXME: Doing minimal validation at present. See #98 for information on what
     we really need to do. *)
  if String.equal bucket ""
  then `Invalid_bucket bucket
  else if String.equal key ""
  then `Invalid_key key
  else
    `Ok
      { partition
      ; service = Service.s3
      ; region = None
      ; account_id = None
      ; resource = key
      ; resource_type = `Slash_delimited bucket
      ; qualifier = `None
      }
;;

let s3_exn ?partition ~bucket ~key () : t =
  match s3 ?partition ~bucket ~key () with
  | `Ok x -> x
  | `Invalid_bucket b -> failwithf "Invalid_bucket %s" b ()
  | `Invalid_key k -> failwithf "Invalid_key %s" k ()
;;

module Exn = struct
  let make = make_exn
  let s3 = s3_exn
end