private static string SetConnectionString(string connectionName)
        {
            var connectionString = new StringBuilder();
            connectionString.Append(System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString);

            if (!connectionString.to_string().EndsWith(";"))
            {
                connectionString.Append(";");
            }

            connectionString.Append("Max Pool Size=300");

            return connectionString.to_string();
        }
Esempio n. 2
0
        private static string SetConnectionString(string connectionName)
        {
            var connectionString = new StringBuilder();

            connectionString.Append(System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString);

            if (!connectionString.to_string().EndsWith(";"))
            {
                connectionString.Append(";");
            }

            connectionString.Append("Max Pool Size=300");

            return(connectionString.to_string());
        }
        private string GetUrl(string filePath, string resourceName = null)
        {
            if (!string.IsNullOrWhiteSpace(resourceName))
            {
                return(resourceName);
            }
            if (string.IsNullOrWhiteSpace(filePath))
            {
                return(filePath);
            }

            var fileName        = Path.GetFileNameWithoutExtension(filePath);
            var hyphenatedValue = new StringBuilder();

            Char previousChar = '^';

            foreach (var valueChar in fileName)
            {
                if (Char.IsUpper(valueChar) && hyphenatedValue.Length != 0)
                {
                    hyphenatedValue.Append("-");
                }

                if (Char.IsDigit(valueChar) && !Char.IsDigit(previousChar) && hyphenatedValue.Length != 0)
                {
                    hyphenatedValue.Append("-");
                }

                previousChar = valueChar;
                hyphenatedValue.Append(valueChar.to_string());
            }

            return(hyphenatedValue.to_string().to_lower());
        }
Esempio n. 4
0
        internal static string AdjustConnectionString(string nameOrConnectionString, string username, string password)
        {
            var connectionString         = SetConnectionString(nameOrConnectionString);
            var returnedConnectionString = new StringBuilder();

            var hasReplacedUser     = false;
            var hasReplacedPassword = false;

            var splits = connectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var connString in splits.OrEmptyListIfNull())
            {
                if (connString.StartsWith("User"))
                {
                    returnedConnectionString.AppendFormat("User Id={0};", username);
                    hasReplacedUser = true;
                }
                else if (connString.StartsWith("Password"))
                {
                    returnedConnectionString.AppendFormat("Password={0};", password);
                    hasReplacedPassword = true;
                }
                else
                {
                    returnedConnectionString.AppendFormat("{0};", connString);
                }
            }

            if (!hasReplacedPassword || !hasReplacedUser)
            {
                Trace.WriteLine("Error adjusting connection string - username and password not found to replace.");
            }

            return(returnedConnectionString.to_string());
        }
        private string get_msi_user_data_key(string name)
        {
            _userDataKey.Clear();
            var match = _guidRegex.Match(name);

            if (!match.Success)
            {
                return(string.Empty);
            }

            for (int i = 0; i < 3; i++)
            {
                var fullGroup = match.Groups["ReverseFull{0}".format_with(i + 1)];
                if (fullGroup != null)
                {
                    _userDataKey.Append(fullGroup.Value.ToCharArray().Reverse().ToArray());
                }
            }
            for (int i = 0; i < 2; i++)
            {
                var pairsGroup = match.Groups["ReversePairs{0}".format_with(i + 1)];
                if (pairsGroup != null)
                {
                    var pairValue = pairsGroup.Value;
                    for (int j = 0; j < pairValue.Length - 1; j++)
                    {
                        _userDataKey.Append("{1}{0}".format_with(pairValue[j], pairValue[j + 1]));
                        j++;
                    }
                }
            }

            return(_userDataKey.to_string());
        }
        private void process_rejection(RejectPackageMessage message)
        {
            var resultsMessage = new StringBuilder();

            resultsMessage.AppendLine("Unfortunately there has not been progress to move {0} v{1} towards an approved status within 15 days after the last review message, so we need to close (reject) the package version at this time. If you want to pick this version up and move it towards approval in the future, use the contact site admins link on the package page and we can move it back into a submitted status so you can submit updates.".format_with(message.PackageId, message.PackageVersion));
 
            EventManager.publish(new FinalResultMessage(message.PackageId, message.PackageVersion, resultsMessage.to_string(), reject: true));
        }
