File: endpoint_test.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.49.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312,636 kB
  • sloc: makefile: 120
file content (1016 lines) | stat: -rw-r--r-- 38,773 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
//go:build go1.7
// +build go1.7

package s3control

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/endpoints"
	"github.com/aws/aws-sdk-go/aws/request"
	"github.com/aws/aws-sdk-go/awstesting/unit"
)

type testParams struct {
	bucket                     string
	config                     *aws.Config
	expectedEndpoint           string
	expectedSigningName        string
	expectedSigningRegion      string
	expectedHeaderForOutpostID string
	expectedHeaderForAccountID bool
	expectedErr                string
}

// Test endpoint from outpost access point
func TestEndpoint_OutpostAccessPointARN(t *testing.T) {
	cases := map[string]testParams{
		"Outpost AccessPoint with no S3UseARNRegion flag set": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedEndpoint:           "https://s3-outposts.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint Cross-Region Enabled": {
			bucket: "arn:aws:s3-outposts:us-east-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts.us-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-east-1",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint Cross-Region Disabled": {
			bucket: "arn:aws:s3-outposts:us-east-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "client region does not match provided ARN region",
		},
		"Outpost AccessPoint other partition": {
			bucket: "arn:aws-cn:s3-outposts:cn-north-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedErr: "ConfigurationError: client partition does not match provided ARN partition",
		},
		"Outpost AccessPoint us-gov region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:         aws.String("us-gov-east-1"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint with client region as FIPS (deprecated)": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region: aws.String("us-west-2-fips"),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint with client region as FIPS": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				UseFIPSEndpoint: endpoints.FIPSEndpointStateEnabled,
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint with client FIPS (deprecated) region and cross-region ARN": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-west-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				EndpointResolver: endpoints.AwsUsGovPartition(),
				Region:           aws.String("us-gov-east-1-fips"),
				S3UseARNRegion:   aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-west-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-west-1",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint with client FIPS region and cross-region ARN": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-west-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				EndpointResolver: endpoints.AwsUsGovPartition(),
				Region:           aws.String("us-gov-east-1"),
				S3UseARNRegion:   aws.Bool(true),
				UseFIPSEndpoint:  endpoints.FIPSEndpointStateEnabled,
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-west-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-west-1",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint FIPS (deprecated) client region with matching ARN region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				EndpointResolver: endpoints.AwsUsGovPartition(),
				Region:           aws.String("fips-us-gov-east-1"),
				S3UseARNRegion:   aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint FIPS client region with matching ARN region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				EndpointResolver: endpoints.AwsUsGovPartition(),
				Region:           aws.String("us-gov-east-1"),
				S3UseARNRegion:   aws.Bool(true),
				UseFIPSEndpoint:  endpoints.FIPSEndpointStateEnabled,
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForAccountID: true,
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"Outpost AccessPoint with DualStack (deprecated)": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:       aws.String("us-west-2"),
				UseDualStack: aws.Bool(true),
			},
			expectedErr: "ConfigurationError: client configured for S3 Dual-stack but is not supported with resource ARN",
		},
		"Outpost AccessPoint with DualStack": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:               aws.String("us-west-2"),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateEnabled,
			},
			expectedErr: "ConfigurationError: client configured for S3 Dual-stack but is not supported with resource ARN",
		},
		"Outpost AccessPoint with Accelerate": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				S3UseAccelerate: aws.Bool(true),
			},
			expectedErr: "ConfigurationError: client configured for S3 Accelerate but is not supported with resource ARN",
		},
		"Invalid outpost resource format": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "outpost resource-id not set",
		},
		"Missing access point for outpost resource": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "incomplete outpost resource type",
		},
		"access point": {
			bucket: "myaccesspoint",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedEndpoint:           "https://123456789012.s3-control.us-west-2.amazonaws.com",
			expectedHeaderForAccountID: true,
			expectedSigningRegion:      "us-west-2",
			expectedSigningName:        "s3",
		},
		"outpost access point with unsupported sub-resource": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:mybucket:object:foo",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "sub resource not supported",
		},
		"Missing outpost identifiers in outpost access point arn": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:accesspoint:myendpoint",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "invalid Amazon s3-outposts ARN",
		},
		"Invalid Outpost AccessPoint ARN with FIPS pseudo-region (prefix)": {
			bucket: "arn:aws-us-gov:s3-outposts:fips-us-east-1:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedErr: "FIPS region not allowed in ARN",
		},
		"Invalid Outpost AccessPoint ARN with FIPS pseudo-region (suffix)": {
			bucket: "arn:aws-us-gov:s3-outposts:us-east-1-fips:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedErr: "FIPS region not allowed in ARN",
		},
	}

	runValidations(t, cases)
}

