Skip to content
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
30 changes: 23 additions & 7 deletions src/Attributes/ApiKeyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
// Determine if the request is read-only
if (context.HttpContext.Request.Method == "GET")
{
if ((!GeneralHelper.DecryptString(config.APIKey!, config.SALTKey!).Equals(extractedApiKey)) && (!GeneralHelper.DecryptString(config.ReadOnlyAPIKey!, config.SALTKey!).Equals(extractedApiKey)))
if ((!GeneralHelper.DecryptString(config.APIKey!, config.SALTKey!).Equals(extractedApiKey)) && (!GeneralHelper.DecryptString(config.ReadOnlyAPIKey!, config.SALTKey!).Equals(extractedApiKey)) && (!GeneralHelper.DecryptString(config.NameGenerationAPIKey!, config.SALTKey!).Equals(extractedApiKey)))
{
context.Result = new ContentResult()
{
Expand All @@ -49,14 +49,30 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
else
{
// Request is a POST. Make sure the provided API Key is for full access
if (!GeneralHelper.DecryptString(config.APIKey!, config.SALTKey!).Equals(extractedApiKey))
// Check if it is a name generation request
if (context.HttpContext.Request.Path.Value.StartsWith("/api/ResourceNamingRequests/"))
{
context.Result = new ContentResult()
if ((!GeneralHelper.DecryptString(config.APIKey!, config.SALTKey!).Equals(extractedApiKey)) && (!GeneralHelper.DecryptString(config.NameGenerationAPIKey!, config.SALTKey!).Equals(extractedApiKey)))
{
StatusCode = 401,
Content = "Full Access Api Key is not valid!"
};
return;
context.Result = new ContentResult()
{
StatusCode = 401,
Content = "Api Key is not valid!"
};
return;
}
}
else
{
if (!GeneralHelper.DecryptString(config.APIKey!, config.SALTKey!).Equals(extractedApiKey))
{
context.Result = new ContentResult()
{
StatusCode = 401,
Content = "Full Access Api Key is not valid!"
};
return;
}
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/AzureNamingTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Version>4.3.0</Version>
<Version>4.3.2</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<UserSecretsId>eca63fb9-b7f9-454f-910b-5088ae877085</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
Expand All @@ -23,13 +23,13 @@
<PackageReference Include="BlazorDownloadFile" Version="2.4.0.2" />
<PackageReference Include="Blazored.Modal" Version="7.3.1" />
<PackageReference Include="Blazored.Toast" Version="4.2.1" />
<PackageReference Include="Markdig" Version="0.38.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="PSC.Blazor.Components.MarkdownEditor" Version="8.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.0.0" />
<PackageReference Include="System.Runtime.Caching" Version="9.0.0" />
<PackageReference Include="Markdig" Version="0.40.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
<PackageReference Include="PSC.Blazor.Components.MarkdownEditor" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="8.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="8.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="8.1.0" />
<PackageReference Include="System.Runtime.Caching" Version="9.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions src/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
{
<div class="w-100">
@(
"User: " + currentuser
)
"User: " + currentuser
)
</div>
}
}
Expand Down Expand Up @@ -220,6 +220,7 @@
config.AdminPassword = "";
config.APIKey = "";
config.ReadOnlyAPIKey = "";
config.NameGenerationAPIKey = "";

await ConfigurationHelper.UpdateSettings(config);
ConfigurationHelper.ResetState(state);
Expand Down
2 changes: 2 additions & 0 deletions src/Components/Modals/PasswordModal.razor
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
config.APIKey = GeneralHelper.EncryptString(guid.ToString(), config.SALTKey!);
guid = Guid.NewGuid();
config.ReadOnlyAPIKey = GeneralHelper.EncryptString(guid.ToString(), config.SALTKey!);
guid = Guid.NewGuid();
config.NameGenerationAPIKey = GeneralHelper.EncryptString(guid.ToString(), config.SALTKey!);

await ConfigurationHelper.UpdateSettings(config);

Expand Down
81 changes: 81 additions & 0 deletions src/Components/Pages/Admin.razor
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,26 @@
</div>
</div>
</div>
<div class="card mb-3">
<div class="card-header bg-default text-dark">
<a class="text-decoration-none text-dark" data-bs-toggle="collapse" style="display:block;font-weight:bold;" href="#updatenamegenerationapikey" role="button" aria-expanded="false" aria-controls="updatenamegenerationapikey">
<span class="oi oi-chevron-bottom" aria-hidden="true"></span> Name Generation API Key
</a>
</div>
<div class="collapse show card card-body @theme.ThemeStyle" id="updatenamegenerationapikey">
<p>
The current <span class="fw-bold">Name Generation API Key</span> is displayed. This key provides <span class="fw-bold">Name Generation</span> access to the API (only for name generation).
</p>
<p>
Click <span class="fw-bold">Generate</span> to create a new random <span class="fw-bold">Name Generation API Key</span>, or update the text to the desired value.
</p>
<div class="form-group">
<input title="Name Generation API Key" id="currentnamegenerationapikey" class="form-control mb-3" type="text" @onchange="@((ui) => { SetFormValue("namegenerationapikey",(string)ui.Value!);})" value="@currentnamegenerationapikey" />
<button type="button" class="btn btn-success" @onclick="@(e => AdminFormAction("namegenerationapikeysave"))" title="Save Name Generation API Key">Save</button>
<button type="button" class="btn btn-success" @onclick="@(e => AdminFormAction("namegenerationapikeygenerate"))" title="Generate Name Generation API Key">Generate</button>
</div>
</div>
</div>
<div class="card mb-3">
<div class="card-header bg-default text-dark">
<a class="text-decoration-none text-dark" data-bs-toggle="collapse" style="display:block;font-weight:bold;" href="#updatereadonlyapikey" role="button" aria-expanded="false" aria-controls="updatereadonlyapikey">
Expand Down Expand Up @@ -808,6 +828,7 @@
private string newpassword = String.Empty;
private string currentapikey = String.Empty;
private string currentreadonlyapikey = String.Empty;
private string currentnamegenerationapikey = String.Empty;
private string currentidentityheadername = String.Empty;
private string currentidentityprovider = String.Empty;
private ResponseMessage message = new();
Expand Down Expand Up @@ -854,6 +875,7 @@
{
currentapikey = GeneralHelper.DecryptString(config.APIKey!, config.SALTKey!);
currentreadonlyapikey = GeneralHelper.DecryptString(config.ReadOnlyAPIKey!, config.SALTKey!);
currentnamegenerationapikey = GeneralHelper.DecryptString(config.NameGenerationAPIKey!, config.SALTKey!);
// Determine if the version notification has been dismissed
var resultVersionAlert = await session.GetAsync<bool>("versionalert-" + appversion + "-shown");
versionalertshown = resultVersionAlert.Success ? resultVersionAlert.Value : false;
Expand Down Expand Up @@ -1056,6 +1078,62 @@
displaymessage = false;
}
break;
case "namegenerationapikeysave":
confirm = await ModalHelper.ShowConfirmationModal(Modal!, "ATTENTION", "<div class=\"my-4\">This will update the Name Generation API Key.</div><div class=\"my-4\">Are you sure?</div>", "bg-danger", theme);
if (confirm)
{
serviceResponse = await AdminService.UpdateAPIKey(currentnamegenerationapikey, "namegeneration");
if (serviceResponse.Success)
{
message.Type = MessageTypesEnum.SUCCESS;
message.Header = "SUCCESS";
message.Message = "Name Generation API Key updated!";
}
else
{
message.Type = MessageTypesEnum.ERROR;
message.Header = "ERROR";
message.Message = "There was a problem updating the Name Generation API Key!";
}
}
else
{
displaymessage = false;
}
break;
case "namegenerationapikeygenerate":
confirm = await ModalHelper.ShowConfirmationModal(Modal!, "ATTENTION", "<div class=\"my-4\">This will generate a new Name Generation API Key.</div><div class=\"my-4\">Are you sure?</div>", "bg-danger", theme);
if (confirm)
{
serviceResponse = await AdminService.GenerateAPIKey("namegeneration");
if (serviceResponse.Success)
{
if (GeneralHelper.IsNotNull(serviceResponse.ResponseObject))
{
currentnamegenerationapikey = serviceResponse.ResponseObject!;
message.Type = MessageTypesEnum.SUCCESS;
message.Header = "SUCCESS";
message.Message = "Name Generation API Key updated!";
}
else
{
message.Type = MessageTypesEnum.ERROR;
message.Header = "ERROR";
message.Message = "There was a problem generating the Name Generation API Key!";
}
}
else
{
message.Type = MessageTypesEnum.ERROR;
message.Header = "ERROR";
message.Message = "There was a problem generating the Name Generation API Key!";
}
}
else
{
displaymessage = false;
}
break;
case "readonlyapikeysave":
confirm = await ModalHelper.ShowConfirmationModal(Modal!, "ATTENTION", "<div class=\"my-4\">This will update the Read-Only API Key.</div><div class=\"my-4\">Are you sure?</div>", "bg-danger", theme);
if (confirm)
Expand Down Expand Up @@ -1377,6 +1455,9 @@
case "apikey":
currentapikey = value;
break;
case "namegenerationapikey":
currentnamegenerationapikey = value;
break;
case "readonlyapikey":
currentreadonlyapikey = value;
break;
Expand Down
78 changes: 78 additions & 0 deletions src/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,84 @@ public async Task<IActionResult> GenerateAPIKey([BindRequired][FromHeader(Name =
}
}




// POST api/<AdminController>
/// <summary>
/// This function will update the Name Generation API Key.
/// </summary>
/// <param name="apikey">string - Name Generation API Key</param>
/// <param name="adminpassword">string - Current Global Admin Password</param>
/// <returns>dttring - Successful update</returns>
[HttpPost]
[Route("[action]")]
public async Task<IActionResult> UpdateNameGenerationAPIKey([BindRequired][FromHeader(Name = "AdminPassword")] string adminpassword, [FromBody] string apikey)
{
try
{
if (GeneralHelper.IsNotNull(adminpassword))
{
if (adminpassword == GeneralHelper.DecryptString(config.AdminPassword!, config.SALTKey!))
{
serviceResponse = await AdminService.UpdateAPIKey(apikey, "namegeneration");
return (serviceResponse.Success ? Ok("SUCCESS") : Ok("FAILURE - There was a problem updating the Name Generation API Key."));
}
else
{
return Ok("FAILURE - Incorrect Global Admin Password.");
}

}
else
{
return Ok("FAILURE - You must provide the Global Admin Password.");
}
}
catch (Exception ex)
{
AdminLogService.PostItem(new AdminLogMessage() { Title = "ERROR", Message = ex.Message });
return BadRequest(ex);
}
}

// POST api/<AdminController>
/// <summary>
/// This function will generate a new Name Generation API Key.
/// </summary>
/// <param name="adminpassword">string - Current Global Admin Password</param>
/// <returns>string - Successful update / Generated Name Generation API Key</returns>
[HttpPost]
[Route("[action]")]
public async Task<IActionResult> GenerateNameGenerationAPIKey([BindRequired][FromHeader(Name = "AdminPassword")] string adminpassword)
{
try
{
if (GeneralHelper.IsNotNull(adminpassword))
{
if (adminpassword == GeneralHelper.DecryptString(config.AdminPassword!, config.SALTKey!))
{
serviceResponse = await AdminService.GenerateAPIKey("namegeneration");
return (serviceResponse.Success ? Ok("SUCCESS") : Ok("FAILURE - There was a problem generating the Name Generation API Key."));
}
else
{
return Ok("FAILURE - Incorrect Global Admin Password.");
}

}
else
{
return Ok("FAILURE - You must provide the Global Admin Password.");
}
}
catch (Exception ex)
{
AdminLogService.PostItem(new AdminLogMessage() { Title = "ERROR", Message = ex.Message });
return BadRequest(ex);
}
}

// POST api/<AdminController>
/// <summary>
/// This function will update the Read-Only API Key.
Expand Down
2 changes: 2 additions & 0 deletions src/Controllers/ResourceComponentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AzureNamingTool.Models;
using AzureNamingTool.Services;
using Microsoft.AspNetCore.Mvc;
using PSC.Blazor.Components.MarkdownEditor;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -94,6 +95,7 @@ public async Task<IActionResult> Post([FromBody] ResourceComponent item)
serviceResponse = await ResourceComponentService.PostItem(item);
if (serviceResponse.Success)
{
// Get the item
AdminLogService.PostItem(new AdminLogMessage() { Source = "API", Title = "INFORMATION", Message = "Resource Component (" + item.Name + ") added/updated." });
CacheHelper.InvalidateCacheObject("ResourceComponent");
return Ok(serviceResponse.ResponseObject);
Expand Down
1 change: 1 addition & 0 deletions src/Helpers/ConfigurationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public static async void VerifySecurity(StateContainer state)
config.SALTKey = salt.ToString();
config.APIKey = GeneralHelper.EncryptString(config.APIKey!, salt.ToString());
config.ReadOnlyAPIKey = GeneralHelper.EncryptString(config.ReadOnlyAPIKey!, salt.ToString());
config.NameGenerationAPIKey = GeneralHelper.EncryptString(config.NameGenerationAPIKey!, salt.ToString());

if (!String.IsNullOrEmpty(config.AdminPassword))
{
Expand Down
5 changes: 5 additions & 0 deletions src/Models/ConfigurationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public class ConfigurationData
/// </summary>
public string? ReadOnlyAPIKey { get; set; }

/// <summary>
/// Gets or sets the Name Generation API key.
/// </summary>
public string? NameGenerationAPIKey { get; set; }

/// <summary>
/// Gets or sets the dismissed alerts.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Models/SiteConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class SiteConfiguration
/// </summary>
public string? ReadOnlyAPIKey { get; set; }

/// <summary>
/// Gets or sets the Name Generation API key.
/// </summary>
public string? NameGenerationAPIKey { get; set; }

/// <summary>
/// Gets or sets the theme of the application.
/// </summary>
Expand Down
Loading
Loading