Skip to content

Commit

Permalink
Upgraded extension for VS2019.
Browse files Browse the repository at this point in the history
  • Loading branch information
ernstc committed Aug 27, 2023
1 parent 1175152 commit 968d346
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 304 deletions.
168 changes: 0 additions & 168 deletions SolutionSecrets2019/Commands/PullCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,174 +69,6 @@ private async Task PullSecretsAsync()
var solutionFullName = SolutionSecrets2019Package._dte.Solution.FullName;

await _pullCommand.Execute(solutionFullName);

/*
SolutionFile solution = new SolutionFile(solutionFullName);
ICollection<SecretFile> secretFiles = solution.GetProjectsSecretFiles();
if (secretFiles.Count == 0)
{
await UseStatusBarAsync("No secrets found.");
return;
}
var synchronizationSettings = solution.CustomSynchronizationSettings;
// Select the repository for the curront solution
IRepository repository = Context.Current.GetRepository(synchronizationSettings) ?? Context.Current.Repository;
await UseStatusBarAsync($"Pulling secrets from {repository.RepositoryTypeFullName} for the solution: {solution.Name} ...");
if (repository is AzureKeyVaultRepository azureKvRepository)
{
if (!await azureKvRepository.IsReady())
{
try
{
await azureKvRepository.AuthorizeAsync();
}
catch (AuthenticationFailedException)
{
System.Windows.MessageBox.Show($"Azure authentication failed. Check your credential in\nTools -> Options -> Azure Service Authentication.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Exclamation);
await UseStatusBarAsync(String.Empty);
return;
}
catch (Exception)
{
await UseStatusBarAsync("Error pulling secrets for the solution.");
return;
}
}
if (!await azureKvRepository.IsReady())
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
System.Windows.MessageBox.Show($"Access denied to Azure Key Vault {azureKvRepository.RepositoryName}.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Exclamation);
commandService.OpenOption<Options.AzureKeyVault.AzureKeyVaultOptionPage>();
await UseStatusBarAsync("Error pulling secrets for the solution.");
return;
}
}
else if (!await Context.Current.Cipher.IsReady() || !await repository.IsReady())
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
System.Windows.MessageBox.Show("You need to configure the solution secrets synchronization before using the Push command.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Exclamation);
commandService.OpenOption<Options.GitHubGists.GitHubGistsOptionPage>();
await UseStatusBarAsync("Error pulling secrets for the solution.");
return;
}
var repositoryFiles = await repository.PullFilesAsync(solution);
if (repositoryFiles.Count == 0)
{
await UseStatusBarAsync("Failed, secrets not found.");
return;
}
// Validate header file
HeaderFile header = null;
foreach (var file in repositoryFiles)
{
if (file.name == "secrets" && file.content != null)
{
try
{
header = JsonConvert.DeserializeObject<HeaderFile>(file.content);
}
catch
{ }
break;
}
}
if (header == null)
{
await UseStatusBarAsync("Error pulling secrets for the solution.");
return;
}
if (!header.IsVersionSupported())
{
await UseStatusBarAsync("Secrets format is not compatible.");
return;
}
bool failed = false;
foreach (var repositoryFile in repositoryFiles)
{
if (repositoryFile.name != "secrets")
{
if (repositoryFile.content == null)
{
continue;
}
Dictionary<string, string> remoteSecretFiles = null;
try
{
remoteSecretFiles = JsonConvert.DeserializeObject<Dictionary<string, string>>(repositoryFile.content);
}
catch
{
await UseStatusBarAsync("Error pulling secrets for the solution.");
}
if (remoteSecretFiles == null)
{
failed = true;
break;
}
foreach (var remoteSecretFile in remoteSecretFiles)
{
string secretFileName = remoteSecretFile.Key;
// This check is for compatibility with version 1.0.x
if (secretFileName == "content")
{
secretFileName = "secrets.json";
}
foreach (var localSecretFile in secretFiles)
{
if (localSecretFile.ContainerName == repositoryFile.name
&& localSecretFile.Name == secretFileName)
{
localSecretFile.Content = remoteSecretFile.Value;
bool isFileOk = true;
if (repository.EncryptOnClient)
{
isFileOk = localSecretFile.Decrypt();
}
if (isFileOk)
{
solution.SaveSecretSettingsFile(localSecretFile);
}
else
{
failed = true;
}
break;
}
}
}
if (failed)
{
break;
}
}
}
if (!failed)
await UseStatusBarAsync($"Secrets pulled successfully from {repository.RepositoryTypeFullName}.");
else
await UseStatusBarAsync("Secrets pull has failed!");
*/
}

}
Expand Down
121 changes: 0 additions & 121 deletions SolutionSecrets2019/Commands/PushCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,127 +69,6 @@ private async Task PushSecretsAsync()
var solutionFullName = SolutionSecrets2019Package._dte.Solution.FullName;