// Test endpoint from outpost bucket arn
func TestEndpoint_OutpostBucketARN(t *testing.T) {
	cases := map[string]testParams{
		"Outpost Bucket with no S3UseARNRegion flag set": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedEndpoint:           "https://s3-outposts.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket Cross-Region Enabled": {
			bucket: "arn:aws:s3-outposts:us-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts.us-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-east-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket Cross-Region Disabled": {
			bucket: "arn:aws:s3-outposts:us-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "client region does not match provided ARN region",
		},
		"Outpost Bucket other partition": {
			bucket: "arn:aws-cn:s3-outposts:cn-north-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedErr: "ConfigurationError: client partition does not match provided ARN partition",
		},
		"Outpost Bucket us-gov region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:         aws.String("us-gov-east-1"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket FIPS (deprecated) client region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region: aws.String("fips-us-gov-east-1"),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket FIPS client region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:          aws.String("us-gov-east-1"),
				UseFIPSEndpoint: endpoints.FIPSEndpointStateEnabled,
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket FIPS (deprecated) client region with match ARN region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:         aws.String("fips-us-gov-east-1"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket FIPS client region with match ARN region": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:          aws.String("us-gov-east-1"),
				UseFIPSEndpoint: endpoints.FIPSEndpointStateEnabled,
				S3UseARNRegion:  aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-east-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-east-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket FIPS (deprecated) client region with cross-region ARN": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-west-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				EndpointResolver: endpoints.AwsUsGovPartition(),
				Region:           aws.String("fips-us-gov-east-1"),
				S3UseARNRegion:   aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-west-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-west-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket FIPS client region with cross-region ARN": {
			bucket: "arn:aws-us-gov:s3-outposts:us-gov-west-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				EndpointResolver: endpoints.AwsUsGovPartition(),
				Region:           aws.String("us-gov-east-1"),
				UseFIPSEndpoint:  endpoints.FIPSEndpointStateEnabled,
				S3UseARNRegion:   aws.Bool(true),
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-gov-west-1.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-gov-west-1",
			expectedHeaderForOutpostID: "op-01234567890123456",
			expectedHeaderForAccountID: true,
		},
		"Outpost Bucket with DualStack": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:       aws.String("us-west-2"),
				UseDualStack: aws.Bool(true),
			},
			expectedErr: "ConfigurationError: client configured for S3 Dual-stack but is not supported with resource ARN",
		},
		"Outpost Bucket with Accelerate": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint",
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				S3UseAccelerate: aws.Bool(true),
			},
			expectedErr: "ConfigurationError: client configured for S3 Accelerate but is not supported with resource ARN",
		},
		"Missing bucket id": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket",
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				S3UseAccelerate: aws.Bool(true),
			},
			expectedErr: "invalid Amazon s3-outposts ARN",
		},
		"Invalid ARN": {
			bucket: "arn:aws:s3-outposts:us-west-2:123456789012:bucket:mybucket",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedErr: "invalid Amazon s3-outposts ARN, unknown resource type",
		},
		"Invalid Outpost Bucket ARN with FIPS pseudo-region (prefix)": {
			bucket: "arn:aws:s3-outposts:fips-us-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedErr: "FIPS region not allowed in ARN",
		},
		"Invalid Outpost Bucket ARN with FIPS pseudo-region (suffix)": {
			bucket: "arn:aws:s3-outposts:us-east-1-fips:123456789012:outpost:op-01234567890123456:bucket:mybucket",
			config: &aws.Config{
				Region:         aws.String("us-west-2"),
				S3UseARNRegion: aws.Bool(true),
			},
			expectedErr: "FIPS region not allowed in ARN",
		},
	}

	runValidations(t, cases)
}

