public static bool IsPackageMetadataCompliant(Package package, RequirePackageMetadataState state, out IList <string> complianceFailures) { complianceFailures = new List <string>(); // Author validation ValidatePackageAuthors(package, state, complianceFailures); // Copyright validation if (!state.AllowedCopyrightNotices.Contains(package.Copyright)) { complianceFailures.Add(ServicesStrings.SecurityPolicy_CopyrightNotCompliant); } // LicenseUrl validation if (state.IsLicenseUrlRequired && string.IsNullOrWhiteSpace(package.LicenseUrl)) { complianceFailures.Add(ServicesStrings.SecurityPolicy_RequiredLicenseUrlMissing); } // ProjectUrl validation if (state.IsProjectUrlRequired && string.IsNullOrWhiteSpace(package.ProjectUrl)) { complianceFailures.Add(ServicesStrings.SecurityPolicy_RequiredProjectUrlMissing); } return(!complianceFailures.Any()); }
private static void ValidatePackageAuthors(Package package, RequirePackageMetadataState state, IList <string> complianceFailures) { var packageAuthors = package.FlattenedAuthors .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .ToList(); // Check for duplicate entries var duplicateAuthors = packageAuthors .GroupBy(x => x) .Where(group => group.Count() > 1) .Select(group => group.Key) .ToList(); if (duplicateAuthors.Any()) { complianceFailures.Add(string.Format(CultureInfo.CurrentCulture, ServicesStrings.SecurityPolicy_PackageAuthorDuplicatesNotAllowed, string.Join(",", duplicateAuthors))); } else { if (state.AllowedAuthors?.Length > 0) { foreach (var packageAuthor in packageAuthors) { if (!state.AllowedAuthors.Contains(packageAuthor)) { complianceFailures.Add(string.Format(CultureInfo.CurrentCulture, ServicesStrings.SecurityPolicy_PackageAuthorNotAllowed, packageAuthor)); } } } else { // No list of allowed authors is defined for this policy. // We require the required co-owner to be defined as the only package author. if (packageAuthors.Count() > 1 || packageAuthors.Single() != state.RequiredCoOwnerUsername) { complianceFailures.Add(string.Format(CultureInfo.CurrentCulture, ServicesStrings.SecurityPolicy_RequiredAuthorMissing, state.RequiredCoOwnerUsername)); } } } }