@@ -841,14 +841,19 @@ source_profile = SourceSharedCredentials
841841 },
842842 {
843843 Config : & Config {
844- Region : "us-east-1" ,
845- SkipEC2MetadataApiCheck : true ,
844+ Region : "us-east-1" ,
845+ EC2MetadataServiceEnableState : imds . ClientDisabled ,
846846 },
847847 Description : "skip EC2 Metadata API check" ,
848848 ExpectedError : func (err error ) bool {
849849 return IsNoValidCredentialSourcesError (err )
850850 },
851851 ExpectedRegion : "us-east-1" ,
852+ // The IMDS server must be enabled so that auth will succeed if the IMDS is called
853+ EnableEc2MetadataServer : true ,
854+ MockStsEndpoints : []* servicemocks.MockEndpoint {
855+ servicemocks .MockStsGetCallerIdentityValidEndpoint ,
856+ },
852857 },
853858 {
854859 Config : & Config {
@@ -1847,6 +1852,130 @@ use_fips_endpoint = true
18471852 }
18481853}
18491854
1855+ func TestEC2MetadataServiceClientEnableState (t * testing.T ) {
1856+ testCases := map [string ]struct {
1857+ Config * Config
1858+ EnvironmentVariables map [string ]string
1859+ SharedConfigurationFile string
1860+ ExpectedEC2MetadataServiceClientEnableState imds.ClientEnableState
1861+ }{
1862+ "no configuration" : {
1863+ Config : & Config {
1864+ AccessKey : servicemocks .MockStaticAccessKey ,
1865+ SecretKey : servicemocks .MockStaticSecretKey ,
1866+ },
1867+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDefaultEnableState ,
1868+ },
1869+
1870+ "config enabled" : {
1871+ Config : & Config {
1872+ AccessKey : servicemocks .MockStaticAccessKey ,
1873+ SecretKey : servicemocks .MockStaticSecretKey ,
1874+ EC2MetadataServiceEnableState : imds .ClientEnabled ,
1875+ },
1876+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientEnabled ,
1877+ },
1878+ "config disabled" : {
1879+ Config : & Config {
1880+ AccessKey : servicemocks .MockStaticAccessKey ,
1881+ SecretKey : servicemocks .MockStaticSecretKey ,
1882+ EC2MetadataServiceEnableState : imds .ClientDisabled ,
1883+ },
1884+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDisabled ,
1885+ },
1886+
1887+ "envvar true" : {
1888+ Config : & Config {
1889+ AccessKey : servicemocks .MockStaticAccessKey ,
1890+ SecretKey : servicemocks .MockStaticSecretKey ,
1891+ },
1892+ EnvironmentVariables : map [string ]string {
1893+ "AWS_EC2_METADATA_DISABLED" : "true" ,
1894+ },
1895+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDisabled ,
1896+ },
1897+ "envvar false" : {
1898+ Config : & Config {
1899+ AccessKey : servicemocks .MockStaticAccessKey ,
1900+ SecretKey : servicemocks .MockStaticSecretKey ,
1901+ },
1902+ EnvironmentVariables : map [string ]string {
1903+ "AWS_EC2_METADATA_DISABLED" : "false" ,
1904+ },
1905+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientEnabled ,
1906+ },
1907+
1908+ "config enabled envvar true" : {
1909+ Config : & Config {
1910+ AccessKey : servicemocks .MockStaticAccessKey ,
1911+ SecretKey : servicemocks .MockStaticSecretKey ,
1912+ EC2MetadataServiceEnableState : imds .ClientEnabled ,
1913+ },
1914+ EnvironmentVariables : map [string ]string {
1915+ "AWS_EC2_METADATA_DISABLED" : "true" ,
1916+ },
1917+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientEnabled ,
1918+ },
1919+ "config disabled envvar false" : {
1920+ Config : & Config {
1921+ AccessKey : servicemocks .MockStaticAccessKey ,
1922+ SecretKey : servicemocks .MockStaticSecretKey ,
1923+ EC2MetadataServiceEnableState : imds .ClientDisabled ,
1924+ },
1925+ EnvironmentVariables : map [string ]string {
1926+ "AWS_EC2_METADATA_DISABLED" : "false" ,
1927+ },
1928+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDisabled ,
1929+ },
1930+ }
1931+
1932+ for testName , testCase := range testCases {
1933+ testCase := testCase
1934+
1935+ t .Run (testName , func (t * testing.T ) {
1936+ oldEnv := servicemocks .InitSessionTestEnv ()
1937+ defer servicemocks .PopEnv (oldEnv )
1938+
1939+ for k , v := range testCase .EnvironmentVariables {
1940+ os .Setenv (k , v )
1941+ }
1942+
1943+ if testCase .SharedConfigurationFile != "" {
1944+ file , err := ioutil .TempFile ("" , "aws-sdk-go-base-shared-configuration-file" )
1945+
1946+ if err != nil {
1947+ t .Fatalf ("unexpected error creating temporary shared configuration file: %s" , err )
1948+ }
1949+
1950+ defer os .Remove (file .Name ())
1951+
1952+ err = ioutil .WriteFile (file .Name (), []byte (testCase .SharedConfigurationFile ), 0600 )
1953+
1954+ if err != nil {
1955+ t .Fatalf ("unexpected error writing shared configuration file: %s" , err )
1956+ }
1957+
1958+ testCase .Config .SharedConfigFiles = []string {file .Name ()}
1959+ }
1960+
1961+ testCase .Config .SkipCredsValidation = true
1962+
1963+ awsConfig , err := GetAwsConfig (context .Background (), testCase .Config )
1964+ if err != nil {
1965+ t .Fatalf ("error in GetAwsConfig() '%[1]T': %[1]s" , err )
1966+ }
1967+
1968+ ec2MetadataServiceClientEnableState , _ , err := awsconfig .ResolveEC2IMDSClientEnableState (awsConfig .ConfigSources )
1969+ if err != nil {
1970+ t .Fatalf ("error in ResolveEC2IMDSClientEnableState: %s" , err )
1971+ }
1972+ if a , e := ec2MetadataServiceClientEnableState , testCase .ExpectedEC2MetadataServiceClientEnableState ; a != e {
1973+ t .Errorf ("expected EC2MetadataServiceClientEnableState %q, got: %q" , awsconfig .EC2IMDSClientEnableStateString (e ), awsconfig .EC2IMDSClientEnableStateString (a ))
1974+ }
1975+ })
1976+ }
1977+ }
1978+
18501979func TestEC2MetadataServiceEndpoint (t * testing.T ) {
18511980 testCases := map [string ]struct {
18521981 Config * Config
0 commit comments