Exemplo n.º 1
0
        public BotClient(BotClientOptions options)
        {
            _url    = options.Url;
            _nonce  = 0;
            keyPair = SecurityUtility.GenerateRSAKeyPair(2048);
            var result = RegisterBot(options.Name, keyPair.PublicKey).GetAwaiter().GetResult();

            _botId = result.IdKey;
        }
Exemplo n.º 2
0
        public BotClient(string botName, string url)
        {
            _url    = url;
            _nonce  = 0;
            keyPair = SecurityUtility.GenerateRSAKeyPair(2048);
            var result = RegisterBot(botName, keyPair.PublicKey).GetAwaiter().GetResult();

            _botId = result.IdKey;
        }
Exemplo n.º 3
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));
        }
Exemplo n.º 4
0
        public void AsymetricalEncryption2048()
        {
            var pair       = SecurityUtility.GenerateRSAKeyPair(2048);
            var badPair    = SecurityUtility.GenerateRSAKeyPair(2048);
            var testString = "THIS IS A TEST STRING";

            var encryptedText = SecurityUtility.RsaEncryptWithPublic(testString, pair.PublicKey);
            var decryptedText = SecurityUtility.RsaDecryptWithPrivate(encryptedText, pair.PrivateKey);

            Assert.Equal(testString, decryptedText);
            Assert.NotEqual(encryptedText, testString);
            Assert.Throws <Org.BouncyCastle.Crypto.InvalidCipherTextException>(() => SecurityUtility.RsaDecryptWithPrivate(encryptedText, badPair.PrivateKey));
        }
Exemplo n.º 5
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));
        }
Exemplo n.º 6
0
        public async void EncryptOutputOnCompletion()
        {
            var stepTemplate = SecretSampleData.StepTemplate;
            var testPhrase   = "This is a test";
            var TestStep     = SecretSampleData.StepTemplate.GenerateStep(stepTemplate.ReferenceId, "", "", "", new Dictionary <string, object>()
            {
                { "secret", testPhrase }
            }, null, null, ClusterStateService.GetEncryptionKey());
            Mock <IEntitiesRepository> entitiesRepository = new Mock <IEntitiesRepository>();

            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <StepTemplate>(It.IsAny <Expression <Func <StepTemplate, bool> > >())).Returns(Task.FromResult(stepTemplate));
            _node.Setup(n => n.Handle(It.IsAny <AddShardWriteOperation>())).Returns(Task.FromResult(new AddShardWriteOperationResponse()
            {
                IsSuccessful = true
            }));
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <Step>(It.IsAny <Expression <Func <Step, bool> > >())).Returns(Task.FromResult(TestStep));

            var mockLogger            = new Mock <ILogger <CompleteStepCommandHandler> >();
            Mock <IMediator> mediator = new Mock <IMediator>();

            var mockStateLogger = new Mock <ILogger <ClusterStateService> >();
            var testKey         = SecurityUtility.GenerateRSAKeyPair();


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

            var node = Utility.GetMockConsensusCoreNode();
            Mock <IClusterStateService> service = new Mock <IClusterStateService>();

            var handler = new CompleteStepCommandHandler(entitiesRepository.Object, service.Object, mockLogger.Object, _optionsMonitor.Object, mediator.Object, _node.Object);

            var completeResult = await handler.Handle(new CompleteStepCommand()
            {
                Id      = TestStep.Id,
                Outputs = new Dictionary <string, object>()
                {
                    { "secret", SecurityUtility.RsaEncryptWithPrivate(testPhrase, testKey.PrivateKey) }
                },
                Status     = StepStatuses.Successful,
                StatusCode = 0,
                Log        = "TEST"
            }, new System.Threading.CancellationToken());

            Assert.Equal(TestStep.Id.ToString(), completeResult.ObjectRefId);
        }
Exemplo n.º 7
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"]);
        }
