public void FromJsonTest() { var key = Convert.ToBase64String(Encoding.UTF8.GetBytes("123456789012345678901234567890AB")); const string password = "******"; var secretsDataInJson = "{\n" + " \"currentVersion\": 1,\n" + " \"secrets\": [\n" + " {\"secret\": \"" + key + "\", \"version\": 0, \"isForCustomEncryption\": true},\n" + " {\"secret\": \"" + key + "\", \"version\": 1, \"isKey\": true},\n" + " {\"secret\": \"" + password + "\", \"version\": 2, \"isKey\": false},\n" + " ],\n" + "}"; var secretData = SecretsDataGenerator.FromJson(secretsDataInJson); Assert.AreEqual(1, secretData.CurrentSecret.Version); Assert.AreEqual(3, secretData.Secrets.Count); var secretKey0 = secretData.Secrets.Find(secretKey => secretKey.Version == 0); Assert.NotNull(secretKey0); Assert.AreEqual(0, secretKey0.Version); Assert.AreEqual(Convert.FromBase64String(key), secretKey0.GetSecretBytes()); Assert.AreEqual(typeof(CustomEncryptionKey), secretKey0.GetType()); var secretKey1 = secretData.Secrets.Find(secretKey => secretKey.Version == 1); Assert.NotNull(secretKey1); Assert.AreEqual(1, secretKey1.Version); Assert.AreEqual(Convert.FromBase64String(key), secretKey1.GetSecretBytes()); Assert.AreEqual(typeof(EncryptionKey), secretKey1.GetType()); var secretKey2 = secretData.Secrets.Find(secretKey => secretKey.Version == 2); Assert.NotNull(secretKey2); Assert.AreEqual(2, secretKey2.Version); Assert.AreEqual(Encoding.UTF8.GetBytes(password), secretKey2.GetSecretBytes()); Assert.AreEqual(typeof(EncryptionSecret), secretKey2.GetType()); var exception = Assert.Catch <StorageClientException>(() => SecretsDataGenerator.FromJson("{illegalJsonString:")); Assert.AreEqual("JSON with secret data is incorrect", exception.Message); Assert.NotNull(exception.InnerException); }
public void FromJsonNegativeTest() { const string keyNotBase64 = "1234567890ABCDEFGHIJK"; const string secretsDataInJson = "{\n" + " \"currentVersion\": 1,\n" + " \"secrets\": [\n" + " {\"secret\": \"" + keyNotBase64 + "\", \"version\": 1, \"isKey\": true}" + " ],\n" + "}"; var exception = Assert.Throws <StorageClientException>(() => SecretsDataGenerator.FromJson(secretsDataInJson)); Assert.AreEqual("JSON with secret data is incorrect", exception.Message); Assert.IsNotNull(exception.InnerException); Assert.AreEqual( "The input is not a valid Base-64 string as it contains a non-base 64 character, " + "more than two padding characters, or an illegal character among the padding characters.", exception.InnerException.Message); }
public static void StartMigration() { var secretsData = SecretsDataGenerator.FromJson(SecretsDataInJson); var config = new StorageConfig(EnvironmentId, clientId: ClientId, clientSecret: ClientSecret, secretKeyAccessor: () => secretsData); using var storage = Storage.NewStorage(config); const string country = "US"; var migrationComplete = false; while (!migrationComplete) { var migrationResult = storage.MigrateAsync(country, 50).Result; if (migrationResult.TotalLeft == 0) { migrationComplete = true; } } }