@Override protected void onProfileEndingLine(String prevProfileName) { // Check whether we need to insert new properties into this profile Profile modifiedProfile = newProfiles.get(prevProfileName); if (modifiedProfile != null) { for (Entry<String, String> entry : modifiedProfile.getProperties().entrySet()) { String propertyKey = entry.getKey(); String propertyValue = entry.getValue(); if ( !existingProfileProperties.get(prevProfileName).contains(propertyKey) ) { writeProperty(propertyKey, propertyValue); } } } // flush all the buffered comments and empty lines flush(); }
@Override protected void onEndOfFile() { // Append profiles that don't exist in the original file for (Entry<String, Profile> entry : newProfiles.entrySet()) { String profileName = entry.getKey(); Profile profile = entry.getValue(); if ( !existingProfileProperties.containsKey(profileName) ) { // The profile name is not found in the file // Append the profile properties writeProfile(profile); writeLine(""); } } // Flush the "real" writer try { writer.flush(); } catch (IOException ioe) { throw new SdkClientException( "Unable to write to the target file to persist the profile credentials.", ioe); } }
@Test public void testDumpToFile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); checkCredentialsFile(tmpFile, abcd); // Rewrite the file with overwrite=true Profile[] a = { new Profile("a", basicCredA) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, a); checkCredentialsFile(tmpFile, a); // Rewrite the file with overwrite=false is not allowed try { ProfilesConfigFileWriter.dumpToFile(tmpFile, false, new Profile("a", basicCredA)); fail("Should have thrown exception since the destination file already exists."); } catch (AmazonClientException expected) {} }
@Test public void testModifyProfile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); // a <==> c, b <==> d Profile[] modified = { new Profile("a", sessionCredC), new Profile("b", sessionCredD), new Profile("c", basicCredA), new Profile("d", basicCredB) }; ProfilesConfigFileWriter.modifyOrInsertProfiles(tmpFile, modified); checkCredentialsFile(tmpFile, modified); }
@Test public void testInsertProfile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); // Insert [e] profile Profile e = new Profile("e", basicCredA); ProfilesConfigFileWriter.modifyOrInsertProfiles(tmpFile, e); checkCredentialsFile(tmpFile, abcd[0], abcd[1], abcd[2], abcd[3], e); }
@Test public void testModifyAndInsertProfile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); // a <==> c, b <==> d, +e Profile[] modified = { new Profile("a", sessionCredC), new Profile("b", sessionCredD), new Profile("c", basicCredA), new Profile("d", basicCredB), new Profile("e", basicCredA) }; ProfilesConfigFileWriter.modifyOrInsertProfiles(tmpFile, modified); checkCredentialsFile(tmpFile, modified); }
@Test public void testRenameProfile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); // Rename a to A Profile[] modified = { new Profile("A", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.modifyOneProfile(tmpFile, "a", new Profile("A", basicCredA)); checkCredentialsFile(tmpFile, modified); }
@Test public void testDeleteProfile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); // Delete a and c Profile[] modified = { new Profile("b", basicCredB), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.deleteProfiles(tmpFile, "a", "c"); checkCredentialsFile(tmpFile, modified); }
/** * Tests that the original credentials file is properly restored if the * in-place modification fails with error. */ @Test public void testInPlaceModificationErrorHandling() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); String originalContent = FileUtils.readFileToString(tmpFile); // Insert [e] profile, which throws RuntimeException when the getProperties method is called. Profile e = new ProfileWithException("e", basicCredA); try { ProfilesConfigFileWriter.modifyOrInsertProfiles(tmpFile, e); fail("An exception is expected."); } catch(AmazonClientException expected) {} // Check that the original file is restored assertTrue(tmpFile.exists()); String restoredContent = FileUtils.readFileToString(tmpFile); assertEquals(originalContent, restoredContent); }
@Override protected void configure() { super.configure(); Map<String, Profile> allProfiles = configFile.getAllProfiles(); for (Map.Entry<String, Profile> entry : allProfiles.entrySet()) { String name = entry.getKey(); Profile profile = entry.getValue(); final AWSCredentials profileCredentials = profile.getCredentials(); bind(Key.get(AWSCredentials.class, new ProfileImpl(name))).toProvider( new Provider<AWSCredentials>() { @Override public AWSCredentials get() { return profileCredentials; } }); } }
@Deprecated public Map<String, Profile> getAllProfiles() { Map<String, Profile> legacyProfiles = new HashMap<String, Profile>(); for (Map.Entry<String, BasicProfile> entry : getAllBasicProfiles().entrySet()) { final String profileName = entry.getKey(); legacyProfiles.put(profileName, new Profile(profileName, entry.getValue().getProperties(), new StaticCredentialsProvider( getCredentials(profileName)))); } return legacyProfiles; }
/** * Creates ProfilesConfigFileWriterHelper with the specified new * profiles. * * @param writer * The writer where the modified content is output to. * @param modifications * A map of all the new profiles, keyed by the profile name. * If a profile name is associated with a null value, it's * profile content will be removed. */ public ProfilesConfigFileWriterHelper(Writer writer, Map<String, Profile> modifications) { this.writer = writer; for (Entry<String, Profile> entry : modifications.entrySet()) { String profileName = entry.getKey(); Profile profile = entry.getValue(); if (profile == null) { deletedProfiles.add(profileName); } else { newProfiles.put(profileName, profile); } } }
private void writeProfile(Profile profile) { writeProfileName(profile.getProfileName()); for (Entry<String, String> entry : profile.getProperties().entrySet()) { writeProperty(entry.getKey(), entry.getValue()); } }
@Test public void testUpdate() throws Exception { ProfilesConfigFile fixture = new ProfilesConfigFile( ProfileResourceLoader.basicProfile().asFile()); File modifiable = File.createTempFile("UpdatableProfile", ".tst"); ProfilesConfigFileWriter.dumpToFile(modifiable, true, fixture.getAllProfiles().values() .toArray(new Profile[1])); ProfileCredentialsProvider test = new ProfileCredentialsProvider(modifiable.getPath(), null); AWSCredentials orig = test.getCredentials(); Assert.assertEquals("defaultAccessKey", orig.getAWSAccessKeyId()); Assert.assertEquals("defaultSecretAccessKey", orig.getAWSSecretKey()); //Sleep to ensure that the timestamp on the file (when we modify it) is //distinguishably later from the original write. try { Thread.sleep(2000); } catch (Exception e) { } Profile newProfile = new Profile(DEFAULT_PROFILE_NAME, new BasicAWSCredentials("newAccessKey", "newSecretKey")); ProfilesConfigFileWriter.modifyOneProfile(modifiable, DEFAULT_PROFILE_NAME, newProfile); test.refresh(); AWSCredentials updated = test.getCredentials(); Assert.assertEquals("newAccessKey", updated.getAWSAccessKeyId()); Assert.assertEquals("newSecretKey", updated.getAWSSecretKey()); }
@Test public void testForcedRefresh() throws Exception { ProfilesConfigFile profilesConfigFileBeforeRefresh = new ProfilesConfigFile( ProfileResourceLoader.basicProfile().asFile()); File profilesFile = File.createTempFile("UpdatableProfile", ".tst"); ProfilesConfigFileWriter.dumpToFile(profilesFile, true, profilesConfigFileBeforeRefresh.getAllProfiles() .values().toArray(new Profile[1])); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider( profilesFile.getPath(), null); /* * Sleep for 1 second so that the profiles file last modified time has a chance to update. * If this wait is not here, com.amazonaws.auth.profile.ProfilesConfigFile.refresh() profileFile.lastModified() will not be updated, therefore the * credentials will not refresh. * * This is also in testRefresh() */ Thread.sleep(1000); ProfilesConfigFile profilesConfigFileAfterRefresh = new ProfilesConfigFile( ProfileResourceLoader.basicProfile2().asFile()); ProfilesConfigFileWriter.dumpToFile(profilesFile, true, profilesConfigFileAfterRefresh.getAllProfiles().values() .toArray(new Profile[1])); profileCredentialsProvider.setRefreshForceIntervalNanos(1l); AWSCredentials credentialsAfterRefresh = profileCredentialsProvider.getCredentials(); Assert.assertEquals("credentialsAfterRefresh AWSAccessKeyId", "accessKey2", credentialsAfterRefresh.getAWSAccessKeyId()); Assert.assertEquals("credentialsAfterRefresh AWSSecretKey", "secretAccessKey2", credentialsAfterRefresh.getAWSSecretKey()); }
@Test public void testRefresh() throws Exception { ProfilesConfigFile profilesConfigFileBeforeRefresh = new ProfilesConfigFile( ProfileResourceLoader.basicProfile().asFile()); File profilesFile = File.createTempFile("UpdatableProfile", ".tst"); ProfilesConfigFileWriter.dumpToFile(profilesFile, true, profilesConfigFileBeforeRefresh.getAllProfiles() .values().toArray(new Profile[1])); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider( profilesFile.getPath(), null); Thread.sleep(1000); // see testForcedRefresh() ProfilesConfigFile profilesConfigFileAfterRefresh = new ProfilesConfigFile( ProfileResourceLoader.basicProfile2().asFile()); ProfilesConfigFileWriter.dumpToFile(profilesFile, true, profilesConfigFileAfterRefresh.getAllProfiles().values() .toArray(new Profile[1])); profileCredentialsProvider.setRefreshIntervalNanos(1l); AWSCredentials credentialsAfterRefresh = profileCredentialsProvider.getCredentials(); Assert.assertEquals("credentialsAfterRefresh AWSAccessKeyId", "accessKey2", credentialsAfterRefresh.getAWSAccessKeyId()); Assert.assertEquals("credentialsAfterRefresh AWSSecretKey", "secretAccessKey2", credentialsAfterRefresh.getAWSSecretKey()); }
/** * Loads the given credentials file and checks that it contains the same * set of profiles as expected. */ private static void checkCredentialsFile(File file, Profile... expectedProfiles) { ProfilesConfigFile parsedFile = new ProfilesConfigFile(file); Map<String, Profile> loadedProfiles = parsedFile.getAllProfiles(); assertTrue(expectedProfiles.length == loadedProfiles.size()); for (Profile expectedProfile : expectedProfiles) { Profile loadedProfile = loadedProfiles.get(expectedProfile.getProfileName()); assertEqualProfiles(expectedProfile, loadedProfile); } }
/** * Test verifying we pick up a change to a file. */ @Test public void testReadUpdatedProfile() throws URISyntaxException, IOException { ProfilesConfigFile fixture = new ProfilesConfigFile( ProfileResourceLoader.basicProfile().asFile()); File modifiable = File.createTempFile("UpdatableProfile", ".tst"); ProfilesConfigFileWriter.dumpToFile(modifiable, true, fixture.getAllProfiles().values() .toArray(new Profile[1])); ProfilesConfigFile test = new ProfilesConfigFile(modifiable); AWSCredentials orig = test.getCredentials(DEFAULT_PROFILE_NAME); assertEquals("defaultAccessKey", orig.getAWSAccessKeyId()); assertEquals("defaultSecretAccessKey", orig.getAWSSecretKey()); //Sleep to ensure that the timestamp on the file (when we modify it) is //distinguishably later from the original write. try { Thread.sleep(2000); } catch (Exception e) { } Profile newProfile = new Profile(DEFAULT_PROFILE_NAME, new BasicAWSCredentials("newAccessKey", "newSecretKey")); ProfilesConfigFileWriter.modifyOneProfile(modifiable, DEFAULT_PROFILE_NAME, newProfile); test.refresh(); AWSCredentials updated = test.getCredentials(DEFAULT_PROFILE_NAME); assertEquals("newAccessKey", updated.getAWSAccessKeyId()); assertEquals("newSecretKey", updated.getAWSSecretKey()); }
/** * Tests that comments and unsupported properties are preserved after * profile modification. * @throws URISyntaxException */ @Test public void testModifyAndInsertProfile_WithComments() throws IOException, URISyntaxException { File credWithComments = ProfileResourceLoader.profilesWithComments().asFile(); File tmpFile = copyToTempFile(credWithComments); String originalContent = FileUtils.readFileToString(tmpFile); Profile[] expected = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; // a <==> b, c <==> d, also renaming them to uppercase letters Profile[] modified = { new Profile("A", basicCredB), new Profile("B", basicCredA), new Profile("C", sessionCredD), new Profile("D", sessionCredC) }; ProfilesConfigFileWriter.modifyProfiles(tmpFile, ImmutableMapParameter .of("a", modified[0], "b", modified[1], "c", modified[2], "d", modified[3])); checkCredentialsFile(tmpFile, modified); // Sanity check that the content is altered String modifiedContent = FileUtils.readFileToString(tmpFile); assertFalse(originalContent.equals(modifiedContent)); // Restore the properties ProfilesConfigFileWriter.modifyProfiles(tmpFile, ImmutableMapParameter .of("A", expected[0], "B", expected[1], "C", expected[2], "D", expected[3])); checkCredentialsFile(tmpFile, expected); // Check that the content is now the same as the original String restoredContent = FileUtils.readFileToString(tmpFile); assertEquals(originalContent, restoredContent); }
private static void assertEqualProfiles(Profile expected, Profile actual) { assertEquals(expected.getProfileName(), actual.getProfileName()); assertEqualCredentials(expected.getCredentials(), actual.getCredentials()); }
/** * Modify or insert new profiles into an existing credentials file by * in-place modification. Only the properties of the affected profiles will * be modified; all the unaffected profiles and comment lines will remain * the same. This method does not support renaming a profile. * * @param destination * The destination file to modify * @param profiles * All the credential profiles to be written. */ public static void modifyOrInsertProfiles(File destination, Profile... profiles) { final Map<String, Profile> modifications = new LinkedHashMap<String, Profile>(); for (Profile profile : profiles) { modifications.put(profile.getProfileName(), profile); } modifyProfiles(destination, modifications); }
/** * Remove one or more profiles from the existing credentials file by * in-place modification. * * @param destination * The destination file to modify * @param profileNames * The names of all the profiles to be deleted. */ public static void deleteProfiles(File destination, String... profileNames) { final Map<String, Profile> modifications = new LinkedHashMap<String, Profile>(); for (String profileName : profileNames) { modifications.put(profileName, null); // null value indicates a deletion } modifyProfiles(destination, modifications); }
/** * Modify one profile in the existing credentials file by in-place * modification. This method will rename the existing profile if the * specified Profile has a different name. * * @param destination * The destination file to modify * @param profileName * The name of the existing profile to be modified * @param newProfile * The new Profile object. */ public static void modifyOneProfile(File destination, String profileName, Profile newProfile) { final Map<String, Profile> modifications = Collections.singletonMap(profileName, newProfile); modifyProfiles(destination, modifications); }