Skip to content

Commit d59ac28

Browse files
committed
use env raii to fix unit test accidental cred usage
1 parent a0e6914 commit d59ac28

File tree

1 file changed

+56
-61
lines changed

1 file changed

+56
-61
lines changed

tests/aws-cpp-sdk-core-tests/aws/auth/AWSCredentialsProviderTest.cpp

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,6 @@ class EnvironmentModifyingTest : public Aws::Testing::AwsCppSdkGTestSuite
128128

129129
void SetUp()
130130
{
131-
SaveEnvironmentVariable("AWS_SHARED_CREDENTIALS_FILE");
132-
SaveEnvironmentVariable("AWS_CONFIG_FILE");
133-
SaveEnvironmentVariable("AWS_DEFAULT_PROFILE");
134-
SaveEnvironmentVariable("AWS_PROFILE");
135-
SaveEnvironmentVariable("AWS_ACCESS_KEY_ID");
136-
SaveEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
137-
SaveEnvironmentVariable("AWS_EC2_METADATA_DISABLED");
138-
SaveEnvironmentVariable("AWS_ACCOUNT_ID");
139-
140131
Aws::FileSystem::CreateDirectoryIfNotExists(ProfileConfigFileAWSCredentialsProvider::GetProfileDirectory().c_str());
141132
Aws::StringStream ss;
142133
ss << ProfileConfigFileAWSCredentialsProvider::GetCredentialsProfileFilename() << "_blah" << std::this_thread::get_id();
@@ -145,33 +136,18 @@ class EnvironmentModifyingTest : public Aws::Testing::AwsCppSdkGTestSuite
145136

146137
}
147138

148-
void TearDown()
149-
{
150-
RestoreEnvironmentVariable();
151-
}
152-
153-
void SaveEnvironmentVariable(const char* variableName)
154-
{
155-
m_environment.emplace_back(variableName, Aws::Environment::GetEnv(variableName));
156-
}
157-
158-
void RestoreEnvironmentVariable()
159-
{
160-
for(const auto& iter : m_environment)
161-
{
162-
if(iter.second.empty())
163-
{
164-
Aws::Environment::UnSetEnv(iter.first);
165-
}
166-
else
167-
{
168-
Aws::Environment::SetEnv(iter.first, iter.second.c_str(), 1);
169-
}
170-
}
171-
}
172-
173139
Aws::Vector<std::pair<const char*, Aws::String>> m_environment;
174140
Aws::String m_credsFileName;
141+
Aws::Environment::EnvironmentRAII m_saveEnviornment{{
142+
{"AWS_SHARED_CREDENTIALS_FILE", ""},
143+
{"AWS_CONFIG_FILE", ""},
144+
{"AWS_DEFAULT_PROFILE", ""},
145+
{"AWS_PROFILE", ""},
146+
{"AWS_ACCESS_KEY_ID", ""},
147+
{"AWS_SECRET_ACCESS_KEY", ""},
148+
{"AWS_EC2_METADATA_DISABLED", ""},
149+
{"AWS_ACCOUNT_ID", ""},
150+
}};
175151
};
176152

177153
TEST_F(EnvironmentModifyingTest, TestOrderOfAwsDefaultProfileAndAwsProfile)
@@ -195,6 +171,11 @@ TEST_F(EnvironmentModifyingTest, TestOrderOfAwsDefaultProfileAndAwsProfile)
195171

196172
credsFile.close();
197173

174+
Aws::Environment::EnvironmentRAII testEnvironment{{
175+
{"AWS_DEFAULT_PROFILE", ""},
176+
{"AWS_PROFILE", ""},
177+
}};
178+
198179
Aws::Environment::SetEnv("AWS_DEFAULT_PROFILE", "default_profile", 1/*override*/);
199180
Aws::Environment::SetEnv("AWS_PROFILE", "profile", 1/*override*/);
200181

@@ -219,6 +200,9 @@ TEST_F(EnvironmentModifyingTest, TestOrderOfAwsDefaultProfileAndAwsProfile)
219200

