Beispiel #1
0
 /// <summary>
 /// Validate that the typed component is a docker image component with correct properties defined
 /// </summary>
 /// <param name="typedComponent">Component to validate properties</param>
 /// <returns>Validation results.</returns>
 public ValidationResult IsValid(TypedComponent typedComponent)
 {
     return(new ValidationResult()
     {
         IsValid = false,
         Messages = new List <string> {
             string.Format(Resources.ComponentTypeNotSupportedMessage, ComponentType.DockerImage)
         }
     });
 }
Beispiel #2
0
        public void IsValid_PropertyVersionAndNameAreNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent npmComponent = new TypedComponent(new NpmComponent
            {
            });

            var validationResult = generalComponentValidator.IsValid(npmComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(2);
            validationResult.Messages[0].Should().BeEquivalentTo("The property name is required and was not specified. This happens if the property has a typo or was omitted");
            validationResult.Messages[1].Should().BeEquivalentTo("The property version is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #3
0
        public void IsValid_PropertiesAreNotSet_ValidationFailMessagesAreRegistered()
        {
            TypedComponent gitComponent = new TypedComponent(new GitComponent
            {
            });

            var validationResult = gitComponentValidator.IsValid(gitComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(2);
            validationResult.Messages[0].Should().BeEquivalentTo("The property commitHash is required and was not specified. This happens if the property has a typo or was omitted");
            validationResult.Messages[1].Should().BeEquivalentTo("The property RepositoryUrl is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #4
0
        public void IsValid_PropertyRepositoryUrlIsNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent gitComponent = new TypedComponent(new GitComponent
            {
                CommitHash = "bc6bc6b1e51024faee6070940a647845ab8c2c63"
            });

            var validationResult = gitComponentValidator.IsValid(gitComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(1);
            validationResult.Messages[0].Should().BeEquivalentTo("The property RepositoryUrl is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #5
0
        public void IsValid_PropertyCommitHashIsNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent gitComponent = new TypedComponent(new GitComponent
            {
                RepositoryUrl = new Uri("https://github/microsoft/testrepo")
            });

            var validationResult = gitComponentValidator.IsValid(gitComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(1);
            validationResult.Messages[0].Should().BeEquivalentTo("The property commitHash is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #6
0
        public void IsValid_PropertiesAreNotSet_ValidationFailMessagesAreRegistered()
        {
            TypedComponent mavenComponent = new TypedComponent(new MavenComponent
            {
            });

            var validationResult = mavenComponentValidator.IsValid(mavenComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(3);
            validationResult.Messages[0].Should().BeEquivalentTo("The property groupId is required and was not specified. This happens if the property has a typo or was omitted");
            validationResult.Messages[1].Should().BeEquivalentTo("The property artifactId is required and was not specified. This happens if the property has a typo or was omitted");
            validationResult.Messages[2].Should().BeEquivalentTo("The property version is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #7
0
        public void IsValid_PropertyArtifactIdIsNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent mavenComponent = new TypedComponent(new MavenComponent
            {
                GroupId = "testGroupId",
                Version = "1.0.0"
            });

            var validationResult = mavenComponentValidator.IsValid(mavenComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(1);
            validationResult.Messages[0].Should().BeEquivalentTo("The property artifactId is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #8
0
        public void IsValid_PropertyDownloadUrlIsNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent otherComponent = new TypedComponent(new OtherComponent
            {
                Name    = "test",
                Version = "1.0.0"
            });

            var validationResult = otherComponentValidator.IsValid(otherComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(1);
            validationResult.Messages[0].Should().BeEquivalentTo("The property downloadUrl is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #9
0
        public void IsValid_PropertyVersionIsNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent otherComponent = new TypedComponent(new OtherComponent
            {
                Name        = "test",
                DownloadUrl = new Uri("https://github/microsoft/testrepo.zip")
            });

            var validationResult = otherComponentValidator.IsValid(otherComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(1);
            validationResult.Messages[0].Should().BeEquivalentTo("The property version is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #10
0
        public void IsComponentValid_ComponentDefinitionNotMatchComponentType_ComponentIsInvalid()
        {
            var invalidTypedComponent = new TypedComponent
            {
                Type = ComponentType.Npm,
                Cargo = new CargoComponent()
            };

            var result = ComponentValidator.IsComponentValid(invalidTypedComponent);

            result.IsValid.Should().BeFalse();
            result.Messages.Should().HaveCount(1);
            result.Messages.First().Should().BeEquivalentTo("Incorrect component definition. The component type is Npm, but the component definition is for: Cargo.");
        }
Beispiel #11
0
        public void IsValid_AllAttributesArePresent_ValidationSucceed()
        {
            TypedComponent npmTypedComponent = new TypedComponent(new NpmComponent
            {
                Name    = "test",
                Version = "1.0.0"
            });

            var expectedValidationResult = new ValidationResult {
                IsValid = true
            };

            generalComponentValidator.IsValid(npmTypedComponent).Should().BeEquivalentTo(expectedValidationResult);
        }
Beispiel #12
0
        public void IsValid_AllAttributesArePresent_ValidationSucceed()
        {
            TypedComponent gitComponent = new TypedComponent(new GitComponent
            {
                CommitHash    = "bc6bc6b1e51024faee6070940a647845ab8c2c63",
                RepositoryUrl = new Uri("https://github/microsoft/testrepo")
            });

            var expectedValidationResult = new ValidationResult {
                IsValid = true
            };

            gitComponentValidator.IsValid(gitComponent).Should().BeEquivalentTo(expectedValidationResult);
        }
Beispiel #13
0
        private static ComponentReport CreateComponentReportForRegistration(TypedComponent component, ValidationResult validationResult)
        {
            var componentReport = new ComponentReport {
                ValidationPassed = validationResult.IsValid
            };

            if (!componentReport.ValidationPassed)
            {
                componentReport.ComponentJsonRepresentation = JsonConvert.SerializeObject(component);
                componentReport.Messages = validationResult.Messages;
            }

            return(componentReport);
        }
Beispiel #14
0
        /// <summary>
        /// Validate that is a valid component with correct properties defined
        /// This validator is used for:
        /// - npm
        /// - nuget
        /// - cargo
        /// - Go
        /// - Pip
        /// - Pod
        /// - RubyGems
        /// </summary>
        /// <param name="typedComponent">Component to validate properties</param>
        /// <returns>Validation results.</returns>
        public ValidationResult IsValid(TypedComponent typedComponent)
        {
            if (typedComponent is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        Resources.MissingComponentType
                    }
                });
            }

            if (typedComponent.Npm != null)
            {
                return(ValidateGeneralProperties(typedComponent.Npm.Name, typedComponent.Npm.Version));
            }
            if (typedComponent.NuGet != null)
            {
                return(ValidateGeneralProperties(typedComponent.NuGet.Name, typedComponent.NuGet.Version));
            }
            if (typedComponent.Cargo != null)
            {
                return(ValidateGeneralProperties(typedComponent.Cargo.Name, typedComponent.Cargo.Version));
            }
            if (typedComponent.Go != null)
            {
                return(ValidateGeneralProperties(typedComponent.Go.Name, typedComponent.Go.Version));
            }
            if (typedComponent.Pip != null)
            {
                return(ValidateGeneralProperties(typedComponent.Pip.Name, typedComponent.Pip.Version));
            }
            if (typedComponent.Pod != null)
            {
                return(ValidateGeneralProperties(typedComponent.Pod.Name, typedComponent.Pod.Version));
            }
            if (typedComponent.RubyGems != null)
            {
                return(ValidateGeneralProperties(typedComponent.RubyGems.Name, typedComponent.RubyGems.Version));
            }

            return(new ValidationResult
            {
                IsValid = false,
                Messages = new List <string> {
                    string.Format(Resources.MissingComponentDefinition, typedComponent.Type)
                }
            });
        }
Beispiel #15
0
        public void IsValid_GeneralComponentsAreNotPresent_ThrowArgumentNullException()
        {
            var typedComponent = new TypedComponent
            {
                Git = null
            };
            Func <ValidationResult> action = () => gitComponentValidator.IsValid(typedComponent);

            action.Should().NotThrow <Exception>();
            var result = action();

            result.IsValid.Should().BeFalse();
            result.Messages.Should().HaveCount(1);
            result.Messages.First().Should().BeEquivalentTo("The component of type Git do not have a component definition.");
        }
Beispiel #16
0
        public void IsValid_AllAttributesArePresent_ValidationSucceed()
        {
            TypedComponent otherComponent = new TypedComponent(new OtherComponent
            {
                Name        = "test",
                Version     = "1.0.0",
                DownloadUrl = new Uri("https://github/microsoft/testrepo.zip")
            });

            var expectedValidationResult = new ValidationResult {
                IsValid = true
            };

            otherComponentValidator.IsValid(otherComponent).Should().BeEquivalentTo(expectedValidationResult);
        }
Beispiel #17
0
        public void IsValid_AllAttributesArePresent_ValidationSucceed()
        {
            TypedComponent mavenComponent = new TypedComponent(new MavenComponent
            {
                GroupId    = "testGroupId",
                ArtifactId = "testArtifactId",
                Version    = "1.0.0"
            });

            var expectedValidationResult = new ValidationResult {
                IsValid = true
            };

            mavenComponentValidator.IsValid(mavenComponent).Should().BeEquivalentTo(expectedValidationResult);
        }
Beispiel #18
0
        public void IsValid_PropertyVersionIsNotSet_ValidationFailMessageIsRegistered()
        {
            TypedComponent linuxComponent = new TypedComponent(new LinuxComponent
            {
                Name         = "test",
                Release      = "LTS",
                Distribution = LinuxDistribution.Alpine,
            });

            var validationResult = linuxComponentValidator.IsValid(linuxComponent);

            validationResult.IsValid.Should().BeFalse();
            validationResult.Messages.Should().HaveCount(1);
            validationResult.Messages[0].Should().BeEquivalentTo("The property version is required and was not specified. This happens if the property has a typo or was omitted");
        }
Beispiel #19
0
        public void IsValid_AllAttributesArePresent_ValidationSucceed()
        {
            TypedComponent linuxComponent = new TypedComponent(new LinuxComponent
            {
                Name         = "test",
                Release      = "LTS",
                Distribution = LinuxDistribution.Alpine,
                Version      = "1.0.0"
            });

            var expectedValidationResult = new ValidationResult {
                IsValid = true
            };

            linuxComponentValidator.IsValid(linuxComponent).Should().BeEquivalentTo(expectedValidationResult);
        }
Beispiel #20
0
        /// <summary>
        /// Validate that the typed component is a maven component with correct properties defined
        /// </summary>
        /// <param name="typedComponent">Component to validate properties</param>
        /// <returns>Validation results.</returns>
        public ValidationResult IsValid(TypedComponent typedComponent)
        {
            if (typedComponent is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        Resources.MissingComponentType
                    }
                });
            }

            if (typedComponent.Maven is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        string.Format(Resources.MissingComponentDefinition, ComponentType.Maven)
                    }
                });
            }

            var validationResult = new ValidationResult {
                IsValid = true, Messages = new List <string>()
            };

            if (string.IsNullOrWhiteSpace(typedComponent.Maven.GroupId))
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Maven.GroupId)));
            }
            if (string.IsNullOrWhiteSpace(typedComponent.Maven.ArtifactId))
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Maven.ArtifactId)));
            }
            if (string.IsNullOrWhiteSpace(typedComponent.Maven.Version))
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Maven.Version)));
            }


            return(validationResult);
        }