// Runs the test validation
func runValidations(t *testing.T, cases map[string]testParams) {
	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			sess := unit.Session.Copy(c.config)

			svc := New(sess)
			req, _ := svc.GetBucketRequest(&GetBucketInput{
				Bucket:    &c.bucket,
				AccountId: aws.String("123456789012"),
			})

			req.Handlers.Send.Clear()
			req.Handlers.Send.PushBack(func(r *request.Request) {
				defer func() {
					r.HTTPResponse = &http.Response{
						StatusCode:    200,
						ContentLength: 0,
						Body:          ioutil.NopCloser(bytes.NewReader(nil)),
					}
				}()
				if len(c.expectedErr) != 0 {
					return
				}

				endpoint := fmt.Sprintf("%s://%s", r.HTTPRequest.URL.Scheme, r.HTTPRequest.URL.Host)
				if e, a := c.expectedEndpoint, endpoint; e != a {
					t.Errorf("expected %v, got %v", e, a)
				}

				if e, a := c.expectedSigningName, r.ClientInfo.SigningName; e != a {
					t.Errorf("expected %v, got %v", e, a)
				}
				if e, a := c.expectedSigningRegion, r.ClientInfo.SigningRegion; e != a {
					t.Errorf("expected %v, got %v", e, a)
				}

				if e, a := c.expectedHeaderForOutpostID, r.HTTPRequest.Header.Get("x-amz-outpost-id"); e != a {
					if len(e) == 0 {
						t.Errorf("expected no outpost id header set, got %v", a)
					} else if len(a) == 0 {
						t.Errorf("expected outpost id header set as %v, got none", e)
					} else {
						t.Errorf("expected %v as Outpost id header value, got %v", e, a)
					}
				}

				if c.expectedHeaderForAccountID {
					if e, a := "123456789012", r.HTTPRequest.Header.Get("x-amz-account-id"); e != a {
						t.Errorf("expected x-amz-account-id header value to be %v, got %v", e, a)
					}
				}
			})

			err := req.Send()
			if len(c.expectedErr) == 0 && err != nil {
				t.Errorf("expected no error but got: %v", err)
			} else if len(c.expectedErr) != 0 && err == nil {
				t.Errorf("expected err %q, but got nil", c.expectedErr)
			} else if len(c.expectedErr) != 0 && err != nil && !strings.Contains(err.Error(), c.expectedErr) {
				t.Errorf("expected %v, got %v", c.expectedErr, err.Error())
			}
		})
	}
}

type testParamsWithRequestFn struct {
	bucket                     string
	outpostID                  string
	config                     *aws.Config
	requestFn                  func(c *S3Control) *request.Request
	expectedEndpoint           string
	expectedSigningName        string
	expectedSigningRegion      string
	expectedHeaderForOutpostID string
	expectedErr                string
}

func TestCustomEndpoint_SpecialOperations(t *testing.T) {
	cases := map[string]testParamsWithRequestFn{
		"CreateBucketOperation": {
			bucket:    "mockBucket",
			outpostID: "op-01234567890123456",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.CreateBucketRequest(&CreateBucketInput{
					Bucket:    aws.String("mockBucket"),
					OutpostId: aws.String("op-01234567890123456"),
				})
				return req
			},
			expectedEndpoint:           "https://s3-outposts.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"CreateBucketOperation (FIPS)": {
			bucket:    "mockBucket",
			outpostID: "op-01234567890123456",
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				UseFIPSEndpoint: endpoints.FIPSEndpointStateEnabled,
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.CreateBucketRequest(&CreateBucketInput{
					Bucket:    aws.String("mockBucket"),
					OutpostId: aws.String("op-01234567890123456"),
				})
				return req
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"ListRegionalBucketsOperation": {
			bucket:    "mockBucket",
			outpostID: "op-01234567890123456",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.ListRegionalBucketsRequest(&ListRegionalBucketsInput{
					OutpostId: aws.String("op-01234567890123456"),
					AccountId: aws.String("123456789012"),
				})
				return req
			},
			expectedEndpoint:           "https://s3-outposts.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"ListRegionalBucketsOperation (FIPS)": {
			bucket:    "mockBucket",
			outpostID: "op-01234567890123456",
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				UseFIPSEndpoint: endpoints.FIPSEndpointStateEnabled,
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.ListRegionalBucketsRequest(&ListRegionalBucketsInput{
					OutpostId: aws.String("op-01234567890123456"),
					AccountId: aws.String("123456789012"),
				})
				return req
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"CreateAccessPoint bucket arn": {
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.CreateAccessPointRequest(&CreateAccessPointInput{
					AccountId: aws.String("123456789012"),
					Bucket:    aws.String("arn:aws:s3:us-west-2:123456789012:bucket:mockBucket"),
					Name:      aws.String("mockName"),
				})
				return req
			},
			expectedErr: "invalid Amazon s3 ARN, unknown resource type",
		},
		"CreateAccessPoint outpost bucket arn": {
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.CreateAccessPointRequest(&CreateAccessPointInput{
					AccountId: aws.String("123456789012"),
					Bucket:    aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket:mockBucket"),
					Name:      aws.String("mockName"),
				})
				return req
			},
			expectedEndpoint:           "https://s3-outposts.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"CreateAccessPoint (FIPS) outpost bucket arn": {
			config: &aws.Config{
				Region:          aws.String("us-west-2"),
				UseFIPSEndpoint: endpoints.FIPSEndpointStateEnabled,
			},
			requestFn: func(svc *S3Control) *request.Request {
				req, _ := svc.CreateAccessPointRequest(&CreateAccessPointInput{
					AccountId: aws.String("123456789012"),
					Bucket:    aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket:mockBucket"),
					Name:      aws.String("mockName"),
				})
				return req
			},
			expectedEndpoint:           "https://s3-outposts-fips.us-west-2.amazonaws.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			runValidationsWithRequestFn(t, c)
		})
	}
}

