|
| 1 | +// Copyright 2021 Keyfactor |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | +// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, |
| 5 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions |
| 6 | +// and limitations under the License. |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.IO; |
| 10 | +using System.Security.Cryptography; |
| 11 | + |
| 12 | +using CliWrap; |
| 13 | +using CliWrap.Buffered; |
| 14 | + |
| 15 | +using Renci.SshNet; |
| 16 | + |
| 17 | +using Microsoft.Extensions.Logging; |
| 18 | + |
| 19 | +using Keyfactor.Logging; |
| 20 | +using Keyfactor.PKI.PrivateKeys; |
| 21 | +using Keyfactor.PKI.PEM; |
| 22 | + |
| 23 | +namespace Keyfactor.Extensions.Orchestrator.RemoteFile.RemoteHandlers |
| 24 | +{ |
| 25 | + class LinuxLocalHandler : BaseRemoteHandler |
| 26 | + { |
| 27 | + private Command BaseCommand { get; set; } |
| 28 | + |
| 29 | + internal LinuxLocalHandler() |
| 30 | + { |
| 31 | + _logger.MethodEntry(LogLevel.Debug); |
| 32 | + _logger.MethodExit(LogLevel.Debug); |
| 33 | + } |
| 34 | + |
| 35 | + public override void Initialize() |
| 36 | + { |
| 37 | + _logger.MethodEntry(LogLevel.Debug); |
| 38 | + |
| 39 | + BaseCommand = Cli.Wrap("/bin/bash"); |
| 40 | + |
| 41 | + _logger.MethodExit(LogLevel.Debug); |
| 42 | + } |
| 43 | + |
| 44 | + public override void Terminate() |
| 45 | + { |
| 46 | + _logger.MethodEntry(LogLevel.Debug); |
| 47 | + _logger.MethodExit(LogLevel.Debug); |
| 48 | + } |
| 49 | + |
| 50 | + public override string RunCommand(string commandText, object[] arguments, bool withSudo, string[] passwordsToMaskInLog) |
| 51 | + { |
| 52 | + _logger.MethodEntry(LogLevel.Debug); |
| 53 | + |
| 54 | + string sudo = $"echo -e '\n' | sudo -i -S "; |
| 55 | + |
| 56 | + try |
| 57 | + { |
| 58 | + if (withSudo) |
| 59 | + commandText = sudo + commandText; |
| 60 | + |
| 61 | + string displayCommand = commandText; |
| 62 | + if (passwordsToMaskInLog != null) |
| 63 | + { |
| 64 | + foreach (string password in passwordsToMaskInLog) |
| 65 | + displayCommand = displayCommand.Replace(password, PASSWORD_MASK_VALUE); |
| 66 | + } |
| 67 | + |
| 68 | + _logger.LogDebug($"RunCommand: {displayCommand}"); |
| 69 | + |
| 70 | + Command cmd = BaseCommand.WithArguments($@"-c ""{commandText}"""); |
| 71 | + BufferedCommandResult result = cmd.ExecuteBufferedAsync().GetAwaiter().GetResult(); |
| 72 | +
|
| 73 | + _logger.LogDebug($"Linux Local Results: {displayCommand}::: {result.StandardOutput}::: {result.StandardError}"); |
| 74 | + |
| 75 | + if (!String.IsNullOrEmpty(result.StandardError)) |
| 76 | + throw new ApplicationException(result.StandardError); |
| 77 | + |
| 78 | + _logger.MethodExit(LogLevel.Debug); |
| 79 | + |
| 80 | + return result.StandardOutput; |
| 81 | + } |
| 82 | + catch (Exception ex) |
| 83 | + { |
| 84 | + _logger.LogError($"Exception during RunCommand...{RemoteFileException.FlattenExceptionMessages(ex, ex.Message)}"); |
| 85 | + throw ex; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public override void UploadCertificateFile(string path, string fileName, byte[] certBytes) |
| 90 | + { |
| 91 | + _logger.MethodEntry(LogLevel.Debug); |
| 92 | + _logger.LogDebug($"UploadCertificateFile: {path}{fileName}"); |
| 93 | + |
| 94 | + string uploadPath = path+fileName; |
| 95 | + |
| 96 | + try |
| 97 | + { |
| 98 | + File.WriteAllBytes(uploadPath, certBytes); |
| 99 | + } |
| 100 | + catch (Exception ex) |
| 101 | + { |
| 102 | + _logger.LogError($"Error attempting upload file to {uploadPath}..."); |
| 103 | + _logger.LogError($"Upload Exception: {RemoteFileException.FlattenExceptionMessages(ex, ex.Message)}"); |
| 104 | + throw new RemoteFileException($"Error attempting upload file to {uploadPath}.", ex); |
| 105 | + } |
| 106 | + |
| 107 | + _logger.MethodExit(LogLevel.Debug); |
| 108 | + } |
| 109 | + |
| 110 | + public override byte[] DownloadCertificateFile(string path) |
| 111 | + { |
| 112 | + _logger.MethodEntry(LogLevel.Debug); |
| 113 | + _logger.LogDebug($"DownloadCertificateFile: {path}"); |
| 114 | + |
| 115 | + byte[] rtnStore = new byte[] { }; |
| 116 | + |
| 117 | + try |
| 118 | + { |
| 119 | + rtnStore = File.ReadAllBytes(path); |
| 120 | + } |
| 121 | + catch (Exception ex) |
| 122 | + { |
| 123 | + _logger.LogError($"Error attempting download file {path}..."); |
| 124 | + _logger.LogError($"Download Exception: {RemoteFileException.FlattenExceptionMessages(ex, ex.Message)}"); |
| 125 | + throw new RemoteFileException($"Error attempting download file {path}.", ex); |
| 126 | + } |
| 127 | + |
| 128 | + _logger.MethodExit(LogLevel.Debug); |
| 129 | + |
| 130 | + return rtnStore; |
| 131 | + } |
| 132 | + |
| 133 | + public override void CreateEmptyStoreFile(string path, string linuxFilePermissions, string linuxFileOwner) |
| 134 | + { |
| 135 | + _logger.MethodEntry(LogLevel.Debug); |
| 136 | + string[] linuxGroupOwner = linuxFileOwner.Split(":"); |
| 137 | + string linuxFileGroup = linuxFileOwner; |
| 138 | + |
| 139 | + if (linuxGroupOwner.Length == 2) |
| 140 | + { |
| 141 | + linuxFileOwner = linuxGroupOwner[0]; |
| 142 | + linuxFileGroup = linuxGroupOwner[1]; |
| 143 | + } |
| 144 | + |
| 145 | + AreLinuxPermissionsValid(linuxFilePermissions); |
| 146 | + RunCommand($"install -m {linuxFilePermissions} -o {linuxFileOwner} -g {linuxFileGroup} /dev/null {path}", null, ApplicationSettings.UseSudo, null); |
| 147 | + |
| 148 | + _logger.MethodExit(LogLevel.Debug); |
| 149 | + } |
| 150 | + |
| 151 | + public override bool DoesFileExist(string path) |
| 152 | + { |
| 153 | + _logger.MethodEntry(LogLevel.Debug); |
| 154 | + _logger.LogDebug($"DoesFileExist: {path}"); |
| 155 | + |
| 156 | + return File.Exists(path); |
| 157 | + } |
| 158 | + |
| 159 | + public override void RemoveCertificateFile(string path, string fileName) |
| 160 | + { |
| 161 | + _logger.LogDebug($"RemoveCertificateFile: {path} {fileName}"); |
| 162 | + |
| 163 | + RunCommand($"rm {path}{fileName}", null, ApplicationSettings.UseSudo, null); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments