示例#1
0
        public void CallingNextPhase_WithSeveralPhases_LastPhaseBegingOpen_WillReturnDataAvailable()
        {
            var sandboxWithoutPhase = new Sandbox()
            {
                PhaseHistory = new List <SandboxPhaseHistory>()
                {
                    new SandboxPhaseHistory()
                    {
                        Counter = 0, Phase = SandboxPhase.Open
                    },
                    new SandboxPhaseHistory()
                    {
                        Counter = 1, Phase = SandboxPhase.DataAvailable
                    },
                    new SandboxPhaseHistory()
                    {
                        Counter = 2, Phase = SandboxPhase.Open
                    },
                }
            };

            var nextPhaseOfSandbox = SandboxPhaseUtil.GetNextPhase(sandboxWithoutPhase);

            Assert.Equal(SandboxPhase.DataAvailable, nextPhaseOfSandbox);
        }
示例#2
0
        void ValidateAddOrRemoveDataset(Sandbox sandbox)
        {
            var sandboxPhase = SandboxPhaseUtil.GetCurrentPhase(sandbox);

            if (sandboxPhase > SandboxPhase.Open)
            {
                throw new ArgumentException($"Dataset cannot be added to Sandbox. Sandbox phase must be open.");
            }
        }
示例#3
0
        public void CallingNextPhase_WithEmptyPhaseList_WillReturnDataAvailable()
        {
            var sandboxWithoutPhase = new Sandbox()
            {
                PhaseHistory = new List <SandboxPhaseHistory>()
                {
                }
            };
            var nextPhaseOfSandbox = SandboxPhaseUtil.GetNextPhase(sandboxWithoutPhase);

            Assert.Equal(SandboxPhase.DataAvailable, nextPhaseOfSandbox);
        }
示例#4
0
        public void CallingCurrentPhase_WithEmptyPhaseList_WillReturnOpen()
        {
            var sandboxWithoutPhase = new Sandbox()
            {
                PhaseHistory = new List <SandboxPhaseHistory>()
                {
                }
            };
            var phaseOfSandbox = SandboxPhaseUtil.GetCurrentPhase(sandboxWithoutPhase);

            Assert.Equal(SandboxPhase.Open, phaseOfSandbox);
        }
示例#5
0
        public void CallingNextPhase_WithLastPhaseBegingDataAvailable_WillThrow()
        {
            var sandboxWithoutPhase = new Sandbox()
            {
                PhaseHistory = new List <SandboxPhaseHistory>()
                {
                    new SandboxPhaseHistory()
                    {
                        Counter = 0, Phase = SandboxPhase.DataAvailable
                    }
                }
            };

            Assert.Throws <Exception>(() => SandboxPhaseUtil.GetNextPhase(sandboxWithoutPhase));
        }
示例#6
0
        public void CallingCurrentPhase_WithLastPhaseBeingOpen_WillReturnOpen()
        {
            var sandboxWithoutPhase = new Sandbox()
            {
                PhaseHistory = new List <SandboxPhaseHistory>()
                {
                    new SandboxPhaseHistory()
                    {
                        Counter = 0, Phase = SandboxPhase.Open
                    }
                }
            };
            var phaseOfSandbox = SandboxPhaseUtil.GetCurrentPhase(sandboxWithoutPhase);

            Assert.Equal(SandboxPhase.Open, phaseOfSandbox);
        }
示例#7
0
        public async Task <Sandbox> AddAsync(Study study, Sandbox newSandbox)
        {
            var user = await _userService.GetCurrentUserAsync();

            newSandbox.CreatedBy             = user.UserName;
            newSandbox.TechnicalContactName  = user.FullName;
            newSandbox.TechnicalContactEmail = user.EmailAddress;

            SandboxPhaseUtil.InitiatePhaseHistory(newSandbox, user);

            newSandbox.Study = study;
            study.Sandboxes.Add(newSandbox);

            await _db.SaveChangesAsync();

            return(newSandbox);
        }
示例#8
0
 public SandboxPhase Resolve(Sandbox source, IHasCurrentPhase destination, SandboxPhase destMember, ResolutionContext context)
 {
     return(SandboxPhaseUtil.GetCurrentPhase(source));
 }
