/// <summary> /// Function for loading keys from file /// </summary> /// <param name="keyfileJson">The path of key source file.</param> /// <param name="createIfEmpty">The flag for file creation if it does not exist on provided path.</param> /// <returns>returns JsonPublicKeySource object.</returns> /// <exception cref="ArgumentNullException">Thrown when provided path string is null or empty.</exception> /// <exception cref="ArgumentException">Thrown when key source file does not exist on provided path.</exception> /// <exception cref="FileEmptyException">Thrown when key source file is empty.</exception> public static IPublickeySource FromFile(string keyfileJson, bool createIfEmpty = false) { //directly load from file JsonPublicKeySource source = new JsonPublicKeySource(); source.LoadFromFile(keyfileJson, createIfEmpty); return(source); }
/// <summary> /// This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services">The method expects ServiceCollection reference.</param> public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSingleton <IPublickeySource>(serviceProvider => JsonPublicKeySource.FromFile( Path.Combine(Configuration.GetValue <string>("INTERNAL_DIR", "./"), "keyfile.json")) ); services.AddSingleton <IInfluxClient, InfluxClient>(serviceProvider => { var lpcp = Configuration.GetSection("Influx").Get <LineProtocolConnectionParameters>(); return(new InfluxClient(lpcp)); }); }
/// <summary> /// Program entry point /// </summary> /// <param name="args">Command Line arguments, /// INTERNAL_DIR: for setting internal dir path. /// KEYPASS: for password for decryption of SSL Certificate /// STARTSERVICE: flag for indication of starting WebHost, Added for unit testing Program class, as we dont want to actually invoke host.Run in unit tests /// validator: to be used with add or remove command line arg for adding into or removing from key source /// publickey: to be used with add or remove command line arg for adding into or removing from key source /// add: adding into key source /// remove: removing into key source /// </param> public static void Main(string[] args) { // build config string envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); IConfigurationRoot config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{envName}.json", true) .AddEnvironmentVariables("TELEMETRY_") .AddInfluxConfigFromEnvironment() .AddCommandLine(args) .Build(); // If we run a key management function we don't go into daemon mode string keyCommandMode = config.GetValue <string>("keycmd", String.Empty); if (!String.IsNullOrWhiteSpace(keyCommandMode)) { var keystore = JsonPublicKeySource.FromFile( Path.Combine(config.GetValue <string>("INTERNAL_DIR", "./"), "keyfile.json"), true); var keymgr = new KeyManagement(config, keystore); keymgr.ProcessKeyCommand(keyCommandMode); return; } //Getting certificate path for SSL string certificatePath = Path.GetFullPath(Path.Combine(config.GetValue("INTERNAL_DIR", "./"), "telemetry-ingress.pfx")); //verify if certificate exists on given path if (!File.Exists(certificatePath)) { Console.WriteLine($"Error: Unable to read certificate from {certificatePath}. File not found"); return; } //get certificate password string keyPassword = config.GetValue("KEYPASS", String.Empty); //certificate key password validation if (String.IsNullOrWhiteSpace(keyPassword)) { Console.WriteLine($"Error: Certificate password not provided."); return; } // Configure and instantiate Web Host IWebHost host = new WebHostBuilder() .UseConfiguration(config) .UseStartup <Startup>() .UseKestrel(options => { options.Listen(IPAddress.Any, 5000); options.Listen(IPAddress.Any, 5010, listenOptions => { listenOptions.UseHttps(certificatePath, keyPassword); }); }) .Build(); //Added for unit testing Program class, as we dont want to actually invoke host.Run in unit tests string startSignal = config.GetValue("STARTSERVICE", String.Empty); if (String.IsNullOrWhiteSpace(startSignal)) { host.Run(); } }