refactorisation de la configuration

broken/ef
Paul Schneider 3 years ago
parent 838de379fd
commit 749eb645d5
7 changed files with 43 additions and 35 deletions

@ -13,17 +13,16 @@ namespace nuget_host.Controllers
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
readonly IHostingEnvironment _environment; readonly IHostingEnvironment _environment;
public IDataProtector DataProtector { get; } public SmtpSettings _smtpSettings { get; } //set only via Secret Manager
public SmtpSettings Options { get; } //set only via Secret Manager
public HomeController( public HomeController(
IOptions<SmtpSettings> smtpSettings, IOptions<SmtpSettings> smtpSettings,
IDataProtectionProvider provider, Microsoft.AspNetCore.Hosting.IHostingEnvironment environment, ILogger<HomeController> logger) IHostingEnvironment environment,
ILogger<HomeController> logger)
{ {
_environment = environment; _environment = environment;
_logger = logger; _logger = logger;
Options = smtpSettings.Value; _smtpSettings = smtpSettings.Value;
DataProtector = provider.CreateProtector(Options.ProtectionTitle);
} }
public IActionResult Index() public IActionResult Index()

@ -7,7 +7,9 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NuGet.Packaging; using NuGet.Packaging;
using nuget_host.Entities;
namespace nuget_host.Controllers namespace nuget_host.Controllers
{ {
@ -16,10 +18,16 @@ namespace nuget_host.Controllers
private readonly ILogger<PackagesController> logger; private readonly ILogger<PackagesController> logger;
private readonly IDataProtector protector; private readonly IDataProtector protector;
public PackagesController(ILoggerFactory loggerFactory, IDataProtectionProvider provider) private readonly NugetSettings nugetSettings;
public PackagesController(
ILoggerFactory loggerFactory,
IDataProtectionProvider provider,
IOptions<NugetSettings> nugetOptions)
{ {
logger = loggerFactory.CreateLogger<PackagesController>(); logger = loggerFactory.CreateLogger<PackagesController>();
protector = provider.CreateProtector("Packages.v1"); nugetSettings = nugetOptions.Value;
protector = provider.CreateProtector(nugetSettings.ProtectionTitle);
} }
[HttpPut("packages/{*spec}")] [HttpPut("packages/{*spec}")]
@ -61,7 +69,7 @@ namespace nuget_host.Controllers
var version = reader.GetVersion(); var version = reader.GetVersion();
path = Path.Combine(Startup.SourceDir, path = Path.Combine(nugetSettings.PackagesRootDir,
Path.Combine(pkgid, Path.Combine(pkgid,
Path.Combine(version.ToFullString(), Path.Combine(version.ToFullString(),
$"{pkgid}-{version}.nupkg"))); $"{pkgid}-{version}.nupkg")));
@ -85,12 +93,7 @@ namespace nuget_host.Controllers
} }
else else
{ {
ViewData["spec"] = spec; return BadRequest();
// TODO Assert valid sem ver spec
var filelst = new DirectoryInfo(Startup.SourceDir);
var fi = new FileInfo(spec);
var lst = filelst.GetFiles(fi.Name + "*.nupkg");
ViewData["lst"] = lst.Select(entry => entry.Name);
} }
return Ok(ViewData); return Ok(ViewData);
} }
@ -101,18 +104,12 @@ namespace nuget_host.Controllers
if (string.IsNullOrEmpty(spec)) if (string.IsNullOrEmpty(spec))
{ {
ViewData["warn"] = "no spec"; ViewData["warn"] = "no spec";
/*
usr/lib/mono/msbuild/Current/bin/NuGet.targets(128,5): error : Failed to retrieve information about 'Microsoft.VisualStudio.Web.CodeGeneration.Tools' from remote source 'http://localhost:5000/Packages/FindPackagesById()?id='Microsoft.VisualStudio.Web.CodeGeneration.Tools'&semVerLevel=2.0.0'. [/home/paul/workspace/nuget-host/nuget-host.csproj]
*/
} }
else else
{ {
ViewData["spec"] = spec; ViewData["spec"] = spec;
// TODO Assert valid sem ver spec // TODO Assert valid sem ver spec
var filelst = new DirectoryInfo(Startup.SourceDir); var filelst = new DirectoryInfo(nugetSettings.PackagesRootDir);
var fi = new FileInfo(spec); var fi = new FileInfo(spec);
var lst = filelst.GetDirectories(spec); var lst = filelst.GetDirectories(spec);
ViewData["lst"] = lst.Select(entry => entry.Name); ViewData["lst"] = lst.Select(entry => entry.Name);
@ -120,6 +117,8 @@ namespace nuget_host.Controllers
return Ok(ViewData); return Ok(ViewData);
} }
[Authorize] [Authorize]
[HttpGet("api/get-key/{*apikey}")] [HttpGet("api/get-key/{*apikey}")]
public IActionResult GetApiKey(string apiKey) public IActionResult GetApiKey(string apiKey)