Esempio n. 7
0
        private static string SetConnectionString(string nameOrConnectionString)
        {
            var connectionString = new StringBuilder();

            if (!TreatAsConnectionString(nameOrConnectionString))
            {
                connectionString.Append(System.Configuration.ConfigurationManager.ConnectionStrings[nameOrConnectionString].ConnectionString);
            }
            else
            {
                connectionString.Append(nameOrConnectionString);
            }

            if (!connectionString.to_string().EndsWith(";"))
            {
                connectionString.Append(";");
            }

            connectionString.Append("Max Pool Size=300");

            return(connectionString.to_string());
        }
Esempio n. 8
0
        private string GetUrl(string filePath, string docName = null)
        {
            if (!string.IsNullOrWhiteSpace(docName))
            {
                return(docName);
            }
            if (string.IsNullOrWhiteSpace(filePath))
            {
                return(filePath);
            }

            var fileName = Path.GetFileNameWithoutExtension(filePath);

            if (fileName.Equals("CommandsApiKey"))
            {
                fileName = fileName.Replace("CommandsApiKey", "CommandsApikey");
            }
            if (fileName.Equals("CommandsSetapiKey"))
            {
                fileName = fileName.Replace("CommandsSetapiKey", "CommandsSetapikey");
            }

            var hyphenatedValue = new StringBuilder();

            Char previousChar = '^';

            foreach (var valueChar in fileName)
            {
                // Filenames that contain both a "-" and camel casing
                if (fileName.Contains("-") && Char.IsLower(previousChar) && Char.IsUpper(valueChar))
                {
                    hyphenatedValue.Append("-");
                }

                if (Char.IsUpper(valueChar) && hyphenatedValue.Length != 0 && !Char.IsUpper(previousChar) && !fileName.Contains("-"))
                {
                    hyphenatedValue.Append("-");
                }

                if (Char.IsDigit(valueChar) && !Char.IsDigit(previousChar) && hyphenatedValue.Length != 0)
                {
                    hyphenatedValue.Append("-");
                }

                previousChar = valueChar;
                hyphenatedValue.Append(valueChar.to_string());
            }

            return(hyphenatedValue.to_string().to_lower());
        }
        private void process_reminder(ReminderPackageMessage message)
        {
            var resultsMessage = new StringBuilder();

            resultsMessage.AppendLine("We've found {0} v{1} in a submitted status and waiting for your next actions. It has had no updates for 20 or more days since a reviewer has asked for corrections. Please note that if there is no response or fix of the package within 15 days of this message, this package version will automatically be closed (rejected) due to being stale.".format_with(message.PackageId,message.PackageVersion));
            resultsMessage.AppendFormat("{0}Take action:{0}".format_with(Environment.NewLine));
            resultsMessage.AppendLine(" * Log in to the site and respond to the review comments.");
            resultsMessage.AppendLine(" * Resubmit fixes for this version.");
            resultsMessage.AppendLine(" * If the package version is failing automated checks, you can self-reject the package.");
            resultsMessage.AppendLine("{0}If your package is failing automated testing, you can use the [chocolatey test environment](https://github.com/chocolatey/chocolatey-test-environment) to manually run the verification and determine what may need to be fixed.".format_with(Environment.NewLine));
            resultsMessage.AppendLine("{0}**Note**: We don't like to see packages automatically rejected. It doesn't mean that we don't value your contributions, just that we can not continue to hold packages versions in a waiting status that have possibly been abandoned. If you don't believe you will be able to fix up this version of the package within 15 days, we strongly urge you to log in to the site and respond to the review comments until you are able to.".format_with(Environment.NewLine));

            EventManager.publish(new FinalResultMessage(message.PackageId, message.PackageVersion, resultsMessage.to_string(), reject: false));
        }
