Skip to content

Commit 87b30d8

Browse files
Jon Beilkephilpep
authored andcommitted
add feature for checking user password min/max days
1 parent b4d2269 commit 87b30d8

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

images/debian_bullseye/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ RUN echo "user:foo" | chpasswd
6464
RUN echo "*nat\n:PREROUTING ACCEPT [0:0]\n:INPUT ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n:POSTROUTING ACCEPT [0:0]\n-A PREROUTING -d 192.168.0.1/32 -j REDIRECT\nCOMMIT\n*filter\n:INPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT\nCOMMIT" > /etc/iptables/rules.v4
6565

6666
# Expiration date for user "user"
67-
RUN chage -E 20000 user
67+
RUN chage -E 20000 -m 7 -M 90 user
6868

6969
# Some python3 virtualenv
7070
RUN virtualenv /v

test/test_modules.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ def test_user(host):
271271
assert user.password == "*"
272272

273273

274+
def test_user_password_days(host):
275+
assert host.user("root").password_max_days is 99999
276+
assert host.user("root").password_min_days is 0
277+
assert host.user("user").password_max_days == 90
278+
assert host.user("user").password_min_days == 7
279+
280+
274281
def test_user_user(host):
275282
user = host.user("user")
276283
assert user.exists

testinfra/modules/user.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ def password(self):
9191
"""Return the crypted user password"""
9292
return self.check_output("getent shadow %s", self.name).split(":")[1]
9393

94+
@property
95+
def password_max_days(self):
96+
"""Return the maximum number of days between password changes"""
97+
days = self.check_output("getent shadow %s", self.name).split(":")[4]
98+
try:
99+
return int(days)
100+
except ValueError:
101+
return None
102+
103+
@property
104+
def password_min_days(self):
105+
"""Return the minimum number of days between password changes"""
106+
days = self.check_output("getent shadow %s", self.name).split(":")[3]
107+
try:
108+
return int(days)
109+
except ValueError:
110+
return None
111+
94112
@property
95113
def gecos(self):
96114
"""Return the user comment/gecos field"""

0 commit comments

Comments
 (0)