Skip to content

Commit

Permalink
Merge pull request #12 from FrendsPlatform/Frends.LDAP.RemoveUserFrom…
Browse files Browse the repository at this point in the history
…Groups

init
  • Loading branch information
Svenskapojkarna authored Oct 13, 2022
2 parents 18f7ba5 + 03b82b8 commit 0e790bc
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/RemoveUserFromGroups_build_and_test_on_main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: RemoveUserFromGroups build main

on:
push:
branches:
- main
paths:
- 'Frends.LDAP.RemoveUserFromGroups/**'
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_main.yml@main
with:
workdir: Frends.LDAP.RemoveUserFromGroups
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 .github/workflows/RemoveUserFromGroups_build_and_test_on_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: RemoveUserFromGroups build test

on:
push:
branches-ignore:
- main
paths:
- 'Frends.LDAP.RemoveUserFromGroups/**'
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/linux_build_test.yml@main
with:
workdir: Frends.LDAP.RemoveUserFromGroups
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 }}
12 changes: 12 additions & 0 deletions .github/workflows/RemoveUserFromGroups_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: RemoveUserFromGroups release

on:
workflow_dispatch:

jobs:
build:
uses: FrendsPlatform/FrendsTasks/.github/workflows/release.yml@main
with:
workdir: Frends.LDAP.RemoveUserFromGroups
secrets:
feed_api_key: ${{ secrets.TASKS_FEED_API_KEY }}
5 changes: 5 additions & 0 deletions Frends.LDAP.RemoveUserFromGroups/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [1.0.0] - 2022-10-13
### Added
- Initial implementation
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.RemoveUserFromGroups\Frends.LDAP.RemoveUserFromGroups.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Frends.LDAP.RemoveUserFromGroups.Definitions;
using Novell.Directory.Ldap;
namespace Frends.LDAP.RemoveUserFromGroups.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_Throw_Test()
{
var tuser = "Tes Tuser" + Guid.NewGuid().ToString();
var dn = $"CN={tuser},{_path}";
CreateTestUsers(tuser);

input = new()
{
UserDistinguishedName = dn,
GroupDistinguishedName = _groupDn,
HandleLDAPError = HandleLDAPError.Throw
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var ex = Assert.ThrowsException<Exception>(() => LDAP.RemoveUserFromGroups(input, connection));
Assert.IsTrue(ex.Message != null);
}

[TestMethod]
public void Update_HandleLDAPError_Skip_Test()
{
var tuser = "Tes Tuser" + Guid.NewGuid().ToString();
var dn = $"CN={tuser},{_path}";
CreateTestUsers(tuser);

input = new()
{
UserDistinguishedName = dn,
GroupDistinguishedName = _groupDn,
HandleLDAPError = HandleLDAPError.Skip
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.RemoveUserFromGroups(input, connection);
Assert.IsTrue(result.Success.Equals(false) && result.Error != null);
}

[TestMethod]
public void RemoveUserFromGroups_Test()
{
var tuser = "Tes Tuser" + Guid.NewGuid().ToString();
var dn = $"CN={tuser},{_path}";
CreateTestUsers(tuser);
AddUserToGroup(dn);

input = new()
{
UserDistinguishedName = dn,
GroupDistinguishedName = _groupDn
};
connection = new()
{
Host = _host,
User = _user,
Password = _pw,
SecureSocketLayer = false,
Port = _port,
TLS = false,
};

var result = LDAP.RemoveUserFromGroups(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", "inetorgperson"));
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)
{
}
}

public void AddUserToGroup(string dn)
{
try
{
LdapConnection conn = new();
conn.Connect(_host, _port);
conn.Bind(_user, _pw);

LdapModification[] mods = new LdapModification[1];
var member = new LdapAttribute("member", dn);
mods[0] = new LdapModification(LdapModification.Add, member);
conn.Modify(_groupDn, mods);
conn.Disconnect();
}
catch (Exception)
{
}
}
}
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.RemoveUserFromGroups", "Frends.LDAP.RemoveUserFromGroups\Frends.LDAP.RemoveUserFromGroups.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\RemoveUserFromGroups_build_and_test_on_main.yml = ..\.github\workflows\RemoveUserFromGroups_build_and_test_on_main.yml
..\.github\workflows\RemoveUserFromGroups_build_and_test_on_push.yml = ..\.github\workflows\RemoveUserFromGroups_build_and_test_on_push.yml
..\.github\workflows\RemoveUserFromGroups_release.yml = ..\.github\workflows\RemoveUserFromGroups_release.yml
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Frends.LDAP.RemoveUserFromGroups.Tests", "Frends.LDAP.RemoveUserFromGroups.Tests\Frends.LDAP.RemoveUserFromGroups.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.ComponentModel;
namespace Frends.LDAP.RemoveUserFromGroups.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; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Frends.LDAP.RemoveUserFromGroups.Definitions;

/// <summary>
/// How to handle LDAP errors.
/// </summary>
public enum HandleLDAPError
{
/// <summary>
/// Throw an error.
/// </summary>
Throw,

/// <summary>
/// Do nothing and add LDAP error message to the task's result.
/// </summary>
Skip
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.ComponentModel;

namespace Frends.LDAP.RemoveUserFromGroups.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>
/// How to handle LDAP errors.
/// </summary>
/// <example>HandleLDAPError.Throw</example>
[DefaultValue(HandleLDAPError.Throw)]
public HandleLDAPError HandleLDAPError { get; set; }
}
Loading

0 comments on commit 0e790bc

Please sign in to comment.