|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 |
|
3 | 4 | using Amazon.Lambda.Core; |
| 5 | +using Amazon.Lambda.APIGatewayEvents; |
4 | 6 |
|
| 7 | +using Newtonsoft.Json; |
| 8 | +using System.Linq; |
| 9 | +using System.Xml.Serialization; |
| 10 | +using System.Xml.Linq; |
| 11 | +using System.Xml; |
| 12 | +using CsvHelper; |
| 13 | +using CsvHelper.Configuration; |
| 14 | +using YamlDotNet.Serialization; |
5 | 15 | using Zephyr.Crypto; |
| 16 | +using Newtonsoft.Json.Linq; |
| 17 | + |
6 | 18 | using Zephyr.Directory.Ldap; |
7 | 19 |
|
8 | 20 | // Allows Lambda Function's JSON Input to be converted into a .NET class |
9 | 21 | [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] |
10 | | - |
11 | 22 | namespace Zephyr.Directory.Aws |
12 | 23 | { |
13 | 24 | public class Ldap |
14 | 25 | { |
15 | | - public static LdapResponse ProcessRequest(LdapRequest request, ILambdaContext ctx) |
16 | | - { |
17 | | - LdapResponse response = new LdapResponse(); |
18 | | - bool isEncryptionRequest = request.Crypto?.Text != null; |
| 26 | + public static string parse_data(LdapObject data,List<string> headers){ |
| 27 | + List<object> csv_list = new List<object>(); |
| 28 | + string csv_string = null; |
| 29 | + csv_list.Add(data.DistinguishedName); |
| 30 | + csv_string = string.Join(",", csv_list); |
| 31 | + foreach(KeyValuePair<string, dynamic> pair in data.Attributes){ |
| 32 | + Console.WriteLine(pair.Value.GetType()); |
| 33 | + if(pair.Value is string[] || pair.Value is List<string> || pair.Value is int[] || pair.Value is List<int>){ |
| 34 | + string array_string = "[ "; |
| 35 | + array_string += string.Join(", ", pair.Value); |
| 36 | + array_string += " ]"; |
| 37 | + csv_list.Add(array_string); |
| 38 | + } |
| 39 | + else{ |
| 40 | + Console.WriteLine(); |
| 41 | + csv_list.Add(pair.Value); |
| 42 | + } |
| 43 | + csv_string = string.Join(",", csv_list); |
| 44 | + } |
| 45 | + |
| 46 | + return csv_string; |
| 47 | + } |
| 48 | + public static dynamic OutputConverter(LdapResponse response, OutputType? type){ |
| 49 | + dynamic OutputObject = null; |
| 50 | + if(type == OutputType.Json){ |
| 51 | + OutputObject = response; |
| 52 | + } |
| 53 | + else if(type == OutputType.YAML){ |
| 54 | + var serializer = new SerializerBuilder().ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull).Build(); |
| 55 | + var yaml = serializer.Serialize(response); |
| 56 | + OutputObject = yaml; |
| 57 | + } |
| 58 | + else{ |
| 59 | + List<LdapObject> records = response.Records; |
| 60 | + List<string> column_headers = new List<string>(); |
| 61 | + column_headers.Add("dn"); |
| 62 | + foreach(KeyValuePair<string,object> pair in records[0].Attributes) |
| 63 | + column_headers.Add(pair.Key.ToString()); |
| 64 | + string s = string.Join(",", column_headers) + Environment.NewLine; |
| 65 | + foreach (LdapObject record in records){ |
| 66 | + string ss = parse_data(record, column_headers); |
| 67 | + s += $"{ss}{Environment.NewLine}"; |
| 68 | + } |
| 69 | + OutputObject = s; |
| 70 | + } |
| 71 | + return OutputObject; |
| 72 | + } |
| 73 | + public static dynamic ProcessRequest(LdapRequest request, ILambdaContext ctx) |
| 74 | + { |
19 | 75 | bool isPing = request.Ping.HasValue; |
| 76 | + dynamic output_data = null; |
| 77 | + bool isEncryptionRequest = request.Crypto?.Text != null; |
20 | 78 |
|
21 | | - if (!isEncryptionRequest && !isPing) |
22 | | - Console.WriteLine("REQUEST - " + JsonTools.Serialize(request, false)); |
23 | | - |
24 | | - if (isEncryptionRequest) |
25 | | - { |
26 | | - LdapCrypto crypto = LdapUtils.ApplyDefaulsAndValidate(request.Crypto); |
27 | | - response.Message = Rijndael.Encrypt(crypto.Text, crypto.PassPhrase, crypto.SaltValue, crypto.InitVector); |
| 79 | + LdapResponse response = new LdapResponse(); |
| 80 | + LdapConfig test_config = LdapUtils.ApplyDefaulsAndValidate(request.Config); |
| 81 | + if(test_config.batch == true && test_config.retrieval == false){ |
| 82 | + DynamoDBTools dynamo = new DynamoDBTools(); |
| 83 | + LdapBatchResponse new_response = new LdapBatchResponse(); |
| 84 | + new_response = dynamo.invokeLambda(request); |
| 85 | + output_data = new_response; |
28 | 86 | } |
29 | | - else if (isPing) |
30 | | - { |
31 | | - System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); |
32 | | - System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); |
33 | | - string version = fvi.FileVersion; |
34 | | - |
35 | | - response.Message = "Hello From MyriAD (" + version + ")."; |
36 | | - if (request.Ping == PingType.Echo) |
37 | | - Console.WriteLine("Ping"); |
| 87 | + else if(test_config.retrieval == true && test_config.batch == false){ |
| 88 | + DynamoDBTools dynamo = new DynamoDBTools(); |
| 89 | + LdapResponse new_response = new LdapResponse(); |
| 90 | + new_response = dynamo.Batch_Retrieval(request); |
| 91 | + output_data = new_response; |
38 | 92 | } |
39 | | - else |
40 | | - { |
41 | | - try |
| 93 | + else{ |
| 94 | + if (!isEncryptionRequest && !isPing) |
| 95 | + Console.WriteLine("REQUEST - " + JsonTools.Serialize(request, false)); |
| 96 | + |
| 97 | + if (isEncryptionRequest) |
42 | 98 | { |
43 | | - LdapUtils.ApplyDefaulsAndValidate(request); |
44 | | - string searchFilter = LdapUtils.GetSearchString(request); |
45 | | - // if (request.ObjectType != null && request.Union != null) |
46 | | - // throw new FormatException("Warning: Myriad currently does not support this type of call: Union with objectType"); |
47 | | - LdapServer ldap = new LdapServer(request.Config); |
48 | | - ldap.Bind(request.Config); |
49 | | - if(request.Config.TokenType == "Server" || request.Config.TokenType == "Client"){ |
50 | | - response = ldap.Search(request, request.SearchBase, searchFilter, request.Attributes, request.SearchScope, request.MaxResults, request.NextToken, request.Union); |
51 | | - } |
52 | | - else{ |
53 | | - throw new FormatException("Warning: TokenType must be set to Server or Client"); |
54 | | - } |
55 | | - ldap.Disconnect(); |
| 99 | + LdapCrypto crypto = LdapUtils.ApplyDefaulsAndValidate(request.Crypto); |
| 100 | + response.Message = Rijndael.Encrypt(crypto.Text, crypto.PassPhrase, crypto.SaltValue, crypto.InitVector); |
| 101 | + output_data = response; |
| 102 | + } |
| 103 | + else if (isPing) |
| 104 | + { |
| 105 | + System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); |
| 106 | + System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location); |
| 107 | + string version = fvi.FileVersion; |
| 108 | + response.Message = "Hello From MyriAD (" + version + ")."; |
| 109 | + if (request.Ping == PingType.Echo) |
| 110 | + Console.WriteLine("Ping"); |
56 | 111 | } |
57 | | - catch (Exception e) |
| 112 | + else |
58 | 113 | { |
59 | | - response = LdapServer.ReturnError(e, request.Config); |
| 114 | + try |
| 115 | + { |
| 116 | + LdapUtils.ApplyDefaulsAndValidate(request); |
| 117 | + string searchFilter = LdapUtils.GetSearchString(request); |
| 118 | + LdapServer ldap = new LdapServer(request.Config); |
| 119 | + ldap.Bind(request.Config); |
| 120 | + if(request.Config.TokenType == "Server" || request.Config.TokenType == "Client"){ |
| 121 | + try{ |
| 122 | + if(request.Config.batch == true && request.Config.retrieval == true){ |
| 123 | + DynamoDBTools db = new DynamoDBTools(); |
| 124 | + db.add_entry(request); |
| 125 | + } |
| 126 | + } |
| 127 | + catch(Exception e){ |
| 128 | + Console.WriteLine(e); |
| 129 | + } |
| 130 | + response = ldap.Search(request, request.SearchBase, searchFilter, request.Attributes, request.SearchScope, request.MaxResults, request.NextToken, request.Union); |
| 131 | + } |
| 132 | + else{ |
| 133 | + throw new FormatException("Warning: TokenType must be set to Server or Client"); |
| 134 | + } |
| 135 | + ldap.Disconnect(); |
| 136 | + output_data = OutputConverter(response, request.Config.outputType); |
| 137 | + } |
| 138 | + catch (Exception e) |
| 139 | + { |
| 140 | + response = LdapServer.ReturnError(e, request.Config); |
| 141 | + output_data = response; |
| 142 | + } |
| 143 | + try{ |
| 144 | + if(request.Config.batch == true && request.Config.retrieval == true){ |
| 145 | + DynamoDBTools db = new DynamoDBTools(); |
| 146 | + db.update_entry(response, request); |
| 147 | + } |
| 148 | + } |
| 149 | + catch{ |
| 150 | + Console.WriteLine(""); |
| 151 | + } |
60 | 152 | } |
61 | 153 | } |
62 | | - |
63 | | - if (!isEncryptionRequest && !isPing) |
64 | | - Console.WriteLine("RESPONSE - " + JsonTools.Serialize(response, false)); |
65 | | - |
66 | | - return response; |
| 154 | + return output_data; |
67 | 155 | } |
68 | 156 | } |
69 | 157 | } |
0 commit comments