From 8daf736c1b7694930d2700388213c5391e793b56 Mon Sep 17 00:00:00 2001 From: Rob Pearson Date: Sat, 20 May 2017 21:40:10 +1000 Subject: [PATCH 1/5] RandomString() is now using a static Random object. --- .../Fixtures/Util/AesEncryptionFixture.cs | 23 +++++++++++++++++++ source/Calamari/Util/AesEncryption.cs | 5 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs diff --git a/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs b/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs new file mode 100644 index 000000000..5f71f7f55 --- /dev/null +++ b/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs @@ -0,0 +1,23 @@ +using System; +using System.Globalization; +using System.IO; +using System.Xml.Linq; +using Calamari.Util; +using NUnit.Framework; +using Octostache; + +namespace Calamari.Tests.Fixtures.Deployment +{ + [TestFixture] + public class AesEncryptionFixture + { + [Test] + public void CanGetRandomStrings() + { + var randomString1 = AesEncryption.RandomString(8); + var randomString2 = AesEncryption.RandomString(8); + + Assert.That(randomString1, Is.Not.EqualTo(randomString2)); + } + } +} \ No newline at end of file diff --git a/source/Calamari/Util/AesEncryption.cs b/source/Calamari/Util/AesEncryption.cs index 83fab5294..79e679ca7 100644 --- a/source/Calamari/Util/AesEncryption.cs +++ b/source/Calamari/Util/AesEncryption.cs @@ -12,6 +12,8 @@ public class AesEncryption static readonly byte[] PasswordPaddingSalt = Encoding.UTF8.GetBytes("Octopuss"); static readonly byte[] IvPrefix = Encoding.UTF8.GetBytes("IV__"); + static readonly Random RandomGenerator = new Random(); + readonly byte[] key; public AesEncryption(string password) { @@ -98,10 +100,9 @@ public static byte[] GetEncryptionKey(string encryptionPassword) public static string RandomString(int length) { const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; - var random = new Random(); return new string( Enumerable.Repeat(chars, length) - .Select(s => s[random.Next(s.Length)]) + .Select(s => s[RandomGenerator.Next(s.Length)]) .ToArray()); } } From b1f7f6414cedd02dce8db7cb3e8e9cad26e50c98 Mon Sep 17 00:00:00 2001 From: Rob Pearson Date: Mon, 29 May 2017 13:55:58 +1000 Subject: [PATCH 2/5] Replaced RandomString() w/ Guid.NewGuid() to ensure uniqueness. --- .../Conventions/DeployAzureResourceGroupConvention.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/Calamari.Azure/Deployment/Conventions/DeployAzureResourceGroupConvention.cs b/source/Calamari.Azure/Deployment/Conventions/DeployAzureResourceGroupConvention.cs index 723698fd3..3302bae14 100644 --- a/source/Calamari.Azure/Deployment/Conventions/DeployAzureResourceGroupConvention.cs +++ b/source/Calamari.Azure/Deployment/Conventions/DeployAzureResourceGroupConvention.cs @@ -88,7 +88,7 @@ static string GenerateDeploymentNameFromStepName(string stepName) new string(deploymentName.Select(x => (char.IsLetterOrDigit(x) || x == '-') ? x : '-').ToArray()); deploymentName = Regex.Replace(deploymentName, "-+", "-"); deploymentName = deploymentName.Trim('-', '/'); - deploymentName = deploymentName + "-" + AesEncryption.RandomString(6); + deploymentName = deploymentName + "-" + Guid.NewGuid().ToString(); return deploymentName; } From 3f8fda5c66f63e491da51d0f8058a5d9c1684a83 Mon Sep 17 00:00:00 2001 From: Rob Pearson Date: Mon, 29 May 2017 14:57:04 +1000 Subject: [PATCH 3/5] Removed RandomString() and associated fixture. No longer needed. --- .../Fixtures/Util/AesEncryptionFixture.cs | 23 ------------------- source/Calamari/Util/AesEncryption.cs | 11 --------- 2 files changed, 34 deletions(-) delete mode 100644 source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs diff --git a/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs b/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs deleted file mode 100644 index 5f71f7f55..000000000 --- a/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Xml.Linq; -using Calamari.Util; -using NUnit.Framework; -using Octostache; - -namespace Calamari.Tests.Fixtures.Deployment -{ - [TestFixture] - public class AesEncryptionFixture - { - [Test] - public void CanGetRandomStrings() - { - var randomString1 = AesEncryption.RandomString(8); - var randomString2 = AesEncryption.RandomString(8); - - Assert.That(randomString1, Is.Not.EqualTo(randomString2)); - } - } -} \ No newline at end of file diff --git a/source/Calamari/Util/AesEncryption.cs b/source/Calamari/Util/AesEncryption.cs index 79e679ca7..28bea3181 100644 --- a/source/Calamari/Util/AesEncryption.cs +++ b/source/Calamari/Util/AesEncryption.cs @@ -12,8 +12,6 @@ public class AesEncryption static readonly byte[] PasswordPaddingSalt = Encoding.UTF8.GetBytes("Octopuss"); static readonly byte[] IvPrefix = Encoding.UTF8.GetBytes("IV__"); - static readonly Random RandomGenerator = new Random(); - readonly byte[] key; public AesEncryption(string password) { @@ -96,14 +94,5 @@ public static byte[] GetEncryptionKey(string encryptionPassword) var passwordGenerator = new Rfc2898DeriveBytes(encryptionPassword, PasswordPaddingSalt, PasswordSaltIterations); return passwordGenerator.GetBytes(16); } - - public static string RandomString(int length) - { - const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; - return new string( - Enumerable.Repeat(chars, length) - .Select(s => s[RandomGenerator.Next(s.Length)]) - .ToArray()); - } } } From 8bf5471dea6faa721a6197b90675fe45c623deb4 Mon Sep 17 00:00:00 2001 From: Rob Pearson Date: Mon, 29 May 2017 15:04:00 +1000 Subject: [PATCH 4/5] Reverted RandomString() removal. It's still needed. --- .../Fixtures/Util/AesEncryptionFixture.cs | 23 +++++++++++++++++++ source/Calamari/Util/AesEncryption.cs | 11 +++++++++ 2 files changed, 34 insertions(+) create mode 100644 source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs diff --git a/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs b/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs new file mode 100644 index 000000000..5f71f7f55 --- /dev/null +++ b/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs @@ -0,0 +1,23 @@ +using System; +using System.Globalization; +using System.IO; +using System.Xml.Linq; +using Calamari.Util; +using NUnit.Framework; +using Octostache; + +namespace Calamari.Tests.Fixtures.Deployment +{ + [TestFixture] + public class AesEncryptionFixture + { + [Test] + public void CanGetRandomStrings() + { + var randomString1 = AesEncryption.RandomString(8); + var randomString2 = AesEncryption.RandomString(8); + + Assert.That(randomString1, Is.Not.EqualTo(randomString2)); + } + } +} \ No newline at end of file diff --git a/source/Calamari/Util/AesEncryption.cs b/source/Calamari/Util/AesEncryption.cs index 28bea3181..79e679ca7 100644 --- a/source/Calamari/Util/AesEncryption.cs +++ b/source/Calamari/Util/AesEncryption.cs @@ -12,6 +12,8 @@ public class AesEncryption static readonly byte[] PasswordPaddingSalt = Encoding.UTF8.GetBytes("Octopuss"); static readonly byte[] IvPrefix = Encoding.UTF8.GetBytes("IV__"); + static readonly Random RandomGenerator = new Random(); + readonly byte[] key; public AesEncryption(string password) { @@ -94,5 +96,14 @@ public static byte[] GetEncryptionKey(string encryptionPassword) var passwordGenerator = new Rfc2898DeriveBytes(encryptionPassword, PasswordPaddingSalt, PasswordSaltIterations); return passwordGenerator.GetBytes(16); } + + public static string RandomString(int length) + { + const string chars = "abcdefghijklmnopqrstuvwxyz0123456789"; + return new string( + Enumerable.Repeat(chars, length) + .Select(s => s[RandomGenerator.Next(s.Length)]) + .ToArray()); + } } } From b458d61b2cf204ff699b90abaf7c1570a4482c1c Mon Sep 17 00:00:00 2001 From: Rob Pearson Date: Tue, 30 May 2017 10:56:41 +1000 Subject: [PATCH 5/5] Removed RandomString() test as it's not repeatable. --- .../Fixtures/Util/AesEncryptionFixture.cs | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs diff --git a/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs b/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs deleted file mode 100644 index 5f71f7f55..000000000 --- a/source/Calamari.Tests/Fixtures/Util/AesEncryptionFixture.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Globalization; -using System.IO; -using System.Xml.Linq; -using Calamari.Util; -using NUnit.Framework; -using Octostache; - -namespace Calamari.Tests.Fixtures.Deployment -{ - [TestFixture] - public class AesEncryptionFixture - { - [Test] - public void CanGetRandomStrings() - { - var randomString1 = AesEncryption.RandomString(8); - var randomString2 = AesEncryption.RandomString(8); - - Assert.That(randomString1, Is.Not.EqualTo(randomString2)); - } - } -} \ No newline at end of file