Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to create new settings #380

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/DevOpsMetrics.Service/Controllers/BuildsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DevOpsMetrics.Core.Models.AzureDevOps;
using DevOpsMetrics.Core.Models.Common;
using DevOpsMetrics.Core.Models.GitHub;
using DevOpsMetrics.Service.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

Expand Down Expand Up @@ -38,6 +39,7 @@ public async Task<int> UpdateAzureDevOpsBuilds(

//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken) == true)
{
Expand Down Expand Up @@ -73,7 +75,9 @@ public async Task<int> UpdateGitHubActionRuns(

//Get the client id and secret from the settings
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
clientIdName = SecretsProcessing.CleanKey(clientIdName);
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) == true | string.IsNullOrEmpty(clientSecret) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DevOpsMetrics.Core.Models.AzureDevOps;
using DevOpsMetrics.Core.Models.Common;
using DevOpsMetrics.Core.Models.GitHub;
using DevOpsMetrics.Service.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

Expand Down Expand Up @@ -35,6 +36,7 @@ public async Task<DeploymentFrequencyModel> GetAzureDevOpsDeploymentFrequency(bo

//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken) == true)
{
Expand Down Expand Up @@ -72,7 +74,9 @@ public async Task<DeploymentFrequencyModel> GetGitHubDeploymentFrequency(bool ge

//Get the client id and secret from the settings
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
clientIdName = SecretsProcessing.CleanKey(clientIdName);
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) == true | string.IsNullOrEmpty(clientSecret) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DevOpsMetrics.Core.Models.AzureDevOps;
using DevOpsMetrics.Core.Models.Common;
using DevOpsMetrics.Core.Models.GitHub;
using DevOpsMetrics.Service.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

Expand Down Expand Up @@ -35,6 +36,7 @@ public async Task<LeadTimeForChangesModel> GetAzureDevOpsLeadTimeForChanges(bool

//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken) == true)
{
Expand Down Expand Up @@ -73,7 +75,9 @@ public async Task<LeadTimeForChangesModel> GetGitHubLeadTimeForChanges(bool getS

//Get the client id and secret from the settings
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
clientIdName = SecretsProcessing.CleanKey(clientIdName);
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) == true | string.IsNullOrEmpty(clientSecret) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DevOpsMetrics.Core.Models.AzureDevOps;
using DevOpsMetrics.Core.Models.Common;
using DevOpsMetrics.Core.Models.GitHub;
using DevOpsMetrics.Service.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

Expand Down Expand Up @@ -36,6 +37,7 @@ public async Task<int> UpdateAzureDevOpsPullRequests(

//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken) == true)
{
Expand Down Expand Up @@ -71,7 +73,9 @@ public async Task<int> UpdateGitHubActionPullRequests(

//Get the client id and secret from the settings
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
clientIdName = SecretsProcessing.CleanKey(clientIdName);
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) == true | string.IsNullOrEmpty(clientSecret) == true)
Expand Down Expand Up @@ -108,6 +112,7 @@ public async Task<int> UpdateAzureDevOpsPullRequestCommits(

//Get the PAT token from the key vault
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
string patToken = Configuration[patTokenName];
if (string.IsNullOrEmpty(patToken) == true)
{
Expand Down Expand Up @@ -142,7 +147,9 @@ public async Task<int> UpdateGitHubActionPullRequestCommits(

//Get the client id and secret from the settings
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
clientIdName = SecretsProcessing.CleanKey(clientIdName);
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
string clientId = Configuration[clientIdName];
string clientSecret = Configuration[clientSecretName];
if (string.IsNullOrEmpty(clientId) == true | string.IsNullOrEmpty(clientSecret) == true)
Expand Down
22 changes: 13 additions & 9 deletions src/DevOpsMetrics.Service/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using DevOpsMetrics.Core.Models.AzureDevOps;
using DevOpsMetrics.Core.Models.Common;
using DevOpsMetrics.Core.Models.GitHub;
using DevOpsMetrics.Service.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

Expand Down Expand Up @@ -47,10 +48,11 @@ public async Task<bool> UpdateAzureDevOpsSetting(string patToken,
string branch, string buildName, string buildId, string resourceGroup, int itemOrder)
{
//Save the PAT token to the key vault
string patTokenSecretName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
if (patTokenSecretName.Length > 12)
string patTokenName = PartitionKeys.CreateAzureDevOpsSettingsPartitionKeyPatToken(organization, project, repository);
patTokenName = SecretsProcessing.CleanKey(patTokenName);
if (patTokenName.Length > 12)
{
await CreateKeyVaultSecret(patTokenSecretName, patToken);
await CreateKeyVaultSecret(patTokenName, patToken);
}

//Save everything else to table storage
Expand All @@ -65,15 +67,17 @@ public async Task<bool> UpdateGitHubSetting(string clientId, string clientSecret
string branch, string workflowName, string workflowId, string resourceGroup, int itemOrder)
{
//Save the Client Id and Client Secret to the key vault
string clientIdSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
if (clientIdSecretName.Length > 10)
string clientIdName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientId(owner, repo);
clientIdName = SecretsProcessing.CleanKey(clientIdName);
if (clientIdName.Length > 10)
{
await CreateKeyVaultSecret(clientIdSecretName, clientId);
await CreateKeyVaultSecret(clientIdName, clientId);
}
string clientSecretSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
if (clientSecretSecretName.Length > 14)
string clientSecretName = PartitionKeys.CreateGitHubSettingsPartitionKeyClientSecret(owner, repo);
clientSecretName = SecretsProcessing.CleanKey(clientSecretName);
if (clientSecretName.Length > 14)
{
await CreateKeyVaultSecret(clientSecretSecretName, clientSecret);
await CreateKeyVaultSecret(clientSecretName, clientSecret);
}

//Save everything else to table storage
Expand Down
15 changes: 15 additions & 0 deletions src/DevOpsMetrics.Service/Utility/SecretsProcessing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.RegularExpressions;

namespace DevOpsMetrics.Service.Utility
{
public static class SecretsProcessing
{
//https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/resource-name-rules#microsoftkeyvault
//Only Alphanumerics and hyphens allowed
public static string CleanKey(string name)
{
return Regex.Replace(name, @"[^a-zA-Z0-9]+", "-").Trim('-');
}

}
}
77 changes: 77 additions & 0 deletions src/DevOpsMetrics.Tests/Service/SecretsProcessingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using DevOpsMetrics.Service.Utility;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DevOpsMetrics.Tests.Service
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[TestCategory("L0Test")]
[TestClass]
public class SecretsProcessingTests
{
[TestMethod]
public void SecretWithDotTest()
{
//Arrange
string name = "SamLearnsAzure.CI";

//Act
string result = SecretsProcessing.CleanKey(name);

//Assert
Assert.AreEqual("SamLearnsAzure-CI", result);
}

[TestMethod]
public void SecretWithSpaceTest()
{
//Arrange
string name = "SamLearnsAzure CI";

//Act
string result = SecretsProcessing.CleanKey(name);

//Assert
Assert.AreEqual("SamLearnsAzure-CI", result);
}

[TestMethod]
public void SecretWithColonTest()
{
//Arrange
string name = "SamLearnsAzure:CI";

//Act
string result = SecretsProcessing.CleanKey(name);

//Assert
Assert.AreEqual("SamLearnsAzure-CI", result);
}

[TestMethod]
public void SecretWithQuestionTest()
{
//Arrange
string name = "SamLearnsAzure?CI";

//Act
string result = SecretsProcessing.CleanKey(name);

//Assert
Assert.AreEqual("SamLearnsAzure-CI", result);
}

[TestMethod]
public void SecretThatIsValidTest()
{
//Arrange
string name = "SamLearnsAzure123abcCI";

//Act
string result = SecretsProcessing.CleanKey(name);

//Assert
Assert.AreEqual("SamLearnsAzure123abcCI", result);
}

}
}
44 changes: 44 additions & 0 deletions src/DevOpsMetrics.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,50 @@ public async Task<IActionResult> Settings()
return View(result);
}

[HttpGet]
public IActionResult AddAzureDevOpsSetting()
{
return View();
}

[HttpPost]
public async Task<IActionResult> UpdateAzureDevOpsSetting(string patToken,
string organization, string project, string repository,
string branch, string buildName, string buildId, string resourceGroup, int itemOrder)
{
//Find the right project to load
ServiceApiClient serviceApiClient = new ServiceApiClient(Configuration);
if (patToken != null)
{

await serviceApiClient.UpdateAzureDevOpsSetting(patToken,
organization, project, repository,
branch, buildName, buildId, resourceGroup, itemOrder);
}

return RedirectToAction("Settings", "Home");
}

[HttpGet]
public IActionResult AddGitHubSetting()
{
return View();
}

[HttpPost]
public async Task<IActionResult> UpdateGitHubSetting(string clientId, string clientSecret,
string owner, string repo,
string branch, string workflowName, string workflowId, string resourceGroup, int itemOrder)
{
//Find the right project to load
ServiceApiClient serviceApiClient = new ServiceApiClient(Configuration);
await serviceApiClient.UpdateGitHubSetting(clientId, clientSecret,
owner, repo,
branch, workflowName, workflowId, resourceGroup, itemOrder);

return RedirectToAction("Settings", "Home");
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
Expand Down
20 changes: 19 additions & 1 deletion src/DevOpsMetrics.Web/Services/ServiceAPIClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public async Task<MeanTimeToRestoreModel> GetAzureMeanTimeToRestore(bool getSamp
return await GetResponse<MeanTimeToRestoreModel>(Client, url);
}

public async Task<ChangeFailureRateModel> GetChangeFailureRate(bool getSampleData, DevOpsPlatform targetDevOpsPlatform, string organization_owner, string project_repo, string branch, string buildName_workflowName, int numberOfDays, int maxNumberOfItems)
public async Task<ChangeFailureRateModel> GetChangeFailureRate(bool getSampleData, DevOpsPlatform targetDevOpsPlatform, string organization_owner, string project_repo, string branch, string buildName_workflowName, int numberOfDays, int maxNumberOfItems)
{
string url = $"/api/ChangeFailureRate/GetChangeFailureRate?getSampleData={getSampleData}&targetDevOpsPlatform={targetDevOpsPlatform}&organization_owner={organization_owner}&project_repo={project_repo}&branch={branch}&buildName_workflowName={buildName_workflowName}&numberOfDays={numberOfDays}&maxNumberOfItems={maxNumberOfItems}";
return await GetResponse<ChangeFailureRateModel>(Client, url);
Expand All @@ -79,6 +79,24 @@ public async Task<List<GitHubSettings>> GetGitHubSettings()
return await GetResponse<List<GitHubSettings>>(Client, url);
}

public async Task<bool> UpdateAzureDevOpsSetting(string patToken,
string organization, string project, string repository,
string branch, string buildName, string buildId, string resourceGroup, int itemOrder)
{
string url = $"/api/Settings/UpdateAzureDevOpsSetting?patToken={patToken}&organization={organization}&project={project}&repository={repository}&branch={branch}&buildName={buildName}&buildId={buildId}&resourceGroup={resourceGroup}&itemOrder={itemOrder}";

return await GetResponse<bool>(Client, url);
}

public async Task<bool> UpdateGitHubSetting(string clientId, string clientSecret,
string owner, string repo,
string branch, string workflowName, string workflowId, string resourceGroup, int itemOrder)
{
string url = $"/api/Settings/UpdateGitHubSetting?clientId={clientId}&clientSecret={clientSecret}&owner={owner}&repo={repo}&branch={branch}&workflowName={workflowName}&workflowId={workflowId}&resourceGroup={resourceGroup}&itemOrder={itemOrder}";

return await GetResponse<bool>(Client, url);
}

public async Task<List<ProjectLog>> GetAzureDevOpsProjectLogs(string organization, string project, string repository)
{
string url = $"/api/Settings/GetAzureDevOpsProjectLog?organization={organization}&project={project}&repository={repository}";
Expand Down
Loading