Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Add an admin API endpoint to support per-user feature flags #15344

Merged
merged 29 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
5a1f946
add an experimental features table and a class to access it
H-Shay Mar 28, 2023
ec04542
add an admin endpoint and handler class to access table
H-Shay Mar 28, 2023
0841f79
add some tests of basic functionality
H-Shay Mar 28, 2023
28f835a
newsframent
H-Shay Mar 28, 2023
1b68698
Merge branch 'develop' into shay/experimental_flags
H-Shay Mar 28, 2023
daf8a9f
fix schema delta
H-Shay Mar 28, 2023
dc6207a
fix bad import (thanks pycharm)
H-Shay Mar 28, 2023
845c07e
drop unnecessary handler class and user enum for verification
H-Shay Mar 28, 2023
c999fea
rewrite table
H-Shay Apr 4, 2023
5c9c4b7
allow for bulk setting/getting of features, cache result of getting
H-Shay Apr 4, 2023
6280124
update tests
H-Shay Apr 4, 2023
d3f7032
Merge branch 'develop' into shay/experimental_flags
H-Shay Apr 4, 2023
aa74c4a
update schema
H-Shay Apr 4, 2023
985eba9
Merge branch 'develop' into shay/experimental_flags
H-Shay Apr 17, 2023
2c1191d
update comments and schema number
H-Shay Apr 17, 2023
4847df7
add new boolean column to `port_db` script and capitalize FALSE
H-Shay Apr 20, 2023
1ca2989
properly handle boolean column in sqlite
H-Shay Apr 20, 2023
e86e39c
it's postgres, not postgre
H-Shay Apr 21, 2023
fadb5e6
make PUT and GET more symmetrical for ease of use
H-Shay Apr 21, 2023
0496d1d
Update docs/admin_api/experimental_features.md
H-Shay Apr 24, 2023
2044c14
update docs
H-Shay Apr 24, 2023
dcc9c41
fix the urls
H-Shay Apr 26, 2023
37e7f30
get rid of sqlite-specific table
H-Shay Apr 26, 2023
401cb10
schema version is now 75 so move file
H-Shay Apr 26, 2023
8927eae
Apply suggestions from code review
H-Shay Apr 28, 2023
17e48be
Merge branch 'develop' into shay/experimental_flags
H-Shay Apr 28, 2023
427d65f
fix schema drift
H-Shay Apr 28, 2023
80d3f69
Merge branch 'shay/experimental_flags' of https://github.com/matrix-o…
H-Shay Apr 28, 2023
6a994dd
update test to accompdate new error message
H-Shay Apr 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions docs/admin_api/experimental_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,42 @@ for a server admin: see [Admin API](../usage/administration/admin_api/).

## Enabling/Disabling Features

This API allows a server administrator to enable experimental features for a given user, where the
`user_id` is the user id of the user for whom to enable or disable the features. The request must
provide a body listing the features to enable/disable in the following format:
This API allows a server administrator to enable experimental features for a given user. The request must
provide a body containing the user id and listing the features to enable/disable in the following format:
```
{
"features": {"msc3026": True, "msc2654": True}
"user_id": user_id,
"features": {"msc3026": true, "msc2654": true}
}

```
where True is used to enable the feature, and False is used to disable the feature.
where true is used to enable the feature, and false is used to disable the feature.


The API is:

```
PUT /_synapse/admin/v1/experimental_features/<user_id>
PUT /_synapse/admin/v1/experimental_features
```

## Listing Enabled Features

To list the enabled features for a given user, use the following API:
To list which features are enabled/disabled for a given user, send a request with a body in the following format:

```
{
"user_id": user_id
}
```

to the API

```
GET /_synapse/admin/v1/experimental_features
```

