Exemplo n.º 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, signCertDir);
                var signCertName   = certName;
                var signCertFormat = "p12";
                if (actionResult)
                {
                    Directory.CreateDirectory(outputCertDir);
                    commandRunner = new ShellCommandRunner(
                        _loggerFactory,
                        "/bin/bash",
                        $"{_shellScript} {_certificateCommonName} {signCertName} {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.WriteBinaryFile("sign-cert", Path.Combine(_assemblyDir, signCertDir, $"{signCertName}.{signCertFormat}"));
                    result = new X509Certificate2(Path.Combine(_assemblyDir, signCertDir, $"{signCertName}.{signCertFormat}"));
                    DeleteLocalFiles();
                }
            } catch (Exception exc) {
                actionResult = false;
                _logger.LogError(exc, "Certificate generation failded");
            }

            return(result);
        }