Esempio n. 10
0
        private static string gather_process_only_items(string currentValues, IEnumerable <string> originalValues)
        {
            var additionalItems = new StringBuilder();
            var items           = currentValues.Split(
                new[] { ApplicationParameters.Environment.EnvironmentSeparator },
                StringSplitOptions.RemoveEmptyEntries
                );

            foreach (string originalValue in originalValues.or_empty_list_if_null())
            {
                if (!items.Contains(originalValue, StringComparer.InvariantCultureIgnoreCase))
                {
                    additionalItems.AppendFormat("{0};", originalValue);
                }
            }

            return(additionalItems.to_string());
        }
Esempio n. 11
0
        private ActionResult CreatePackageInternal(string apiKey)
        {
            Guid parsedApiKey;

            if (!Guid.TryParse(apiKey, out parsedApiKey))
            {
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.BadRequest, Strings.InvalidApiKey));
            }

            var user = userSvc.FindByApiKey(parsedApiKey);

            if (user == null)
            {
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, String.Format(CultureInfo.CurrentCulture, Strings.ApiKeyNotAuthorized, "push")));
            }
            var requestIp   = GetIpAddress();
            var cfRay       = Request.Headers["CF-RAY"].to_string();
            var cfRequestId = Request.Headers["CF-REQUEST-ID"].to_string();
            var requestId   = "{0}|{1}|{2}|{3}".format_with(cfRay, cfRequestId, requestIp, user.Username);

            if (string.IsNullOrWhiteSpace(cfRay))
            {
                // we are not using cloudflare, so remove some items
                requestId = "{0}|{1}".format_with(requestIp, user.Username);
            }

            Trace.TraceInformation("[{0}] - New package being pushed by {1} from ip address {2}".format_with(requestId, user.Username, requestIp));

            if (user.IsBanned)
            {
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Created, "Package has been pushed and will show up once moderated and approved."));
            }


            Trace.TraceInformation("[{0}] - Package has a content length of: {1} MB".format_with(requestId, Request.ContentLength / ONE_MB));

            if (Request.ContentLength > MAX_ALLOWED_CONTENT_LENGTH)
            {
                Trace.TraceError("[{0}] - Package is too large at '{1}' (max size allowed is '{2}'".format_with(requestId, Request.ContentLength, MAX_ALLOWED_CONTENT_LENGTH));
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.RequestEntityTooLarge, String.Format(CultureInfo.CurrentCulture, Strings.PackageTooLarge, MAX_ALLOWED_CONTENT_LENGTH / ONE_MB)));
            }

            // Tempfile to store the package from stream.
            // Based on https://github.com/NuGet/NuGetGallery/issues/3042
            var temporaryFile = Path.GetTempFileName();

            Trace.TraceInformation("[{0}] - Saving temp file for package at '{1}'".format_with(requestId, temporaryFile));
            var packageToPush = ReadPackageFromRequest(temporaryFile);

            var packageId      = packageToPush.Id;
            var packageVersion = packageToPush.Version;

            Trace.TraceInformation("[{0}] - Package being pushed is {1} (v{2})".format_with(requestId, packageId, packageVersion.to_string()));
            requestId += "|{0}|{1}".format_with(packageId, packageVersion);

            // don't allow forbidden package names to be pushed
            if (_forbiddenPackageNames.Contains(packageId, StringComparer.InvariantCultureIgnoreCase))
            {
                Trace.TraceError("[{0}] - Package is using a forbidden name of '{1}'".format_with(requestId, packageId));
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, String.Format(CultureInfo.CurrentCulture, Strings.ApiKeyNotAuthorized, "push")));
            }

            // Ensure that the user can push packages for this partialId.
            var packageRegistration = packageSvc.FindPackageRegistrationById(packageId, useCache: false);

            if (packageRegistration != null)
            {
                if (!packageRegistration.IsOwner(user))
                {
                    Trace.TraceError("[{0}] - User '{1}' doesn't have rights to push package '{2}'".format_with(requestId, user.Username, packageId));
                    return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Forbidden, String.Format(CultureInfo.CurrentCulture, Strings.ApiKeyNotAuthorized, "push")));
                }

                var existingPackage = packageRegistration.Packages.FirstOrDefault(p => p.Version.Equals(packageToPush.Version.ToString(), StringComparison.OrdinalIgnoreCase));

                if (existingPackage != null)
                {
                    switch (existingPackage.Status)
                    {
                    case PackageStatusType.Rejected:
                        var testReporterUser = userSvc.FindByUserId(settings.PackageOperationsUserKey);

                        if (existingPackage.PackageCleanupResultDate.HasValue &&
                            testReporterUser != null &&
                            existingPackage.ReviewedById == testReporterUser.Key
                            )
                        {
                            Trace.TraceInformation("[{0}] - Package version has been rejected, but change is allowed by cleanup service.".format_with(requestId));
                            //allow rejected by cleanup to return a value
                            break;
                        }

                        Trace.TraceError("[{0}] - Package version has been rejected and can no longer be submitted.".format_with(requestId));
                        return(new HttpStatusCodeWithBodyResult(
                                   HttpStatusCode.Conflict, string.Format("This package has been {0} and can no longer be submitted.", existingPackage.Status.GetDescriptionOrValue().ToLower())));

                    case PackageStatusType.Submitted:
                        //continue on
                        break;

                    default:
                        Trace.TraceError("[{0}] - Package version is in state {1} and can no longer be submitted.".format_with(requestId, existingPackage.Status.GetDescriptionOrValue().ToLower()));
                        return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Conflict, String.Format(CultureInfo.CurrentCulture, Strings.PackageExistsAndCannotBeModified, packageToPush.Id, packageToPush.Version)));
                    }
                }
                else if (!packageRegistration.Packages.Any(p => !p.IsPrerelease && p.Status == PackageStatusType.Approved) &&
                         packageRegistration.Packages.Any(p => p.Status == PackageStatusType.Submitted))
                {
                    Trace.TraceError("[{0}] - Package has a previous version in a submitted state with no approved stable releases.".format_with(requestId));
                    return(new HttpStatusCodeWithBodyResult(
                               HttpStatusCode.Forbidden,
                               string.Format("The package {0} has a previous version in a submitted state, and no approved stable releases. Please work to have the existing package version approved or rejected first.",
                                             packageId),
                               string.Format(@"
Please wait until a minimum of 1 version of the {0} package have been approved,
before pushing a new version.

If the package is currently failing, please see any failure emails sent
out on why it could be failing, as well as instructions on how to fix
any moderation related failures.",
                                             packageId)));
                }

                var allowedNumberOfPackageVersionsInSubmittedStatus = 10;
                var packageVersionsInModerationCount = packageRegistration.Packages.Count(p => p.Status == PackageStatusType.Submitted);
                if (packageVersionsInModerationCount >= allowedNumberOfPackageVersionsInSubmittedStatus)
                {
                    Trace.TraceError("[{0}] - Package has {1} versions currently in submitted state. We limit to {2} versions in moderation at a time.".format_with(requestId, packageVersionsInModerationCount, allowedNumberOfPackageVersionsInSubmittedStatus));
                    return(new HttpStatusCodeWithBodyResult(
                               HttpStatusCode.Forbidden,
                               string.Format("The package {0} has {1} versions currently in a submitted state. For moderation purposes we limit to {2} versions in moderation at a time. Please wait to have the existing package version(s) approved or rejected first.",
                                             packageId, packageVersionsInModerationCount, allowedNumberOfPackageVersionsInSubmittedStatus),
                               string.Format(@"
Please wait to submit more versions of {0} until the current versions are approved or rejected.

If the package is currently failing, please see any failure emails sent
out on why it could be failing, as well as instructions on how to fix
any moderation related failures.",
                                             packageId)));
                }
            }

            try
            {
                Trace.TraceInformation("[{0}] - Creating/updating package information in database.".format_with(requestId));
                packageSvc.CreatePackage(packageToPush, user, requestId);

                packageToPush = null;
            }
            catch (DbEntityValidationException dbvex)
            {
                Trace.TraceError("[{0}] - Pushing package '{1}' (v{2}) had error(s):", requestId, packageId, packageVersion.to_string());
                Trace.TraceError("[{0}] - {1}", requestId, dbvex.Message);

                foreach (var entityValidationError in dbvex.EntityValidationErrors)
                {
                    Trace.TraceError("[{0}] - Entity of type '{1}' in state '{2}' has the following validation errors:", requestId, entityValidationError.Entry.Entity.GetType().Name, entityValidationError.Entry.State);
                    foreach (var validationError in entityValidationError.ValidationErrors)
                    {
                        Trace.TraceError("[{0}]   - Property: '{1}', Error: '{2}'", requestId, validationError.PropertyName, validationError.ErrorMessage);
                    }
                }

                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Conflict, string.Format("This package had an issue pushing: {0}", dbvex.Message)));
            }
            catch (Exception ex)
            {
                var errorMessage = new StringBuilder();
                errorMessage.Append(ex.Message);
                foreach (var innerException in ex.get_inner_exceptions().OrEmptyListIfNull())
                {
                    errorMessage.AppendLine(innerException.Message);
                }

                // NOTE: The following code was added to establish whether a lack of hard drive space on the webhosts was causing
                // the Out of Memory exceptions that were occuring when pushing embedded packages.  It was found that lack of hard
                // drive space was not the issue.  As such, this logging has been commented out, and left for reference, should
                // it be needed again.
                //// Capture the amount of hard drive space left on the web server
                //// There is a suspicion that the web server might be having an issue with clearing up resources
                ////Trace.TraceInformation("[{0}] - Available Hard Drive Space:".format_with(requestId));
                ////DriveInfo[] allDrives = DriveInfo.GetDrives();
                ////foreach (DriveInfo drive in allDrives)
                ////{
                ////    Trace.TraceInformation("[{0}]   - Drive {1}".format_with(requestId, drive.Name));
                ////    if (drive.IsReady == true)
                ////    {
                ////        Trace.TraceInformation("[{0}]     - Available space to current user: {1} MBs. Total available space: {2} MBs. Total size of drive: {3} MBs", requestId, drive.AvailableFreeSpace / ONE_MB, drive.TotalFreeSpace / ONE_MB, drive.TotalSize / ONE_MB);
                ////    }
                ////}

                Trace.TraceError("[{0}] - Pushing package '{1}' (v{2}) had error(s):{3} {4}", requestId, packageId, packageVersion.to_string(), Environment.NewLine, errorMessage.to_string());
                return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Conflict, string.Format("This package had an issue pushing: {0}", ex.Message)));
            }
            finally
            {
                OptimizedZipPackage.PurgeCache();

                try
                {
                    Trace.TraceInformation("[{0}] - Deleting the temp file at '{1}'.".format_with(requestId, temporaryFile));
                    System.IO.File.Delete(temporaryFile);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("[{0}] - Unable to delete temporary file at '{1}':{2} {3}.".format_with(requestId, temporaryFile, Environment.NewLine, ex.to_string()));
                    ErrorSignal.FromCurrentContext().Raise(ex);
                    //todo: is this really an error if we can't remove a file? Everything else was successful for the user
                    //return new HttpStatusCodeWithBodyResult(HttpStatusCode.InternalServerError, "Could not remove temporary upload file: {0}", ex.Message);
                }
            }

            Trace.TraceInformation("[{0}] - Package {1} (v{2}) has been pushed successfully.".format_with(requestId, packageId, packageVersion.to_string()));
            return(new HttpStatusCodeWithBodyResult(HttpStatusCode.Created, "Package has been pushed and will show up once moderated and approved."));
        }
