Skip to content

Commit

Permalink
Fix for #1674 and update for Container Tool other than 'docker'
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Kodden committed Jan 9, 2025
1 parent 6e5fcf6 commit efcafee
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .test.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ LDAP_BIND_DN=cn=admin,dc=sram,dc=tld
LDAP_SIZELIMIT=5

SBS_API_RECORDING=Yes
SBS_URL=http://localhost:3000
SBS_USER=sysread
SBS_PASS=secret

CONTAINER_TOOL=podman
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Makefile

include .test.env
export $(shell sed 's/=.*//' .test.env)

# Set CONTAINER_TOOL to 'docker' if not defined in .test.env
CONTAINER_TOOL ?= dockers

all: pytest

image:
docker build -t plsc .
$(CONTAINER_TOOL) build -t plsc .

ldap_start:
etc/ldap_start.sh
Expand All @@ -15,6 +21,6 @@ ldap_stop:
etc/ldap_stop.sh

pytest: image ldap_start
docker run --rm -ti --network host --volume ${PWD}/api:/opt/plsc/api plsc pytest
$(CONTAINER_TOOL) run --rm -ti --network host --volume ${PWD}/api:/opt/plsc/api plsc pytest

clean: ldap_stop
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ SBS_USER=sysread
SBS_PASS=secret
SBS_API_RECORDING=Yes
SBS_VERIFY_SSL=Yes

CONTAINER_TOOL=docker
```
You have the option to run against an operational instance of SBS by specifing the **SBS_URL** and **SBS_USER** /**SBS_PASS** constants as shown above. If you do not want to access an SBS instance, just leave these constant out.
Expand All @@ -108,6 +110,14 @@ In case you are testing against an operational SBS instance, you have the option
When you omit the **SBS_URL** variable, the tests will run API requests agains the contents off this local **./api/...** directory
You may choose to use a different tool than **docker** to run build/run the containers, for example if you want to use **podman** then simply set:
```
...
CONTAINER_TOOL=podman
...
```
When all these preperations are completed, you can now run the tests:
```bash
Expand Down
3 changes: 2 additions & 1 deletion etc/ldap_show.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ else
source .test.env
fi

CONTAINER_TOOL=${CONTAINER_TOOL:-docker}

# Display result...
docker exec my-ldap ldapsearch -x -H ldap://localhost -b "${LDAP_BASE_DN:-dc=example,dc=org}"
$CONTAINER_TOOL exec my-ldap ldapsearch -x -H ldap://localhost -b "${LDAP_BASE_DN:-dc=example,dc=org}"
18 changes: 10 additions & 8 deletions etc/ldap_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ else
source .test.env
fi

CONTAINER_TOOL=${CONTAINER_TOOL:-docker}

etc/ldap_stop.sh 2>&1 >/dev/null

# Start LDAP server and record pid
docker run \
$CONTAINER_TOOL run \
--name my-ldap \
--env LDAP_DOMAIN="${LDAP_DOMAIN:-example.org}" \
--env LDAP_BASE_DN="${LDAP_BASE_DN:-dc=example,dc=org}" \
Expand All @@ -22,14 +24,14 @@ docker run \
osixia/openldap:latest --loglevel debug --copy-service

# copy LDIF files into running container...
docker cp etc/ldif my-ldap:/tmp
$CONTAINER_TOOL cp etc/ldif my-ldap:/tmp

sleep 5

# Add schemas
docker exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/access.ldif
docker exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/config.ldif
docker exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/eduPerson.ldif
docker exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/voPerson.ldif
docker exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/groupOfMembers.ldif
docker exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/sramPerson.ldif
$CONTAINER_TOOL exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/access.ldif
$CONTAINER_TOOL exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/config.ldif
$CONTAINER_TOOL exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/eduPerson.ldif
$CONTAINER_TOOL exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/voPerson.ldif
$CONTAINER_TOOL exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/groupOfMembers.ldif
$CONTAINER_TOOL exec my-ldap ldapadd -H ldap://localhost -D cn=admin,cn=config -w "${LDAP_CONFIG_PASSWORD:-changeme}" -f /tmp/ldif/sramPerson.ldif
5 changes: 4 additions & 1 deletion etc/ldap_stop.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash


CONTAINER_TOOL=${CONTAINER_TOOL:-docker}

# Kill previous server
docker stop my-ldap
$CONTAINER_TOOL stop my-ldap
23 changes: 1 addition & 22 deletions plsc_ordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,7 @@ def sbs2ldap_record(sbs_uid: str, sbs_user: SBSPerson) -> Tuple[str, LDAPEntry]:
record['objectClass'].append('ldapPublicKey')

record['voPersonStatus'] = [sbs_user.get('status', 'undefined')]

# sramPerson attributes
lld = sbs_user.get('last_login_date')
if not lld or lld == "None":
lld = "1970-01-01 00:00:00"

last_login_date = datetime.datetime.strptime(lld + "+0000", '%Y-%m-%d %H:%M:%S%z')
now = datetime.datetime.now().astimezone()
inactive_days = (now - last_login_date).days

def res(days, interval):
(div, mod) = divmod(days, interval)
return div * interval

if inactive_days >= YEAR:
inactive_days = res(inactive_days, YEAR)
elif inactive_days >= MONTH:
inactive_days = res(inactive_days, MONTH)
elif inactive_days >= WEEK:
inactive_days = res(inactive_days, WEEK)

record['sramInactiveDays'] = [inactive_days]
record['sramInactiveDays'] = [sbs_user.get('sram_inactive_days', 'undefined')]

# clean up the lists, such that we return empty lists if no attribute is present, rather than [None]
for key, val in record.items():
Expand Down
8 changes: 8 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def check_people(rdn, people, context_checks):
self.assertTrue('ldapPublicKey' in user_object[list(user_object)[0]]['objectClass'])
self.assertTrue('sshPublicKey' in user_object[list(user_object)[0]].keys())

# Check sram inactive days
if u['user'].get('sram_inactive_days', None):
self.assertTrue('sramInactiveDays' in user_object[list(user_object)[0]].keys())
self.assertEqual(
user_object[list(user_object)[0]]['sramInactiveDays'],
[str(u['user']['sram_inactive_days'])]
)

# Here a sequence of function can be initiated to verify this person in a particular context
for f in context_checks:
f(u, user_object)
Expand Down

0 comments on commit efcafee

Please sign in to comment.