func TestCustomEndpointURL(t *testing.T) {
	account := "123456789012"
	cases := map[string]testParamsWithRequestFn{
		"standard GetAccesspoint with custom endpoint url": {
			config: &aws.Config{
				Endpoint: aws.String("beta.example.com"),
				Region:   aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetAccessPointRequest(&GetAccessPointInput{
					AccountId: aws.String(account),
					Name:      aws.String("apname"),
				})
				return req
			},
			expectedEndpoint:      "https://123456789012.beta.example.com",
			expectedSigningName:   "s3",
			expectedSigningRegion: "us-west-2",
		},
		"Outpost Accesspoint ARN with GetAccesspoint and custom endpoint url": {
			config: &aws.Config{
				Endpoint: aws.String("beta.example.com"),
				Region:   aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetAccessPointRequest(&GetAccessPointInput{
					AccountId: aws.String(account),
					Name:      aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint"),
				})
				return req
			},
			expectedEndpoint:           "https://beta.example.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"standard CreateBucket with custom endpoint url": {
			config: &aws.Config{
				Endpoint: aws.String("beta.example.com"),
				Region:   aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.CreateBucketRequest(&CreateBucketInput{
					Bucket:    aws.String("bucketname"),
					OutpostId: aws.String("op-123"),
				})
				return req
			},
			expectedEndpoint:           "https://beta.example.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-123",
		},
		"Outpost Accesspoint for GetBucket with custom endpoint url": {
			config: &aws.Config{
				Endpoint: aws.String("beta.example.com"),
				Region:   aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetBucketRequest(&GetBucketInput{
					Bucket: aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket:mybucket"),
				})
				return req
			},
			expectedEndpoint:           "https://beta.example.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-01234567890123456",
		},
		"GetAccesspoint with dualstack (deprecated) and custom endpoint url": {
			config: &aws.Config{
				Endpoint:     aws.String("beta.example.com"),
				Region:       aws.String("us-west-2"),
				UseDualStack: aws.Bool(true),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetAccessPointRequest(&GetAccessPointInput{
					AccountId: aws.String(account),
					Name:      aws.String("apname"),
				})
				return req
			},
			expectedEndpoint:      "https://123456789012.beta.example.com",
			expectedSigningName:   "s3",
			expectedSigningRegion: "us-west-2",
		},
		"GetAccesspoint with dualstack and custom endpoint url": {
			config: &aws.Config{
				Endpoint:             aws.String("beta.example.com"),
				Region:               aws.String("us-west-2"),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateEnabled,
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetAccessPointRequest(&GetAccessPointInput{
					AccountId: aws.String(account),
					Name:      aws.String("apname"),
				})
				return req
			},
			expectedEndpoint:      "https://123456789012.beta.example.com",
			expectedSigningName:   "s3",
			expectedSigningRegion: "us-west-2",
		},
		"GetAccesspoint with Outposts accesspoint ARN and dualstack (deprecated)": {
			config: &aws.Config{
				Endpoint:     aws.String("beta.example.com"),
				UseDualStack: aws.Bool(true),
				Region:       aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetAccessPointRequest(&GetAccessPointInput{
					AccountId: aws.String(account),
					Name:      aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint"),
				})
				return req
			},
			expectedErr: "client configured for S3 Dual-stack but is not supported with resource ARN",
		},
		"GetAccesspoint with Outposts accesspoint ARN and dualstack": {
			config: &aws.Config{
				Endpoint:             aws.String("beta.example.com"),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateEnabled,
				Region:               aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetAccessPointRequest(&GetAccessPointInput{
					AccountId: aws.String(account),
					Name:      aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:accesspoint:myaccesspoint"),
				})
				return req
			},
			expectedErr: "client configured for S3 Dual-stack but is not supported with resource ARN",
		},
		"standard CreateBucket with dualstack (deprecated)": {
			config: &aws.Config{
				Endpoint:     aws.String("beta.example.com"),
				UseDualStack: aws.Bool(true),
				Region:       aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.CreateBucketRequest(&CreateBucketInput{
					Bucket:    aws.String("bucketname"),
					OutpostId: aws.String("op-1234567890123456"),
				})
				return req
			},
			expectedEndpoint:           "https://beta.example.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-1234567890123456",
		},
		"standard CreateBucket with dualstack": {
			config: &aws.Config{
				Endpoint:             aws.String("beta.example.com"),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateEnabled,
				Region:               aws.String("us-west-2"),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.CreateBucketRequest(&CreateBucketInput{
					Bucket:    aws.String("bucketname"),
					OutpostId: aws.String("op-1234567890123456"),
				})
				return req
			},
			expectedEndpoint:           "https://beta.example.com",
			expectedSigningName:        "s3-outposts",
			expectedSigningRegion:      "us-west-2",
			expectedHeaderForOutpostID: "op-1234567890123456",
		},
		"GetBucket with Outpost bucket ARN": {
			config: &aws.Config{
				Endpoint:     aws.String("beta.example.com"),
				Region:       aws.String("us-west-2"),
				UseDualStack: aws.Bool(true),
			},
			requestFn: func(c *S3Control) *request.Request {
				req, _ := c.GetBucketRequest(&GetBucketInput{
					Bucket: aws.String("arn:aws:s3-outposts:us-west-2:123456789012:outpost:op-01234567890123456:bucket:mybucket"),
				})
				return req
			},
			expectedErr: "client configured for S3 Dual-stack but is not supported with resource ARN",
		},
	}

	for name, c := range cases {
		t.Run(name, func(t *testing.T) {
			runValidationsWithRequestFn(t, c)
		})
	}
}