Esempio n. 12
0
        private static string append_process_items(string currentValues, IEnumerable<string> originalValues)
        {
            var additionalItems = new StringBuilder();
            var items = currentValues.Split(
                new[] { ApplicationParameters.Environment.EnvironmentSeparator },
                StringSplitOptions.RemoveEmptyEntries
                );

            foreach (string originalValue in originalValues.or_empty_list_if_null())
            {
                if (!items.Contains(originalValue, StringComparer.InvariantCultureIgnoreCase))
                {
                    additionalItems.AppendFormat("{0};", originalValue);
                }
            }

            return additionalItems.to_string();
        }
Esempio n. 13
0
        private string GetUrl(string filePath, string articleName = null)
        {
            if (!string.IsNullOrWhiteSpace(articleName)) return articleName;
            if (string.IsNullOrWhiteSpace(filePath)) return filePath;

            var fileName = Path.GetFileNameWithoutExtension(filePath);
            var hyphenatedValue = new StringBuilder();

            Char previousChar = '^';
            foreach (var valueChar in fileName)
            {
                if (Char.IsUpper(valueChar) && hyphenatedValue.Length != 0)
                {
                    hyphenatedValue.Append("-");
                }

                if (Char.IsDigit(valueChar) && !Char.IsDigit(previousChar) && hyphenatedValue.Length != 0)
                {
                    hyphenatedValue.Append("-");
                }

                previousChar = valueChar;
                hyphenatedValue.Append(valueChar.to_string());
            }

            return hyphenatedValue.to_string().to_lower();
        }