Beispiel #21
0
        public void IsComponentValid_ValidComponent_ValidResultIsReturned()
        {
            var validTypedComponent = new TypedComponent
            {
                Type = ComponentType.Npm,
                Npm = new NpmComponent
                {
                    Name = "test",
                    Version ="1.0.0"
                }
            };

            var result = ComponentValidator.IsComponentValid(validTypedComponent);

            result.IsValid.Should().BeTrue();
            result.Messages.Should().HaveCount(0);
        }
Beispiel #22
0
        /// <summary>
        /// Validate that the typed component is a other component with correct properties defined
        /// </summary>
        /// <param name="typedComponent">Component to validate properties</param>
        /// <returns>Validation results.</returns>
        public ValidationResult IsValid(TypedComponent typedComponent)
        {
            if (typedComponent is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        Resources.MissingComponentType
                    }
                });
            }

            if (typedComponent.Other is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        string.Format(Resources.MissingComponentDefinition, ComponentType.Other)
                    }
                });
            }

            var validationResult = new ValidationResult {
                IsValid = true
            };

            if (string.IsNullOrWhiteSpace(typedComponent.Other.Name))
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Other.Name)));
            }
            if (string.IsNullOrWhiteSpace(typedComponent.Other.Version))
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Other.Version)));
            }
            if (typedComponent.Other.DownloadUrl is null)
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Other.DownloadUrl)));
            }

            return(validationResult);
        }