@ -0,0 +1,9 @@
namespace nuget_host.Entities
{
public class NugetSettings
{
public string ProtectionTitle {get; set;}
public string PackagesRootDir {get; set;}
}
}

@ -8,6 +8,5 @@ namespace nuget_host.Entities
public string SenderEMail {get; set;} public string SenderEMail {get; set;}
public string UserName {get; set;} public string UserName {get; set;}
public string Password {get; set;} public string Password {get; set;}
public string ProtectionTitle {get; set;}
} }
} }

@ -29,8 +29,6 @@ namespace nuget_host
} }
public IConfiguration Configuration { get; } public IConfiguration Configuration { get; }
public static string ExternalUrl { get; private set; }
public static string SourceDir { get; private set; }
public static string RootApiKeySecret { get; private set; } public static string RootApiKeySecret { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
@ -55,6 +53,8 @@ namespace nuget_host
var smtpSettingsconf = Configuration.GetSection("SmtpSettings"); var smtpSettingsconf = Configuration.GetSection("SmtpSettings");
services.Configure<SmtpSettings>(smtpSettingsconf); services.Configure<SmtpSettings>(smtpSettingsconf);
var nugetSettingsconf = Configuration.GetSection("NugetSettings");
services.Configure<NugetSettings>(nugetSettingsconf);
} }
@ -71,9 +71,6 @@ namespace nuget_host
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
app.UseHsts(); app.UseHsts();
} }
ExternalUrl = Configuration["NuGet:ExternalUrl"];
SourceDir = Configuration["NuGet:SourceDir"];
RootApiKeySecret = Configuration["RootApiKeySecret"];
app.UseStaticFiles(); app.UseStaticFiles();

@ -1,7 +1,12 @@
{ {
"NuGet": { "NuGet": {
"ExternalUrl" : "<http://localhost:5000/packages", "ExternalUrl" : "<http://localhost:5000/packages",
"SourceDir" : "packages" "PackagesRootDir" : "packages"
}, },
"RootApiKeySecret": "secret-key" "Smtp": {
"Server": "localhost",
"Port": 25,
"SenderName": "Paul Schneider",
"SenderEmail": "paul@pschneider.fr"
}
} }

@ -2,18 +2,18 @@
"RootApiKeySecret": "<your-root-api-clear-key>", "RootApiKeySecret": "<your-root-api-clear-key>",
"NuGet": { "NuGet": {
"ExternalUrl" : "<http://your-external.url", "ExternalUrl" : "<http://your-external.url",
"SourceDir" : "<your-Source-dir>" "PackagesRootDir" : "<your-Source-dir>",
"ProtectionTitle": "protected-data-v1"
}, },
"ConnectionStrings": { "ConnectionStrings": {
"DefaultConnection": "Server=<pqserver>;Port=<pqport>;Database=<dbname>;Username=<dbusername>;Password=<dbpass>;" "DefaultConnection": "Server=<pqserver>;Port=<pqport>;Database=<dbname>;Username=<dbusername>;Password=<dbpass>;"
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"SmtpSettings": { "Smtp": {
"Server": "localhost", "Server": "<smtp.server.address>",
"Port": 25, "Port": 25,
"SenderName": "Paul Schneider", "SenderName": "<from-name>",
"SenderEmail": "paul@pschneider.fr", "SenderEmail": "<from-email>"
"ProtectionTitle": "protected-data-v1"
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {

Loading…