It will return a list of enabled features in the following format:
It will return a list of possible features and indicate whether they are enabled or disabled for the
user like so:
```
{"user_id": user_id, "features": ["msc3026"]}
{"user_id": user_id, "features": {"msc3026": true, "msc2654": true, "msc3881": false, "msc3967": false}}
```
38 changes: 30 additions & 8 deletions synapse/rest/admin/experimental_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ExperimentalFeaturesRestServlet(RestServlet):
for a given user
"""

PATTERNS = admin_patterns("/experimental_features/(?P<user_id>[^/]*)")
PATTERNS = admin_patterns("/experimental_features")

def __init__(self, hs: "HomeServer"):
super().__init__()
Expand All @@ -55,40 +55,62 @@ def __init__(self, hs: "HomeServer"):
async def on_GET(
self,
request: SynapseRequest,
user_id: str,
) -> Tuple[int, JsonDict]:
"""
List which features are enabled for a given user
"""
await assert_requester_is_admin(self.auth, request)

body = parse_json_object_from_request(request)

if "user_id" not in body:
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"Missing property 'user_id' in the request body",
)

user_id = body["user_id"]

target_user = UserID.from_string(user_id)
if not self.is_mine(target_user):
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"User must be local to check what experimental features are enabled.",
)

enabled_list = await self.store.list_enabled_features(user_id)
enabled_features = await self.store.list_enabled_features(user_id)

return HTTPStatus.OK, {"user_id": user_id, "features": enabled_list}
user_features = {}
for feature in ExperimentalFeature:
if feature in enabled_features:
user_features[feature] = True
else:
user_features[feature] = False
return HTTPStatus.OK, {"user_id": user_id, "features": user_features}

async def on_PUT(
self, request: SynapseRequest, user_id: str
) -> Tuple[HTTPStatus, Dict]:
async def on_PUT(self, request: SynapseRequest) -> Tuple[HTTPStatus, Dict]:
"""
Enable or disable the provided features for the requester
"""
await assert_requester_is_admin(self.auth, request)

body = parse_json_object_from_request(request)

if "user_id" not in body:
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"Missing property 'user_id' in the request body",
)

user_id = body["user_id"]

target_user = UserID.from_string(user_id)
if not self.is_mine(target_user):
raise SynapseError(
HTTPStatus.BAD_REQUEST,
"User must be local to enable experimental features.",
)

body = parse_json_object_from_request(request)
features = body.get("features")
if not features:
raise SynapseError(
Expand Down
61 changes: 37 additions & 24 deletions tests/rest/admin/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,14 @@ def test_enable_and_disable(self) -> None:
Test basic functionality of ExperimentalFeatures endpoint
"""
# test enabling features works
url = f"{self.url}/{self.other_user}"
url = f"{self.url}"
channel = self.make_request(
"PUT",
url,
content={"features": {"msc3026": True, "msc2654": True}},
content={
"user_id": self.other_user,
"features": {"msc3026": True, "msc2654": True},
},
access_token=self.admin_user_tok,
)
self.assertEqual(channel.code, 200)
Expand All @@ -409,70 +412,80 @@ def test_enable_and_disable(self) -> None:
channel = self.make_request(
"GET",
url,
content={"user_id": self.other_user},
access_token=self.admin_user_tok,
)
self.assertEqual(channel.code, 200)
self.assertIn(
"msc3026",
channel.json_body["features"],
self.assertEqual(
True,
channel.json_body["features"]["msc3026"],
)
self.assertIn(
"msc2654",
channel.json_body["features"],
self.assertEqual(
True,
channel.json_body["features"]["msc2654"],
)
self.assertIn(
self.other_user,
channel.json_body["user_id"],
)

# test disabling a feature works
url = f"{self.url}/{self.other_user}"
url = f"{self.url}"
channel = self.make_request(
"PUT",
url,
content={"features": {"msc3026": False}},
content={"user_id": self.other_user, "features": {"msc3026": False}},
access_token=self.admin_user_tok,
)
self.assertEqual(channel.code, 200)

# list which features are enabled and ensure the disabled one is not listed
# list the features enabled/disabled and ensure they are still are correct
self.assertEqual(channel.code, 200)
url = f"{self.url}/{self.other_user}"
url = f"{self.url}"
channel = self.make_request(
"GET",
url,
content={"user_id": self.other_user},
access_token=self.admin_user_tok,
)
self.assertEqual(channel.code, 200)
self.assertNotIn(
"msc3026",
channel.json_body["features"],
self.assertEqual(
False,
channel.json_body["features"]["msc3026"],
)
self.assertIn(
"msc2654",
channel.json_body["features"],
self.assertEqual(
True,
channel.json_body["features"]["msc2654"],
)
self.assertEqual(
False,
channel.json_body["features"]["msc3881"],
)
self.assertEqual(
False,
channel.json_body["features"]["msc3967"],
)
self.assertIn(
self.other_user,
channel.json_body["user_id"],
)

# test nothing blows up if you try to disable a feature that isn't already enabled
url = f"{self.url}/{self.other_user}"
url = f"{self.url}"
channel = self.make_request(
"PUT",
url,
content={"features": {"msc3026": False}},
content={"user_id": self.other_user, "features": {"msc3026": False}},
access_token=self.admin_user_tok,
)
self.assertEqual(channel.code, 200)

# test trying to enable a feature without an admin access token is denied
url = f"{self.url}/{self.other_user}"
url = f"{self.url}"
channel = self.make_request(
"PUT",
url,
content={"features": {"msc3881": True}},
content={"user_id": self.other_user, "features": {"msc3881": True}},
access_token=self.other_user_tok,
)
self.assertEqual(channel.code, 403)
Expand All @@ -482,11 +495,11 @@ def test_enable_and_disable(self) -> None:
)

# test trying to enable a bogus msc is denied
url = f"{self.url}/{self.other_user}"
url = f"{self.url}"
channel = self.make_request(
"PUT",
url,
content={"features": {"msc6666": True}},
content={"user_id": self.other_user, "features": {"msc6666": True}},
access_token=self.admin_user_tok,
)
self.assertEqual(channel.code, 400)
Expand Down