Example #1
0
        public async void AssignStepWithSecret()
        {
            var testPhrase = "This is a test";

            Mock <IEntitiesRepository> entitiesRepository = new Mock <IEntitiesRepository>();
            var newStep = SecretSampleData.StepTemplate.GenerateStep(SecretSampleData.StepTemplate.ReferenceId, "", "", "", new Dictionary <string, object>()
            {
                { "secret", testPhrase }
            }, null, null, ClusterStateService.GetEncryptionKey());

            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <StepTemplate, bool> > >())).Returns(Task.FromResult(SecretSampleData.StepTemplate));

            entitiesRepository.Setup(st => st.GetAsync(It.IsAny <Expression <Func <Step, bool> > >(), null, It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult((IEnumerable <Step>) new List <Step> {
                newStep
            }));

            var testKey = SecurityUtility.GenerateRSAKeyPair();

            entitiesRepository.Setup(kr => kr.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <BotKey, bool> > >())).Returns(Task.FromResult(new BotKey()
            {
                PublicEncryptionKey = testKey.PublicKey
            }));



            var mockLogger = new Mock <ILogger <AssignStepCommandHandler> >();

            var node = Utility.GetMockConsensusCoreNode();

            node.Setup(s => s.Handle(It.IsAny <RequestDataShard>())).Returns(Task.FromResult(new RequestDataShardResponse()
            {
                AppliedLocked = true,
                IsSuccessful  = true
            }));

            var handler = new AssignStepCommandHandler(entitiesRepository.Object, clusterMoq.Object, mockLogger.Object, node.Object);

            var result = await handler.Handle(new AssignStepCommand()
            {
            }, new System.Threading.CancellationToken());

            var assignedStep = result.Result;

            Assert.NotEqual(testPhrase, (string)result.Result.Inputs["secret"]);

            var encryptionTestResult = SecurityUtility.RsaEncryptWithPublic(testPhrase, testKey.PublicKey);

            //Randomized padding is used
            Assert.NotEqual(encryptionTestResult, (string)result.Result.Inputs["secret"]);
            //Decryption using private key should work
            Assert.Equal(SecurityUtility.RsaDecryptWithPrivate(encryptionTestResult, testKey.PrivateKey), SecurityUtility.RsaDecryptWithPrivate((string)result.Result.Inputs["secret"], testKey.PrivateKey));
        }
Example #2
0
        public async void IgnoreEscapedReferenceSymbol()
        {
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <StepTemplate>(It.IsAny <Expression <Func <StepTemplate, bool> > >())).Returns(Task.FromResult(SecretSampleData.StepTemplate));
            var testPhrase   = "$secret";
            var stepTemplate = await entitiesRepository.Object.GetFirstOrDefaultAsync <StepTemplate>(st => st.ReferenceId == SecretSampleData.StepTemplate.ReferenceId);

            var newStep = stepTemplate.GenerateStep(stepTemplate.ReferenceId, "", "", "", new Dictionary <string, object>()
            {
                { "secret", "\\$secret" }
            }, null, null, ClusterStateService.GetEncryptionKey());



            entitiesRepository.Setup(st => st.GetAsync <Step>(It.IsAny <Expression <Func <Step, bool> > >(), null, It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult((IEnumerable <Step>) new List <Step> {
                newStep
            }));

            var testKey = SecurityUtility.GenerateRSAKeyPair();


            entitiesRepository.Setup(kr => kr.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <BotKey, bool> > >())).Returns(Task.FromResult(new BotKey()
            {
                PublicEncryptionKey = testKey.PublicKey
            }));

            var mockLogger = new Mock <ILogger <AssignStepCommandHandler> >();


            entitiesRepository.Setup(s => s.GetFirstOrDefaultAsync <GlobalValue>(It.IsAny <Expression <Func <GlobalValue, bool> > >())).Returns(
                Task.FromResult(new GlobalValue("secret", InputDataTypes.Secret, "", SecurityUtility.SymmetricallyEncrypt(testPhrase, ClusterStateService.GetEncryptionKey()), GlobalValueStatuses.Enabled, Guid.NewGuid(), "admin", DateTime.UtcNow))
                );

            var node = Utility.GetMockConsensusCoreNode();

            node.Setup(s => s.Handle(It.IsAny <RequestDataShard>())).Returns(Task.FromResult(new RequestDataShardResponse()
            {
                AppliedLocked = true, IsSuccessful = true
            }));

            var handler = new AssignStepCommandHandler(entitiesRepository.Object, clusterMoq.Object, mockLogger.Object, node.Object);

            var result = await handler.Handle(new AssignStepCommand()
            {
            }, new System.Threading.CancellationToken());

            Assert.NotEqual(testPhrase, (string)result.Result.Inputs["secret"]);
            var encryptionTestResult = SecurityUtility.RsaEncryptWithPublic(testPhrase, testKey.PublicKey);

            Assert.NotEqual(encryptionTestResult, (string)result.Result.Inputs["secret"]);
            Assert.Equal(SecurityUtility.RsaDecryptWithPrivate(encryptionTestResult, testKey.PrivateKey), SecurityUtility.RsaDecryptWithPrivate((string)result.Result.Inputs["secret"], testKey.PrivateKey));
        }
Example #3
0
        public async void EvaluateIntValueByValueReference()
        {
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <StepTemplate>(It.IsAny <Expression <Func <StepTemplate, bool> > >())).Returns(Task.FromResult(FibonacciSampleData.StepTemplate));
            var newStep = FibonacciSampleData.StepTemplate.GenerateStep(FibonacciSampleData.StepTemplate.ReferenceId, "", "", "", new Dictionary <string, object>()
            {
                { "n-1", "$1" },
                { "n-2", 2 }
            }, null, null, ClusterStateService.GetEncryptionKey());



            entitiesRepository.Setup(st => st.GetAsync <Step>(It.IsAny <Expression <Func <Step, bool> > >(), null, It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())).Returns(Task.FromResult((IEnumerable <Step>) new List <Step> {
                newStep
            }));

            var testKey = SecurityUtility.GenerateRSAKeyPair();


            entitiesRepository.Setup(kr => kr.GetFirstOrDefaultAsync(It.IsAny <Expression <Func <BotKey, bool> > >())).Returns(Task.FromResult(new BotKey()
            {
                PublicEncryptionKey = testKey.PublicKey
            }));

            var mockLogger = new Mock <ILogger <AssignStepCommandHandler> >();


            entitiesRepository.Setup(s => s.GetFirstOrDefaultAsync <GlobalValue>(It.IsAny <Expression <Func <GlobalValue, bool> > >())).Returns(
                Task.FromResult(new GlobalValue("1", InputDataTypes.Int, "", 1, GlobalValueStatuses.Enabled, Guid.NewGuid(), "admin", DateTime.UtcNow))
                );

            var node = Utility.GetMockConsensusCoreNode();

            node.Setup(s => s.Handle(It.IsAny <RequestDataShard>())).Returns(Task.FromResult(new RequestDataShardResponse()
            {
                AppliedLocked = true,
                IsSuccessful  = true
            }));

            var handler = new AssignStepCommandHandler(entitiesRepository.Object, clusterMoq.Object, mockLogger.Object, node.Object);

            var result = await handler.Handle(new AssignStepCommand()
            {
            }, new System.Threading.CancellationToken());

            Assert.Equal(1, (int)result.Result.Inputs["n-1"]);
            Assert.Equal(2, (int)result.Result.Inputs["n-2"]);
        }