await _pushCommand.Execute(solutionFullName);

/*
SolutionFile solution = new SolutionFile(solutionFullName);
var secretFiles = solution.GetProjectsSecretFiles();
if (secretFiles.Count == 0)
{
await UseStatusBarAsync("No secrets found.");
return;
}
var synchronizationSettings = solution.CustomSynchronizationSettings;
// Select the repository for the curront solution
IRepository repository = Context.Current.GetRepository(synchronizationSettings) ?? Context.Current.Repository;
await UseStatusBarAsync($"Pushing secrets to {repository.RepositoryTypeFullName} for the solution: {solution.Name} ...");
if (repository is AzureKeyVaultRepository azureKvRepository)
{
if (!await azureKvRepository.IsReady())
{
try
{
await azureKvRepository.AuthorizeAsync();
}
catch (AuthenticationFailedException ex)
{
System.Windows.MessageBox.Show(ex.Message, Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Exclamation);
await UseStatusBarAsync(String.Empty);
return;
}
catch (Exception)
{
await UseStatusBarAsync("Error pushing secrets for the solution.");
return;
}
}
if (!await azureKvRepository.IsReady())
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
System.Windows.MessageBox.Show($"Access denied to Azure Key Vault {azureKvRepository.RepositoryName}.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Exclamation);
commandService.OpenOption<Options.AzureKeyVault.AzureKeyVaultOptionPage>();
await UseStatusBarAsync("Error pushing secrets for the solution.");
return;
}
}
else if (!await Context.Current.Cipher.IsReady() || !await repository.IsReady())
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
System.Windows.MessageBox.Show("You need to configure the solution secrets synchronization before using the Push command.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Exclamation);
commandService.OpenOption<Options.GitHubGists.GitHubGistsOptionPage>();
await UseStatusBarAsync("Error pushing secrets for the solution.");
return;
}
var headerFile = new HeaderFile
{
visualStudioSolutionSecretsVersion = Versions.VersionString,
lastUpload = DateTime.UtcNow,
solutionFile = solution.Name,
solutionGuid = solution.Uid
};
var files = new List<(string fileName, string content)>
{
("secrets", JsonConvert.SerializeObject(headerFile, Formatting.None))
};
var secrets = new Dictionary<string, Dictionary<string, string>>();
bool isEmpty = true;
bool failed = false;
foreach (var secretFile in secretFiles)
{
if (secretFile.Content != null)
{
isEmpty = false;
bool isFileOk = true;
if (repository.EncryptOnClient)
{
isFileOk = secretFile.Encrypt();
}
if (isFileOk)
{
if (!secrets.ContainsKey(secretFile.ContainerName))
{
secrets.Add(secretFile.ContainerName, new Dictionary<string, string>());
}
secrets[secretFile.ContainerName].Add(secretFile.Name, secretFile.Content);
}
else
{
failed = true;
break;
}
}
}
foreach (var group in secrets)
{
string groupContent = JsonConvert.SerializeObject(group.Value);
files.Add((group.Key, groupContent));
}
if (!isEmpty && !failed)
{
if (await repository.PushFilesAsync(solution, files))
await UseStatusBarAsync($"Secrets pushed successfully to {repository.RepositoryTypeFullName}.");
else
failed = true;
}
if (failed)
{
await UseStatusBarAsync("Secrets push has failed!");
}
*/
}

}
Expand Down
41 changes: 29 additions & 12 deletions SolutionSecrets2019/ConfigDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,23 @@ private async void btnOk_Click(object sender, RoutedEventArgs e)
if (cboxRepositoryType.SelectedIndex == AZURE_KV)
{
var repository = (AzureKeyVaultRepository)CoreContext.Current.GetService<IRepository>(nameof(RepositoryType.AzureKV));
repository.RepositoryName = txtAKVUrl.Text;
if (repository.RepositoryName == null)
if (String.IsNullOrWhiteSpace(txtAKVUrl.Text))
{
System.Windows.MessageBox.Show("The key vault URL is not correct.", Constants.MESSAGE_BOX_TITLE, MessageBoxButton.OK);
return;
repository.RepositoryName = null;
}
else if (txtAKVUrl.Text != repository.RepositoryName)
else
{
await _package.JoinableTaskFactory.SwitchToMainThreadAsync();
txtAKVUrl.Text = repository.RepositoryName;
repository.RepositoryName = txtAKVUrl.Text;
if (repository.RepositoryName == null)
{
System.Windows.MessageBox.Show("The key vault URL is not correct.", Constants.MESSAGE_BOX_TITLE, MessageBoxButton.OK);
return;
}
else if (txtAKVUrl.Text != repository.RepositoryName)
{
await _package.JoinableTaskFactory.SwitchToMainThreadAsync();
txtAKVUrl.Text = repository.RepositoryName;
}
}
}

