Skip to content

Commit 54750fd

Browse files
authored
Merge pull request #213 from pwrdrvr/cert-missing-ok
Issue 2 - Handle missing cert file
2 parents cf260c1 + 5731689 commit 54750fd

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/PwrDrvr.LambdaDispatch.Router/Program.cs

+39-12
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,33 @@ public static void Main(string[] args)
8181
CreateHostBuilder(args).Build().Run();
8282
}
8383

84-
public static string GetCertPath(string filename)
84+
public static string? GetCertPath(string filename)
8585
{
8686
// Check if the 'certs/' folder is in the current directory
87-
if (Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "certs")))
87+
var currentDirectory = Directory.GetCurrentDirectory();
88+
if (Directory.Exists(Path.Combine(currentDirectory, "certs")))
8889
{
89-
return Path.Combine(Directory.GetCurrentDirectory(), "certs", filename);
90+
return Path.Combine(currentDirectory, "certs", filename);
9091
}
9192

9293
// Check if the 'certs/' folder is two directories up
93-
var twoDirectoriesUp = Directory.GetParent(Directory.GetParent(Directory.GetCurrentDirectory()).FullName).FullName;
94+
var parent1 = Directory.GetParent(currentDirectory);
95+
if (parent1 == null)
96+
{
97+
return null;
98+
}
99+
var parent2 = Directory.GetParent(parent1.FullName);
100+
if (parent2 == null)
101+
{
102+
return null;
103+
}
104+
var twoDirectoriesUp = parent2.FullName;
94105
if (Directory.Exists(Path.Combine(twoDirectoriesUp, "certs")))
95106
{
96107
return Path.Combine(twoDirectoriesUp, "certs", filename);
97108
}
98109

99-
// If the 'certs/' folder is not found, throw
100-
throw new Exception("Could not find the 'certs/' folder");
110+
return null;
101111
}
102112

103113
public static IHostBuilder CreateHostBuilder(string[] args) =>
@@ -176,27 +186,44 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
176186
// Remove the `Server: Kestrel` response header
177187
serverOptions.AddServerHeader = false;
178188

189+
// See if we have a cert
190+
var certPath = GetCertPath("lambdadispatch.local.pfx");
191+
179192
// We have to reparse the config once, bummer
180193
var config = Config.CreateAndValidate(context.Configuration);
181194
//
182195
// Incoming Requests
183196
//
184197
serverOptions.ListenAnyIP(config.IncomingRequestHTTPPort);
185-
serverOptions.ListenAnyIP(config.IncomingRequestHTTPSPort, listenOptions =>
198+
if (certPath == null)
186199
{
187-
listenOptions.UseHttps(GetCertPath("lambdadispatch.local.pfx"));
188-
});
200+
Console.WriteLine("No cert found, not starting HTTPS incoming request channel");
201+
}
202+
else
203+
{
204+
serverOptions.ListenAnyIP(config.IncomingRequestHTTPSPort, listenOptions =>
205+
{
206+
listenOptions.UseHttps(certPath);
207+
});
208+
}
189209
//
190210
// Control Channels
191211
//
192212
if (config.AllowInsecureControlChannel)
193213
{
194214
serverOptions.ListenAnyIP(config.ControlChannelInsecureHTTP2Port, o => o.Protocols = HttpProtocols.Http2);
195215
}
196-
serverOptions.ListenAnyIP(config.ControlChannelHTTP2Port, listenOptions =>
216+
if (certPath == null)
197217
{
198-
listenOptions.UseHttps(GetCertPath("lambdadispatch.local.pfx"));
199-
});
218+
Console.WriteLine("No cert found, not starting HTTPS control channel");
219+
}
220+
else
221+
{
222+
serverOptions.ListenAnyIP(config.ControlChannelHTTP2Port, listenOptions =>
223+
{
224+
listenOptions.UseHttps(certPath);
225+
});
226+
}
200227
});
201228
webBuilder.UseStartup<Startup>();
202229
});

0 commit comments

Comments
 (0)