Skip to content

Commit ae9ec5d

Browse files
psi19ceee5Philip Ruehlschorschii
authored
correct import of timezone (#56)
* Correct import of timezone * Made datetime objects timezone-aware --------- Co-authored-by: Philip Ruehl <philip.ruehl@gonicus.de> Co-authored-by: Georg Sieber <schorschii@users.noreply.github.com>
1 parent 9c20275 commit ae9ec5d

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

laps-runner/laps_runner/filetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
from datetime import datetime
4+
from datetime import datetime, timezone
55

66

77
# Microsoft Timestamp Conversion
@@ -15,4 +15,4 @@ def dt_to_filetime(dt):
1515

1616
def filetime_to_dt(ft):
1717
# ft is in UTC, fromtimestamp() converts to local time
18-
return datetime.fromtimestamp(int((ft / HUNDREDS_OF_NANOSECONDS) - EPOCH_TIMESTAMP))
18+
return datetime.fromtimestamp(int((ft / HUNDREDS_OF_NANOSECONDS) - EPOCH_TIMESTAMP), timezone.utc)

laps-runner/laps_runner/laps_runner.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pathlib import Path
88
from os import path
9-
from datetime import datetime, timedelta
9+
from datetime import datetime, timedelta, timezone
1010
from dns import resolver, rdatatype
1111
from shutil import which
1212
from pid import PidFile, PidFileAlreadyLockedError, PidFileAlreadyRunningError
@@ -161,7 +161,7 @@ def searchComputer(self):
161161
self.tmpExpiryDate = filetime_to_dt( int(str(entry[self.cfgLdapAttributePasswordExpiry])) )
162162
except Exception as e:
163163
print('Unable to parse date '+str(self.tmpExpiry)+' - assuming that no expiration date is set.')
164-
self.tmpExpiryDate = datetime.fromtimestamp(0, datetime.timezone.utc)
164+
self.tmpExpiryDate = datetime.fromtimestamp(0, timezone.utc)
165165
return True
166166

167167
# no result found
@@ -181,7 +181,7 @@ def updatePassword(self):
181181
# generate new values
182182
newPassword = self.generatePassword()
183183
newPasswordHashed = CryptContext(schemes=['sha512_crypt']).hash(newPassword)
184-
newExpirationDate = datetime.now() + timedelta(days=self.cfgDaysValid)
184+
newExpirationDate = datetime.now().astimezone(timezone.utc) + timedelta(days=self.cfgDaysValid)
185185

186186
# update password in local database
187187
self.updateLocalPassword(self.cfgUsername, newPasswordHashed)
@@ -232,7 +232,7 @@ def setPasswordAndExpiry(self, newPassword, newExpirationDate):
232232
newPassword = json.dumps({
233233
'p': newPassword,
234234
'n': self.cfgUsername,
235-
't': ('%0.2X' % dt_to_filetime(datetime.now())).lower()
235+
't': ('%0.2X' % dt_to_filetime(datetime.now().astimezone(timezone.utc))).lower()
236236
})
237237

238238
# encrypt Native LAPS content
@@ -305,7 +305,7 @@ def encryptPassword(self, content):
305305
# 8-12 - blob size, uint32
306306
# 12-16 - flags, currently always 0
307307
preMagic = (
308-
self.rotate_and_pack_msdatetime(dt_to_filetime(datetime.now()))
308+
self.rotate_and_pack_msdatetime(dt_to_filetime(datetime.now().astimezone(timezone.utc)))
309309
+ struct.pack('<i', len(encrypted))
310310
+ b'\x00\x00\x00\x00'
311311
)
@@ -416,7 +416,7 @@ def main():
416416
runner.connectToServer()
417417
runner.searchComputer()
418418

419-
if runner.tmpExpiryDate < datetime.now():
419+
if runner.tmpExpiryDate < datetime.now().astimezone(timezone.utc):
420420
print('Updating password (expired '+str(runner.tmpExpiryDate)+')')
421421
runner.updatePassword()
422422

@@ -436,7 +436,7 @@ def main():
436436
if runner.cfgPamGracePeriod:
437437
runner.logger.debug(__title__+': PAM grace period - waiting '+str(runner.cfgPamGracePeriod)+' seconds...')
438438
# set expiration in directory, e.g. to handle reboots
439-
runner.setExpiry(datetime.now() + timedelta(seconds=runner.cfgPamGracePeriod))
439+
runner.setExpiry(datetime.now().astimezone(timezone.utc) + timedelta(seconds=runner.cfgPamGracePeriod))
440440
# wait grace period
441441
time.sleep(runner.cfgPamGracePeriod)
442442
print('Updating password (forced update by PAM)...')

0 commit comments

Comments
 (0)