forked from noobaa/noobaa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiam_utils.js
More file actions
1465 lines (1375 loc) · 53.9 KB
/
Copy pathiam_utils.js
File metadata and controls
1465 lines (1375 loc) · 53.9 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2024 NooBaa */
'use strict';
const _ = require('lodash');
const s3_utils = require('../s3/s3_utils');
const { IamError } = require('./iam_errors');
const { AWS_IAM_PATH_REGEXP, AWS_IAM_LIST_MARKER, AWS_IAM_ACCESS_KEY_INPUT_REGEXP, AWS_POLICY_NAME_REGEXP,
AWS_POLICY_DOCUMENT_REGEXP, AWS_POLICY_SID_REGEXP, AWS_ROLE_NAME_REGEXP, AWS_ROLE_DESCRIPTION_REGEXP,
AWS_OIDC_PROVIDER_ARN_REGEXP } = require('../../util/string_utils');
const iam_constants = require('./iam_constants');
const { RpcError } = require('../../rpc');
const validation_utils = require('../../util/validation_utils');
const dbg = require('../../util/debug_module')(__filename);
const access_policy_utils = require('../../util/access_policy_utils');
/**
* format_iam_xml_date return the date without milliseconds
* @param {any} input
*/
function format_iam_xml_date(input) {
const date_iso = s3_utils.format_s3_xml_date(input);
const date_iso_no_millis = date_iso.replace(/\.\d+/, ""); // remove the milliseconds part
return date_iso_no_millis;
}
/**
* create_arn_for_root creates the AWS ARN for root account user
* see: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-arns
* @param {string} account_id (the root user account id)
*/
function create_arn_for_root(account_id) {
return `arn:aws:iam::${account_id}:root`;
}
/**
* _create_arn_for_iam_entity creates the AWS ARN for IAM user/role
* @param {string} account_id
* @param {'user'|'role'} entity_type
* @param {string} entity_name
* @param {string} iam_path
*/
function _create_arn_for_iam_entity(account_id, entity_type, entity_name, iam_path) {
const basic_structure = `arn:aws:iam::${account_id}:${entity_type}`;
if (entity_name === undefined) return `${basic_structure}/`;
if (check_iam_path_was_set(iam_path)) {
return `${basic_structure}${iam_path}${entity_name}`;
}
return `${basic_structure}/${entity_name}`;
}
/**
* create_arn_for_user creates the AWS ARN for user
* see: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-arns
* @param {string} account_id (the root user account id)
* @param {string} username
* @param {string} iam_path
*/
function create_arn_for_user(account_id, username, iam_path) {
return _create_arn_for_iam_entity(account_id, 'user', username, iam_path);
}
/**
* create_arn_for_role creates the AWS ARN for role
* see: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_identifiers.html#identifiers-arns
* @param {string} account_id
* @param {string} role_name
* @param {string} iam_path
*/
function create_arn_for_role(account_id, role_name, iam_path) {
return _create_arn_for_iam_entity(account_id, 'role', role_name, iam_path);
}
/**
* parse_role_arn extracts account id and role name from a role ARN
* @param {string} role_arn
* @returns {{account_id?: string, role_name?: string, error?: string}}
*/
function parse_role_arn(role_arn) {
if (!role_arn) return { error: 'MISSING_ROLE_ARN' };
const account_id = role_arn.split(':')[4];
const role_name = role_arn.slice(role_arn.lastIndexOf('/') + 1);
if (!account_id || !role_name) return { error: 'INVALID_ROLE_ARN' };
return { account_id, role_name };
}
/**
* resolve_iam_role_by_arn resolves IAM role entity from a role ARN via iam_roles_cache
* @param {string} role_arn
* @param {nb.BucketSpace} bucketspace
* @returns {Promise<{iam_role?: object, account_id?: string, role_name?: string, error?: string}>}
*/
async function resolve_iam_role_by_arn(role_arn, bucketspace) {
const parsed = parse_role_arn(role_arn);
if (parsed.error) return { error: parsed.error };
const { account_id, role_name } = parsed;
if (!bucketspace) {
return { error: 'NO_SUCH_ENTITY', account_id, role_name };
}
try {
// Lazy-load to avoid pulling object_sdk (and namespace_cache) into server paths
// that only need ARN helpers from this module .
const { iam_roles_cache } = require('../../sdk/object_sdk');
const iam_role = await iam_roles_cache.get_with_cache({
bucketspace,
role_name,
owner_account_id: String(account_id),
});
if (!iam_role) return { error: 'NO_SUCH_ROLE', account_id, role_name };
return { iam_role, account_id, role_name };
} catch (err) {
if (err.rpc_code === 'NO_SUCH_ENTITY' || err.rpc_code === 'NO_SUCH_ROLE') {
return { error: 'NO_SUCH_ROLE', account_id, role_name };
}
throw err;
}
}
/**
* get_action_message_title returns the full action name
* @param {string} action (The action name as it is in AccountSpace)
*/
function get_action_message_title(action) {
return `${iam_constants.IAM_SERVICE_SMALL_LETTERS}:${iam_constants.ACTION_MESSAGE_TITLE_MAP[action]}`;
}
/**
* check_iam_path_was_set return true if the iam_path was set
* @param {string} iam_path
*/
function check_iam_path_was_set(iam_path) {
return iam_path && iam_path !== iam_constants.IAM_DEFAULT_PATH;
}
/**
* create_detailed_message_for_iam_user_access returns a detailed message with details needed for user who
* tried to perform S3 operation
* - resource_arn is only relevant for operations related to a bucket
* @param {object} user_account
* @param {string|string[]} method
* @param {string} resource_arn
* @param {string} [principal_arn]
*/
function create_detailed_message_for_iam_user_access(user_account, method, resource_arn, principal_arn) {
const owner_account_id = get_owner_account_id(user_account);
const arn_for_requesting_account = principal_arn || create_arn_for_user(owner_account_id,
user_account.name.unwrap(), user_account.iam_path);
const full_action_name = Array.isArray(method) && method.length > 1 ? method[1] : method; // special case for get_object_attributes
const message_start = `User: ${arn_for_requesting_account} is not authorized to perform: ${full_action_name} `;
const message_resource = `on resource: ${resource_arn} `;
const message_end = `because no identity-based policy allows the ${full_action_name} action`;
let message_with_details;
if (full_action_name === 's3:ListAllMyBuckets' || full_action_name === 's3vectors:ListVectorBuckets') {
message_with_details = message_start + message_end;
} else {
message_with_details = message_start + message_resource + message_end;
}
return message_with_details;
}
/**
* parse_max_items converts the input to the needed type
* assuming that we've got only sting type
* @param {any} input_max_items
*/
function parse_max_items(input_max_items) {
if (input_max_items === undefined) return;
const input_type = 'number';
const parameter_name = 'MaxItems';
const value_as_number = Number(input_max_items);
if (Number.isNaN(value_as_number)) {
const message_with_details = `1 validation error detected: Value ${input_max_items} at ` +
`'${parameter_name}' failed to satisfy constraint: Member must be ${input_type}`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return value_as_number;
}
/**
* validate_params will call the equivalent function (for example: user, access key, tagging, etc.)
* Note: The order of conditions matters - check 'tag' before 'user' to avoid misrouting
* @param {string} action
* @param {object} params
*/
function validate_params(action, params) {
if (action.includes('tag')) {
validate_tagging_params(action, params);
} else if (action.includes('policy') || action.includes('policies')) {
validate_policy_params(action, params);
} else if (action.includes('user')) {
validate_user_params(action, params);
} else if (action.includes('access_key')) {
validate_access_keys_params(action, params);
} else if (action.includes('role')) {
validate_role_params(action, params);
} else {
throw new RpcError('INTERNAL_ERROR', `${action} is not supported`);
}
}
/**
* validate_user_params will call the aquivalent function for each action in user API
* @param {string} action
* @param {object} params
*/
function validate_user_params(action, params) {
switch (action) {
case iam_constants.IAM_ACTIONS.CREATE_USER:
validate_create_user(params);
break;
case iam_constants.IAM_ACTIONS.GET_USER:
validate_get_user(params);
break;
case iam_constants.IAM_ACTIONS.UPDATE_USER:
validate_update_user(params);
break;
case iam_constants.IAM_ACTIONS.DELETE_USER:
validate_delete_user(params);
break;
case iam_constants.IAM_ACTIONS.LIST_USERS:
validate_list_users(params);
break;
default:
throw new RpcError('INTERNAL_ERROR', `${action} is not supported`);
}
}
/**
* validate_access_keys_params will call the aquivalent function for each action in access key API
* @param {string} action
* @param {object} params
*/
function validate_access_keys_params(action, params) {
switch (action) {
case iam_constants.IAM_ACTIONS.CREATE_ACCESS_KEY:
validate_create_access_key(params);
break;
case iam_constants.IAM_ACTIONS.GET_ACCESS_KEY_LAST_USED:
validate_get_access_key_last_used(params);
break;
case iam_constants.IAM_ACTIONS.UPDATE_ACCESS_KEY:
validate_update_access_key(params);
break;
case iam_constants.IAM_ACTIONS.DELETE_ACCESS_KEY:
validate_delete_access_key(params);
break;
case iam_constants.IAM_ACTIONS.LIST_ACCESS_KEYS:
validate_list_access_keys(params);
break;
default:
throw new RpcError('INTERNAL_ERROR', `${action} is not supported`);
}
}
/**
* validate_tagging_params will call the equivalent function for each action in tagging API
* @param {string} action
* @param {object} params
*/
function validate_tagging_params(action, params) {
switch (action) {
case iam_constants.IAM_ACTIONS.TAG_USER:
validate_tag_user_params(params);
break;
case iam_constants.IAM_ACTIONS.UNTAG_USER:
validate_untag_user_params(params);
break;
case iam_constants.IAM_ACTIONS.LIST_USER_TAGS:
validate_list_user_tags_params(params);
break;
default:
throw new RpcError('INTERNAL_ERROR', `${action} is not supported`);
}
}
/**
* validate_policy_params will call the equivalent function for each action in user and role policy APIs
* @param {string} action
* @param {object} params
*/
function validate_policy_params(action, params) {
switch (action) {
case iam_constants.IAM_ACTIONS.PUT_USER_POLICY:
validate_put_user_policy(params);
break;
case iam_constants.IAM_ACTIONS.GET_USER_POLICY:
validate_get_user_policy(params);
break;
case iam_constants.IAM_ACTIONS.DELETE_USER_POLICY:
validate_delete_user_policy(params);
break;
case iam_constants.IAM_ACTIONS.LIST_USER_POLICIES:
validate_list_user_policies(params);
break;
case iam_constants.IAM_ACTIONS.PUT_ROLE_POLICY:
validate_put_role_policy(params);
break;
case iam_constants.IAM_ACTIONS.GET_ROLE_POLICY:
validate_get_role_policy(params);
break;
case iam_constants.IAM_ACTIONS.DELETE_ROLE_POLICY:
validate_delete_role_policy(params);
break;
case iam_constants.IAM_ACTIONS.LIST_ROLE_POLICIES:
validate_list_role_policies(params);
break;
case iam_constants.IAM_ACTIONS.UPDATE_ASSUME_ROLE_POLICY:
validate_update_assume_role_policy(params);
break;
default:
throw new RpcError('INTERNAL_ERROR', `${action} is not supported`);
}
}
/**
* validate_role_params will call the equivalent function for each action in role API
* @param {string} action
* @param {object} params
*/
function validate_role_params(action, params) {
switch (action) {
case iam_constants.IAM_ACTIONS.CREATE_ROLE:
validate_create_role(params);
break;
case iam_constants.IAM_ACTIONS.GET_ROLE:
validate_get_role(params);
break;
case iam_constants.IAM_ACTIONS.UPDATE_ROLE:
validate_update_role(params);
break;
case iam_constants.IAM_ACTIONS.DELETE_ROLE:
validate_delete_role(params);
break;
case iam_constants.IAM_ACTIONS.LIST_ROLES:
validate_list_roles(params);
break;
default:
throw new RpcError('INTERNAL_ERROR', `${action} is not supported`);
}
}
/**
* check_required_username checks if the username was set
* @param {object} params
*/
function check_required_username(params) {
check_required_key(params.username, 'user-name');
}
/**
* check_required_access_key_id checks if the access key id was set
* @param {object} params
*/
function check_required_access_key_id(params) {
check_required_key(params.access_key, 'access-key-id');
}
/**
* check_required_status checks if the status was set
* @param {object} params
*/
function check_required_status(params) {
check_required_key(params.status, 'status');
}
/**
* check_required_policy_name checks if the policy name was set
* @param {object} params
*/
function check_required_policy_name(params) {
check_required_key(params.policy_name, 'policy-name');
}
/**
* check_required_policy_document checks if the policy document was set
* @param {object} params
*/
function check_required_policy_document(params) {
check_required_key(params.policy_document, 'policy-document');
}
/**
* check_required_role_name checks if the role name was set
* @param {object} params
*/
function check_required_role_name(params) {
check_required_key(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
}
/**
* check_required_assume_role_policy_document checks if the assume role policy document was set
* @param {object} params
*/
function check_required_assume_role_policy_document(params) {
check_required_key(params.assume_role_policy_document,
iam_constants.IAM_ROLE_PARAMETER_NAME.ASSUME_ROLE_POLICY_DOCUMENT);
}
/**
* check_required_key checks if a required key was set
* @param {any} value
* @param {string} parameter_name
*/
function check_required_key(value, parameter_name) {
if (value === undefined) {
const message_with_details = `the following arguments are required: --${parameter_name}`; // copied from AWS CLI
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
}
/**
* validate_create_user checks the params for create_user action
* @param {object} params
*/
function validate_create_user(params) {
try {
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
validate_iam_path(params.iam_path, iam_constants.IAM_PARAMETER_NAME.IAM_PATH);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_get_user checks the params for get_user action
* @param {object} params
*/
function validate_get_user(params) {
try {
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_update_user checks the params for update_user action
* @param {object} params
*/
function validate_update_user(params) {
try {
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
validation_utils.validate_username(params.new_username, iam_constants.IAM_PARAMETER_NAME.NEW_USERNAME);
validate_iam_path(params.new_iam_path, iam_constants.IAM_PARAMETER_NAME.NEW_IAM_PATH);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_delete_user checks the params for delete_user action
* @param {object} params
*/
function validate_delete_user(params) {
try {
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_list_users checks the params for list_users action
* @param {object} params
*/
function validate_list_users(params) {
try {
validate_marker(params.marker);
validate_max_items(params.max_items);
validate_iam_path(params.iam_path_prefix, iam_constants.IAM_PARAMETER_NAME.IAM_PATH_PREFIX);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_create_access_key checks the params for create_access_key action
* @param {object} params
*/
function validate_create_access_key(params) {
try {
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_get_access_key_last_used checks the params for get_access_key_last_used action
* @param {object} params
*/
function validate_get_access_key_last_used(params) {
try {
check_required_access_key_id(params);
validate_access_key_id(params.access_key);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_update_access_key checks the params for update_access_key action
* @param {object} params
*/
function validate_update_access_key(params) {
try {
check_required_access_key_id(params);
check_required_status(params);
validate_access_key_id(params.access_key);
validate_status(params.status);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_delete_access_key checks the params for delete_access_key action
* @param {object} params
*/
function validate_delete_access_key(params) {
try {
check_required_access_key_id(params);
validate_access_key_id(params.access_key);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_list_access_keys checks the params for list_access_keys action
* @param {object} params
*/
function validate_list_access_keys(params) {
try {
validate_marker(params.marker);
validate_max_items(params.max_items);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_put_user_policy checks the params for put_user_policy action
* @param {object} params
*/
function validate_put_user_policy(params) {
try {
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
check_required_policy_name(params);
validate_policy_name(params.policy_name, iam_constants.IAM_PARAMETER_NAME.POLICY_NAME);
check_required_policy_document(params);
validate_policy_document(params.policy_document, iam_constants.IAM_PARAMETER_NAME.POLICY_DOCUMENT);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_get_user_policy checks the params for get_user_policy action
* @param {object} params
*/
function validate_get_user_policy(params) {
try {
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
check_required_policy_name(params);
validate_policy_name(params.policy_name, iam_constants.IAM_PARAMETER_NAME.POLICY_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_delete_user_policy checks the params for delete_user_policy action
* @param {object} params
*/
function validate_delete_user_policy(params) {
try {
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
check_required_policy_name(params);
validate_policy_name(params.policy_name, iam_constants.IAM_PARAMETER_NAME.POLICY_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_list_user_policies checks the params for list_user_policies action
* @param {object} params
*/
function validate_list_user_policies(params) {
try {
validate_marker(params.marker);
validate_max_items(params.max_items);
check_required_username(params);
validation_utils.validate_username(params.username, iam_constants.IAM_PARAMETER_NAME.USERNAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_create_role checks the params for create_role action
* @param {object} params
*/
function validate_create_role(params) {
try {
check_required_role_name(params);
check_required_assume_role_policy_document(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
validate_iam_path(params.iam_path, iam_constants.IAM_ROLE_PARAMETER_NAME.IAM_PATH);
validate_role_description(params.description);
validate_assume_role_policy_document(params.assume_role_policy_document,
iam_constants.IAM_ROLE_PARAMETER_NAME.ASSUME_ROLE_POLICY_DOCUMENT);
validate_max_session_duration(params.max_session_duration);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_get_role checks the params for get_role action
* @param {object} params
*/
function validate_get_role(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_update_role checks the params for update_role action
* @param {object} params
*/
function validate_update_role(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
validate_role_description(params.description);
validate_max_session_duration(params.max_session_duration);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_delete_role checks the params for delete_role action
* @param {object} params
*/
function validate_delete_role(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_list_roles checks the params for list_roles action
* @param {object} params
*/
function validate_list_roles(params) {
try {
validate_marker(params.marker);
validate_max_items(params.max_items);
validate_iam_path(params.iam_path_prefix, iam_constants.IAM_ROLE_PARAMETER_NAME.IAM_PATH_PREFIX);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_put_role_policy checks the params for put_role_policy action
* @param {object} params
*/
function validate_put_role_policy(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
check_required_policy_name(params);
validate_policy_name(params.policy_name, iam_constants.IAM_ROLE_PARAMETER_NAME.POLICY_NAME);
check_required_policy_document(params);
validate_policy_document(params.policy_document, iam_constants.IAM_ROLE_PARAMETER_NAME.POLICY_DOCUMENT);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_delete_role_policy checks the params for delete_role_policy action
* @param {object} params
*/
function validate_delete_role_policy(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
check_required_policy_name(params);
validate_policy_name(params.policy_name, iam_constants.IAM_ROLE_PARAMETER_NAME.POLICY_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_get_role_policy checks the params for get_role_policy action
* @param {object} params
*/
function validate_get_role_policy(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
check_required_policy_name(params);
validate_policy_name(params.policy_name, iam_constants.IAM_ROLE_PARAMETER_NAME.POLICY_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_list_role_policies checks the params for list_role_policies action
* @param {object} params
*/
function validate_list_role_policies(params) {
try {
validate_marker(params.marker);
validate_max_items(params.max_items);
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_update_assume_role_policy checks the params for update_assume_role_policy action
* @param {object} params
*/
function validate_update_assume_role_policy(params) {
try {
check_required_role_name(params);
validate_role_name(params.role_name, iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME);
check_required_policy_document(params);
validate_assume_role_policy_document(params.policy_document, iam_constants.IAM_ROLE_PARAMETER_NAME.POLICY_DOCUMENT);
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_iam_path will validate:
* 1. type
* 2. length
* 3. regex (from AWS docs)
* @param {string} input_path
* @param {string} parameter_name
*/
function validate_iam_path(input_path, parameter_name = iam_constants.IAM_PARAMETER_NAME.IAM_PATH) {
try {
if (input_path === undefined) return;
// type check
validation_utils._type_check_input('string', input_path, parameter_name);
// length check
const min_length = 1;
const max_length = 512;
validation_utils._length_check_input(min_length, max_length, input_path, parameter_name);
// regex check
const valid_aws_path = input_path.startsWith('/') && input_path.endsWith('/') && AWS_IAM_PATH_REGEXP.test(input_path);
if (!valid_aws_path) {
const message_with_details = `The specified value for ${_.lowerFirst(parameter_name)} is invalid. ` +
`It must begin and end with / and contain only alphanumeric characters and/or / characters.`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return true;
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_marker will validate:
* 1. type
* 2. length
* 3. regex (from AWS docs)
* @param {string} input_marker
*/
function validate_marker(input_marker) {
try {
const parameter_name = 'Marker';
if (input_marker === undefined) return;
// type check
validation_utils._type_check_input('string', input_marker, parameter_name);
// length check
const min_length = 1;
validation_utils._length_min_check_input(min_length, input_marker, parameter_name);
// regex check
const valid_marker = AWS_IAM_LIST_MARKER.test(input_marker);
if (!valid_marker) {
const message_with_details = `The specified value for ${_.lowerFirst(parameter_name)} is invalid. `;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return true;
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_max_items will validate:
* 1. type
* (note: in the flow the type is validated when parse the value)
* 2. length
* @param {number} input_max_items
*/
function validate_max_items(input_max_items) {
const parameter_name = 'MaxItems';
if (input_max_items === undefined) return;
// type check
validation_utils._type_check_input('number', input_max_items, parameter_name);
// value check
const min_value = 1;
const max_value = 1000;
if (input_max_items < min_value) {
// using AWS CLI there is not actual validation for this (can even send zero and negative value)
const message_with_details = `Invalid value for parameter ${_.lowerFirst(parameter_name)}, ` +
`value: ${input_max_items}, valid min: ${min_value}`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
if (input_max_items > max_value) {
// using AWS CLI there is not actual validation for this (can send 2000 value)
const message_with_details = `Invalid value for parameter ${parameter_name}, ` +
`value: ${input_max_items}, valid max: ${min_value}`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return true;
}
/**
* validate_access_key_id will validate:
* 1. type
* 2. length
* 3. regex (from AWS docs)
* @param {string} input_access_key_id
*/
function validate_access_key_id(input_access_key_id) {
try {
const parameter_name = 'AccessKeyId';
if (input_access_key_id === undefined) return;
// type check
validation_utils._type_check_input('string', input_access_key_id, parameter_name);
// length check
const min_length = 16;
const max_length = 128;
validation_utils._length_check_input(min_length, max_length, input_access_key_id, parameter_name);
// regex check
const valid_access_key_id = AWS_IAM_ACCESS_KEY_INPUT_REGEXP.test(input_access_key_id);
if (!valid_access_key_id) {
const message_with_details = `The specified value for ${_.lowerFirst(parameter_name)} is invalid. ` +
`It must contain only alphanumeric characters`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return true;
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_status will validate:
* the the input is only from the defined enum
* @param {string} input_status
*/
function validate_status(input_status) {
const parameter_name = 'Status';
if (input_status === undefined) return;
if (input_status !== iam_constants.ACCESS_KEY_STATUS_ENUM.ACTIVE &&
input_status !== iam_constants.ACCESS_KEY_STATUS_ENUM.INACTIVE) {
const message_with_details = `Value ${input_status} at '${parameter_name}' ` +
`failed to satisfy constraint: Member must satisfy enum value set: ` +
`[${iam_constants.ACCESS_KEY_STATUS_ENUM.ACTIVE}, ${iam_constants.ACCESS_KEY_STATUS_ENUM.INACTIVE}]`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return true;
}
/**
* validate_policy_document will validate:
* 1. type
* 2. length
* 3. regex (from AWS docs)
* 4. valid JSON (like we do in bucket policy)
* 5. structure - basic (currently - only that we don't have Principal NotPrincipal)
* @param {string} input_policy_document
* @param {string} parameter_name
*/
function validate_policy_document(input_policy_document, parameter_name = iam_constants.IAM_PARAMETER_NAME.POLICY_DOCUMENT) {
try {
if (input_policy_document === undefined) return;
// syntax + JSON validation
const policy_document = _validate_policy_document_syntax(input_policy_document, parameter_name);
_validate_policy_document_iam_structure(policy_document);
return true;
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_assume_role_policy_document validates policy document syntax for role trust policy input
* @param {string} input_policy_document
* @param {string} parameter_name
*/
function validate_assume_role_policy_document(
input_policy_document,
parameter_name = iam_constants.IAM_ROLE_PARAMETER_NAME.ASSUME_ROLE_POLICY_DOCUMENT
) {
try {
if (input_policy_document === undefined) return;
const policy_document = _validate_policy_document_syntax(input_policy_document, parameter_name);
_validate_assume_role_policy_document_iam_structure(policy_document);
return true;
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_max_session_duration validates max session duration for role
* @param {number} max_session_duration
*/
function validate_max_session_duration(max_session_duration) {
if (max_session_duration === undefined) return;
const parameter_name = iam_constants.IAM_ROLE_PARAMETER_NAME.MAX_SESSION_DURATION;
validation_utils._type_check_input('number', max_session_duration, parameter_name);
const message_with_details = `1 validation error detected: Value ${max_session_duration} at ` +
`'${parameter_name}' failed to satisfy constraint: ` +
'Member must have value between 3600 and 43200';
if (!Number.isInteger(max_session_duration)) {
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
try {
validation_utils._length_check_input(3600, 43200, max_session_duration, parameter_name);
} catch (err) {
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
}
/**
* validate_role_description validates role description according to AWS rules
* @param {string} description
* @param {string} parameter_name
*/
function validate_role_description(description, parameter_name = iam_constants.IAM_ROLE_PARAMETER_NAME.DESCRIPTION) {
try {
if (description === undefined) return;
validation_utils._type_check_input('string', description, parameter_name);
validation_utils._length_max_check_input(1000, description, parameter_name);
if (!AWS_ROLE_DESCRIPTION_REGEXP.test(description)) {
const message_with_details = `The specified value for ${_.lowerFirst(parameter_name)} is invalid.`;
const { code, http_code, type } = IamError.ValidationError;
throw new IamError({ code, message: message_with_details, http_code, type });
}
return true;
} catch (err) {
translate_rpc_error(err);
}
}
/**
* validate_role_name validates role name according to AWS naming rules
* @param {string} input_role_name
* @param {string} parameter_name
*/
function validate_role_name(input_role_name, parameter_name = iam_constants.IAM_ROLE_PARAMETER_NAME.ROLE_NAME) {
try {
if (input_role_name === undefined) return;