private PSRepositoryInfo UpdateRepositoryStoreHelper(string repoName, Uri repoUrl, int repoPriority, bool repoTrusted) { if (repoUrl != null && !(repoUrl.Scheme == Uri.UriSchemeHttp || repoUrl.Scheme == Uri.UriSchemeHttps || repoUrl.Scheme == Uri.UriSchemeFtp || repoUrl.Scheme == Uri.UriSchemeFile)) { throw new ArgumentException("Invalid url, must be one of the following Uri schemes: HTTPS, HTTP, FTP, File Based"); } // check repoName can't contain * or just be whitespace // remove trailing and leading whitespaces, and if Name is just whitespace Name should become null now and be caught by following condition repoName = repoName.Trim(); if (String.IsNullOrEmpty(repoName) || repoName.Contains("*")) { throw new ArgumentException("Name cannot be null/empty, contain asterisk or be just whitespace"); } // check PSGallery URL is not trying to be set if (repoName.Equals("PSGallery", StringComparison.OrdinalIgnoreCase) && repoUrl != null) { throw new ArgumentException("The PSGallery repository has a pre-defined URL. Setting the -URL parmeter for this repository is not allowed, instead try running 'Register-PSResourceRepository -PSGallery'."); } // determine trusted value to pass in (true/false if set, null otherwise, hence the nullable bool variable) bool?_trustedNullable = isSet ? new bool?(repoTrusted) : new bool?(); // determine if either 1 of 3 values are attempting to be set: URL, Priority, Trusted. // if none are (i.e only Name parameter was provided, write error) if (repoUrl == null && repoPriority == DefaultPriority && _trustedNullable == null) { throw new ArgumentException("Either URL, Priority or Trusted parameters must be requested to be set"); } WriteVerbose("All required values to set repository provided, calling internal Update() API now"); if (!ShouldProcess(repoName, "Set repository's value(s) in repository store")) { return(null); } return(RepositorySettings.Update(repoName, repoUrl, repoPriority, _trustedNullable)); }
private PSRepositoryInfo UpdateRepositoryStoreHelper(string repoName, Uri repoUri, int repoPriority, bool repoTrusted, PSCredentialInfo repoCredentialInfo) { if (repoUri != null && !(repoUri.Scheme == System.Uri.UriSchemeHttp || repoUri.Scheme == System.Uri.UriSchemeHttps || repoUri.Scheme == System.Uri.UriSchemeFtp || repoUri.Scheme == System.Uri.UriSchemeFile)) { throw new ArgumentException("Invalid Uri, must be one of the following Uri schemes: HTTPS, HTTP, FTP, File Based"); } // check repoName can't contain * or just be whitespace // remove trailing and leading whitespaces, and if Name is just whitespace Name should become null now and be caught by following condition repoName = repoName.Trim(); if (String.IsNullOrEmpty(repoName) || repoName.Contains("*")) { throw new ArgumentException("Name cannot be null/empty, contain asterisk or be just whitespace"); } // check PSGallery Uri is not trying to be set if (repoName.Equals("PSGallery", StringComparison.OrdinalIgnoreCase) && repoUri != null) { throw new ArgumentException("The PSGallery repository has a pre-defined Uri. Setting the -Uri parameter for this repository is not allowed, instead try running 'Register-PSResourceRepository -PSGallery'."); } // check PSGallery CredentialInfo is not trying to be set if (repoName.Equals("PSGallery", StringComparison.OrdinalIgnoreCase) && repoCredentialInfo != null) { throw new ArgumentException("The PSGallery repository does not require authentication. Setting the -CredentialInfo parameter for this repository is not allowed, instead try running 'Register-PSResourceRepository -PSGallery'."); } // determine trusted value to pass in (true/false if set, null otherwise, hence the nullable bool variable) bool?_trustedNullable = isSet ? new bool?(repoTrusted) : new bool?(); if (repoCredentialInfo != null) { bool isSecretManagementModuleAvailable = Utils.IsSecretManagementModuleAvailable(repoName, this); if (repoCredentialInfo.Credential != null) { if (!isSecretManagementModuleAvailable) { ThrowTerminatingError(new ErrorRecord( new PSInvalidOperationException($"Microsoft.PowerShell.SecretManagement module is not found, but is required for saving PSResourceRepository {repoName}'s Credential in a vault."), "RepositoryCredentialSecretManagementUnavailableModule", ErrorCategory.ResourceUnavailable, this)); } else { Utils.SaveRepositoryCredentialToSecretManagementVault(repoName, repoCredentialInfo, this); } } if (!isSecretManagementModuleAvailable) { WriteWarning($"Microsoft.PowerShell.SecretManagement module cannot be found. Make sure it is installed before performing PSResource operations in order to successfully authenticate to PSResourceRepository \"{repoName}\" with its CredentialInfo."); } } // determine if either 1 of 4 values are attempting to be set: Uri, Priority, Trusted, CredentialInfo. // if none are (i.e only Name parameter was provided, write error) if (repoUri == null && repoPriority == DefaultPriority && _trustedNullable == null && repoCredentialInfo == null) { throw new ArgumentException("Either Uri, Priority, Trusted or CredentialInfo parameters must be requested to be set"); } WriteVerbose("All required values to set repository provided, calling internal Update() API now"); if (!ShouldProcess(repoName, "Set repository's value(s) in repository store")) { return(null); } return(RepositorySettings.Update(repoName, repoUri, repoPriority, _trustedNullable, repoCredentialInfo)); }