From ba5bdfc541afd5e7b1adfc9046c519bc85aac925 Mon Sep 17 00:00:00 2001 From: vinsymoyya <186405436+vinsymoyya@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:00:50 +0530 Subject: [PATCH] Advance incremental LDAP timestamps before calendar formatting --- nss_cache/sources/ldapsource.py | 8 +-- .../ldapsource_incremental_time_test.py | 64 +++++++++++++++++++ 2 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 nss_cache/sources/ldapsource_incremental_time_test.py diff --git a/nss_cache/sources/ldapsource.py b/nss_cache/sources/ldapsource.py index 9f03226..6e41c12 100644 --- a/nss_cache/sources/ldapsource.py +++ b/nss_cache/sources/ldapsource.py @@ -718,16 +718,12 @@ def GetUpdates(self, source, search_base, search_filter, search_scope, since): self.attrs.append("modifyTimestamp") if since is not None: - ts = self.FromTimestampToLdap(since) # since openldap disallows modifyTimestamp "greater than" we have to - # increment by one second. + # increment by one second before formatting the calendar timestamp. + ts = self.FromTimestampToLdap(since + 1) if self.conf.get("ad"): - ts = int(ts.rstrip(".0Z")) + 1 - ts = "%s.0Z" % ts search_filter = "(&%s(whenChanged>=%s))" % (search_filter, ts) else: - ts = int(ts.rstrip("Z")) + 1 - ts = "%sZ" % ts search_filter = "(&%s(modifyTimestamp>=%s))" % (search_filter, ts) if search_scope == "base": diff --git a/nss_cache/sources/ldapsource_incremental_time_test.py b/nss_cache/sources/ldapsource_incremental_time_test.py new file mode 100644 index 0000000..6d3728f --- /dev/null +++ b/nss_cache/sources/ldapsource_incremental_time_test.py @@ -0,0 +1,64 @@ +# Copyright 2026 Google Inc. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. +"""Calendar-aware LDAP incremental search-filter regressions.""" + +import calendar +import unittest + +from nss_cache.sources import ldapsource + + +class SearchSource(list): + def Search(self, **kwargs): + self.search_filter = kwargs["search_filter"] + + +class IncrementalTimeTest(unittest.TestCase): + def assertSinceFilter(self, before, after): + since = calendar.timegm(before + (0, 0, 0)) + for ad in (False, True): + with self.subTest(ad=ad): + source = SearchSource() + getter = ldapsource.ShadowUpdateGetter({"ad": ad}) + getter.GetUpdates(source, "dc=example", "(objectClass=*)", "sub", since) + attribute = "whenChanged" if ad else "modifyTimestamp" + suffix = ".0Z" if ad else "Z" + self.assertEqual( + "(&(objectClass=*)(%s>=%s%s))" % (attribute, after, suffix), + source.search_filter, + ) + + def testMinuteRollover(self): + self.assertSinceFilter((2024, 1, 1, 12, 34, 59), "20240101123500") + + def testYearRollover(self): + self.assertSinceFilter((2023, 12, 31, 23, 59, 59), "20240101000000") + + def testLeapDayRollover(self): + self.assertSinceFilter((2024, 2, 28, 23, 59, 59), "20240229000000") + + def testZeroSeconds(self): + self.assertSinceFilter((2024, 1, 1, 12, 34, 0), "20240101123401") + + def testFullUpdateFilterUnchanged(self): + source = SearchSource() + getter = ldapsource.ShadowUpdateGetter({}) + getter.GetUpdates(source, "dc=example", "(objectClass=*)", "sub", None) + self.assertEqual("(objectClass=*)", source.search_filter) + + +if __name__ == "__main__": + unittest.main()