Skip to content

Commit 8513a49

Browse files
committed
associate users with stewarded monitors; encode monitors in jwt token
1 parent 3412eea commit 8513a49

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/fides/api/cryptography/schemas/jwt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
JWE_PAYLOAD_ROLES = "roles"
55
JWE_PAYLOAD_SYSTEMS = "systems"
66
JWE_PAYLOAD_CONNECTIONS = "connections"
7+
JWE_PAYLOAD_MONITORS = "monitors"

src/fides/api/models/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
JWE_ISSUED_AT,
1818
JWE_PAYLOAD_CLIENT_ID,
1919
JWE_PAYLOAD_CONNECTIONS,
20+
JWE_PAYLOAD_MONITORS,
2021
JWE_PAYLOAD_ROLES,
2122
JWE_PAYLOAD_SCOPES,
2223
JWE_PAYLOAD_SYSTEMS,
@@ -30,6 +31,7 @@
3031
DEFAULT_ROLES: list[str] = []
3132
DEFAULT_SYSTEMS: list[str] = []
3233
DEFAULT_CONNECTIONS: list[str] = []
34+
DEFAULT_MONITORS: list[str] = []
3335

3436

3537
class ClientDetail(Base):
@@ -47,6 +49,7 @@ def __tablename__(self) -> str:
4749
connections = Column(
4850
ARRAY(String), nullable=False, server_default="{}", default=dict
4951
)
52+
monitors = Column(ARRAY(String), nullable=False, server_default="{}", default=dict)
5053
fides_key = Column(String, index=True, unique=True, nullable=True)
5154
user_id = Column(
5255
String, ForeignKey(FidesUser.id_field_path), nullable=True, unique=True
@@ -67,6 +70,7 @@ def create_client_and_secret(
6770
roles: list[str] | None = None,
6871
systems: list[str] | None = None,
6972
connections: list[str] | None = None,
73+
monitors: list[str] | None = None,
7074
in_memory: bool | None = False,
7175
) -> tuple["ClientDetail", str]:
7276
"""Creates a ClientDetail and returns that along with the unhashed secret
@@ -88,6 +92,9 @@ def create_client_and_secret(
8892
if not connections:
8993
connections = DEFAULT_CONNECTIONS
9094

95+
if not monitors:
96+
monitors = DEFAULT_MONITORS
97+
9198
salt = generate_salt()
9299
hashed_secret = hash_credential_with_salt(
93100
secret.encode(encoding),
@@ -104,6 +111,7 @@ def create_client_and_secret(
104111
"roles": roles,
105112
"systems": systems,
106113
"connections": connections,
114+
"monitors": monitors,
107115
}
108116

109117
if in_memory:
@@ -142,6 +150,7 @@ def create_access_code_jwe(self, encryption_key: str) -> str:
142150
JWE_PAYLOAD_ROLES: self.roles,
143151
JWE_PAYLOAD_SYSTEMS: self.systems,
144152
JWE_PAYLOAD_CONNECTIONS: self.connections,
153+
JWE_PAYLOAD_MONITORS: self.monitors,
145154
}
146155
return generate_jwe(json.dumps(payload), encryption_key)
147156

@@ -176,6 +185,7 @@ def _get_root_client_detail(
176185
roles=roles,
177186
systems=[],
178187
connections=[],
188+
monitors=[],
179189
)
180190

181191
return ClientDetail(
@@ -186,4 +196,5 @@ def _get_root_client_detail(
186196
roles=DEFAULT_ROLES,
187197
systems=DEFAULT_SYSTEMS,
188198
connections=DEFAULT_CONNECTIONS,
199+
monitors=DEFAULT_MONITORS,
189200
)

src/fides/api/models/detection_discovery/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class MonitorConfig(Base):
213213
stewards = relationship(
214214
FidesUser,
215215
secondary="monitorsteward",
216+
back_populates="stewarded_monitors",
216217
lazy="selectin",
217218
)
218219

src/fides/api/models/fides_user.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from fides.config import CONFIG
2727

2828
if TYPE_CHECKING:
29+
from fides.api.models.detection_discovery import MonitorConfig
2930
from fides.api.models.fides_user_permissions import FidesUserPermissions
3031
from fides.api.models.fides_user_respondent_email_verification import (
3132
FidesUserRespondentEmailVerification,
@@ -77,6 +78,15 @@ class FidesUser(Base):
7778
systems = relationship(
7879
"System", secondary="systemmanager", back_populates="data_stewards"
7980
) # type: ignore
81+
82+
# Monitors for which this user is a steward
83+
stewarded_monitors = relationship(
84+
"MonitorConfig",
85+
secondary="monitorsteward",
86+
back_populates="stewards",
87+
lazy="selectin",
88+
) # type: ignore
89+
8090
# permissions relationship is defined via backref in FidesUserPermissions
8191
email_verifications = relationship(
8292
"FidesUserRespondentEmailVerification",
@@ -104,6 +114,11 @@ class FidesUser(Base):
104114
def system_ids(self) -> List[str]:
105115
return [system.id for system in self.systems]
106116

117+
@property
118+
def stewarded_monitor_ids(self) -> List[str]:
119+
"""Returns list of monitor IDs for which this user is a steward."""
120+
return [monitor.id for monitor in self.stewarded_monitors]
121+
107122
@classmethod
108123
def hash_password(cls, password: str, encoding: str = "UTF-8") -> tuple[str, str]:
109124
"""Utility function to hash a user's password with a generated salt"""

0 commit comments

Comments
 (0)