Skip to content

Commit 4fb9f09

Browse files
committed
chore: Add logging for login for SSDLC
1 parent 0d7fa61 commit 4fb9f09

5 files changed

Lines changed: 83 additions & 5 deletions

File tree

tests/publisher/snaps/tests_get_metrics.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ def test_get_active_devices_weekly_installed_by_version(
6060
):
6161
mock_is_authenticated.return_value = True
6262

63+
with self.client.session_transaction() as sess:
64+
sess["publisher"] = {
65+
"nickname": "test_username",
66+
"fullname": "Test User",
67+
"email": "test@example.com",
68+
}
69+
6370
mock_get_item_details.return_value = {"snap_id": "id"}
6471
random_values = random.sample(range(1, 30), 29)
6572
dates = [
@@ -125,6 +132,13 @@ def test_get_active_devices_weekly_installed_by_channel(
125132
]
126133
}
127134

135+
with self.client.session_transaction() as sess:
136+
sess["publisher"] = {
137+
"nickname": "test_username",
138+
"fullname": "Test User",
139+
"email": "test@example.com",
140+
}
141+
128142
response = self.client.get(
129143
self.endpoint_url + "?active-devices=channel"
130144
)
@@ -175,6 +189,13 @@ def test_get_active_devices_weekly_installed_by_os(
175189
]
176190
}
177191

192+
with self.client.session_transaction() as sess:
193+
sess["publisher"] = {
194+
"nickname": "test_username",
195+
"fullname": "Test User",
196+
"email": "test@example.com",
197+
}
198+
178199
response = self.client.get(self.endpoint_url + "?active-devices=os")
179200
self.assertEqual(response.status_code, 200)
180201
response_json = response.json
@@ -218,6 +239,13 @@ def test_get_active_devices_in_3_months_period(
218239
]
219240
}
220241

242+
with self.client.session_transaction() as sess:
243+
sess["publisher"] = {
244+
"nickname": "test_username",
245+
"fullname": "Test User",
246+
"email": "test@example.com",
247+
}
248+
221249
response = self.client.get(
222250
self.endpoint_url + "?active-devices=architecture&period=3m"
223251
)
@@ -281,6 +309,13 @@ def test_get_active_devices_weekly_installed_by_version(
281309

282310
mock_get_snap_info.return_value = self.snap_payload
283311

312+
with self.client.session_transaction() as sess:
313+
sess["publisher"] = {
314+
"nickname": "test_username",
315+
"fullname": "Test User",
316+
"email": "test@example.com",
317+
}
318+
284319
response = self.client.get(self.endpoint_url)
285320
self.assertEqual(response.status_code, 200)
286321
response_json = response.json
@@ -361,6 +396,13 @@ def test_get_active_devices_weekly_installed_by_version(
361396
]
362397
}
363398

399+
with self.client.session_transaction() as sess:
400+
sess["publisher"] = {
401+
"nickname": "test_username",
402+
"fullname": "Test User",
403+
"email": "test@example.com",
404+
}
405+
364406
response = self.client.get(self.endpoint_url)
365407
self.assertEqual(response.status_code, 200)
366408
response_json = response.json

tests/publisher/tests_account_snaps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def test_get_is_user_snap(self, mock_get_snap_info, mock_is_authenticated):
352352
sess["publisher"] = {
353353
"nickname": "test_username",
354354
"fullname": "Test User",
355+
"email": "test@example.com",
355356
}
356357

357358
response = self.client.get("/snap_info/user_snap/test_snap")

tests/publisher/tests_publisher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def _log_in(self, client):
7777
"image": None,
7878
"nickname": "Toto",
7979
"fullname": "El Toto",
80+
"email": "test@example.com",
8081
}
8182
s["macaroon_root"] = root.serialize()
8283
s["macaroon_discharge"] = discharge.serialize()

webapp/decorators.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# Core packages
22
import functools
3+
from datetime import datetime, timezone
34

45
# Third party packages
56
import flask
7+
import logging
8+
69

710
from canonicalwebteam.store_api.publishergw import PublisherGW
811