func runValidationsWithRequestFn(t *testing.T, c testParamsWithRequestFn) {
	sess := unit.Session.Copy(c.config)
	svc := New(sess)

	req := c.requestFn(svc)
	req.Handlers.Send.Clear()
	req.Handlers.Send.PushBack(func(r *request.Request) {
		defer func() {
			r.HTTPResponse = &http.Response{
				StatusCode:    200,
				ContentLength: 0,
				Body:          ioutil.NopCloser(bytes.NewReader(nil)),
			}
		}()
		if len(c.expectedErr) != 0 {
			return
		}

		endpoint := fmt.Sprintf("%s://%s", r.HTTPRequest.URL.Scheme, r.HTTPRequest.URL.Host)
		if e, a := c.expectedEndpoint, endpoint; e != a {
			t.Errorf("expected %v, got %v", e, a)
		}

		if e, a := c.expectedSigningName, r.ClientInfo.SigningName; e != a {
			t.Errorf("expected %v, got %v", e, a)
		}
		if e, a := c.expectedSigningRegion, r.ClientInfo.SigningRegion; e != a {
			t.Errorf("expected %v, got %v", e, a)
		}

		if e, a := c.expectedHeaderForOutpostID, r.HTTPRequest.Header.Get("x-amz-outpost-id"); e != a {
			if len(e) == 0 {
				t.Errorf("expected no outpost id header set, got %v", a)
			} else if len(a) == 0 {
				t.Errorf("expected outpost id header set as %v, got none", e)
			} else {
				t.Errorf("expected %v as Outpost id header value, got %v", e, a)
			}
		}
	})

	err := req.Send()
	if len(c.expectedErr) == 0 && err != nil {
		t.Errorf("expected no error but got: %v", err)
	} else if len(c.expectedErr) != 0 && err == nil {
		t.Errorf("expected err %q, but got nil", c.expectedErr)
	} else if len(c.expectedErr) != 0 && err != nil && !strings.Contains(err.Error(), c.expectedErr) {
		t.Errorf("expected %v, got %v", c.expectedErr, err.Error())
	}
}

