//---------------------------------------------------------------------------- // // Copyright (C) 2023 Intel Corporation // // File: ProgramInput.cs // // Contents: Program input getting functions. // //---------------------------------------------------------------------------- using Newtonsoft.Json; using System.IO; using Intel.Manageability; using System.Security; using System; namespace Common.Utils { public static class ProgramInput { #region Consts private const string sampleArgsFileName = "SampleArgs.json"; private const string defaultSampleArgsPath = @"..\..\..\..\Common\"; private const string fallBackSampleArgsPath = @"..\..\..\..\..\..\Common\Utils\C#\"; #endregion #region Enum public enum PASSWORD_TYPE { AMT_USER, PROXY, REDIRECTION_PROXY } #endregion #region Public Static Functions public static ConnectionInfoEX DeserializeJsonToStruct(string jsonPath = null) { // config location = input; if (jsonPath == null) { // config location = working directory; jsonPath = Path.Combine(Directory.GetCurrentDirectory(), sampleArgsFileName); if (!File.Exists(jsonPath)) { // location = ..\..\..\..\Common\ jsonPath = Path.GetFullPath(Path.Combine(defaultSampleArgsPath, sampleArgsFileName)); if (!File.Exists(jsonPath)) { // location = ..\..\..\..\..\..\Common\Utils\C#\ jsonPath = Path.GetFullPath(Path.Combine(fallBackSampleArgsPath, sampleArgsFileName)); } } } if (!File.Exists(jsonPath)) { throw new IOException($"The file {jsonPath} does not exist."); } Console.WriteLine($"Loading config file from: {jsonPath}"); string json = File.ReadAllText(jsonPath); ConnectionInfoEX connection; try { connection = JsonConvert.DeserializeObject(json); } catch (JsonReaderException e) { throw new IOException(e.Message); } //check if Json has UserName if (connection?.UserName != null) { connection.Password = PasswordPrompt(PASSWORD_TYPE.AMT_USER.ToString()); } //check if Proxy param was provided if (connection?.Proxy != null) { connection.Proxy.Password = PasswordPrompt(PASSWORD_TYPE.PROXY.ToString()); } //check if RedirectionProxy param was provided if (connection?.RedirectionProxy != null) { connection.RedirectionProxy.Password = PasswordPrompt(PASSWORD_TYPE.REDIRECTION_PROXY.ToString()); } return connection; } /// /// Prompts for Password, does not display the password on the console, and returns a Secure String password /// /// public static SecureString PasswordPrompt(string passwordType) { SecureString secPass = new SecureString(); Console.WriteLine($"Please enter {passwordType} password:"); ConsoleKeyInfo nextPassKey = Console.ReadKey(true); while (nextPassKey.Key != ConsoleKey.Enter) { if (nextPassKey.Key == ConsoleKey.Backspace) { if (secPass.Length > 0) { secPass.RemoveAt(secPass.Length - 1); Console.Write("\b \b"); } } else { secPass.AppendChar(nextPassKey.KeyChar); Console.Write('*'); } nextPassKey = Console.ReadKey(true); } secPass.MakeReadOnly(); Console.Write('\n'); return secPass; } #endregion } }