Skip to content

Commit

Permalink
Remove latest hardcoded secrets in this project
Browse files Browse the repository at this point in the history
  • Loading branch information
Elanis committed Aug 12, 2021
1 parent 5688dfb commit a0e6aac
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 8 deletions.
9 changes: 9 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ steps:
from_secret: PORTFOLIO_RECAPTCHA_SECRETKEY
PORTFOLIO_RECAPTCHA_SITEKEY:
from_secret: PORTFOLIO_RECAPTCHA_SITEKEY
MAIL_SERVER:
from_secret: MAIL_SERVER
MAIL_ADDRESS:
from_secret: MAIL_ADDRESS
MAIL_PASSWORD:
from_secret: MAIL_PASSWORD
settings:
build_args_from_env:
- SONAR_HOST
- SONAR_TOKEN
- PORTFOLIO_RECAPTCHA_SECRETKEY
- PORTFOLIO_RECAPTCHA_SITEKEY
- MAIL_SERVER
- MAIL_ADDRESS
- MAIL_PASSWORD


when:
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ ARG SONAR_HOST
ARG SONAR_TOKEN
ARG PORTFOLIO_RECAPTCHA_SECRETKEY
ARG PORTFOLIO_RECAPTCHA_SITEKEY
ARG MAIL_SERVER
ARG MAIL_ADDRESS
ARG MAIL_PASSWORD

# Build Project
COPY . ./

RUN jq ".RecaptchaSettings.SecretKey = \"$PORTFOLIO_RECAPTCHA_SECRETKEY\"" Portfolio/appsettings.json > tmp.appsettings.json && mv tmp.appsettings.json Portfolio/appsettings.json
RUN jq ".RecaptchaSettings.SiteKey = \"$PORTFOLIO_RECAPTCHA_SITEKEY\"" Portfolio/appsettings.json > tmp.appsettings.json && mv tmp.appsettings.json Portfolio/appsettings.json
RUN jq ".AppSettings.MailServer = \"$MAIL_SERVER\"" Dysnomia.Website.WebApp/appsettings.json > tmp.appsettings.json && mv tmp.appsettings.json Dysnomia.Website.WebApp/appsettings.json
RUN jq ".AppSettings.MailAddress = \"$MAIL_ADDRESS\"" Dysnomia.Website.WebApp/appsettings.json > tmp.appsettings.json && mv tmp.appsettings.json Dysnomia.Website.WebApp/appsettings.json
RUN jq ".AppSettings.MailPassword = \"$MAIL_PASSWORD\"" Dysnomia.Website.WebApp/appsettings.json > tmp.appsettings.json && mv tmp.appsettings.json Dysnomia.Website.WebApp/appsettings.json

RUN dotnet sonarscanner begin /k:"portfolio" /d:sonar.host.url="$SONAR_HOST" /d:sonar.login="$SONAR_TOKEN" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" /d:sonar.coverage.exclusions="**Test*.cs"
RUN dotnet restore Portfolio.sln --ignore-failed-sources /p:EnableDefaultItems=false
Expand Down
10 changes: 9 additions & 1 deletion Portfolio.Test/HomeControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

using Portfolio.Controllers;

Expand All @@ -16,12 +17,19 @@ namespace Portfolio.Test {
public class HomeControllerTest {
public HttpClient client { get; }
public TestServer server { get; }
public AppSettings appSettings { get; }
public HomeControllerTest() {
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.AddUserSecrets<Startup>()
.Build();

this.appSettings = new AppSettings() {
MailServer = config["Appsettings:MailServer"],
MailAddress = config["Appsettings:MailAddress"],
MailPassword = config["Appsettings:MailPassword"]
};

var builder = new WebHostBuilder()
.UseConfiguration(config)
.UseStartup<Startup>()
Expand Down Expand Up @@ -118,7 +126,7 @@ public class HomeControllerTest {

[Fact]
public async void SendMail() {
var controller = new HomeController(null);
var controller = new HomeController(null, Options.Create(appSettings));

controller.SendMail("unitTest@dysnomia.studio", "Unit test mail sending");
}
Expand Down
7 changes: 7 additions & 0 deletions Portfolio/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Portfolio {
public class AppSettings {
public string MailServer { get; set; }
public string MailAddress { get; set; }
public string MailPassword { get; set; }
}
}
17 changes: 12 additions & 5 deletions Portfolio/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

using MailKit.Net.Smtp;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

using MimeKit;

Expand All @@ -14,8 +16,11 @@
namespace Portfolio.Controllers {
public class HomeController : Controller {
private readonly IRecaptchaService _recaptcha;
public HomeController(IRecaptchaService recaptcha) {
private readonly AppSettings appSettings;

public HomeController(IRecaptchaService recaptcha, IOptions<AppSettings> appSettings) {
_recaptcha = recaptcha;
this.appSettings = appSettings.Value;
}

[HttpGet]
Expand Down Expand Up @@ -43,8 +48,10 @@ public class HomeController : Controller {
@ViewData["Message"] = "CONTACT_OK";

} catch (System.ComponentModel.DataAnnotations.ValidationException e) {
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);

@ViewData["Message"] = "CONTACT_NOK";
// TODO: Log it
}

return View("Index");
Expand All @@ -66,10 +73,10 @@ public class HomeController : Controller {
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;

client.Connect("***REMOVED***", 587, false);
client.Connect(appSettings.MailServer, 587, false);

// Note: only needed if the SMTP server requires authentication
client.Authenticate("***REMOVED***", "***REMOVED***");
client.Authenticate(appSettings.MailAddress, appSettings.MailPassword);

client.Send(message);
client.Disconnect(true);
Expand Down
4 changes: 3 additions & 1 deletion Portfolio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ public class Startup {

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
var appSettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appSettingsSection);

// Captcha
services.Configure<RecaptchaSettings>(Configuration.GetSection("RecaptchaSettings"));
services.AddTransient<IRecaptchaService, RecaptchaService>();


services.AddLocalization(options => options.ResourcesPath = "Translation")
.AddMvc()
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
Expand Down
2 changes: 1 addition & 1 deletion Portfolio/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Don't copy this without permission
I hope this code is readable.
-->
<html lang="fr">
<html lang="">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down

0 comments on commit a0e6aac

Please sign in to comment.