220201
TEST_F(EnvironmentModifyingTest, ProfileConfigTestWithEnvVars)
221202
{
203+
Aws::Environment::EnvironmentRAII testEnvironment{{
204+
{"AWS_DEFAULT_PROFILE", ""},
205+
}};
222206
Aws::Environment::SetEnv("AWS_DEFAULT_PROFILE", "someProfile", 1);
223207
Aws::OFStream credsFile(m_credsFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
224208

@@ -238,6 +222,9 @@ TEST_F(EnvironmentModifyingTest, ProfileConfigTestWithEnvVars)
238222

239223
TEST_F(EnvironmentModifyingTest, ProfileConfigTestWithEnvVarsButSpecifiedProfile)
240224
{
225+
Aws::Environment::EnvironmentRAII testEnvironment{{
226+
{"AWS_DEFAULT_PROFILE", ""},
227+
}};
241228
Aws::Environment::SetEnv("AWS_DEFAULT_PROFILE", "someProfile", 1);
242229
Aws::OFStream credsFile(m_credsFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
243230

@@ -262,9 +249,9 @@ TEST_F(EnvironmentModifyingTest, ProfileConfigTestWithEnvVarsButSpecifiedProfile
262249

263250
TEST_F(EnvironmentModifyingTest, ProfileConfigTestNotSetup)
264251
{
265-
Aws::Environment::UnSetEnv("AWS_ACCESS_KEY_ID");
266-
Aws::Environment::UnSetEnv("AWS_SECRET_ACCESS_KEY");
267-
Aws::Environment::UnSetEnv("AWS_SHARED_CREDENTIALS_FILE");
252+
Aws::Environment::EnvironmentRAII testEnvironment{{
253+
{"AWS_DEFAULT_PROFILE", ""},
254+
}};
268255
//On windows we don't redirect the home directory
269256
//This is to prevent when user actually sets .aws/credentials with Keys, this test would fail.
270257
Aws::Environment::SetEnv("AWS_DEFAULT_PROFILE", "SomeUnknownProfileThatDoesNotExist", 1);
@@ -276,6 +263,12 @@ TEST_F(EnvironmentModifyingTest, ProfileConfigTestNotSetup)
276263

277264
TEST_F(EnvironmentModifyingTest, TestEnvironmentVariablesExist)
278265
{
266+
Aws::Environment::EnvironmentRAII testEnvironment{{
267+
{"AWS_ACCESS_KEY_ID", ""},
268+
{"AWS_SECRET_ACCESS_KEY", ""},
269+
{"AWS_SESSION_TOKEN", ""},
270+
{"AWS_ACCOUNT_ID", ""},
271+
}};
279272
Aws::Environment::SetEnv("AWS_ACCESS_KEY_ID", "Access Key", 1);
280273
Aws::Environment::SetEnv("AWS_SECRET_ACCESS_KEY", "Secret Key", 1);
281274
Aws::Environment::SetEnv("AWS_SESSION_TOKEN", "Session Token", 1);
@@ -290,14 +283,38 @@ TEST_F(EnvironmentModifyingTest, TestEnvironmentVariablesExist)
290283

291284
TEST_F(EnvironmentModifyingTest, TestEnvironmentVariablesDoNotExist)
292285
{
293-
Aws::Environment::UnSetEnv("AWS_ACCESS_KEY_ID");
294-
Aws::Environment::UnSetEnv("AWS_SECRET_ACCESS_KEY");
286+
Aws::Environment::EnvironmentRAII testEnvironment{{
287+
{"AWS_ACCESS_KEY_ID", ""},
288+
{"AWS_SECRET_ACCESS_KEY", ""},
289+
}};
295290

296291
EnvironmentAWSCredentialsProvider provider;
297292
ASSERT_EQ("", provider.GetAWSCredentials().GetAWSAccessKeyId());
298293
ASSERT_EQ("", provider.GetAWSCredentials().GetAWSSecretKey());
299294
}
300295

296+
TEST_F(EnvironmentModifyingTest, TestDefaultAWSCredentialsProviderChainWithConfig)
297+
{
298+
// Create a credentials file with a custom profile
299+
Aws::OFStream credsFile(m_credsFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
300+
credsFile << "[custom-profile]" << std::endl;
301+
credsFile << "aws_access_key_id = CustomProfileAccessKey" << std::endl;
302+
credsFile << "aws_secret_access_key = CustomProfileSecretKey" << std::endl;
303+
credsFile.close();
304+
305+
// Create config with custom profile
306+
Aws::Client::ClientConfiguration::CredentialProviderConfiguration config;
307+
config.profile = "custom-profile";
308+
309+
// Test the constructor with config
310+
DefaultAWSCredentialsProviderChain providerChain(config);
311+
312+
// Verify it uses the custom profile
313+
AWSCredentials creds = providerChain.GetAWSCredentials();
314+
EXPECT_STREQ("CustomProfileAccessKey", creds.GetAWSAccessKeyId().c_str());
315+
EXPECT_STREQ("CustomProfileSecretKey", creds.GetAWSSecretKey().c_str());
316+
}
317+
301318
class InstanceProfileCredentialsProviderTest : public Aws::Testing::AwsCppSdkGTestSuite
302319
{
303320
};
@@ -1305,25 +1322,3 @@ TEST_F(AWSCachedCredentialsTest, ShouldCacheCredenitalAsync)
13051322
ASSERT_TRUE(containCredentials(creds, {"and", "no", "surprises"}));
13061323
ASSERT_FALSE(containCredentials(creds, {"a", "quiet", "life"}));
13071324
}
1308-
1309-
TEST_F(EnvironmentModifyingTest, TestDefaultAWSCredentialsProviderChainWithConfig)
1310-
{
1311-
// Create a credentials file with a custom profile
1312-
Aws::OFStream credsFile(m_credsFileName.c_str(), Aws::OFStream::out | Aws::OFStream::trunc);
1313-
credsFile << "[custom-profile]" << std::endl;
1314-
credsFile << "aws_access_key_id = CustomProfileAccessKey" << std::endl;
1315-
credsFile << "aws_secret_access_key = CustomProfileSecretKey" << std::endl;
1316-
credsFile.close();
1317-
1318-
// Create config with custom profile
1319-
Aws::Client::ClientConfiguration::CredentialProviderConfiguration config;
1320-
config.profile = "custom-profile";
1321-
1322-
// Test the constructor with config
1323-
DefaultAWSCredentialsProviderChain providerChain(config);
1324-
1325-
// Verify it uses the custom profile
1326-
AWSCredentials creds = providerChain.GetAWSCredentials();
1327-
EXPECT_STREQ("CustomProfileAccessKey", creds.GetAWSAccessKeyId().c_str());
1328-
EXPECT_STREQ("CustomProfileSecretKey", creds.GetAWSSecretKey().c_str());
1329-
}

0 commit comments

Comments
 (0)