/// <summary> /// Updates a repository name, URL, priority, or installation policy /// Returns: void /// </summary> public static PSRepositoryInfo Update(string repoName, Uri repoURL, int repoPriority, bool?repoTrusted) { PSRepositoryInfo updatedRepo; try { // Open file XDocument doc = LoadXDocument(FullRepositoryPath); XElement node = FindRepositoryElement(doc, repoName); if (node == null) { throw new ArgumentException("Cannot find the repository because it does not exist. Try registering the repository using 'Register-PSResourceRepository'"); } // Else, keep going // Get root of XDocument (XElement) var root = doc.Root; // A null URL value passed in signifies the URL was not attempted to be set. // So only set Url attribute if non-null value passed in for repoUrl if (repoURL != null) { node.Attribute("Url").Value = repoURL.AbsoluteUri; } // A negative Priority value passed in signifies the Priority value was not attempted to be set. // So only set Priority attribute if non-null value passed in for repoPriority if (repoPriority >= 0) { node.Attribute("Priority").Value = repoPriority.ToString(); } // A null Trusted value passed in signifies the Trusted value was not attempted to be set. // So only set Trusted attribute if non-null value passed in for repoTrusted. if (repoTrusted != null) { node.Attribute("Trusted").Value = repoTrusted.ToString(); } // Create Uri from node Url attribute to create PSRepositoryInfo item to return. if (!Uri.TryCreate(node.Attribute("Url").Value, UriKind.Absolute, out Uri thisUrl)) { throw new PSInvalidOperationException(String.Format("Unable to read incorrectly formatted URL for repo {0}", repoName)); } updatedRepo = new PSRepositoryInfo(repoName, thisUrl, Int32.Parse(node.Attribute("Priority").Value), Boolean.Parse(node.Attribute("Trusted").Value)); // Close the file root.Save(FullRepositoryPath); } catch (Exception e) { throw new PSInvalidOperationException(String.Format("Updating to repository store failed: {0}", e.Message)); } return(updatedRepo); }
public static List <PSRepositoryInfo> Read(string[] repoNames, out string[] errorList) { List <string> tempErrorList = new List <string>(); var foundRepos = new List <PSRepositoryInfo>(); XDocument doc; try { // Open file doc = LoadXDocument(FullRepositoryPath); } catch (Exception e) { throw new PSInvalidOperationException(String.Format("Loading repository store failed: {0}", e.Message)); } if (repoNames == null || !repoNames.Any() || string.Equals(repoNames[0], "*") || repoNames[0] == null) { // Name array or single value is null so we will list all repositories registered // iterate through the doc foreach (XElement repo in doc.Descendants("Repository")) { if (!Uri.TryCreate(repo.Attribute("Uri").Value, UriKind.Absolute, out Uri thisUri)) { tempErrorList.Add(String.Format("Unable to read incorrectly formatted Uri for repo {0}", repo.Attribute("Name").Value)); continue; } PSCredentialInfo thisCredentialInfo; string credentialInfoErrorMessage = $"Repository {repo.Attribute("Name").Value} has invalid CredentialInfo. {PSCredentialInfo.VaultNameAttribute} and {PSCredentialInfo.SecretNameAttribute} should both be present and non-empty"; // both keys are present if (repo.Attribute(PSCredentialInfo.VaultNameAttribute) != null && repo.Attribute(PSCredentialInfo.SecretNameAttribute) != null) { try { // both values are non-empty // = valid credentialInfo thisCredentialInfo = new PSCredentialInfo(repo.Attribute(PSCredentialInfo.VaultNameAttribute).Value, repo.Attribute(PSCredentialInfo.SecretNameAttribute).Value); } catch (Exception) { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } } // both keys are missing else if (repo.Attribute(PSCredentialInfo.VaultNameAttribute) == null && repo.Attribute(PSCredentialInfo.SecretNameAttribute) == null) { // = valid credentialInfo thisCredentialInfo = null; } // one of the keys is missing else { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(repo.Attribute("Name").Value, thisUri, Int32.Parse(repo.Attribute("Priority").Value), Boolean.Parse(repo.Attribute("Trusted").Value), thisCredentialInfo); foundRepos.Add(currentRepoItem); } } else { foreach (string repo in repoNames) { bool repoMatch = false; WildcardPattern nameWildCardPattern = new WildcardPattern(repo, WildcardOptions.IgnoreCase); foreach (var node in doc.Descendants("Repository").Where(e => nameWildCardPattern.IsMatch(e.Attribute("Name").Value))) { repoMatch = true; if (!Uri.TryCreate(node.Attribute("Uri").Value, UriKind.Absolute, out Uri thisUri)) { //debug statement tempErrorList.Add(String.Format("Unable to read incorrectly formatted Uri for repo {0}", node.Attribute("Name").Value)); continue; } PSCredentialInfo thisCredentialInfo; string credentialInfoErrorMessage = $"Repository {node.Attribute("Name").Value} has invalid CredentialInfo. {PSCredentialInfo.VaultNameAttribute} and {PSCredentialInfo.SecretNameAttribute} should both be present and non-empty"; // both keys are present if (node.Attribute(PSCredentialInfo.VaultNameAttribute) != null && node.Attribute(PSCredentialInfo.SecretNameAttribute) != null) { try { // both values are non-empty // = valid credentialInfo thisCredentialInfo = new PSCredentialInfo(node.Attribute(PSCredentialInfo.VaultNameAttribute).Value, node.Attribute(PSCredentialInfo.SecretNameAttribute).Value); } catch (Exception) { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } } // both keys are missing else if (node.Attribute(PSCredentialInfo.VaultNameAttribute) == null && node.Attribute(PSCredentialInfo.SecretNameAttribute) == null) { // = valid credentialInfo thisCredentialInfo = null; } // one of the keys is missing else { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(node.Attribute("Name").Value, thisUri, Int32.Parse(node.Attribute("Priority").Value), Boolean.Parse(node.Attribute("Trusted").Value), thisCredentialInfo); foundRepos.Add(currentRepoItem); } if (!repo.Contains("*") && !repoMatch) { tempErrorList.Add(String.Format("Unable to find repository with Name '{0}'. Use Get-PSResourceRepository to see all available repositories.", repo)); } } } errorList = tempErrorList.ToArray(); // Sort by priority, then by repo name var reposToReturn = foundRepos.OrderBy(x => x.Priority).ThenBy(x => x.Name); return(reposToReturn.ToList()); }
/// <summary> /// Updates a repository name, Uri, priority, installation policy, or credential information /// Returns: void /// </summary> public static PSRepositoryInfo Update(string repoName, Uri repoUri, int repoPriority, bool?repoTrusted, PSCredentialInfo repoCredentialInfo) { PSRepositoryInfo updatedRepo; try { // Open file XDocument doc = LoadXDocument(FullRepositoryPath); XElement node = FindRepositoryElement(doc, repoName); if (node == null) { throw new ArgumentException("Cannot find the repository because it does not exist. Try registering the repository using 'Register-PSResourceRepository'"); } // Else, keep going // Get root of XDocument (XElement) var root = doc.Root; // A null Uri value passed in signifies the Uri was not attempted to be set. // So only set Uri attribute if non-null value passed in for repoUri if (repoUri != null) { node.Attribute("Uri").Value = repoUri.AbsoluteUri; } // A negative Priority value passed in signifies the Priority value was not attempted to be set. // So only set Priority attribute if non-null value passed in for repoPriority if (repoPriority >= 0) { node.Attribute("Priority").Value = repoPriority.ToString(); } // A null Trusted value passed in signifies the Trusted value was not attempted to be set. // So only set Trusted attribute if non-null value passed in for repoTrusted. if (repoTrusted != null) { node.Attribute("Trusted").Value = repoTrusted.ToString(); } // A null CredentialInfo value passed in signifies that CredentialInfo was not attempted to be set. // Set VaultName and SecretName attributes if non-null value passed in for repoCredentialInfo if (repoCredentialInfo != null) { if (node.Attribute(PSCredentialInfo.VaultNameAttribute) == null) { node.Add(new XAttribute(PSCredentialInfo.VaultNameAttribute, repoCredentialInfo.VaultName)); } else { node.Attribute(PSCredentialInfo.VaultNameAttribute).Value = repoCredentialInfo.VaultName; } if (node.Attribute(PSCredentialInfo.SecretNameAttribute) == null) { node.Add(new XAttribute(PSCredentialInfo.SecretNameAttribute, repoCredentialInfo.SecretName)); } else { node.Attribute(PSCredentialInfo.SecretNameAttribute).Value = repoCredentialInfo.SecretName; } } // Create Uri from node Uri attribute to create PSRepositoryInfo item to return. if (!Uri.TryCreate(node.Attribute("Uri").Value, UriKind.Absolute, out Uri thisUri)) { throw new PSInvalidOperationException(String.Format("Unable to read incorrectly formatted Uri for repo {0}", repoName)); } // Create CredentialInfo based on new values or whether it was empty to begin with PSCredentialInfo thisCredentialInfo = null; if (node.Attribute(PSCredentialInfo.VaultNameAttribute)?.Value != null && node.Attribute(PSCredentialInfo.SecretNameAttribute)?.Value != null) { thisCredentialInfo = new PSCredentialInfo( node.Attribute(PSCredentialInfo.VaultNameAttribute).Value, node.Attribute(PSCredentialInfo.SecretNameAttribute).Value); } updatedRepo = new PSRepositoryInfo(repoName, thisUri, Int32.Parse(node.Attribute("Priority").Value), Boolean.Parse(node.Attribute("Trusted").Value), thisCredentialInfo); // Close the file root.Save(FullRepositoryPath); } catch (Exception e) { throw new PSInvalidOperationException(String.Format("Updating to repository store failed: {0}", e.Message)); } return(updatedRepo); }
public static List <PSRepositoryInfo> Read(string[] repoNames, out string[] errorList) { List <string> tempErrorList = new List <string>(); var foundRepos = new List <PSRepositoryInfo>(); XDocument doc; try { // Open file doc = LoadXDocument(FullRepositoryPath); } catch (Exception e) { throw new PSInvalidOperationException(String.Format("Loading repository store failed: {0}", e.Message)); } if (repoNames == null || repoNames.Length == 0 || string.Equals(repoNames[0], "*") || repoNames[0] == null) { // Name array or single value is null so we will list all repositories registered // iterate through the doc foreach (XElement repo in doc.Descendants("Repository")) { if (repo.Attribute("Name") == null) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Name' attribute, in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } if (repo.Attribute("Priority") == null) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Priority' attribute, in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } if (repo.Attribute("Trusted") == null) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Trusted' attribute, in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } bool urlAttributeExists = repo.Attribute("Url") != null; bool uriAttributeExists = repo.Attribute("Uri") != null; // case: neither Url nor Uri attributes exist if (!urlAttributeExists && !uriAttributeExists) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Url' or equivalent 'Uri' attribute (it must contain one), in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } Uri thisUrl = null; // case: either attribute Uri or Url exists // TODO: do we only allow both to exist, across repositories? (i.e if a file has Uri attribute for one repo and Url attribute for another --> is that invalid?) if (urlAttributeExists) { if (!Uri.TryCreate(repo.Attribute("Url").Value, UriKind.Absolute, out thisUrl)) { tempErrorList.Add(String.Format("Unable to read incorrectly formatted Url for repo {0}", repo.Attribute("Name").Value)); continue; } } else if (uriAttributeExists) { if (!Uri.TryCreate(repo.Attribute("Uri").Value, UriKind.Absolute, out thisUrl)) { tempErrorList.Add(String.Format("Unable to read incorrectly formatted Uri for repo {0}", repo.Attribute("Name").Value)); continue; } } PSCredentialInfo thisCredentialInfo; string credentialInfoErrorMessage = $"Repository {repo.Attribute("Name").Value} has invalid CredentialInfo. {PSCredentialInfo.VaultNameAttribute} and {PSCredentialInfo.SecretNameAttribute} should both be present and non-empty"; // both keys are present if (repo.Attribute(PSCredentialInfo.VaultNameAttribute) != null && repo.Attribute(PSCredentialInfo.SecretNameAttribute) != null) { try { // both values are non-empty // = valid credentialInfo thisCredentialInfo = new PSCredentialInfo(repo.Attribute(PSCredentialInfo.VaultNameAttribute).Value, repo.Attribute(PSCredentialInfo.SecretNameAttribute).Value); } catch (Exception) { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } } // both keys are missing else if (repo.Attribute(PSCredentialInfo.VaultNameAttribute) == null && repo.Attribute(PSCredentialInfo.SecretNameAttribute) == null) { // = valid credentialInfo thisCredentialInfo = null; } // one of the keys is missing else { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(repo.Attribute("Name").Value, thisUrl, Int32.Parse(repo.Attribute("Priority").Value), Boolean.Parse(repo.Attribute("Trusted").Value), thisCredentialInfo); foundRepos.Add(currentRepoItem); } } else { foreach (string repo in repoNames) { bool repoMatch = false; WildcardPattern nameWildCardPattern = new WildcardPattern(repo, WildcardOptions.IgnoreCase); foreach (var node in doc.Descendants("Repository").Where(e => e.Attribute("Name") != null && nameWildCardPattern.IsMatch(e.Attribute("Name").Value))) { if (node.Attribute("Priority") == null) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Priority' attribute, in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } if (node.Attribute("Trusted") == null) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Trusted' attribute, in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } repoMatch = true; bool urlAttributeExists = node.Attribute("Url") != null; bool uriAttributeExists = node.Attribute("Uri") != null; // case: neither Url nor Uri attributes exist if (!urlAttributeExists && !uriAttributeExists) { tempErrorList.Add(String.Format("Repository element does not contain neccessary 'Url' or equivalent 'Uri' attribute (it must contain one), in file located at path: {0}. Fix this in your file and run again.", FullRepositoryPath)); continue; } Uri thisUrl = null; // case: either attribute Uri or Url exists // TODO: do we only allow both to exist, across repositories? (i.e if a file has Uri attribute for one repo and Url attribute for another --> is that invalid?) if (urlAttributeExists) { if (!Uri.TryCreate(node.Attribute("Url").Value, UriKind.Absolute, out thisUrl)) { tempErrorList.Add(String.Format("Unable to read incorrectly formatted Url for repo {0}", node.Attribute("Name").Value)); continue; } } else if (uriAttributeExists) { if (!Uri.TryCreate(node.Attribute("Uri").Value, UriKind.Absolute, out thisUrl)) { tempErrorList.Add(String.Format("Unable to read incorrectly formatted Uri for repo {0}", node.Attribute("Name").Value)); continue; } } PSCredentialInfo thisCredentialInfo; string credentialInfoErrorMessage = $"Repository {node.Attribute("Name").Value} has invalid CredentialInfo. {PSCredentialInfo.VaultNameAttribute} and {PSCredentialInfo.SecretNameAttribute} should both be present and non-empty"; // both keys are present if (node.Attribute(PSCredentialInfo.VaultNameAttribute) != null && node.Attribute(PSCredentialInfo.SecretNameAttribute) != null) { try { // both values are non-empty // = valid credentialInfo thisCredentialInfo = new PSCredentialInfo(node.Attribute(PSCredentialInfo.VaultNameAttribute).Value, node.Attribute(PSCredentialInfo.SecretNameAttribute).Value); } catch (Exception) { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } } // both keys are missing else if (node.Attribute(PSCredentialInfo.VaultNameAttribute) == null && node.Attribute(PSCredentialInfo.SecretNameAttribute) == null) { // = valid credentialInfo thisCredentialInfo = null; } // one of the keys is missing else { thisCredentialInfo = null; tempErrorList.Add(credentialInfoErrorMessage); continue; } PSRepositoryInfo currentRepoItem = new PSRepositoryInfo(node.Attribute("Name").Value, thisUrl, Int32.Parse(node.Attribute("Priority").Value), Boolean.Parse(node.Attribute("Trusted").Value), thisCredentialInfo); foundRepos.Add(currentRepoItem); } if (!repo.Contains("*") && !repoMatch) { tempErrorList.Add(String.Format("Unable to find repository with Name '{0}'. Use Get-PSResourceRepository to see all available repositories.", repo)); } } } errorList = tempErrorList.ToArray(); // Sort by priority, then by repo name var reposToReturn = foundRepos.OrderBy(x => x.Priority).ThenBy(x => x.Name); return(reposToReturn.ToList()); }
/// <summary> /// Updates a repository name, Uri, priority, installation policy, or credential information /// Returns: void /// </summary> public static PSRepositoryInfo Update(string repoName, Uri repoUri, int repoPriority, bool?repoTrusted, PSCredentialInfo repoCredentialInfo, PSCmdlet cmdletPassedIn, out string errorMsg) { errorMsg = string.Empty; PSRepositoryInfo updatedRepo; try { // Open file XDocument doc = LoadXDocument(FullRepositoryPath); XElement node = FindRepositoryElement(doc, repoName); if (node == null) { bool repoIsTrusted = !(repoTrusted == null || repoTrusted == false); repoPriority = repoPriority < 0 ? DefaultPriority : repoPriority; return(AddToRepositoryStore(repoName, repoUri, repoPriority, repoIsTrusted, repoCredentialInfo, force: true, cmdletPassedIn, out errorMsg)); } // Check that repository node we are attempting to update has all required attributes: Name, Url (or Uri), Priority, Trusted. // Name attribute is already checked for in FindRepositoryElement() if (node.Attribute("Priority") == null) { errorMsg = $"Repository element does not contain neccessary 'Priority' attribute, in file located at path: {FullRepositoryPath}. Fix this in your file and run again."; return(null); } if (node.Attribute("Trusted") == null) { errorMsg = $"Repository element does not contain neccessary 'Trusted' attribute, in file located at path: {FullRepositoryPath}. Fix this in your file and run again."; return(null); } bool urlAttributeExists = node.Attribute("Url") != null; bool uriAttributeExists = node.Attribute("Uri") != null; if (!urlAttributeExists && !uriAttributeExists) { errorMsg = $"Repository element does not contain neccessary 'Url' attribute (or alternatively 'Uri' attribute), in file located at path: {FullRepositoryPath}. Fix this in your file and run again."; return(null); } // Else, keep going // Get root of XDocument (XElement) var root = doc.Root; // A null Uri (or Url) value passed in signifies the Uri was not attempted to be set. // So only set Uri attribute if non-null value passed in for repoUri // determine if existing repository node (which we wish to update) had Url or Uri attribute Uri thisUrl = null; if (repoUri != null) { if (!Uri.TryCreate(repoUri.AbsoluteUri, UriKind.Absolute, out thisUrl)) { throw new PSInvalidOperationException(String.Format("Unable to read incorrectly formatted Url for repo {0}", repoName)); } if (urlAttributeExists) { node.Attribute("Url").Value = thisUrl.AbsoluteUri; } else { node.Attribute("Uri").Value = thisUrl.AbsoluteUri; } } else { if (urlAttributeExists) { if (!Uri.TryCreate(node.Attribute("Url").Value, UriKind.Absolute, out thisUrl)) { throw new PSInvalidOperationException(String.Format("The 'Url' for repository {0} is invalid and the repository cannot be used. Please update the Url field or remove the repository entry.", repoName)); } } else { if (!Uri.TryCreate(node.Attribute("Uri").Value, UriKind.Absolute, out thisUrl)) { throw new PSInvalidOperationException(String.Format("The 'Url' for repository {0} is invalid and the repository cannot be used. Please update the Url field or remove the repository entry.", repoName)); } } } // A negative Priority value passed in signifies the Priority value was not attempted to be set. // So only set Priority attribute if non-null value passed in for repoPriority if (repoPriority >= 0) { node.Attribute("Priority").Value = repoPriority.ToString(); } // A null Trusted value passed in signifies the Trusted value was not attempted to be set. // So only set Trusted attribute if non-null value passed in for repoTrusted. if (repoTrusted != null) { node.Attribute("Trusted").Value = repoTrusted.ToString(); } // A null CredentialInfo value passed in signifies that CredentialInfo was not attempted to be set. // Set VaultName and SecretName attributes if non-null value passed in for repoCredentialInfo if (repoCredentialInfo != null) { if (node.Attribute(PSCredentialInfo.VaultNameAttribute) == null) { node.Add(new XAttribute(PSCredentialInfo.VaultNameAttribute, repoCredentialInfo.VaultName)); } else { node.Attribute(PSCredentialInfo.VaultNameAttribute).Value = repoCredentialInfo.VaultName; } if (node.Attribute(PSCredentialInfo.SecretNameAttribute) == null) { node.Add(new XAttribute(PSCredentialInfo.SecretNameAttribute, repoCredentialInfo.SecretName)); } else { node.Attribute(PSCredentialInfo.SecretNameAttribute).Value = repoCredentialInfo.SecretName; } } // Create CredentialInfo based on new values or whether it was empty to begin with PSCredentialInfo thisCredentialInfo = null; if (node.Attribute(PSCredentialInfo.VaultNameAttribute)?.Value != null && node.Attribute(PSCredentialInfo.SecretNameAttribute)?.Value != null) { thisCredentialInfo = new PSCredentialInfo( node.Attribute(PSCredentialInfo.VaultNameAttribute).Value, node.Attribute(PSCredentialInfo.SecretNameAttribute).Value); } updatedRepo = new PSRepositoryInfo(repoName, thisUrl, Int32.Parse(node.Attribute("Priority").Value), Boolean.Parse(node.Attribute("Trusted").Value), thisCredentialInfo); // Close the file root.Save(FullRepositoryPath); } catch (Exception e) { throw new PSInvalidOperationException(String.Format("Updating to repository store failed: {0}", e.Message)); } return(updatedRepo); }