-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from FrendsPlatform/Frends.LDAP.AddUserToGroups
init
- Loading branch information
Showing
16 changed files
with
466 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
.github/workflows/AddUserToGroups_build_and_test_on_main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: AddUserToGroups build main | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'Frends.LDAP.AddUserToGroups/**' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_main.yml@main | ||
with: | ||
workdir: Frends.LDAP.AddUserToGroups | ||
prebuild_command: docker run -d -i --rm -p 10389:10389 dwimberger/ldap-ad-it | ||
secrets: | ||
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }} |
19 changes: 19 additions & 0 deletions
19
.github/workflows/AddUserToGroups_build_and_test_on_push.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: AddUserToGroups build test | ||
|
||
on: | ||
push: | ||
branches-ignore: | ||
- main | ||
paths: | ||
- 'Frends.LDAP.AddUserToGroups/**' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_test.yml@main | ||
with: | ||
workdir: Frends.LDAP.AddUserToGroups | ||
prebuild_command: docker run -d -i --rm -p 10389:10389 dwimberger/ldap-ad-it | ||
secrets: | ||
badge_service_api_key: ${{ secrets.BADGE_SERVICE_API_KEY }} | ||
test_feed_api_key: ${{ secrets.TASKS_TEST_FEED_API_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: AddUserToGroups release | ||
|
||
on: | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
uses: FrendsPlatform/FrendsTasks/.github/workflows/release.yml@main | ||
with: | ||
workdir: Frends.LDAP.AddUserToGroups | ||
secrets: | ||
feed_api_key: ${{ secrets.TASKS_FEED_API_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## [1.0.0] - 2022-10-07 | ||
### Added | ||
- Initial implementation |
22 changes: 22 additions & 0 deletions
22
...ddUserToGroups/Frends.LDAP.AddUserToGroups.Tests/Frends.LDAP.AddUserToGroups.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" /> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" /> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Frends.LDAP.AddUserToGroups\Frends.LDAP.AddUserToGroups.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
95 changes: 95 additions & 0 deletions
95
Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups.Tests/UnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Frends.LDAP.AddUserToGroups.Definitions; | ||
using Novell.Directory.Ldap; | ||
namespace Frends.LDAP.AddUserToGroups.Tests; | ||
|
||
[TestClass] | ||
public class UnitTests | ||
{ | ||
/* | ||
LDAP server to docker. | ||
docker run -d -it --rm -p 10389:10389 dwimberger/ldap-ad-it | ||
*/ | ||
private readonly string? _host = "127.0.0.1"; | ||
private readonly int _port = 10389; | ||
private readonly string? _user = "uid=admin,ou=system"; | ||
private readonly string? _pw = "secret"; | ||
private readonly string _path = "ou=users,dc=wimpi,dc=net"; | ||
private readonly string? _groupDn = "cn=admin,ou=roles,dc=wimpi,dc=net"; | ||
|
||
Input? input; | ||
Connection? connection; | ||
|
||
[TestMethod] | ||
public void Update_HandleLDAPError_Test() | ||
{ | ||
input = new() | ||
{ | ||
UserDistinguishedName = "CN=Common Name,CN=Users,DC=Example,DC=Com", | ||
GroupDistinguishedName = "CN=Admins,DC=Example,DC=Com", | ||
UserExistsAction = UserExistsAction.Throw | ||
}; | ||
connection = new() | ||
{ | ||
Host = _host, | ||
User = _user, | ||
Password = _pw, | ||
SecureSocketLayer = false, | ||
Port = _port, | ||
TLS = false, | ||
}; | ||
|
||
var ex = Assert.ThrowsException<Exception>(() => LDAP.AddUserToGroups(input, connection)); | ||
Assert.IsTrue(ex.Message.Contains("No Such Object")); | ||
} | ||
|
||
[TestMethod] | ||
public void AddUserToGroups_Test() | ||
{ | ||
var tuser = "Tes Tuser" + Guid.NewGuid().ToString(); | ||
var dn = $"CN={tuser},ou=users,dc=wimpi,dc=net"; | ||
CreateTestUsers(tuser); | ||
|
||
input = new() | ||
{ | ||
UserDistinguishedName = dn, | ||
GroupDistinguishedName = _groupDn | ||
}; | ||
connection = new() | ||
{ | ||
Host = _host, | ||
User = _user, | ||
Password = _pw, | ||
SecureSocketLayer = false, | ||
Port = _port, | ||
TLS = false, | ||
}; | ||
|
||
var result = LDAP.AddUserToGroups(input, connection); | ||
Assert.IsTrue(result.Success.Equals(true)); | ||
} | ||
|
||
public void CreateTestUsers(string tuser) | ||
{ | ||
try | ||
{ | ||
LdapConnection conn = new(); | ||
conn.Connect(_host, _port); | ||
conn.Bind(_user, _pw); | ||
|
||
var attributeSet = new LdapAttributeSet(); | ||
attributeSet.Add(new LdapAttribute("objectclass", "user")); | ||
attributeSet.Add(new LdapAttribute("cn", tuser)); | ||
attributeSet.Add(new LdapAttribute("givenname", "Tes")); | ||
attributeSet.Add(new LdapAttribute("sn", tuser.Split(' ', 1))); | ||
|
||
var entry = $"CN={tuser},{_path}"; | ||
LdapEntry newEntry = new(entry, attributeSet); | ||
conn.Add(newEntry); | ||
conn.Disconnect(); | ||
} | ||
catch (Exception) | ||
{ | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.1.32319.34 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Frends.LDAP.AddUserToGroups", "Frends.LDAP.AddUserToGroups\Frends.LDAP.AddUserToGroups.csproj", "{35C305C0-8108-4A98-BB1D-AFE5C926239E}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{78F7F22E-6E20-4BCE-8362-0C558568B729}" | ||
ProjectSection(SolutionItems) = preProject | ||
CHANGELOG.md = CHANGELOG.md | ||
..\.github\workflows\AddUserToGroups_build_and_test_on_main.yml = ..\.github\workflows\AddUserToGroups_build_and_test_on_main.yml | ||
..\.github\workflows\AddUserToGroups_build_and_test_on_push.yml = ..\.github\workflows\AddUserToGroups_build_and_test_on_push.yml | ||
..\.github\workflows\AddUserToGroups_release.yml = ..\.github\workflows\AddUserToGroups_release.yml | ||
README.md = README.md | ||
EndProjectSection | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Frends.LDAP.AddUserToGroups.Tests", "Frends.LDAP.AddUserToGroups.Tests\Frends.LDAP.AddUserToGroups.Tests.csproj", "{57A63142-3694-4833-AA13-20233A6B57B0}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{35C305C0-8108-4A98-BB1D-AFE5C926239E}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{57A63142-3694-4833-AA13-20233A6B57B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{57A63142-3694-4833-AA13-20233A6B57B0}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{57A63142-3694-4833-AA13-20233A6B57B0}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{57A63142-3694-4833-AA13-20233A6B57B0}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {8986D685-9988-4F5F-B8D9-E42A4E44BFED} | ||
EndGlobalSection | ||
EndGlobal |
60 changes: 60 additions & 0 deletions
60
Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups/AddUserToGroups.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using Frends.LDAP.AddUserToGroups.Definitions; | ||
using System.ComponentModel; | ||
using Novell.Directory.Ldap; | ||
using System; | ||
|
||
namespace Frends.LDAP.AddUserToGroups; | ||
|
||
/// <summary> | ||
/// LDAP task. | ||
/// </summary> | ||
public class LDAP | ||
{ | ||
/// <summary> | ||
/// Add user to Active Directory groups. | ||
/// [Documentation](https://tasks.frends.com/tasks/frends-tasks/Frends.LDAP.AddUserToGroups) | ||
/// </summary> | ||
/// <param name="input">Input parameters.</param> | ||
/// <param name="connection">Connection parameters.</param> | ||
/// <returns>Object { bool Success, string Error, string CommonName, string Path }</returns> | ||
public static Result AddUserToGroups([PropertyTab] Input input, [PropertyTab] Connection connection) | ||
{ | ||
if (string.IsNullOrWhiteSpace(connection.Host) || string.IsNullOrWhiteSpace(connection.User) || string.IsNullOrWhiteSpace(connection.Password)) | ||
throw new Exception("AddUserToGroups error: Connection parameters missing."); | ||
|
||
LdapConnection conn = new(); | ||
|
||
try | ||
{ | ||
var defaultPort = connection.SecureSocketLayer ? 636 : 389; | ||
|
||
conn.SecureSocketLayer = connection.SecureSocketLayer; | ||
conn.Connect(connection.Host, connection.Port == 0 ? defaultPort : connection.Port); | ||
if (connection.TLS) conn.StartTls(); | ||
conn.Bind(connection.User, connection.Password); | ||
|
||
LdapModification[] mods = new LdapModification[1]; | ||
var member = new LdapAttribute("member", input.UserDistinguishedName); | ||
mods[0] = new LdapModification(LdapModification.Add, member); | ||
conn.Modify(input.GroupDistinguishedName, mods); | ||
|
||
return new Result(true, null, input.UserDistinguishedName, input.GroupDistinguishedName); | ||
} | ||
catch (LdapException ex) | ||
{ | ||
if (ex.Message.Equals("Attribute Or Value Exists") && input.UserExistsAction.Equals(UserExistsAction.Skip)) | ||
return new Result(false, ex.Message, input.UserDistinguishedName, input.GroupDistinguishedName); | ||
else | ||
throw new Exception($"AddUserToGroups LDAP error: {ex.Message}"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new Exception($"AddUserToGroups error: {ex}"); | ||
} | ||
finally | ||
{ | ||
if (connection.TLS) conn.StopTls(); | ||
conn.Disconnect(); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups/Definitions/Connection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.ComponentModel; | ||
namespace Frends.LDAP.AddUserToGroups.Definitions; | ||
|
||
/// <summary> | ||
/// Connection parameters. | ||
/// </summary> | ||
public class Connection | ||
{ | ||
/// <summary> | ||
/// Host. | ||
/// </summary> | ||
/// <example>adserver.westeurope.cloudapp.azure.com</example> | ||
public string Host { get; set; } | ||
|
||
/// <summary> | ||
/// Port. Value 0 = use LDAP/LDAPS default port which is 389 or 636 depending on (SecureSocketLayer) and (TLS). | ||
/// </summary> | ||
/// <example>389</example> | ||
[DefaultValue(0)] | ||
public int Port { get; set; } | ||
|
||
/// <summary> | ||
/// Perform secure operation. | ||
/// </summary> | ||
/// <example>true</example> | ||
public bool SecureSocketLayer { get; set; } | ||
|
||
/// <summary> | ||
/// Connection is protected by TLS. | ||
/// </summary> | ||
/// <example>true</example> | ||
public bool TLS { get; set; } | ||
|
||
/// <summary> | ||
/// User. | ||
/// </summary> | ||
/// <example>Foo</example> | ||
public string User { get; set; } | ||
|
||
/// <summary> | ||
/// Password. | ||
/// </summary> | ||
/// <example>Bar123</example> | ||
[PasswordPropertyText] | ||
public string Password { get; set; } | ||
} |
17 changes: 17 additions & 0 deletions
17
Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups/Definitions/Enums.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Frends.LDAP.AddUserToGroups.Definitions; | ||
|
||
/// <summary> | ||
/// Options if user is already in target group. | ||
/// </summary> | ||
public enum UserExistsAction | ||
{ | ||
/// <summary> | ||
/// Throw an error. | ||
/// </summary> | ||
Throw, | ||
|
||
/// <summary> | ||
/// Do nothing and add LDAP error message to the task's result. | ||
/// </summary> | ||
Skip | ||
} |
28 changes: 28 additions & 0 deletions
28
Frends.LDAP.AddUserToGroups/Frends.LDAP.AddUserToGroups/Definitions/Input.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.ComponentModel; | ||
|
||
namespace Frends.LDAP.AddUserToGroups.Definitions; | ||
|
||
/// <summary> | ||
/// Input parameters. | ||
/// </summary> | ||
public class Input | ||
{ | ||
/// <summary> | ||
/// User's distinguished name (DN) | ||
/// </summary> | ||
/// <example>CN=Tes Tuser,ou=users,dc=wimpi,dc=net</example> | ||
public string UserDistinguishedName { get; set; } | ||
|
||
/// <summary> | ||
/// Group's distinguished name (DN) | ||
/// </summary> | ||
/// <example>cn=admin,ou=roles,dc=wimpi,dc=net</example> | ||
public string GroupDistinguishedName { get; set; } | ||
|
||
/// <summary> | ||
/// Handle user exists exception. | ||
/// </summary> | ||
/// <example>UserExistsAction.Throw</example> | ||
[DefaultValue(UserExistsAction.Throw)] | ||
public UserExistsAction UserExistsAction { get; set; } | ||
} |
Oops, something went wrong.