Beispiel #23
0
 /// <summary>
 /// Pick the right validator given the component's type.
 /// </summary>
 /// <param name="typedComponent">Component to validate</param>
 /// <returns>Validation results.</returns>
 public static ValidationResult IsComponentValid(TypedComponent typedComponent)
 {
     try
     {
         var validationResult = ValidateComponenTypeDefinition(typedComponent);
         return(validationResult.IsValid
             ? getValidatorForType(typedComponent.Type).IsValid(typedComponent)
             : validationResult);
     }
     catch (ComponentTypeNotSupportedException ex)
     {
         return(new ValidationResult
         {
             IsValid = false,
             Messages = new List <string> {
                 ex.Message
             }
         });
     }
 }
Beispiel #24
0
        /// <summary>
        /// Validate that the typed component is a git component with correct properties defined
        /// </summary>
        /// <param name="typedComponent">Component to validate properties</param>
        /// <returns>Validation results.</returns>
        public ValidationResult IsValid(TypedComponent typedComponent)
        {
            if (typedComponent is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        Resources.MissingComponentType
                    }
                });
            }

            if (typedComponent.Git is null)
            {
                return(new ValidationResult
                {
                    IsValid = false,
                    Messages = new List <string> {
                        string.Format(Resources.MissingComponentDefinition, ComponentType.Git)
                    }
                });
            }

            var validationResult = new ValidationResult {
                IsValid = true
            };

            if (string.IsNullOrWhiteSpace(typedComponent.Git.CommitHash))
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Git.CommitHash)));
            }
            if (typedComponent.Git.RepositoryUrl is null)
            {
                validationResult.IsValid = false;
                validationResult.Messages.Add(string.Format(Resources.MissingRequiredProperty, nameof(typedComponent.Git.RepositoryUrl)));
            }

            return(validationResult);
        }
        public static int PopulateViolationsBookmarks(ReportData reportData, Violation[] violations, int violationCounter, List <string> rowData, int cellidx, string ruleName, List <CellAttributes> cellProps, bool hasPreviousSnapshot, string domainId, string snapshotId, string metric)
        {
            foreach (Violation _violation in violations)
            {
                violationCounter++;
                rowData.Add("");
                cellidx++;
                rowData.Add(Labels.Violation + " #" + violationCounter + "    " + ruleName);
                cellProps.Add(new CellAttributes(cellidx, Color.Gainsboro));
                cellidx++;
                rowData.Add(Labels.ObjectName + ": " + _violation.Component.Name);
                cellProps.Add(new CellAttributes(cellidx, Color.White));
                cellidx++;

                TypedComponent objectComponent = reportData.SnapshotExplorer.GetTypedComponent(reportData.CurrentSnapshot.DomainId, _violation.Component.GetComponentId(), reportData.CurrentSnapshot.GetId());
                rowData.Add(Labels.IFPUG_ObjectType + ": " + objectComponent.Type.Label);
                cellProps.Add(new CellAttributes(cellidx, Color.White));
                cellidx++;

                if (hasPreviousSnapshot)
                {
                    rowData.Add(Labels.Status + ": " + _violation.Diagnosis.Status);
                    cellProps.Add(new CellAttributes(cellidx, Color.White));
                    cellidx++;
                }

                AssociatedValue associatedValue = reportData.SnapshotExplorer.GetAssociatedValue(domainId, _violation.Component.GetComponentId(), snapshotId, metric);
                if (associatedValue == null)
                {
                    continue;
                }

                if (associatedValue.Type == null || associatedValue.Type.Equals("integer"))
                {
                    IEnumerable <IEnumerable <CodeBookmark> > bookmarks = associatedValue.Bookmarks;
                    if (bookmarks == null || !bookmarks.Any())
                    {
                        cellidx = AddSourceCode(reportData, rowData, cellidx, cellProps, domainId, snapshotId, _violation);
                    }
                    else
                    {
                        foreach (IEnumerable <CodeBookmark> _codeBookmarks in bookmarks)
                        {
                            IEnumerable <CodeBookmark> _bookmarks = _codeBookmarks.ToList();
                            foreach (CodeBookmark _bookmark in _bookmarks)
                            {
                                rowData.Add(Labels.FilePath + ": " + _bookmark.CodeFragment.CodeFile.Name);
                                cellProps.Add(new CellAttributes(cellidx, Color.Lavender));
                                cellidx++;
                                Dictionary <int, string> codeLines = reportData.SnapshotExplorer.GetSourceCodeBookmark(domainId, _bookmark, 3);
                                if (codeLines == null)
                                {
                                    continue;
                                }
                                foreach (KeyValuePair <int, string> codeLine in codeLines)
                                {
                                    rowData.Add(codeLine.Key + " : " + codeLine.Value);
                                    cellProps.Add(codeLine.Key >= _bookmark.CodeFragment.StartLine && codeLine.Key <= _bookmark.CodeFragment.EndLine
                                        ? new CellAttributes(cellidx, Color.LightYellow)
                                        : new CellAttributes(cellidx, Color.White));
                                    cellidx++;
                                }
                            }
                        }
                    }
                }

                if (associatedValue.Type != null && associatedValue.Type.Equals("path"))
                {
                    // manage case when type="path" and values contains the different path
                    AssociatedValuePath associatedValueEx            = reportData.SnapshotExplorer.GetAssociatedValuePath(domainId, _violation.Component.GetComponentId(), snapshotId, metric);
                    IEnumerable <IEnumerable <CodeBookmark> > values = associatedValueEx?.Values;
                    if (values == null || !values.Any())
                    {
                        cellidx = AddSourceCode(reportData, rowData, cellidx, cellProps, domainId, snapshotId, _violation);
                    }
                    else
                    {
                        int pathCounter = 0;
                        foreach (IEnumerable <CodeBookmark> _value in values)
                        {
                            pathCounter++;
                            IEnumerable <CodeBookmark> _bookmarksValue = _value.ToList();
                            rowData.Add(Labels.ViolationPath + " #" + pathCounter);
                            cellProps.Add(new CellAttributes(cellidx, Color.Lavender));
                            cellidx++;
                            string previousFile = string.Empty;
                            foreach (CodeBookmark _bookval in _bookmarksValue)
                            {
                                if (string.IsNullOrEmpty(previousFile) || !previousFile.Equals(_bookval.CodeFragment.CodeFile.Name))
                                {
                                    previousFile = _bookval.CodeFragment.CodeFile.Name;
                                    rowData.Add(Labels.FilePath + ": " + _bookval.CodeFragment.CodeFile.Name);
                                    cellProps.Add(new CellAttributes(cellidx, Color.White));
                                    cellidx++;
                                }

                                Dictionary <int, string> codeLines = reportData.SnapshotExplorer.GetSourceCodeBookmark(domainId, _bookval, 0);

                                foreach (KeyValuePair <int, string> codeLine in codeLines)
                                {
                                    rowData.Add(codeLine.Key + " : " + codeLine.Value);
                                    cellProps.Add(codeLine.Key == _bookval.CodeFragment.StartLine
                                        ? new CellAttributes(cellidx, Color.LightYellow)
                                        : new CellAttributes(cellidx, Color.White));
                                    cellidx++;
                                }
                            }
                            rowData.Add("");
                            cellidx++;
                        }
                    }
                }
                if (associatedValue.Type != null && associatedValue.Type.Equals("group"))
                {
                    // manage case when type="group" and values contains an array of array of components
                    AssociatedValueGroup associatedValueEx           = reportData.SnapshotExplorer.GetAssociatedValueGroup(domainId, _violation.Component.GetComponentId(), snapshotId, metric);
                    IEnumerable <IEnumerable <CodeBookmark> > values = associatedValueEx?.Values;
                    if (values == null || !values.Any())
                    {
                        cellidx = AddSourceCode(reportData, rowData, cellidx, cellProps, domainId, snapshotId, _violation);
                    }
                    else
                    {
                        foreach (IEnumerable <CodeBookmark> components in values)
                        {
                            foreach (CodeBookmark _component in components)
                            {
                                rowData.Add(_component.Component.Name);
                                cellProps.Add(new CellAttributes(cellidx, Color.White));
                                cellidx++;
                            }
                            rowData.Add("");
                            cellidx++;
                        }
                    }
                }
                if (associatedValue.Type != null && (associatedValue.Type.Equals("object") || associatedValue.Type.Equals("text") || associatedValue.Type.Equals("percentage")))
                {
                    // manage case when type="object", "text" or "percentage"
                    cellidx = AddSourceCode(reportData, rowData, cellidx, cellProps, domainId, snapshotId, _violation);
                }
            }
            return(cellidx);
        }