internal static byte[] GenerateKeyFile(ITestOutputHelper output, IKeyCapture capture, IKeyFileService service)
 {
     lock (Sync)
     {
         var @out  = new XunitDuplexTextWriter(output, Console.Out);
         var error = new XunitDuplexTextWriter(output, Console.Error);
         Assert.True(KeyFileManager.TryGenerateKeyFile(service.GetKeyFileStream(), @out, error, capture));
         capture.Reset();
         return(Crypto.SigningPublicKeyFromSigningKey(service, capture));
     }
 }
Esempio n. 2
0
        public static string AddPublicKeyIdentifier(this IServiceCollection services, string eggPath, int port, IKeyCapture capture, IWebHostEnvironment env, IConfiguration config)
        {
            var keyFileService = new ServerKeyFileService();

            services.AddSingleton <IKeyFileService>(keyFileService);

            capture ??= new ServerConsoleKeyCapture();
            services.AddSingleton(capture);

            if (capture is IPersistedKeyCapture persisted)
            {
                services.AddSingleton(persisted);
            }

            var publicKey = Crypto.SigningPublicKeyFromSigningKey(keyFileService, capture);

            capture.Reset();

            var appString = $"{env.ApplicationName}:" +
                            $"{env.EnvironmentName}:" +
                            $"{port}";

            var serverId        = Crypto.Fingerprint(publicKey, appString);
            var publicKeyString = Crypto.ToHexString(publicKey);

            services.Configure <WebServerOptions>(config.GetSection("WebServer"));
            services.Configure <WebServerOptions>(o =>
            {
                o.PublicKey       = publicKey;
                o.ServerPort      = port;
                o.PublicKeyString = publicKeyString;
                o.ServerId        = serverId;
                o.EggPath         = eggPath;
            });

            return(publicKeyString);
        }
Esempio n. 3
0
        private static void RunAsServer(int?port, Queue <string> arguments, IKeyCapture capture, bool interactive)
        {
            var keyPath = arguments.EndOfSubArguments() ? Constants.DefaultKeyFilePath : arguments.Dequeue();

            if (!KeyFileManager.TryResolveKeyPath(keyPath, out keyFilePath, false, true))
            {
                return;
            }

            var shouldCreateKeyFile = !File.Exists(keyFilePath) || new FileInfo(keyFilePath).Length == 0;

            if (shouldCreateKeyFile)
            {
                File.WriteAllBytes(keyFilePath, new byte[KeyFileManager.KeyFileBytes]);
            }

            Console.Out.WriteInfoLine($"Key file path resolved to '{keyFilePath}'");

            if (shouldCreateKeyFile &&
                !KeyFileManager.Create(keyFilePath, false, true, capture ?? Constants.ConsoleKeyCapture))
            {
                Console.Error.WriteErrorLine("Cannot start server without a key file");
                return;
            }

            if (_exclusiveLock)
            {
                try
                {
                    keyFileStream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.None);
                }
                catch (IOException)
                {
                    Console.Error.WriteErrorLine("Could not obtain exclusive lock on key file");
                    return;
                }
            }
            else
            {
                try
                {
                    keyFileStream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                }
                catch (IOException)
                {
                    Console.Error.WriteErrorLine("Could not open key file");
                    return;
                }
            }

            var eggPath = Environment.GetEnvironmentVariable(Constants.EnvVars.EggFilePath);

            if (string.IsNullOrWhiteSpace(eggPath))
            {
                eggPath = Constants.DefaultEggPath;
            }

            Console.Out.WriteInfoLine($"Egg file path resolved to '{eggPath}'");

            if (!File.Exists(eggPath) && !EggFileManager.Create(eggPath))
            {
                Console.Error.WriteWarningLine("Server started without an egg");
            }

            capture?.Reset();

            if (!interactive)
            {
                LaunchBrowserUrl($"https://localhost:{port.GetValueOrDefault(Constants.DefaultPort)}");
            }

            PrintMasthead();
            var builder = CreateHostBuilder(port, eggPath, capture, arguments.ToArray());
            var host    = builder.Build();

            host.Run();
        }