internal static async Task AppendEnterpriseCommands(this IEnterpriseContext context, CliCommands cli) { cli.Commands.Add("enterprise-sync-down", new SimpleCommand { Order = 60, Description = "Retrieve enterprise data", Action = async _ => { await context.Enterprise.PopulateEnterprise(); }, }); cli.Commands.Add("enterprise-node", new ParsableCommand <EnterpriseNodeOptions> { Order = 61, Description = "Display node structure", Action = async options => { await context.EnterpriseNodeCommand(options); }, }); cli.Commands.Add("enterprise-user", new ParsableCommand <EnterpriseUserOptions> { Order = 62, Description = "List Enterprise Users", Action = async options => { await context.EnterpriseUserCommand(options); }, }); cli.Commands.Add("enterprise-team", new ParsableCommand <EnterpriseTeamOptions> { Order = 63, Description = "List Enterprise Teams", Action = async options => { await context.EnterpriseTeamCommand(options); }, }); cli.Commands.Add("enterprise-device", new ParsableCommand <EnterpriseDeviceOptions> { Order = 64, Description = "Manage User Devices", Action = async options => { await context.EnterpriseDeviceCommand(options); }, }); cli.Commands.Add("audit-report", new ParsableCommand <AuditReportOptions> { Order = 64, Description = "Run an audit trail report.", Action = async options => { await context.RunAuditEventsReport(options); }, }); cli.CommandAliases["esd"] = "enterprise-sync-down"; cli.CommandAliases["en"] = "enterprise-node"; cli.CommandAliases["eu"] = "enterprise-user"; cli.CommandAliases["et"] = "enterprise-team"; cli.CommandAliases["ed"] = "enterprise-device"; var entRq = new GetEnterpriseDataCommand { include = new[] { "keys" } }; var entRs = await context.Enterprise.Auth.ExecuteAuthCommand <GetEnterpriseDataCommand, GetEnterpriseDataResponse>(entRq); if (string.IsNullOrEmpty(entRs.Keys?.EccEncryptedPrivateKey)) { cli.Commands.Add("enterprise-add-key", new SimpleCommand { Order = 63, Description = "Register ECC key pair", Action = async options => { await context.EnterpriseRegisterEcKey(cli); }, }); } else { var privateKeyData = CryptoUtils.DecryptAesV2( entRs.Keys.EccEncryptedPrivateKey.Base64UrlDecode(), context.Enterprise.TreeKey); context.EnterprisePrivateKey = CryptoUtils.LoadPrivateEcKey(privateKeyData); } }
internal static async Task EnterpriseRegisterEcKey(this IEnterpriseContext context, CliCommands cli) { if (context.Enterprise.TreeKey == null) { Console.WriteLine("Cannot get tree key"); return; } CryptoUtils.GenerateEcKey(out var privateKey, out var publicKey); var exportedPublicKey = CryptoUtils.UnloadEcPublicKey(publicKey); var exportedPrivateKey = CryptoUtils.UnloadEcPrivateKey(privateKey); var encryptedPrivateKey = CryptoUtils.EncryptAesV2(exportedPrivateKey, context.Enterprise.TreeKey); var request = new EnterpriseKeyPairRequest { KeyType = KeyType.Ecc, EnterprisePublicKey = ByteString.CopyFrom(exportedPublicKey), EncryptedEnterprisePrivateKey = ByteString.CopyFrom(encryptedPrivateKey), }; await context.Enterprise.Auth.ExecuteAuthRest("enterprise/set_enterprise_key_pair", request); cli.Commands.Remove("enterprise-add-key"); }
public static async Task EnterpriseTeamCommand(this IEnterpriseContext context, EnterpriseTeamOptions arguments) { if (arguments.Force) { await context.Enterprise.PopulateEnterprise(); } if (string.IsNullOrEmpty(arguments.Command)) { arguments.Command = "list"; } if (string.CompareOrdinal(arguments.Command, "list") == 0) { var teams = context.Enterprise.Teams .Where(x => { if (string.IsNullOrEmpty(arguments.Name)) { return(true); } if (arguments.Name == x.Uid) { return(true); } var m = Regex.Match(x.Name, arguments.Name, RegexOptions.IgnoreCase); return(m.Success); }) .ToArray(); var tab = new Tabulate(7) { DumpRowNo = true }; tab.AddHeader("Team Name", "Team UID", "Node Name", "Restrict Edit", "Restrict Share", "Restrict View", "Users"); foreach (var team in teams) { EnterpriseNode node = null; if (team.ParentNodeId > 0) { context.Enterprise.TryGetNode(team.ParentNodeId, out node); } else { node = context.Enterprise.RootNode; } tab.AddRow(team.Name, team.Uid, node != null ? node.DisplayName : "", team.RestrictEdit ? "X" : "-", team.RestrictSharing ? "X" : "-", team.RestrictView ? "X" : "-", team.Users.Count.ToString()); } tab.Sort(1); tab.Dump(); } else { var team = context.Enterprise.Teams .FirstOrDefault(x => { if (string.IsNullOrEmpty(arguments.Name)) { return(true); } if (arguments.Name == x.Uid) { return(true); } return(string.Compare(x.Name, arguments.Name, StringComparison.CurrentCultureIgnoreCase) == 0); }); if (string.CompareOrdinal(arguments.Command, "delete") == 0) { if (team == null) { Console.WriteLine($"Team \"{arguments.Name}\" not found"); return; } await context.Enterprise.DeleteTeam(team.Uid); } else if (string.CompareOrdinal(arguments.Command, "view") == 0) { if (team == null) { Console.WriteLine($"Team \"{arguments.Name}\" not found"); return; } var tab = new Tabulate(2) { DumpRowNo = false }; tab.SetColumnRightAlign(0, true); tab.AddRow(" Team Name:", team.Name); tab.AddRow(" Team UID:", team.Uid); tab.AddRow(" Restrict Edit:", team.RestrictEdit ? "Yes" : "No"); tab.AddRow(" Restrict Share:", team.RestrictSharing ? "Yes" : "No"); tab.AddRow(" Restrict View:", team.RestrictView ? "Yes" : "No"); var users = team.Users .Select(x => context.Enterprise.TryGetUserById(x, out var user) ? user.Email : null) .Where(x => !string.IsNullOrEmpty(x)) .ToArray(); Array.Sort(users); tab.AddRow(" Users:", users.Length > 0 ? users[0] : ""); for (var i = 1; i < users.Length; i++) { tab.AddRow("", users[i]); } if (context.Enterprise.TryGetNode(team.ParentNodeId, out var node)) { var nodes = context.GetNodePath(node).ToArray(); Array.Reverse(nodes); tab.AddRow(" Node:", string.Join(" -> ", nodes)); } tab.Dump(); } else if (string.CompareOrdinal(arguments.Command, "update") == 0 || string.CompareOrdinal(arguments.Command, "add") == 0) { if (team == null) { if (string.CompareOrdinal(arguments.Command, "update") == 0 || string.CompareOrdinal(arguments.Command, "view") == 0) { Console.WriteLine($"Team \"{arguments.Name}\" not found"); return; } team = new EnterpriseTeam { ParentNodeId = context.Enterprise.RootNode.Id }; } else { if (string.CompareOrdinal(arguments.Command, "add") == 0) { Console.WriteLine($"Team with name \"{arguments.Name}\" already exists.\nDo you want to create a new one? Yes/No"); var answer = await Program.GetInputManager().ReadLine(); if (string.Compare("y", answer, StringComparison.InvariantCultureIgnoreCase) == 0) { answer = "yes"; } if (string.Compare(answer, "yes", StringComparison.InvariantCultureIgnoreCase) != 0) { return; } } } team.Name = arguments.Name; if (CliCommands.ParseBoolOption(arguments.RestrictEdit, out var b)) { team.RestrictEdit = b; } if (CliCommands.ParseBoolOption(arguments.RestrictShare, out b)) { team.RestrictSharing = b; } if (CliCommands.ParseBoolOption(arguments.RestrictView, out b)) { team.RestrictView = b; } if (!string.IsNullOrEmpty(arguments.Node)) { long?asId = null; if (arguments.Node.All(char.IsDigit)) { if (long.TryParse(arguments.Node, out var l)) { asId = l; } } var node = context.Enterprise.Nodes .FirstOrDefault(x => { if (asId.HasValue && asId.Value == x.Id) { return(true); } return(string.Compare(x.DisplayName, arguments.Node, StringComparison.CurrentCultureIgnoreCase) == 0); }); if (node != null) { team.ParentNodeId = node.Id; } } await context.Enterprise.UpdateTeam(team); } else { Console.WriteLine($"Unsupported command \"{arguments.Command}\". Valid commands are \"list\", \"view\", \"add\", \"delete\", \"update\""); } } }