func TestInputIsNotModified(t *testing.T) {
	inputBucket := "arn:aws:s3-outposts:us-east-1:123456789012:outpost:op-01234567890123456:bucket:mybucket"
	expectedAccountID := "123456789012"
	sess := unit.Session.Copy(&aws.Config{
		Region:         aws.String("us-west-2"),
		S3UseARNRegion: aws.Bool(true),
	})

	svc := New(sess)
	params := &DeleteBucketInput{
		Bucket: aws.String(inputBucket),
	}
	req, _ := svc.DeleteBucketRequest(params)

	req.Handlers.Send.Clear()
	req.Handlers.Send.PushBack(func(r *request.Request) {
		defer func() {
			r.HTTPResponse = &http.Response{
				StatusCode:    200,
				ContentLength: 0,
				Body:          ioutil.NopCloser(bytes.NewReader(nil)),
			}
		}()
	})

	err := req.Send()
	if err != nil {
		t.Fatalf("expected no error, got %v", err)
	}

	// check if req params were modified
	if e, a := *params.Bucket, inputBucket; !strings.EqualFold(e, a) {
		t.Fatalf("expected no modification for operation input, "+
			"expected %v, got %v as bucket input", e, a)
	}

	if params.AccountId != nil {
		t.Fatalf("expected original input to be unmodified, but account id was backfilled")
	}

	modifiedInput, ok := req.Params.(*DeleteBucketInput)
	if !ok {
		t.Fatalf("expected modified input of type *DeleteBucketInput")
	}

	if modifiedInput.AccountId == nil {
		t.Fatalf("expected AccountID value to be backfilled, was not")
	}

	if e, a := expectedAccountID, *modifiedInput.AccountId; !strings.EqualFold(e, a) {
		t.Fatalf("expected account id backfilled on request params to be %v, got %v", e, a)
	}

	if modifiedInput.Bucket == nil {
		t.Fatalf("expected Bucket value to be set, was not")
	}

	if e, a := "mybucket", *modifiedInput.Bucket; !strings.EqualFold(e, a) {
		t.Fatalf("expected modified input bucket name to be %v, got %v", e, a)
	}
}

func TestUseDualStackClientBehavior(t *testing.T) {
	cases := map[string]testParams{
		"UseDualStack unset, UseDualStackEndpoints unset": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region: aws.String("us-west-2"),
			},
			expectedEndpoint:      "https://123456789012.s3-control.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
		"UseDualStack false, UseDualStackEndpoints unset": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region:       aws.String("us-west-2"),
				UseDualStack: aws.Bool(false),
			},
			expectedEndpoint:      "https://123456789012.s3-control.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
		"UseDualStack true, UseDualStackEndpoints unset": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region:       aws.String("us-west-2"),
				UseDualStack: aws.Bool(true),
			},
			expectedEndpoint:      "https://123456789012.s3-control.dualstack.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
		"UseDualStack unset, UseDualStackEndpoints disabled": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region:               aws.String("us-west-2"),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateDisabled,
			},
			expectedEndpoint:      "https://123456789012.s3-control.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
		"UseDualStack unset, UseDualStackEndpoint enabled": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region:               aws.String("us-west-2"),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateEnabled,
			},
			expectedEndpoint:      "https://123456789012.s3-control.dualstack.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
		"UseDualStack true, UseDualStackEndpoint disabled": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region:               aws.String("us-west-2"),
				UseDualStack:         aws.Bool(true),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateDisabled,
			},
			expectedEndpoint:      "https://123456789012.s3-control.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
		"UseDualStack false, UseDualStackEndpoint enabled": {
			bucket: "test-bucket",
			config: &aws.Config{
				Region:               aws.String("us-west-2"),
				UseDualStack:         aws.Bool(false),
				UseDualStackEndpoint: endpoints.DualStackEndpointStateEnabled,
			},
			expectedEndpoint:      "https://123456789012.s3-control.dualstack.us-west-2.amazonaws.com",
			expectedSigningRegion: "us-west-2",
			expectedSigningName:   "s3",
		},
	}
	runValidations(t, cases)
}