Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="blockGraph"></param>
        /// <returns></returns>
        private async Task <BlockGraph> SignBlockGraph(BlockGraph blockGraph)
        {
            Guard.Argument(blockGraph, nameof(blockGraph)).NotNull();
            await _signing.GetOrUpsertKeyName(_signing.DefaultSigningKeyName);

            var signature = await _signing.Sign(_signing.DefaultSigningKeyName, blockGraph.ToHash());

            var pubKey = await _signing.GetPublicKey(_signing.DefaultSigningKeyName);

            blockGraph.PublicKey = pubKey;
            blockGraph.Signature = signature;
            return(blockGraph);
        }
        /// <summary>
        ///
        /// </summary>
        public async Task <byte[]> GetPublicKey()
        {
            byte[] publicKey = null;

            try
            {
                publicKey = await _signingProvider.GetPublicKey(_signingProvider.DefaultSigningKeyName);
            }
            catch (Exception ex)
            {
                _logger.Here().Error(ex, "Unable to get the public key");
            }

            return(publicKey);
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="applicationLifetime"></param>
        /// <returns></returns>
        public async Task StartAsync(IHostApplicationLifetime applicationLifetime)
        {
            if (_serfClient.ProcessStarted)
            {
                return;
            }

            if (_serfClient.SerfConfigurationOptions.Disabled)
            {
                Disabled = true;
                return;
            }

            if (IsRunning())
            {
                _logger.Here().Warning("Serf is already running. It's OK if you are running on a different port.");
            }

            var useExisting = await TryUseExisting();

            if (useExisting)
            {
                _serfClient.ProcessStarted = true;
                _logger.Here().Information("Process Id cannot be found at this moment.");
                return;
            }

            try
            {
                applicationLifetime.ApplicationStopping.Register(() =>
                {
                    try
                    {
                        var process = Process.GetProcessById(_serfClient.ProcessId);
                        process?.Kill();
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                });

                var pubKey = await _signing.GetPublicKey(_signing.DefaultSigningKeyName);

                _serfClient.Name = $"{_serfClient.SerfConfigurationOptions.NodeName}-{Helper.Util.Sha384ManagedHash(pubKey).ByteToHex().Substring(0, 16)}";

                var serfPath = GetFilePath();

                _logger.Here().Information("Serf assembly path: {@SerfPath}", serfPath);

                //  Chmod before attempting to execute serf on Linux and Mac
                if (new[] { OSPlatform.Linux, OSPlatform.OSX }.Contains(Helper.Util.GetOperatingSystemPlatform()))
                {
                    _logger.Here().Information("Granting execute permission on serf assembly");

                    var chmodCmd = Cli.Wrap("chmod")
                                   .WithArguments(a => a
                                                  .Add("+x")
                                                  .Add(serfPath));

                    await chmodCmd.ExecuteAsync();
                }

                var cmd = Cli.Wrap(serfPath)
                          .WithArguments(a => a
                                         .Add("agent")
                                         .Add($"-bind={_serfClient.SerfConfigurationOptions.Listening}")
                                         .Add($"-rpc-addr={_serfClient.SerfConfigurationOptions.RPC}")
                                         .Add($"-advertise={_serfClient.SerfConfigurationOptions.Advertise}")
                                         .Add($"-encrypt={ _serfClient.SerfConfigurationOptions.Encrypt}")
                                         .Add($"-node={_serfClient.Name}")
                                         .Add($"-snapshot={_serfClient.SerfConfigurationOptions.SnapshotPath}")
                                         .Add($"-rejoin={_serfClient.SerfConfigurationOptions.Rejoin}")
                                         .Add($"-broadcast-timeout={_serfClient.SerfConfigurationOptions.BroadcastTimeout}")
                                         .Add($"-retry-max={_serfClient.SerfConfigurationOptions.RetryMax}")
                                         .Add($"-log-level={_serfClient.SerfConfigurationOptions.Loglevel}")
                                         .Add($"-profile={_serfClient.SerfConfigurationOptions.Profile}")
                                         .Add("-tag")
                                         .Add($"rest={_serfClient.ApiConfigurationOptions.Advertise}")
                                         .Add("-tag")
                                         .Add($"pubkey={pubKey.ByteToHex()}")
                                         .Add("-tag")
                                         .Add($"nodeversion={Assembly.GetExecutingAssembly().GetName().Version}"));

                await cmd.Observe().ForEachAsync(cmdEvent =>
                {
                    switch (cmdEvent)
                    {
                    case StartedCommandEvent started:
                        _logger.Here().Information("Process started; ID: {@ID}", started.ProcessId);
                        _serfClient.ProcessId = started.ProcessId;
                        break;

                    case StandardOutputCommandEvent stdOut:
                        if (stdOut.Text.Contains("agent: Serf agent starting"))
                        {
                            _logger.Here().Information("Serf has started!");
                            _serfClient.ProcessStarted = true;
                        }
                        _logger.Here().Debug("Out> {@StdOut}", stdOut.Text);
                        break;

                    case StandardErrorCommandEvent stdErr:
                        _logger.Here().Error("Err> {@StdErr}", stdErr.Text);
                        _serfClient.ProcessError = stdErr.Text;
                        break;

                    case ExitedCommandEvent exited:
                        _logger.Here().Information("Process exited; Code: {@ExitCode}", exited.ExitCode);
                        applicationLifetime.StopApplication();
                        break;
                    }
                }, applicationLifetime.ApplicationStopping).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _logger.Here().Error(ex, "Unable to initialize Serf");
                applicationLifetime.StopApplication();
            }
        }