@@ -804,14 +804,19 @@ source_profile = SourceSharedCredentials
804804 },
805805 {
806806 Config : & Config {
807- Region : "us-east-1" ,
808- SkipEC2MetadataApiCheck : true ,
807+ Region : "us-east-1" ,
808+ EC2MetadataServiceEnableState : imds . ClientDisabled ,
809809 },
810810 Description : "skip EC2 Metadata API check" ,
811811 ExpectedError : func (err error ) bool {
812812 return IsNoValidCredentialSourcesError (err )
813813 },
814814 ExpectedRegion : "us-east-1" ,
815+ // The IMDS server must be enabled so that auth will succeed if the IMDS is called
816+ EnableEc2MetadataServer : true ,
817+ MockStsEndpoints : []* servicemocks.MockEndpoint {
818+ servicemocks .MockStsGetCallerIdentityValidEndpoint ,
819+ },
815820 },
816821 {
817822 Config : & Config {
@@ -1803,6 +1808,130 @@ use_fips_endpoint = true
18031808 }
18041809}
18051810
1811+ func TestEC2MetadataServiceClientEnableState (t * testing.T ) {
1812+ testCases := map [string ]struct {
1813+ Config * Config
1814+ EnvironmentVariables map [string ]string
1815+ SharedConfigurationFile string
1816+ ExpectedEC2MetadataServiceClientEnableState imds.ClientEnableState
1817+ }{
1818+ "no configuration" : {
1819+ Config : & Config {
1820+ AccessKey : servicemocks .MockStaticAccessKey ,
1821+ SecretKey : servicemocks .MockStaticSecretKey ,
1822+ },
1823+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDefaultEnableState ,
1824+ },
1825+
1826+ "config enabled" : {
1827+ Config : & Config {
1828+ AccessKey : servicemocks .MockStaticAccessKey ,
1829+ SecretKey : servicemocks .MockStaticSecretKey ,
1830+ EC2MetadataServiceEnableState : imds .ClientEnabled ,
1831+ },
1832+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientEnabled ,
1833+ },
1834+ "config disabled" : {
1835+ Config : & Config {
1836+ AccessKey : servicemocks .MockStaticAccessKey ,
1837+ SecretKey : servicemocks .MockStaticSecretKey ,
1838+ EC2MetadataServiceEnableState : imds .ClientDisabled ,
1839+ },
1840+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDisabled ,
1841+ },
1842+
1843+ "envvar true" : {
1844+ Config : & Config {
1845+ AccessKey : servicemocks .MockStaticAccessKey ,
1846+ SecretKey : servicemocks .MockStaticSecretKey ,
1847+ },
1848+ EnvironmentVariables : map [string ]string {
1849+ "AWS_EC2_METADATA_DISABLED" : "true" ,
1850+ },
1851+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDisabled ,
1852+ },
1853+ "envvar false" : {
1854+ Config : & Config {
1855+ AccessKey : servicemocks .MockStaticAccessKey ,
1856+ SecretKey : servicemocks .MockStaticSecretKey ,
1857+ },
1858+ EnvironmentVariables : map [string ]string {
1859+ "AWS_EC2_METADATA_DISABLED" : "false" ,
1860+ },
1861+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientEnabled ,
1862+ },
1863+
1864+ "config enabled envvar true" : {
1865+ Config : & Config {
1866+ AccessKey : servicemocks .MockStaticAccessKey ,
1867+ SecretKey : servicemocks .MockStaticSecretKey ,
1868+ EC2MetadataServiceEnableState : imds .ClientEnabled ,
1869+ },
1870+ EnvironmentVariables : map [string ]string {
1871+ "AWS_EC2_METADATA_DISABLED" : "true" ,
1872+ },
1873+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientEnabled ,
1874+ },
1875+ "config disabled envvar false" : {
1876+ Config : & Config {
1877+ AccessKey : servicemocks .MockStaticAccessKey ,
1878+ SecretKey : servicemocks .MockStaticSecretKey ,
1879+ EC2MetadataServiceEnableState : imds .ClientDisabled ,
1880+ },
1881+ EnvironmentVariables : map [string ]string {
1882+ "AWS_EC2_METADATA_DISABLED" : "false" ,
1883+ },
1884+ ExpectedEC2MetadataServiceClientEnableState : imds .ClientDisabled ,
1885+ },
1886+ }
1887+
1888+ for testName , testCase := range testCases {
1889+ testCase := testCase
1890+
1891+ t .Run (testName , func (t * testing.T ) {
1892+ oldEnv := servicemocks .InitSessionTestEnv ()
1893+ defer servicemocks .PopEnv (oldEnv )
1894+
1895+ for k , v := range testCase .EnvironmentVariables {
1896+ os .Setenv (k , v )
1897+ }
1898+
1899+ if testCase .SharedConfigurationFile != "" {
1900+ file , err := ioutil .TempFile ("" , "aws-sdk-go-base-shared-configuration-file" )
1901+
1902+ if err != nil {
1903+ t .Fatalf ("unexpected error creating temporary shared configuration file: %s" , err )
1904+ }
1905+
1906+ defer os .Remove (file .Name ())
1907+
1908+ err = ioutil .WriteFile (file .Name (), []byte (testCase .SharedConfigurationFile ), 0600 )
1909+
1910+ if err != nil {
1911+ t .Fatalf ("unexpected error writing shared configuration file: %s" , err )
1912+ }
1913+
1914+ testCase .Config .SharedConfigFiles = []string {file .Name ()}
1915+ }
1916+
1917+ testCase .Config .SkipCredsValidation = true
1918+
1919+ awsConfig , err := GetAwsConfig (context .Background (), testCase .Config )
1920+ if err != nil {
1921+ t .Fatalf ("error in GetAwsConfig() '%[1]T': %[1]s" , err )
1922+ }
1923+
1924+ ec2MetadataServiceClientEnableState , _ , err := awsconfig .ResolveEC2IMDSClientEnableState (awsConfig .ConfigSources )
1925+ if err != nil {
1926+ t .Fatalf ("error in ResolveEC2IMDSClientEnableState: %s" , err )
1927+ }
1928+ if a , e := ec2MetadataServiceClientEnableState , testCase .ExpectedEC2MetadataServiceClientEnableState ; a != e {
1929+ t .Errorf ("expected EC2MetadataServiceClientEnableState %q, got: %q" , awsconfig .EC2IMDSClientEnableStateString (e ), awsconfig .EC2IMDSClientEnableStateString (a ))
1930+ }
1931+ })
1932+ }
1933+ }
1934+
18061935func TestEC2MetadataServiceEndpoint (t * testing.T ) {
18071936 testCases := map [string ]struct {
18081937 Config * Config
0 commit comments