Expand Down Expand Up @@ -206,14 +213,24 @@ private async Task<bool> ConfigureSolutionAsync()
return true;
}

SyncConfiguration.SetCustomSynchronizationSettings(_solution.Uid, new SolutionSynchronizationSettings
SolutionSynchronizationSettings solutionSettings = null;
if (!String.IsNullOrWhiteSpace(txtAKVUrl.Text))
{
Repository = RepositoryType.AzureKV,
AzureKeyVaultName = txtAKVUrl.Text
});
solutionSettings = new SolutionSynchronizationSettings
{
Repository = RepositoryType.AzureKV,
AzureKeyVaultName = txtAKVUrl.Text
};
}

SyncConfiguration.SetCustomSynchronizationSettings(_solution.Uid, solutionSettings);
SyncConfiguration.Save();

await UseStatusBarAsync($"Configured Azure Key Vault as the repository for the solution secrets.");
if (solutionSettings != null)
await UseStatusBarAsync($"Configured Azure Key Vault as the repository for the solution secrets.");
else
await UseStatusBarAsync($"Reverted solution to default settings for solution secrets.");

return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions SolutionSecrets2019/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="SolutionSecrets2019.4ee3c3b7-0e84-447d-9860-a2a08bb0f43a" Version="2.0.0" Language="en-US" Publisher="Ernesto Cianciotta" />
<Identity Id="SolutionSecrets2019.4ee3c3b7-0e84-447d-9860-a2a08bb0f43a" Version="2.1.0" Language="en-US" Publisher="Ernesto Cianciotta" />
<DisplayName>Solution Secrets 2019</DisplayName>
<Description xml:space="preserve">This extension allows you to synchronize Visual Studio solution secrets across different development workstations.</Description>
<License>LICENSE</License>
Expand All @@ -14,11 +14,11 @@
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
</Dependencies>
</Dependencies>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[16.0,17.0)" DisplayName="Visual Studio core editor" />
</Prerequisites>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
</Assets>
</Assets>
</PackageManifest>

0 comments on commit 968d346

Please sign in to comment.