|
| 1 | +package com.walmartlabs.concord.plugins.ldap; |
| 2 | + |
| 3 | +/*- |
| 4 | + * ***** |
| 5 | + * Concord |
| 6 | + * ----- |
| 7 | + * Copyright (C) 2017 - 2026 Walmart Inc., Concord Authors |
| 8 | + * ----- |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * ===== |
| 21 | + */ |
| 22 | + |
| 23 | +import com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables; |
| 24 | +import org.junit.jupiter.api.Disabled; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.*; |
| 34 | + |
| 35 | +/** |
| 36 | + * Manual integration tests against a real Active Directory instance. |
| 37 | + * <p> |
| 38 | + * All tests in this class are {@link Disabled} and must be run explicitly. |
| 39 | + * Supply connection details as system properties, e.g.: |
| 40 | + * <pre> |
| 41 | + * mvn test -pl tasks/ldap -Dtest=LdapActiveDirectoryIT#testGetGroup \ |
| 42 | + * -DadServer=ldap://ad.corp.example.com \ |
| 43 | + * -DbindUserDn="CN=svc_ldap,OU=ServiceAccounts,DC=corp,DC=example,DC=com" \ |
| 44 | + * -DbindPassword=secret \ |
| 45 | + * -DsearchBase="DC=corp,DC=example,DC=com" \ |
| 46 | + * -DgroupName="My Security Group" \ |
| 47 | + * -DsecurityEnabled=true \ |
| 48 | + * -DsecurityGroupTypes="-2147483640,-2147483646" |
| 49 | + * </pre> |
| 50 | + * |
| 51 | + * <table border="1"> |
| 52 | + * <caption>System properties</caption> |
| 53 | + * <tr><th>Property</th><th>Description</th><th>Required</th></tr> |
| 54 | + * <tr><td>{@code adServer}</td><td>LDAP URL, e.g. {@code ldap://ad.corp.example.com}</td><td>yes</td></tr> |
| 55 | + * <tr><td>{@code bindUserDn}</td><td>Full DN of the bind user</td><td>yes</td></tr> |
| 56 | + * <tr><td>{@code bindPassword}</td><td>Bind user password</td><td>yes</td></tr> |
| 57 | + * <tr><td>{@code searchBase}</td><td>LDAP search base, e.g. {@code DC=corp,DC=example,DC=com}</td><td>yes</td></tr> |
| 58 | + * <tr><td>{@code groupName}</td><td>AD group name to look up</td><td>yes</td></tr> |
| 59 | + * <tr><td>{@code securityEnabled}</td><td>{@code true} for security groups, {@code false} otherwise (default: {@code false})</td><td>no</td></tr> |
| 60 | + * <tr><td>{@code securityGroupTypes}</td><td>Comma-separated list of AD {@code groupType} values to treat as security groups</td><td>no</td></tr> |
| 61 | + * </table> |
| 62 | + */ |
| 63 | +@Disabled("Requires a real Active Directory instance — run manually with the system properties documented in the class Javadoc") |
| 64 | +class LdapActiveDirectoryIT { |
| 65 | + |
| 66 | + private static final Logger log = LoggerFactory.getLogger(LdapActiveDirectoryIT.class); |
| 67 | + |
| 68 | + @Test |
| 69 | + void testGetGroup() { |
| 70 | + String adServer = requireProp("adServer"); |
| 71 | + String bindUserDn = requireProp("bindUserDn"); |
| 72 | + String bindPassword = requireProp("bindPassword"); |
| 73 | + String searchBase = requireProp("searchBase"); |
| 74 | + String groupName = requireProp("groupName"); |
| 75 | + boolean securityEnabled = Boolean.parseBoolean(System.getProperty("securityEnabled", "false")); |
| 76 | + List<String> securityGroupTypes = parseList(System.getProperty("securityGroupTypes", "")); |
| 77 | + |
| 78 | + var vars = new MapBackedVariables(Map.of( |
| 79 | + "action", "getGroup", |
| 80 | + "ldapAdServer", adServer, |
| 81 | + "bindUserDn", bindUserDn, |
| 82 | + "bindPassword", bindPassword, |
| 83 | + "searchBase", searchBase, |
| 84 | + "group", groupName, |
| 85 | + "securityEnabled", securityEnabled, |
| 86 | + "securityGroupTypes", securityGroupTypes |
| 87 | + )); |
| 88 | + |
| 89 | + var result = new LdapTaskCommon().execute(TaskParams.of(vars, Map.of(), Map.of())); |
| 90 | + |
| 91 | + assertTrue((Boolean) result.get("success"), |
| 92 | + "Expected group '" + groupName + "' to be found in AD"); |
| 93 | + assertNotNull(result.get("result"), "Expected a non-null group result"); |
| 94 | + |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + var attrs = (Map<String, Object>) ((Map<String, Object>) result.get("result")).get("attributes"); |
| 97 | + assertNotNull(attrs, "Expected attributes to be present in the result"); |
| 98 | + for (Map.Entry<String, Object> e : attrs.entrySet()) { |
| 99 | + log.info("{} = {}", e.getKey(), e.getValue()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static String requireProp(String name) { |
| 104 | + String value = System.getProperty(name); |
| 105 | + if (value == null || value.isBlank()) { |
| 106 | + throw new IllegalStateException( |
| 107 | + "Required system property '" + name + "' is not set. " |
| 108 | + + "See the class Javadoc for usage."); |
| 109 | + } |
| 110 | + return value; |
| 111 | + } |
| 112 | + |
| 113 | + private static List<String> parseList(String csv) { |
| 114 | + if (csv == null || csv.isBlank()) { |
| 115 | + return List.of(); |
| 116 | + } |
| 117 | + return Arrays.stream(csv.split(",")) |
| 118 | + .map(String::trim) |
| 119 | + .filter(s -> !s.isEmpty()) |
| 120 | + .toList(); |
| 121 | + } |
| 122 | +} |
0 commit comments