github.com/aavshr/aws-sdk-go@v1.41.3/aws/endpoints/v3model_legacy_region_test.go (about)

     1  //go:build go1.7
     2  // +build go1.7
     3  
     4  package endpoints
     5  
     6  import (
     7  	"regexp"
     8  	"testing"
     9  )
    10  
    11  func TestEndpointFor_STSRegionalFlag(t *testing.T) {
    12  
    13  	// mock STS regional endpoints model
    14  	mockSTSModelPartition := partition{
    15  		ID:        "aws",
    16  		Name:      "AWS Standard",
    17  		DNSSuffix: "amazonaws.com",
    18  		RegionRegex: regionRegex{
    19  			Regexp: func() *regexp.Regexp {
    20  				reg, _ := regexp.Compile("^(us|eu|ap|sa|ca|me)\\-\\w+\\-\\d+$")
    21  				return reg
    22  			}(),
    23  		},
    24  		Defaults: endpoint{
    25  			Hostname:          "{service}.{region}.{dnsSuffix}",
    26  			Protocols:         []string{"https"},
    27  			SignatureVersions: []string{"v4"},
    28  		},
    29  		Regions: regions{
    30  			"ap-east-1": region{
    31  				Description: "Asia Pacific (Hong Kong)",
    32  			},
    33  			"ap-northeast-1": region{
    34  				Description: "Asia Pacific (Tokyo)",
    35  			},
    36  			"ap-northeast-2": region{
    37  				Description: "Asia Pacific (Seoul)",
    38  			},
    39  			"ap-south-1": region{
    40  				Description: "Asia Pacific (Mumbai)",
    41  			},
    42  			"ap-southeast-1": region{
    43  				Description: "Asia Pacific (Singapore)",
    44  			},
    45  			"ap-southeast-2": region{
    46  				Description: "Asia Pacific (Sydney)",
    47  			},
    48  			"ca-central-1": region{
    49  				Description: "Canada (Central)",
    50  			},
    51  			"eu-central-1": region{
    52  				Description: "EU (Frankfurt)",
    53  			},
    54  			"eu-north-1": region{
    55  				Description: "EU (Stockholm)",
    56  			},
    57  			"eu-west-1": region{
    58  				Description: "EU (Ireland)",
    59  			},
    60  			"eu-west-2": region{
    61  				Description: "EU (London)",
    62  			},
    63  			"eu-west-3": region{
    64  				Description: "EU (Paris)",
    65  			},
    66  			"me-south-1": region{
    67  				Description: "Middle East (Bahrain)",
    68  			},
    69  			"sa-east-1": region{
    70  				Description: "South America (Sao Paulo)",
    71  			},
    72  			"us-east-1": region{
    73  				Description: "US East (N. Virginia)",
    74  			},
    75  			"us-east-2": region{
    76  				Description: "US East (Ohio)",
    77  			},
    78  			"us-west-1": region{
    79  				Description: "US West (N. California)",
    80  			},
    81  			"us-west-2": region{
    82  				Description: "US West (Oregon)",
    83  			},
    84  		},
    85  		Services: services{
    86  			"sts": service{
    87  				PartitionEndpoint: "aws-global",
    88  				Defaults:          endpoint{},
    89  				Endpoints: endpoints{
    90  					"ap-east-1":      endpoint{},
    91  					"ap-northeast-1": endpoint{},
    92  					"ap-northeast-2": endpoint{},
    93  					"ap-south-1":     endpoint{},
    94  					"ap-southeast-1": endpoint{},
    95  					"ap-southeast-2": endpoint{},
    96  					"aws-global": endpoint{
    97  						Hostname: "sts.amazonaws.com",
    98  						CredentialScope: credentialScope{
    99  							Region: "us-east-1",
   100  						},
   101  					},
   102  					"ca-central-1": endpoint{},
   103  					"eu-central-1": endpoint{},
   104  					"eu-north-1":   endpoint{},
   105  					"eu-west-1":    endpoint{},
   106  					"eu-west-2":    endpoint{},
   107  					"eu-west-3":    endpoint{},
   108  					"me-south-1":   endpoint{},
   109  					"sa-east-1":    endpoint{},
   110  					"us-east-1":    endpoint{},
   111  					"us-east-1-fips": endpoint{
   112  						Hostname: "sts-fips.us-east-1.amazonaws.com",
   113  						CredentialScope: credentialScope{
   114  							Region: "us-east-1",
   115  						},
   116  					},
   117  					"us-east-2": endpoint{},
   118  					"us-east-2-fips": endpoint{
   119  						Hostname: "sts-fips.us-east-2.amazonaws.com",
   120  						CredentialScope: credentialScope{
   121  							Region: "us-east-2",
   122  						},
   123  					},
   124  					"us-west-1": endpoint{},
   125  					"us-west-1-fips": endpoint{
   126  						Hostname: "sts-fips.us-west-1.amazonaws.com",
   127  						CredentialScope: credentialScope{
   128  							Region: "us-west-1",
   129  						},
   130  					},
   131  					"us-west-2": endpoint{},
   132  					"us-west-2-fips": endpoint{
   133  						Hostname: "sts-fips.us-west-2.amazonaws.com",
   134  						CredentialScope: credentialScope{
   135  							Region: "us-west-2",
   136  						},
   137  					},
   138  				},
   139  			},
   140  		},
   141  	}
   142  
   143  	// resolver for mock STS regional endpoints model
   144  	resolver := mockSTSModelPartition
   145  
   146  	cases := map[string]struct {
   147  		service, region                                     string
   148  		regional                                            bool
   149  		ExpectURL, ExpectSigningMethod, ExpectSigningRegion string
   150  		ExpectSigningNameDerived                            bool
   151  	}{
   152  		// STS Endpoints resolver tests :
   153  		"sts/us-west-2/regional": {
   154  			service:                  "sts",
   155  			region:                   "us-west-2",
   156  			regional:                 true,
   157  			ExpectURL:                "https://sts.us-west-2.amazonaws.com",
   158  			ExpectSigningMethod:      "v4",
   159  			ExpectSigningNameDerived: true,
   160  			ExpectSigningRegion:      "us-west-2",
   161  		},
   162  		"sts/us-west-2/legacy": {
   163  			service:                  "sts",
   164  			region:                   "us-west-2",
   165  			regional:                 false,
   166  			ExpectURL:                "https://sts.amazonaws.com",
   167  			ExpectSigningMethod:      "v4",
   168  			ExpectSigningNameDerived: true,
   169  			ExpectSigningRegion:      "us-east-1",
   170  		},
   171  		"sts/ap-east-1/regional": {
   172  			service:                  "sts",
   173  			region:                   "ap-east-1",
   174  			regional:                 true,
   175  			ExpectURL:                "https://sts.ap-east-1.amazonaws.com",
   176  			ExpectSigningMethod:      "v4",
   177  			ExpectSigningNameDerived: true,
   178  			ExpectSigningRegion:      "ap-east-1",
   179  		},
   180  		"sts/ap-east-1/legacy": {
   181  			service:                  "sts",
   182  			region:                   "ap-east-1",
   183  			regional:                 false,
   184  			ExpectURL:                "https://sts.ap-east-1.amazonaws.com",
   185  			ExpectSigningMethod:      "v4",
   186  			ExpectSigningNameDerived: true,
   187  			ExpectSigningRegion:      "ap-east-1",
   188  		},
   189  		"sts/us-west-2-fips/regional": {
   190  			service:                  "sts",
   191  			region:                   "us-west-2-fips",
   192  			regional:                 true,
   193  			ExpectURL:                "https://sts-fips.us-west-2.amazonaws.com",
   194  			ExpectSigningMethod:      "v4",
   195  			ExpectSigningNameDerived: true,
   196  			ExpectSigningRegion:      "us-west-2",
   197  		},
   198  		"sts/us-west-2-fips/legacy": {
   199  			service:                  "sts",
   200  			region:                   "us-west-2-fips",
   201  			regional:                 false,
   202  			ExpectURL:                "https://sts-fips.us-west-2.amazonaws.com",
   203  			ExpectSigningMethod:      "v4",
   204  			ExpectSigningNameDerived: true,
   205  			ExpectSigningRegion:      "us-west-2",
   206  		},
   207  		"sts/aws-global/regional": {
   208  			service:                  "sts",
   209  			region:                   "aws-global",
   210  			regional:                 true,
   211  			ExpectURL:                "https://sts.amazonaws.com",
   212  			ExpectSigningMethod:      "v4",
   213  			ExpectSigningNameDerived: true,
   214  			ExpectSigningRegion:      "us-east-1",
   215  		},
   216  		"sts/aws-global/legacy": {
   217  			service:                  "sts",
   218  			region:                   "aws-global",
   219  			regional:                 false,
   220  			ExpectURL:                "https://sts.amazonaws.com",
   221  			ExpectSigningMethod:      "v4",
   222  			ExpectSigningNameDerived: true,
   223  			ExpectSigningRegion:      "us-east-1",
   224  		},
   225  		"sts/ap-south-1/regional": {
   226  			service:                  "sts",
   227  			region:                   "ap-south-1",
   228  			regional:                 true,
   229  			ExpectURL:                "https://sts.ap-south-1.amazonaws.com",
   230  			ExpectSigningMethod:      "v4",
   231  			ExpectSigningNameDerived: true,
   232  			ExpectSigningRegion:      "ap-south-1",
   233  		},
   234  		"sts/ap-south-1/legacy": {
   235  			service:                  "sts",
   236  			region:                   "ap-south-1",
   237  			regional:                 false,
   238  			ExpectURL:                "https://sts.amazonaws.com",
   239  			ExpectSigningMethod:      "v4",
   240  			ExpectSigningNameDerived: true,
   241  			ExpectSigningRegion:      "us-east-1",
   242  		},
   243  		"sts/ap-northeast-1/regional": {
   244  			service:                  "sts",
   245  			region:                   "ap-northeast-1",
   246  			regional:                 true,
   247  			ExpectURL:                "https://sts.ap-northeast-1.amazonaws.com",
   248  			ExpectSigningMethod:      "v4",
   249  			ExpectSigningNameDerived: true,
   250  			ExpectSigningRegion:      "ap-northeast-1",
   251  		},
   252  		"sts/ap-northeast-1/legacy": {
   253  			service:                  "sts",
   254  			region:                   "ap-northeast-1",
   255  			regional:                 false,
   256  			ExpectURL:                "https://sts.amazonaws.com",
   257  			ExpectSigningMethod:      "v4",
   258  			ExpectSigningNameDerived: true,
   259  			ExpectSigningRegion:      "us-east-1",
   260  		},
   261  		"sts/ap-southeast-1/regional": {
   262  			service:                  "sts",
   263  			region:                   "ap-southeast-1",
   264  			regional:                 true,
   265  			ExpectURL:                "https://sts.ap-southeast-1.amazonaws.com",
   266  			ExpectSigningMethod:      "v4",
   267  			ExpectSigningNameDerived: true,
   268  			ExpectSigningRegion:      "ap-southeast-1",
   269  		},
   270  		"sts/ap-southeast-1/legacy": {
   271  			service:                  "sts",
   272  			region:                   "ap-southeast-1",
   273  			regional:                 false,
   274  			ExpectURL:                "https://sts.amazonaws.com",
   275  			ExpectSigningMethod:      "v4",
   276  			ExpectSigningNameDerived: true,
   277  			ExpectSigningRegion:      "us-east-1",
   278  		},
   279  		"sts/ca-central-1/regional": {
   280  			service:                  "sts",
   281  			region:                   "ca-central-1",
   282  			regional:                 true,
   283  			ExpectURL:                "https://sts.ca-central-1.amazonaws.com",
   284  			ExpectSigningMethod:      "v4",
   285  			ExpectSigningNameDerived: true,
   286  			ExpectSigningRegion:      "ca-central-1",
   287  		},
   288  		"sts/ca-central-1/legacy": {
   289  			service:                  "sts",
   290  			region:                   "ca-central-1",
   291  			regional:                 false,
   292  			ExpectURL:                "https://sts.amazonaws.com",
   293  			ExpectSigningMethod:      "v4",
   294  			ExpectSigningNameDerived: true,
   295  			ExpectSigningRegion:      "us-east-1",
   296  		},
   297  		"sts/eu-central-1/regional": {
   298  			service:                  "sts",
   299  			region:                   "eu-central-1",
   300  			regional:                 true,
   301  			ExpectURL:                "https://sts.eu-central-1.amazonaws.com",
   302  			ExpectSigningMethod:      "v4",
   303  			ExpectSigningNameDerived: true,
   304  			ExpectSigningRegion:      "eu-central-1",
   305  		},
   306  		"sts/eu-central-1/legacy": {
   307  			service:                  "sts",
   308  			region:                   "eu-central-1",
   309  			regional:                 false,
   310  			ExpectURL:                "https://sts.amazonaws.com",
   311  			ExpectSigningMethod:      "v4",
   312  			ExpectSigningNameDerived: true,
   313  			ExpectSigningRegion:      "us-east-1",
   314  		},
   315  		"sts/eu-north-1/regional": {
   316  			service:                  "sts",
   317  			region:                   "eu-north-1",
   318  			regional:                 true,
   319  			ExpectURL:                "https://sts.eu-north-1.amazonaws.com",
   320  			ExpectSigningMethod:      "v4",
   321  			ExpectSigningNameDerived: true,
   322  			ExpectSigningRegion:      "eu-north-1",
   323  		},
   324  		"sts/eu-north-1/legacy": {
   325  			service:                  "sts",
   326  			region:                   "eu-north-1",
   327  			regional:                 false,
   328  			ExpectURL:                "https://sts.amazonaws.com",
   329  			ExpectSigningMethod:      "v4",
   330  			ExpectSigningNameDerived: true,
   331  			ExpectSigningRegion:      "us-east-1",
   332  		},
   333  		"sts/eu-west-1/regional": {
   334  			service:                  "sts",
   335  			region:                   "eu-west-1",
   336  			regional:                 true,
   337  			ExpectURL:                "https://sts.eu-west-1.amazonaws.com",
   338  			ExpectSigningMethod:      "v4",
   339  			ExpectSigningNameDerived: true,
   340  			ExpectSigningRegion:      "eu-west-1",
   341  		},
   342  		"sts/eu-west-1/legacy": {
   343  			service:                  "sts",
   344  			region:                   "eu-west-1",
   345  			regional:                 false,
   346  			ExpectURL:                "https://sts.amazonaws.com",
   347  			ExpectSigningMethod:      "v4",
   348  			ExpectSigningNameDerived: true,
   349  			ExpectSigningRegion:      "us-east-1",
   350  		},
   351  		"sts/eu-west-2/regional": {
   352  			service:                  "sts",
   353  			region:                   "eu-west-2",
   354  			regional:                 true,
   355  			ExpectURL:                "https://sts.eu-west-2.amazonaws.com",
   356  			ExpectSigningMethod:      "v4",
   357  			ExpectSigningNameDerived: true,
   358  			ExpectSigningRegion:      "eu-west-2",
   359  		},
   360  		"sts/eu-west-2/legacy": {
   361  			service:                  "sts",
   362  			region:                   "eu-west-2",
   363  			regional:                 false,
   364  			ExpectURL:                "https://sts.amazonaws.com",
   365  			ExpectSigningMethod:      "v4",
   366  			ExpectSigningNameDerived: true,
   367  			ExpectSigningRegion:      "us-east-1",
   368  		},
   369  		"sts/eu-west-3/regional": {
   370  			service:                  "sts",
   371  			region:                   "eu-west-3",
   372  			regional:                 true,
   373  			ExpectURL:                "https://sts.eu-west-3.amazonaws.com",
   374  			ExpectSigningMethod:      "v4",
   375  			ExpectSigningNameDerived: true,
   376  			ExpectSigningRegion:      "eu-west-3",
   377  		},
   378  		"sts/eu-west-3/legacy": {
   379  			service:                  "sts",
   380  			region:                   "eu-west-3",
   381  			regional:                 false,
   382  			ExpectURL:                "https://sts.amazonaws.com",
   383  			ExpectSigningMethod:      "v4",
   384  			ExpectSigningNameDerived: true,
   385  			ExpectSigningRegion:      "us-east-1",
   386  		},
   387  		"sts/sa-east-1/regional": {
   388  			service:                  "sts",
   389  			region:                   "sa-east-1",
   390  			regional:                 true,
   391  			ExpectURL:                "https://sts.sa-east-1.amazonaws.com",
   392  			ExpectSigningMethod:      "v4",
   393  			ExpectSigningNameDerived: true,
   394  			ExpectSigningRegion:      "sa-east-1",
   395  		},
   396  		"sts/sa-east-1/legacy": {
   397  			service:                  "sts",
   398  			region:                   "sa-east-1",
   399  			regional:                 false,
   400  			ExpectURL:                "https://sts.amazonaws.com",
   401  			ExpectSigningMethod:      "v4",
   402  			ExpectSigningNameDerived: true,
   403  			ExpectSigningRegion:      "us-east-1",
   404  		},
   405  		"sts/us-east-1/regional": {
   406  			service:                  "sts",
   407  			region:                   "us-east-1",
   408  			regional:                 true,
   409  			ExpectURL:                "https://sts.us-east-1.amazonaws.com",
   410  			ExpectSigningMethod:      "v4",
   411  			ExpectSigningNameDerived: true,
   412  			ExpectSigningRegion:      "us-east-1",
   413  		},
   414  		"sts/us-east-1/legacy": {
   415  			service:                  "sts",
   416  			region:                   "us-east-1",
   417  			regional:                 false,
   418  			ExpectURL:                "https://sts.amazonaws.com",
   419  			ExpectSigningMethod:      "v4",
   420  			ExpectSigningNameDerived: true,
   421  			ExpectSigningRegion:      "us-east-1",
   422  		},
   423  		"sts/us-east-2/regional": {
   424  			service:                  "sts",
   425  			region:                   "us-east-2",
   426  			regional:                 true,
   427  			ExpectURL:                "https://sts.us-east-2.amazonaws.com",
   428  			ExpectSigningMethod:      "v4",
   429  			ExpectSigningNameDerived: true,
   430  			ExpectSigningRegion:      "us-east-2",
   431  		},
   432  		"sts/us-east-2/legacy": {
   433  			service:                  "sts",
   434  			region:                   "us-east-2",
   435  			regional:                 false,
   436  			ExpectURL:                "https://sts.amazonaws.com",
   437  			ExpectSigningMethod:      "v4",
   438  			ExpectSigningNameDerived: true,
   439  			ExpectSigningRegion:      "us-east-1",
   440  		},
   441  		"sts/us-west-1/regional": {
   442  			service:                  "sts",
   443  			region:                   "us-west-1",
   444  			regional:                 true,
   445  			ExpectURL:                "https://sts.us-west-1.amazonaws.com",
   446  			ExpectSigningMethod:      "v4",
   447  			ExpectSigningNameDerived: true,
   448  			ExpectSigningRegion:      "us-west-1",
   449  		},
   450  		"sts/us-west-1/legacy": {
   451  			service:                  "sts",
   452  			region:                   "us-west-1",
   453  			regional:                 false,
   454  			ExpectURL:                "https://sts.amazonaws.com",
   455  			ExpectSigningMethod:      "v4",
   456  			ExpectSigningNameDerived: true,
   457  			ExpectSigningRegion:      "us-east-1",
   458  		},
   459  	}
   460  
   461  	for name, c := range cases {
   462  		t.Run(name, func(t *testing.T) {
   463  			var optionSlice []func(o *Options)
   464  			optionSlice = append(optionSlice, func(o *Options) {
   465  				if c.regional {
   466  					o.STSRegionalEndpoint = RegionalSTSEndpoint
   467  				}
   468  			})
   469  
   470  			actual, err := resolver.EndpointFor(c.service, c.region, optionSlice...)
   471  			if err != nil {
   472  				t.Fatalf("failed to resolve endpoint, %v", err)
   473  			}
   474  
   475  			if e, a := c.ExpectURL, actual.URL; e != a {
   476  				t.Errorf("expect %v, got %v", e, a)
   477  			}
   478  
   479  			if e, a := c.ExpectSigningMethod, actual.SigningMethod; e != a {
   480  				t.Errorf("expect %v, got %v", e, a)
   481  			}
   482  
   483  			if e, a := c.ExpectSigningNameDerived, actual.SigningNameDerived; e != a {
   484  				t.Errorf("expect %v, got %v", e, a)
   485  			}
   486  
   487  			if e, a := c.ExpectSigningRegion, actual.SigningRegion; e != a {
   488  				t.Errorf("expect %v, got %v", e, a)
   489  			}
   490  
   491  		})
   492  	}
   493  }
   494  
   495  func TestEndpointFor_S3UsEast1RegionalFlag(t *testing.T) {
   496  
   497  	// mock S3 regional endpoints model
   498  	mockS3ModelPartition := partition{
   499  		ID:        "aws",
   500  		Name:      "AWS Standard",
   501  		DNSSuffix: "amazonaws.com",
   502  		RegionRegex: regionRegex{
   503  			Regexp: func() *regexp.Regexp {
   504  				reg, _ := regexp.Compile("^(us|eu|ap|sa|ca|me)\\-\\w+\\-\\d+$")
   505  				return reg
   506  			}(),
   507  		},
   508  		Defaults: endpoint{
   509  			Hostname:          "{service}.{region}.{dnsSuffix}",
   510  			Protocols:         []string{"https"},
   511  			SignatureVersions: []string{"v4"},
   512  		},
   513  		Regions: regions{
   514  			"ap-east-1": region{
   515  				Description: "Asia Pacific (Hong Kong)",
   516  			},
   517  			"ap-northeast-1": region{
   518  				Description: "Asia Pacific (Tokyo)",
   519  			},
   520  			"ap-northeast-2": region{
   521  				Description: "Asia Pacific (Seoul)",
   522  			},
   523  			"ap-south-1": region{
   524  				Description: "Asia Pacific (Mumbai)",
   525  			},
   526  			"ap-southeast-1": region{
   527  				Description: "Asia Pacific (Singapore)",
   528  			},
   529  			"ap-southeast-2": region{
   530  				Description: "Asia Pacific (Sydney)",
   531  			},
   532  			"ca-central-1": region{
   533  				Description: "Canada (Central)",
   534  			},
   535  			"eu-central-1": region{
   536  				Description: "EU (Frankfurt)",
   537  			},
   538  			"eu-north-1": region{
   539  				Description: "EU (Stockholm)",
   540  			},
   541  			"eu-west-1": region{
   542  				Description: "EU (Ireland)",
   543  			},
   544  			"eu-west-2": region{
   545  				Description: "EU (London)",
   546  			},
   547  			"eu-west-3": region{
   548  				Description: "EU (Paris)",
   549  			},
   550  			"me-south-1": region{
   551  				Description: "Middle East (Bahrain)",
   552  			},
   553  			"sa-east-1": region{
   554  				Description: "South America (Sao Paulo)",
   555  			},
   556  			"us-east-1": region{
   557  				Description: "US East (N. Virginia)",
   558  			},
   559  			"us-east-2": region{
   560  				Description: "US East (Ohio)",
   561  			},
   562  			"us-west-1": region{
   563  				Description: "US West (N. California)",
   564  			},
   565  			"us-west-2": region{
   566  				Description: "US West (Oregon)",
   567  			},
   568  		},
   569  		Services: services{
   570  			"s3": service{
   571  				PartitionEndpoint: "aws-global",
   572  				IsRegionalized:    boxedTrue,
   573  				Defaults: endpoint{
   574  					Protocols:         []string{"http", "https"},
   575  					SignatureVersions: []string{"s3v4"},
   576  
   577  					HasDualStack:      boxedTrue,
   578  					DualStackHostname: "{service}.dualstack.{region}.{dnsSuffix}",
   579  				},
   580  				Endpoints: endpoints{
   581  					"ap-east-1": endpoint{},
   582  					"ap-northeast-1": endpoint{
   583  						Hostname:          "s3.ap-northeast-1.amazonaws.com",
   584  						SignatureVersions: []string{"s3", "s3v4"},
   585  					},
   586  					"ap-northeast-2": endpoint{},
   587  					"ap-northeast-3": endpoint{},
   588  					"ap-south-1":     endpoint{},
   589  					"ap-southeast-1": endpoint{
   590  						Hostname:          "s3.ap-southeast-1.amazonaws.com",
   591  						SignatureVersions: []string{"s3", "s3v4"},
   592  					},
   593  					"ap-southeast-2": endpoint{
   594  						Hostname:          "s3.ap-southeast-2.amazonaws.com",
   595  						SignatureVersions: []string{"s3", "s3v4"},
   596  					},
   597  					"aws-global": endpoint{
   598  						Hostname: "s3.amazonaws.com",
   599  						CredentialScope: credentialScope{
   600  							Region: "us-east-1",
   601  						},
   602  					},
   603  					"ca-central-1": endpoint{},
   604  					"eu-central-1": endpoint{},
   605  					"eu-north-1":   endpoint{},
   606  					"eu-west-1": endpoint{
   607  						Hostname:          "s3.eu-west-1.amazonaws.com",
   608  						SignatureVersions: []string{"s3", "s3v4"},
   609  					},
   610  					"eu-west-2":  endpoint{},
   611  					"eu-west-3":  endpoint{},
   612  					"me-south-1": endpoint{},
   613  					"s3-external-1": endpoint{
   614  						Hostname:          "s3-external-1.amazonaws.com",
   615  						SignatureVersions: []string{"s3", "s3v4"},
   616  						CredentialScope: credentialScope{
   617  							Region: "us-east-1",
   618  						},
   619  					},
   620  					"sa-east-1": endpoint{
   621  						Hostname:          "s3.sa-east-1.amazonaws.com",
   622  						SignatureVersions: []string{"s3", "s3v4"},
   623  					},
   624  					"us-east-1": endpoint{
   625  						Hostname:          "s3.us-east-1.amazonaws.com",
   626  						SignatureVersions: []string{"s3", "s3v4"},
   627  					},
   628  					"us-east-2": endpoint{},
   629  					"us-west-1": endpoint{
   630  						Hostname:          "s3.us-west-1.amazonaws.com",
   631  						SignatureVersions: []string{"s3", "s3v4"},
   632  					},
   633  					"us-west-2": endpoint{
   634  						Hostname:          "s3.us-west-2.amazonaws.com",
   635  						SignatureVersions: []string{"s3", "s3v4"},
   636  					},
   637  				},
   638  			},
   639  		},
   640  	}
   641  
   642  	// resolver for mock S3 regional endpoints model
   643  	resolver := mockS3ModelPartition
   644  
   645  	cases := map[string]struct {
   646  		service, region     string
   647  		regional            S3UsEast1RegionalEndpoint
   648  		ExpectURL           string
   649  		ExpectSigningRegion string
   650  	}{
   651  		// S3 Endpoints resolver tests:
   652  		"s3/us-east-1/regional": {
   653  			service:             "s3",
   654  			region:              "us-east-1",
   655  			regional:            RegionalS3UsEast1Endpoint,
   656  			ExpectURL:           "https://s3.us-east-1.amazonaws.com",
   657  			ExpectSigningRegion: "us-east-1",
   658  		},
   659  		"s3/us-east-1/legacy": {
   660  			service:             "s3",
   661  			region:              "us-east-1",
   662  			ExpectURL:           "https://s3.amazonaws.com",
   663  			ExpectSigningRegion: "us-east-1",
   664  		},
   665  		"s3/us-west-1/regional": {
   666  			service:             "s3",
   667  			region:              "us-west-1",
   668  			regional:            RegionalS3UsEast1Endpoint,
   669  			ExpectURL:           "https://s3.us-west-1.amazonaws.com",
   670  			ExpectSigningRegion: "us-west-1",
   671  		},
   672  		"s3/us-west-1/legacy": {
   673  			service:             "s3",
   674  			region:              "us-west-1",
   675  			regional:            RegionalS3UsEast1Endpoint,
   676  			ExpectURL:           "https://s3.us-west-1.amazonaws.com",
   677  			ExpectSigningRegion: "us-west-1",
   678  		},
   679  	}
   680  
   681  	for name, c := range cases {
   682  		t.Run(name, func(t *testing.T) {
   683  			var optionSlice []func(o *Options)
   684  			optionSlice = append(optionSlice, func(o *Options) {
   685  				o.S3UsEast1RegionalEndpoint = c.regional
   686  			})
   687  
   688  			actual, err := resolver.EndpointFor(c.service, c.region, optionSlice...)
   689  			if err != nil {
   690  				t.Fatalf("failed to resolve endpoint, %v", err)
   691  			}
   692  
   693  			if e, a := c.ExpectURL, actual.URL; e != a {
   694  				t.Errorf("expect %v, got %v", e, a)
   695  			}
   696  
   697  			if e, a := c.ExpectSigningRegion, actual.SigningRegion; e != a {
   698  				t.Errorf("expect %v, got %v", e, a)
   699  			}
   700  
   701  		})
   702  	}
   703  }
   704  
   705  func TestSTSRegionalEndpoint_CNPartition(t *testing.T) {
   706  	mockSTSCNPartition := partition{
   707  		ID:        "aws-cn",
   708  		Name:      "AWS China",
   709  		DNSSuffix: "amazonaws.com.cn",
   710  		RegionRegex: regionRegex{
   711  			Regexp: func() *regexp.Regexp {
   712  				reg, _ := regexp.Compile("^cn\\-\\w+\\-\\d+$")
   713  				return reg
   714  			}(),
   715  		},
   716  		Defaults: endpoint{
   717  			Hostname:          "{service}.{region}.{dnsSuffix}",
   718  			Protocols:         []string{"https"},
   719  			SignatureVersions: []string{"v4"},
   720  		},
   721  		Regions: regions{
   722  			"cn-north-1": region{
   723  				Description: "China (Beijing)",
   724  			},
   725  			"cn-northwest-1": region{
   726  				Description: "China (Ningxia)",
   727  			},
   728  		},
   729  		Services: services{
   730  			"sts": service{
   731  				Endpoints: endpoints{
   732  					"cn-north-1":     endpoint{},
   733  					"cn-northwest-1": endpoint{},
   734  				},
   735  			},
   736  		},
   737  	}
   738  
   739  	resolver := mockSTSCNPartition
   740  
   741  	cases := map[string]struct {
   742  		service, region                                     string
   743  		regional                                            bool
   744  		ExpectURL, ExpectSigningMethod, ExpectSigningRegion string
   745  		ExpectSigningNameDerived                            bool
   746  	}{
   747  		"sts/cn-north-1/regional": {
   748  			service:                  "sts",
   749  			region:                   "cn-north-1",
   750  			regional:                 true,
   751  			ExpectURL:                "https://sts.cn-north-1.amazonaws.com.cn",
   752  			ExpectSigningMethod:      "v4",
   753  			ExpectSigningNameDerived: true,
   754  			ExpectSigningRegion:      "cn-north-1",
   755  		},
   756  		"sts/cn-north-1/legacy": {
   757  			service:                  "sts",
   758  			region:                   "cn-north-1",
   759  			regional:                 false,
   760  			ExpectURL:                "https://sts.cn-north-1.amazonaws.com.cn",
   761  			ExpectSigningMethod:      "v4",
   762  			ExpectSigningNameDerived: true,
   763  			ExpectSigningRegion:      "cn-north-1",
   764  		},
   765  	}
   766  
   767  	for name, c := range cases {
   768  		var optionSlice []func(o *Options)
   769  		t.Run(name, func(t *testing.T) {
   770  			if c.regional {
   771  				optionSlice = append(optionSlice, STSRegionalEndpointOption)
   772  			}
   773  			actual, err := resolver.EndpointFor(c.service, c.region, optionSlice...)
   774  			if err != nil {
   775  				t.Fatalf("failed to resolve endpoint, %v", err)
   776  			}
   777  
   778  			if e, a := c.ExpectURL, actual.URL; e != a {
   779  				t.Errorf("expect %v, got %v", e, a)
   780  			}
   781  			if e, a := c.ExpectSigningMethod, actual.SigningMethod; e != a {
   782  				t.Errorf("expect %v, got %v", e, a)
   783  			}
   784  			if e, a := c.ExpectSigningNameDerived, actual.SigningNameDerived; e != a {
   785  				t.Errorf("expect %v, got %v", e, a)
   786  			}
   787  			if e, a := c.ExpectSigningRegion, actual.SigningRegion; e != a {
   788  				t.Errorf("expect %v, got %v", e, a)
   789  			}
   790  		})
   791  	}
   792  
   793  }