示例#9
0
        async Task ValidateRuleUpdateInputThrowIfNot(CloudResource vm, List <VmRuleDto> existingRules, List <VmRuleDto> updatedRuleSet)
        {
            var validationErrors = new List <string>();

            var sandbox = await _db.Sandboxes.Include(sb => sb.PhaseHistory).FirstOrDefaultAsync(sb => sb.Id == vm.SandboxId);

            var curPhase = SandboxPhaseUtil.GetCurrentPhase(sandbox);

            //VALIDATE OUTBOUND RULE, THERE SHOULD BE ONLY ONE

            var outboundRules = updatedRuleSet.Where(r => r.Direction == RuleDirection.Outbound).ToList();

            if (outboundRules.Count != 1)
            {
                validationErrors.Add($"Multiple outbound rule(s) provided");
                ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors);
            }

            var onlyOutboundRuleFromExisting = existingRules.SingleOrDefault(r => r.Direction == RuleDirection.Outbound);
            var onlyOutboundRuleFromClient   = outboundRules.SingleOrDefault();

            if (onlyOutboundRuleFromExisting.Name != onlyOutboundRuleFromClient.Name)
            {
                validationErrors.Add($"Illegal outbound rule(s) provided");
                ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors);
            }

            //If Sandbox is not open, make sure outbound rule has not changed
            if (curPhase > SandboxPhase.Open)
            {
                if (onlyOutboundRuleFromClient.Direction == RuleDirection.Outbound)
                {
                    if (onlyOutboundRuleFromClient.ToString() != onlyOutboundRuleFromExisting.ToString())
                    {
                        var currentUser = await _userService.GetCurrentUserAsync();

                        if (!currentUser.Admin)
                        {
                            validationErrors.Add($"Only admin can updated outgoing rules when Sandbox is in phase {curPhase}");
                            ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors);
                        }
                    }
                }
            }

            //VALIDATE INBOUND RULES

            foreach (var curInboundRule in updatedRuleSet.Where(r => r.Direction == RuleDirection.Inbound).ToList())
            {
                if (curInboundRule.Direction > RuleDirection.Outbound)
                {
                    validationErrors.Add($"Invalid direction for rule {curInboundRule.Description}: {curInboundRule.Direction}");
                }

                if (String.IsNullOrWhiteSpace(curInboundRule.Ip))
                {
                    validationErrors.Add($"Missing ip for rule {curInboundRule.Description}");
                }

                if (curInboundRule.Port <= 0)
                {
                    validationErrors.Add($"Invalid port for rule {curInboundRule.Description}: {curInboundRule.Port}");
                }

                if (String.IsNullOrWhiteSpace(curInboundRule.Description))
                {
                    validationErrors.Add($"Missing Description for rule {curInboundRule.Description}");
                }
            }

            ValidationUtils.ThrowIfValidationErrors("Rule update not allowed", validationErrors);
        }
示例#10
0
        public async Task <SandboxDetails> MoveToNextPhaseAsync(int sandboxId, CancellationToken cancellation = default)
        {
            _logger.LogInformation(_sandboxNextPhaseEventId, "Sandbox {0}: Starting", sandboxId);

            SandboxPhaseHistory newestHistoryItem = null;

            bool dataMightHaveBeenChanged = false;

            try
            {
                var user = await _userService.GetCurrentUserAsync();

                var sandboxFromDb = await GetSandboxForPhaseShift(sandboxId);

                var currentPhaseItem = SandboxPhaseUtil.GetCurrentPhaseHistoryItem(sandboxFromDb);

                if (currentPhaseItem == null)
                {
                    SandboxPhaseUtil.InitiatePhaseHistory(sandboxFromDb, user);
                    currentPhaseItem = SandboxPhaseUtil.GetCurrentPhaseHistoryItem(sandboxFromDb);
                }

                var nextPhase = SandboxPhaseUtil.GetNextPhase(sandboxFromDb);

                await ValidatePhaseMoveThrowIfNot(sandboxFromDb, currentPhaseItem.Phase, nextPhase, cancellation);

                _logger.LogInformation(_sandboxNextPhaseEventId, "Sandbox {0}: Moving from {1} to {2}", sandboxId, currentPhaseItem.Phase, nextPhase);

                newestHistoryItem = new SandboxPhaseHistory()
                {
                    Counter = currentPhaseItem.Counter + 1, Phase = nextPhase, CreatedBy = user.UserName
                };
                dataMightHaveBeenChanged = true;
                sandboxFromDb.PhaseHistory.Add(newestHistoryItem);
                await _db.SaveChangesAsync();

                _logger.LogInformation(_sandboxNextPhaseEventId, "Sandbox {0}: Phase added to db. Proceeding to make data available", sandboxId);

                if (nextPhase == SandboxPhase.DataAvailable)
                {
                    await MakeDatasetsAvailable(sandboxFromDb, cancellation);
                }

                _logger.LogInformation(_sandboxNextPhaseEventId, "Sandbox {0}: Done", sandboxId);

                return(await GetSandboxDetailsInternalAsync(sandboxId));
            }
            catch (Exception ex)
            {
                _logger.LogWarning(_sandboxNextPhaseEventId, ex, "Sandbox {0}: Phase shift failed.", sandboxId);

                if (dataMightHaveBeenChanged)
                {
                    _logger.LogWarning(_sandboxNextPhaseEventId, ex, "Data might have been changed. Rolling back");
                    await MakeDatasetsUnAvailable(sandboxId);
                    await AttemptRollbackPhase(sandboxId, newestHistoryItem);
                }

                throw;
            }
        }
示例#11
0
        public void CallingNextPhase_WithPhaseListBeingNull_WillThrow()
        {
            var sandboxWithoutPhase = new Sandbox();

            Assert.Throws <Exception>(() => SandboxPhaseUtil.GetNextPhase(sandboxWithoutPhase));
        }
示例#12
0
 public void CallingNextPhase_WithNullArgument_WillThrow()
 {
     Assert.Throws <ArgumentNullException>(() => SandboxPhaseUtil.GetNextPhase(null));
 }