912
from webapp import authentication
1013
from webapp.helpers import api_publisher_session
1114

1215
publisher_gateway = PublisherGW(api_publisher_session)
16+
logger = logging.getLogger(__name__)
1317

1418

1519
def login_required(func):
@@ -20,10 +24,35 @@ def login_required(func):
2024

2125
@functools.wraps(func)
2226
def is_user_logged_in(*args, **kwargs):
27+
date = datetime.now(timezone.utc)
28+
date_str = date.strftime("%Y-%m-%dT%H:%M:%S")
29+
2330
if not authentication.is_authenticated(flask.session):
2431
authentication.empty_session(flask.session)
32+
33+
logger.warning(
34+
"User login failed",
35+
extra={
36+
"datetime": date_str,
37+
"appid": "snapcraft-io",
38+
"event": "authn_login_fail",
39+
},
40+
)
41+
2542
return flask.redirect(f"/login?next={flask.request.path}")
2643

44+
publisher = flask.session.get("publisher")
45+
user = publisher["email"]
46+
47+
logger.info(
48+
f"User {user} login successfully",
49+
extra={
50+
"datetime": date_str,
51+
"appid": "snapcraft-io",
52+
"event": f"authn_successafterfail:{user}",
53+
},
54+
)
55+
2756
return func(*args, **kwargs)
2857

2958
return is_user_logged_in

yarn.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,12 @@
947947
resolved "https://registry.yarnpkg.com/@canonical/cookie-policy/-/cookie-policy-3.6.5.tgz#50d82df6da2970b6b5d78d066dd94fcc1317e591"
948948
integrity sha512-CQJKSkuLXhSumkIIw4YPSjR7k9561hZ3EEdMPQTkAVTxXURhm3WzJm/uyrvcAuU/a//jzWa8ScltCaHFBdACew==
949949

950-
"@canonical/global-nav@3.8.0":
951-
version "3.8.0"
952-
resolved "https://registry.yarnpkg.com/@canonical/global-nav/-/global-nav-3.8.0.tgz#3f016b337f93a2b50fccb0cbf8f13c9a10dcee5a"
953-
integrity sha512-ioJE4tUENS2L8ubD/lUAA+CvIrtqXB1RTv89vyAW/6eDJWRW8U9Fks+SdKoeoa/IwOtzSC02AOWTPo7rxgj+Mw==
950+
"@canonical/global-nav@3.7.3":
951+
version "3.7.3"
952+
resolved "https://registry.yarnpkg.com/@canonical/global-nav/-/global-nav-3.7.3.tgz#cac51536f54c88298571301e12504daa3408c1d3"
953+
integrity sha512-1G4A2dgIHcqOCWfNJ5F9MWmXNSVGF39Nx53bIk2imiGXGALhDCOGFcnELx0q5uKP2aIy1pMd3D58qX93yX8tzg==
954954
dependencies:
955-
vanilla-framework "4.35.0"
955+
vanilla-framework "4.26.1"
956956

957957
"@canonical/react-components@1.9.0":
958958
version "1.9.0"
@@ -8314,6 +8314,11 @@ uuid@^8.3.2:
83148314
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
83158315
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
83168316

8317+
vanilla-framework@4.26.1:
8318+
version "4.26.1"
8319+
resolved "https://registry.yarnpkg.com/vanilla-framework/-/vanilla-framework-4.26.1.tgz#d076f26a7215cf8b0babae81753ef535e6486257"
8320+
integrity sha512-7m3qX4rm+I+go5mNjzO8gkp2qxr5m4n1jAxTh+bJ8hx/Wrsx3hiWDmJHylZ2GqjeqdtOiidvWgU2ToZJK6ELUg==
8321+
83178322
vanilla-framework@4.35.0:
83188323
version "4.35.0"
83198324
resolved "https://registry.yarnpkg.com/vanilla-framework/-/vanilla-framework-4.35.0.tgz#b01bbb525ad3211690c9f10580ca2e00a37a8ab1"

0 commit comments

Comments
 (0)