Exemplo n.º 8
0
        public async void CompleteStepWithWorkflow()
        {
            var TestWorkflow = FibonacciSampleData.Workflow;

            TestWorkflow.Journal = new Domain.Entities.JournalEntries.Journal(
                new Domain.Entities.JournalEntries.JournalEntry()
            {
                CreatedOn = DateTime.UtcNow,
                CreatedBy = "*****@*****.**",
                Updates   = new List <Update>()
                {
                    new Update()
                    {
                        FieldName = "status",
                        Value     = StepStatuses.Unassigned,
                        Type      = UpdateType.Override
                    }
                }
            }
                );

            var TestStep = FibonacciSampleData.Step;

            TestStep.UpdateJournal(new JournalEntry()
            {
                Updates = new List <Update>()
                {
                    new Update()
                    {
                        FieldName = "sequenceid",
                        Type      = UpdateType.Create,
                        Value     = TestWorkflow.Id
                    },
                    new Update()
                    {
                        FieldName = "workflowstepid",
                        Type      = UpdateType.Create,
                        Value     = 0
                    },
                }
            });

            Mock <IEntitiesRepository> entitiesRepository = new Mock <IEntitiesRepository>();

            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <Step>(It.IsAny <Expression <Func <Step, bool> > >())).Returns(Task.FromResult(TestStep));
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <StepTemplate>(It.IsAny <Expression <Func <StepTemplate, bool> > >())).Returns(Task.FromResult(FibonacciSampleData.StepTemplate));
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <WorkflowTemplate>(It.IsAny <Expression <Func <WorkflowTemplate, bool> > >())).Returns(Task.FromResult(FibonacciSampleData.WorkflowTemplate));
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <Workflow>(It.IsAny <Expression <Func <Workflow, bool> > >())).Returns(Task.FromResult(TestWorkflow));
            entitiesRepository.Setup(sr => sr.GetAsync <Step>(It.IsAny <Expression <Func <Step, bool> > >(), null, null, 10, 0)).Returns(Task.FromResult((IEnumerable <Step>) new List <Step>()
            {
                TestStep
            }));


            var mockLogger      = new Mock <ILogger <CompleteStepCommandHandler> >();
            var mockStateLogger = new Mock <ILogger <ClusterStateService> >();

            Mock <IMediator> mediator = new Mock <IMediator>();

            var secondStepId = Guid.NewGuid();

            mediator.Setup(m => m.Send(It.IsAny <CreateStepCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new CommandResult <Step>()
            {
                ObjectRefId = secondStepId.ToString(),
                Result      = new Step(secondStepId
                                       , "",
                                       "",
                                       FibonacciSampleData.StepTemplate.ReferenceId,
                                       "admin",
                                       new Dictionary <string, object>()
                {
                    { "n-1", 2 },
                    { "n-2", 1 }
                }
                                       )
            }));
            var testKey = SecurityUtility.GenerateRSAKeyPair();


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

            Mock <IClusterStateService> service = new Mock <IClusterStateService>();

            service.Setup(m => m.IsLogicBlockLocked(It.IsAny <Guid>(), It.IsAny <string>())).Returns(false);
            service.Setup(m => m.LockLogicBlock(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <string>())).Returns(Task.FromResult(1));
            service.Setup(m => m.WasLockObtained(It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <string>())).Returns(true);

            var handler = new CompleteStepCommandHandler(entitiesRepository.Object, service.Object, mockLogger.Object, _optionsMonitor.Object, mediator.Object, _node.Object);

            Assert.Equal(TestStep.Id.ToString(), (await handler.Handle(new CompleteStepCommand()
            {
                Id = TestStep.Id,
                Outputs = new Dictionary <string, object>()
                {
                    { "n", 0 }
                },
                Status = StepStatuses.Successful,
                StatusCode = 0,
                Log = "TEST"
            }, new System.Threading.CancellationToken())).ObjectRefId);
        }
Exemplo n.º 9
0
        public async void CompleteStepWithNoWorkflow()
        {
            var TestStep = FibonacciSampleData.Step;
            Mock <IEntitiesRepository> entitiesRepository = new Mock <IEntitiesRepository>();

            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <StepTemplate>(It.IsAny <Expression <Func <StepTemplate, bool> > >())).Returns(Task.FromResult(FibonacciSampleData.StepTemplate));
            entitiesRepository.Setup(sr => sr.GetFirstOrDefaultAsync <Step>(It.IsAny <Expression <Func <Step, bool> > >())).Returns(Task.FromResult(TestStep));
            _node.Setup(n => n.Handle(It.IsAny <AddShardWriteOperation>())).Returns(Task.FromResult(new AddShardWriteOperationResponse()
            {
                IsSuccessful = true
            }));

            var mockLogger            = new Mock <ILogger <CompleteStepCommandHandler> >();
            Mock <IMediator> mediator = new Mock <IMediator>();

            var secondStepId = Guid.NewGuid();

            mediator.Setup(m => m.Send(It.IsAny <CreateStepCommand>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(new CommandResult <Step>()
            {
                ObjectRefId = secondStepId.ToString(),
                Result      = new Step(secondStepId,
                                       "",
                                       "",
                                       FibonacciSampleData.StepTemplate.ReferenceId,
                                       "admin",
                                       new Dictionary <string, object>()
                {
                    { "n-1", 2 },
                    { "n-2", 1 }
                }
                                       )
            }));

            var mockStateLogger = new Mock <ILogger <ClusterStateService> >();
            var testKey         = SecurityUtility.GenerateRSAKeyPair();


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

            Mock <IClusterStateService> service = new Mock <IClusterStateService>();

            var handler = new CompleteStepCommandHandler(entitiesRepository.Object, service.Object, mockLogger.Object, _optionsMonitor.Object, mediator.Object, _node.Object);

            var completeResult = await handler.Handle(new CompleteStepCommand()
            {
                Id      = TestStep.Id,
                Outputs = new Dictionary <string, object>()
                {
                    { "n", 0 }
                },
                Status     = StepStatuses.Successful,
                StatusCode = 0,
                Log        = "TEST"
            }, new System.Threading.CancellationToken());

            Assert.Equal(TestStep.Id.ToString(), completeResult.ObjectRefId);
        }