Example #1
0
        public void ValidateCipherTest()
        {
            var cipher1 = new CipherStub("cipherStub1");
            var cipher2 = new CipherStub("cipherStub2");
            var cipher3 = new FernetCipher("Fernet cipher");

            using var provider = new CryptoProvider(cipher1);
            provider.RegisterCipher(cipher2)
            .RegisterCipher(cipher3);

            var customEncryptionKey =
                new CustomEncryptionKey(1, Encoding.UTF8.GetBytes("Custom Encryption Key 32 symbols"));

            provider.ValidateCustomCiphers(new SecretsData(new List <Secret> {
                customEncryptionKey
            },
                                                           customEncryptionKey));
            Assert.IsNotNull(provider);

            var encryptionSecret = new EncryptionSecret(2, Encoding.UTF8.GetBytes("EncryptionSecret"));

            provider.ValidateCustomCiphers(new SecretsData(new List <Secret> {
                customEncryptionKey, encryptionSecret
            },
                                                           encryptionSecret));
            Assert.IsNotNull(provider);
        }
Example #2
0
        public void ValidateCipherNegativeTest()
        {
            var cipher1 = new CipherStub("cipherStub1");
            var cipher2 = new WrongCipher("WrongCipher");

            using var provider = new CryptoProvider(cipher1);
            provider.RegisterCipher(cipher2);

            var customEncryptionKey = new CustomEncryptionKey(1, Encoding.UTF8.GetBytes("CustomEncryptionKey"));
            var exception           = Assert.Throws <StorageClientException>(() =>
            {
                provider.ValidateCustomCiphers(new SecretsData(new List <Secret> {
                    customEncryptionKey
                },
                                                               customEncryptionKey));
            });

            Assert.AreEqual("Validation failed for custom cipher with version 'WrongCipher'", exception.Message);

            provider.UnregisterCipher(cipher2);

            var cipher3 = new CipherWithException("CipherWithException");

            provider.RegisterCipher(cipher3);

            exception = Assert.Throws <StorageClientException>(() =>
            {
                provider.ValidateCustomCiphers(new SecretsData(new List <Secret> {
                    customEncryptionKey
                },
                                                               customEncryptionKey));
            });
            Assert.AreEqual("Validation failed for custom cipher with version 'CipherWithException'",
                            exception.Message);
            Assert.NotNull(exception.InnerException);
            Assert.IsInstanceOf <NotImplementedException>(exception.InnerException);

            var secretData = SecretsDataGenerator.FromPassword("password");

            exception = Assert.Throws <StorageClientException>(() => provider.ValidateCustomCiphers(secretData));
            Assert.AreEqual("There is no custom encryption key for the custom ciphers", exception.Message);
        }
Example #3
0
        private Storage(StorageConfig config)
        {
            s_helper.Check <StorageClientException>(config == null, Messages.Storage.s_errNullConfig);
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(VersionInfo.ProductName,
                                                                                       VersionInfo.ProductVersion));
#pragma warning disable CA1062
            _httpClient.Timeout = new TimeSpan(0, 0, config.HttpTimeout);
#pragma warning restore CA1062
            ITokenClient tokenClient = null;
            if (!string.IsNullOrEmpty(config.ApiKey))
            {
                tokenClient = new ApiKeyTokenClient(config.ApiKey);
            }

            if (!(string.IsNullOrEmpty(config.ClientId) || string.IsNullOrEmpty(config.ClientSecret)))
            {
                tokenClient = new OAuthTokenClient(config.DefaultAuthEndpoint, config.AuthEndpoints,
                                                   config.EnvironmentId, config.ClientId, config.ClientSecret, _httpClient);
            }


            s_helper.Check <StorageClientException>(tokenClient == null, Messages.Storage.s_errNullCredentials);
            _cryptoProvider = config.CryptoProvider;
            if (config.CryptoProvider == null)
            {
                _cryptoProvider = new CryptoProvider();
            }

            _cryptoProvider.ValidateCustomCiphers(config.SecretKeyAccessor?.Invoke());
            _hashUtils   = new HashUtils(config.EnvironmentId, config.NormalizeKeys, Encoding.UTF8);
            _transformer = new DtoTransformer(_cryptoProvider, _hashUtils, config.HashSearchKeys,
                                              config.SecretKeyAccessor);
            _dao = HttpDao.NewDao(config.EnvironmentId, tokenClient, _httpClient, config.EndPoint, config.EndpointMask,
                                  config.CountriesEndPoint);
        }