Esempio n. 14
0
        private void process_rejection(RejectPackageMessage message)
        {
            var resultsMessage = new StringBuilder();

            resultsMessage.AppendLine("Unfortunately there has not been progress to move {0} v{1} towards an approved status within 15 days after the last review message, so we need to close (reject) the package version at this time. If you want to pick this version up and move it towards approval in the future, use the contact site admins link on the package page and we can move it back into a submitted status so you can submit updates.".format_with(message.PackageId, message.PackageVersion));

            EventManager.publish(new FinalResultMessage(message.PackageId, message.PackageVersion, resultsMessage.to_string(), reject: true));
        }
Esempio n. 15
0
        private void process_reminder(ReminderPackageMessage message)
        {
            var resultsMessage = new StringBuilder();

            resultsMessage.AppendLine("We've found {0} v{1} in a submitted status and waiting for your next actions. It has had no updates for 20 or more days since a reviewer has asked for corrections. Please note that if there is no response or fix of the package within 15 days of this message, this package version will automatically be closed (rejected) due to being stale.".format_with(message.PackageId, message.PackageVersion));
            resultsMessage.AppendFormat("{0}Take action:{0}".format_with(Environment.NewLine));
            resultsMessage.AppendLine(" * Log in to the site and respond to the review comments.");
            resultsMessage.AppendLine(" * Resubmit fixes for this version.");
            resultsMessage.AppendLine(" * If the package version is failing automated checks, you can self-reject the package.");
            resultsMessage.AppendLine("{0}If your package is failing automated testing, you can use the [chocolatey test environment](https://github.com/chocolatey/chocolatey-test-environment) to manually run the verification and determine what may need to be fixed.".format_with(Environment.NewLine));
            resultsMessage.AppendLine("{0}**Note**: We don't like to see packages automatically rejected. It doesn't mean that we don't value your contributions, just that we can not continue to hold packages versions in a waiting status that have possibly been abandoned. If you don't believe you will be able to fix up this version of the package within 15 days, we strongly urge you to log in to the site and respond to the review comments until you are able to.".format_with(Environment.NewLine));

            EventManager.publish(new FinalResultMessage(message.PackageId, message.PackageVersion, resultsMessage.to_string(), reject: false));
        }