Skip to content

Commit 55b0aac

Browse files
authored
Merge pull request #2 from SynapseProject/develop
MyriAD Version 1.1.24219.0
2 parents c159497 + b89047a commit 55b0aac

30 files changed

Lines changed: 1472 additions & 617 deletions
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
using Microsoft.AspNetCore.Mvc;
7+
using Zephyr.Directory.Ldap;
8+
9+
namespace Zephyr.Directory.ASPDotNet.Controllers
10+
{
11+
[ApiController]
12+
[Route("/")]
13+
public class MyriADController : ControllerBase
14+
{
15+
private readonly ILogger<MyriADController> _logger;
16+
17+
public MyriADController(ILogger<MyriADController> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
[HttpGet]
23+
[Route("/{objectType}/{*value}")]
24+
public LdapResponse Get(string objectType, string value)
25+
{
26+
LdapRequest request = new LdapRequest();
27+
28+
request.ObjectType = Enum.Parse<ObjectType>(objectType, true);
29+
request.SearchValue = value;
30+
if (Request.Query.ContainsKey("domain"))
31+
request.Domain = Request.Query["domain"];
32+
if (Request.Query.ContainsKey("searchBase"))
33+
request.SearchBase = Request.Query["searchBase"];
34+
if (Request.Query.ContainsKey("searchScope"))
35+
request.SearchScope = Enum.Parse<SearchScopeType>(Request.Query["searchScope"], true);
36+
if (Request.Query.ContainsKey("maxResults"))
37+
request.MaxResults = Int32.Parse(Request.Query["maxResults"]);
38+
if (Request.Query.ContainsKey("nextToken"))
39+
request.NextToken = Request.Query["nextToken"];
40+
if (Request.Query.ContainsKey("attr"))
41+
{
42+
request.Attributes = new List<string>();
43+
foreach (string attr in Request.Query["attr"])
44+
request.Attributes.Add(attr);
45+
}
46+
47+
return Search(request);
48+
}
49+
50+
[HttpPost]
51+
[Route("/search")]
52+
public LdapResponse Search(LdapRequest request)
53+
{
54+
Console.WriteLine(JsonTools.Serialize(request, true));
55+
56+
LdapResponse response = request.Process();
57+
58+
Console.WriteLine(JsonTools.Serialize(response, true));
59+
60+
return response;
61+
}
62+
63+
}
64+
}
65+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Text.Json.Serialization;
2+
3+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
builder.Services.AddControllers().AddJsonOptions(opt =>
7+
{
8+
opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
9+
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
10+
});
11+
12+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
13+
builder.Services.AddEndpointsApiExplorer();
14+
builder.Services.AddSwaggerGen();
15+
16+
WebApplication app = builder.Build();
17+
18+
// Configure the HTTP request pipeline.
19+
if (app.Environment.IsDevelopment())
20+
{
21+
app.UseSwagger();
22+
app.UseSwaggerUI();
23+
}
24+
25+
app.UseHttpsRedirection();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllers();
30+
31+
app.Run();
32+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:58075",
8+
"sslPort": 44325
9+
}
10+
},
11+
"profiles": {
12+
"Zephyr.Directory.ASPDotNet": {
13+
"commandName": "Project",
14+
"launchUrl": "swagger",
15+
"applicationUrl": "https://localhost:7081;http://localhost:5131",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development",
18+
"DEFAULT_CONFIG": "{\"server\":\"VM-ADCONTROLLER.local\",\"ssl\":false,\"username\":\"SANDBOX\\\\Administrator\",\"password\":\"Pm8sgIkgGk2JwYKp9Gcjrw==\",\"maxRetries\": 3,\"maxPageSize\":750, \"tokenType\": \"Client\"}",
19+
"SB2_CONFIG": "{\"server\":\"VM-AD-SB2.local\",\"username\":\"SB2\\\\Administrator\",\"maxRetries\": 3, \"tokenType\": \"Client\"}",
20+
"RETURN_TYPES": "{ \"customAttr003\": \"StringArray\" } ",
21+
"DOMAIN_MAPPINGS": "{\"SANDBOX.LOCAL\":\"DEFAULT_CONFIG\",\"SANDBOX\":\"DEFAULT_CONFIG\",\"SB2.LOCAL\":\"SB2_CONFIG\",\"SB2\":\"SB2_CONFIG\"}"
22+
},
23+
"dotnetRunMessages": true
24+
},
25+
"IIS Express": {
26+
"commandName": "IISExpress",
27+
"launchUrl": "swagger",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
}
32+
}
33+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\Zephyr.Directory.Ldap\Zephyr.Directory.Ldap.csproj">
15+
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
16+
</ProjectReference>
17+
<ProjectReference Include="..\Zephyr.Version\Zephyr.Version.csproj">
18+
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
19+
</ProjectReference>
20+
</ItemGroup>
21+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
9+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}
10+

Zephyr.Directory.Aws/Ldap.cs

Lines changed: 132 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,157 @@
11
using System;
2+
using System.Collections.Generic;
23

34
using Amazon.Lambda.Core;
5+
using Amazon.Lambda.APIGatewayEvents;
46

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;
515
using Zephyr.Crypto;
16+
using Newtonsoft.Json.Linq;
17+
618
using Zephyr.Directory.Ldap;
719

820
// Allows Lambda Function's JSON Input to be converted into a .NET class
921
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
10-
1122
namespace Zephyr.Directory.Aws
1223
{
1324
public class Ldap
1425
{
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+
{
1975
bool isPing = request.Ping.HasValue;
76+
dynamic output_data = null;
77+
bool isEncryptionRequest = request.Crypto?.Text != null;
2078

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;
2886
}
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;
3892
}
39-
else
40-
{
41-
try
93+
else{
94+
if (!isEncryptionRequest && !isPing)
95+
Console.WriteLine("REQUEST - " + JsonTools.Serialize(request, false));
96+
97+
if (isEncryptionRequest)
4298
{
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");
56111
}
57-
catch (Exception e)
112+
else
58113
{
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+
}
60152
}
61153
}
62-
63-
if (!isEncryptionRequest && !isPing)
64-
Console.WriteLine("RESPONSE - " + JsonTools.Serialize(response, false));
65-
66-
return response;
154+
return output_data;
67155
}
68156
}
69157
}

Zephyr.Directory.Aws/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
[assembly: AssemblyConfiguration("")]
1010
[assembly: AssemblyCompany("")]
1111
[assembly: AssemblyProduct("Zephyr.Directory.Aws")]
12-
[assembly: AssemblyCopyright("Copyright © Guy Waguespack, 2019 - 2023")]
12+
[assembly: AssemblyCopyright("Copyright © Guy Waguespack, 2019 - 2024")]
1313
[assembly: AssemblyTrademark("")]
1414
[assembly: AssemblyCulture("")]
1515

1616
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
1717
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
1818
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
1919

20-
[assembly: AssemblyVersion("1.1.24059.0")]
21-
[assembly: AssemblyFileVersion("1.1.24059.0")]
20+
[assembly: AssemblyVersion("1.1.24219.0")]
21+
[assembly: AssemblyFileVersion("1.1.24219.0")]
2222

2323
// The following attributes are used to specify the signing key for the assembly,
2424
// if desired. See the Mono documentation for more information about signing.

0 commit comments

Comments
 (0)