Beispiel #1
0
        public X509Certificate2 Generate(string certName)
        {
            bool             actionResult = true;
            X509Certificate2 result       = null;

            try {
                // Make shell script executable
                var commandRunner = new ShellCommandRunner(
                    _loggerFactory,
                    "/bin/bash",
                    $"-c \"chmod +x '{_shellScript}'\"");

                var commandResult = commandRunner.Run();
                if (commandResult == null || commandResult.ExitCode != 0)
                {
                    _logger.LogError($"Make {_shellScript} script executable failed:\n {commandResult.ErrorStream}");
                    actionResult = false;
                }

                // Run shell script executable
                var outputCertDir = Path.Combine(_assemblyDir, tlsCertDir);
                var tlsCertName   = certName;
                if (actionResult)
                {
                    Directory.CreateDirectory(outputCertDir);
                    commandRunner = new ShellCommandRunner(
                        _loggerFactory,
                        "/bin/bash",
                        $"{_shellScript} {_certificateCommonName} {tlsCertName} {outputCertDir}");

                    commandResult = commandRunner.Run();
                    if (commandResult == null || commandResult.ExitCode != 0)
                    {
                        _logger.LogError($"Command '{_shellScript} {_certificateCommonName} {outputCertDir}' failed:\n {commandResult.ErrorStream}");
                        actionResult = false;
                    }
                }

                if (actionResult)
                {
                    // Output certificate
                    _certificateFileWriter.WriteTlsCertificate(
                        "srs-tls",
                        Path.Combine(_assemblyDir, tlsCertDir, $"{tlsCertName}.crt"),
                        Path.Combine(_assemblyDir, tlsCertDir, $"{tlsCertName}.key"));

                    result = new X509Certificate2(Path.Combine(_assemblyDir, tlsCertDir, $"{tlsCertName}.crt"));
                    DeleteLocalFiles();
                }
            } catch (Exception exc) {
                _logger.LogError(exc, "Certificate generation failded");
            }

            return(result);
        }
Beispiel #2
0
        public void RunCommandThatTimesOut()
        {
            // Arrange
            var shell     = "pwsh";
            var arguments = "--command 'Start-Sleep -Seconds 20'";
            var actor     = new ShellCommandRunner(_loggerFactoryMock.Object, shell, arguments, 100);

            // Act
            var actual = actor.Run();

            // Assert
            Assert.IsNull(actual);
        }
Beispiel #3
0
        public void RunDirCommandInPowerShell()
        {
            // Arrange
            var shell     = "pwsh";
            var arguments = "--command dir";
            var actor     = new ShellCommandRunner(_loggerFactoryMock.Object, shell, arguments);

            // Act
            var actual = actor.Run();

            // Assert
            Assert.NotNull(actual);
            Assert.AreEqual(0, actual.ExitCode);
            Assert.NotNull(actual.OutputStream);
            Assert.IsEmpty(actual.ErrorStream);
        }
Beispiel #4
0
        public void RunWrongCommand()
        {
            // Arrange
            var shell     = "inexistent";
            var arguments = "fake fake fake";
            var actor     = new ShellCommandRunner(_loggerFactoryMock.Object, shell, arguments);

            /// Logger LogError mock is not supported by Moq because it is ExtensionMethod
            //var loggerMock = new Mock<ILogger>();
            //loggerMock.Setup(x => x.LogError(It.IsAny<Exception>(), It.IsAny<string>()));

            //_loggerFactoryMock.Setup(
            //   x => x.CreateLogger(typeof(ShellCommandRunner).FullName))
            //   .Returns(loggerMock.Object);

            //loggerMock.Verify(x => x.LogError(It.IsAny<Exception>(), It.IsAny<string>()), Times.Once());

            // Act
            var actual = actor.Run();

            // Assert
            Assert.IsNull(actual);
        }