Skip to content

Commit

Permalink
settings endpoint + other changes
Browse files Browse the repository at this point in the history
Signed-off-by: Shaanjot Gill <[email protected]>
  • Loading branch information
shaangill025 committed Jun 27, 2023
1 parent caea160 commit 9dc9f4d
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 31 deletions.
38 changes: 19 additions & 19 deletions Multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,24 +381,24 @@ curl -X POST "${ACAPY_ADMIN_URL}/multitenancy/wallet/{wallet_id}/remove" \

To allow configurablity of ACA-Py startup parameters/environment variables at a tenant/subwallet level. [PR#2233](https://github.com/hyperledger/aries-cloudagent-python/pull/2233) will provide the ability to update the following subset of settings when creating or updating the subwallet:

| Label | Setting |
|---|---|
| ACAPY_LOG_LEVEL | log.level |
| ACAPY_INVITE_PUBLIC | debug.invite_public |
| ACAPY_PUBLIC_INVITES | public_invites |
| ACAPY_AUTO_ACCEPT_INVITES | debug.auto_accept_invites |
| ACAPY_AUTO_ACCEPT_REQUESTS | debug.auto_accept_requests |
| ACAPY_AUTO_PING_CONNECTION | auto_ping_connection |
| ACAPY_MONITOR_PING | debug.monitor_ping |
| ACAPY_AUTO_RESPOND_MESSAGES | debug.auto_respond_messages |
| ACAPY_AUTO_RESPOND_CREDENTIAL_OFFER | debug.auto_resopnd_credential_offer |
| ACAPY_AUTO_RESPOND_CREDENTIAL_REQUEST | debug.auto_respond_credential_request |
| ACAPY_AUTO_VERIFY_PRESENTATION | debug.auto_verify_presentation |
| ACAPY_NOTIFY_REVOCATION | revocation.notify |
| ACAPY_AUTO_REQUEST_ENDORSEMENT | endorser.auto_request |
| ACAPY_AUTO_WRITE_TRANSACTIONS | endorser.auto_write |
| ACAPY_CREATE_REVOCATION_TRANSACTIONS | endorser.auto_create_rev_reg |
| ACAPY_ENDORSER_ROLE | endorser.protocol_role |
| Labels | | Setting |
|---|---|---|
| ACAPY_LOG_LEVEL | log_level | log.level |
| ACAPY_INVITE_PUBLIC | invite_public | debug.invite_public |
| ACAPY_PUBLIC_INVITES | public_invites | public_invites |
| ACAPY_AUTO_ACCEPT_INVITES | auto_accept_invites | debug.auto_accept_invites |
| ACAPY_AUTO_ACCEPT_REQUESTS | auto_accept_requests | debug.auto_accept_requests |
| ACAPY_AUTO_PING_CONNECTION | auto_ping_connection | auto_ping_connection |
| ACAPY_MONITOR_PING | monitor_ping | debug.monitor_ping |
| ACAPY_AUTO_RESPOND_MESSAGES | auto_respond_messages | debug.auto_respond_messages |
| ACAPY_AUTO_RESPOND_CREDENTIAL_OFFER | auto_respond_credential_offer | debug.auto_resopnd_credential_offer |
| ACAPY_AUTO_RESPOND_CREDENTIAL_REQUEST | auto_respond_credential_request | debug.auto_respond_credential_request |
| ACAPY_AUTO_VERIFY_PRESENTATION | auto_verify_presentation | debug.auto_verify_presentation |
| ACAPY_NOTIFY_REVOCATION | notify_revocation | revocation.notify |
| ACAPY_AUTO_REQUEST_ENDORSEMENT | auto_request_endorsement | endorser.auto_request |
| ACAPY_AUTO_WRITE_TRANSACTIONS | auto_write_transactions | endorser.auto_write |
| ACAPY_CREATE_REVOCATION_TRANSACTIONS | auto_create_revocation_transactions | endorser.auto_create_rev_reg |
| ACAPY_ENDORSER_ROLE | endorser_protocol_role | endorser.protocol_role |

- `POST /multitenancy/wallet`

Expand All @@ -417,7 +417,7 @@ To allow configurablity of ACA-Py startup parameters/environment variables at a
"extra_settings": {
"ACAPY_LOG_LEVEL": "INFO",
"ACAPY_INVITE_PUBLIC": true,
"ACAPY_PUBLIC_INVITES": true
"public_invites": true
},
}
```
Expand Down
4 changes: 4 additions & 0 deletions aries_cloudagent/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ def copy(self) -> "BaseSettings":
def extend(self, other: Mapping[str, Any]) -> "BaseSettings":
"""Merge another mapping to produce a new settings instance."""

@abstractmethod
def to_dict(self) -> dict:
"""Return a dict of the settings instance."""

def __repr__(self) -> str:
"""Provide a human readable representation of this object."""
items = ("{}={}".format(k, self[k]) for k in self)
Expand Down
1 change: 1 addition & 0 deletions aries_cloudagent/config/default_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ async def load_plugins(self, context: InjectionContext):
plugin_registry.register_plugin("aries_cloudagent.messaging.jsonld")
plugin_registry.register_plugin("aries_cloudagent.revocation")
plugin_registry.register_plugin("aries_cloudagent.resolver")
plugin_registry.register_plugin("aries_cloudagent.settings")
plugin_registry.register_plugin("aries_cloudagent.wallet")

if context.settings.get("multitenant.admin_enabled"):
Expand Down
14 changes: 7 additions & 7 deletions aries_cloudagent/config/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,11 +634,11 @@ def get_logger_with_handlers(
logger.addHandler(std_out_handler)
if did_ident:
logger = logging.LoggerAdapter(logger, {"did": did_ident})
# set log level
logger_level = (
(settings.get("log.level")).upper()
if settings.get("log.level")
else logging.INFO
)
logger.setLevel(logger_level)
# set log level
logger_level = (
(settings.get("log.level")).upper()
if settings.get("log.level")
else logging.INFO
)
logger.setLevel(logger_level)
return logger
7 changes: 7 additions & 0 deletions aries_cloudagent/config/plugin_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def extend(self, other: Mapping[str, Any]) -> BaseSettings:
vals.update(other)
return PluginSettings(vals)

def to_dict(self) -> dict:
"""Return a dict of the settings instance."""
setting_dict = {}
for k in self:
setting_dict[k] = self[k]
return setting_dict

def get_value(self, *var_names: str, default: Any = None):
"""Fetch a setting.
Expand Down
7 changes: 7 additions & 0 deletions aries_cloudagent/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def extend(self, other: Mapping[str, Any]) -> BaseSettings:
vals.update(other)
return Settings(vals)

def to_dict(self) -> dict:
"""Return a dict of the settings instance."""
setting_dict = {}
for k in self:
setting_dict[k] = self[k]
return setting_dict

def update(self, other: Mapping[str, Any]):
"""Update the settings in place."""
self._values.update(other)
Expand Down
38 changes: 33 additions & 5 deletions aries_cloudagent/multitenant/admin/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"ACAPY_AUTO_PING_CONNECTION": "auto_ping_connection",
"ACAPY_MONITOR_PING": "debug.monitor_ping",
"ACAPY_AUTO_RESPOND_MESSAGES": "debug.auto_respond_messages",
"ACAPY_AUTO_RESPOND_CREDENTIAL_OFFER": "debug.auto_resopnd_credential_offer",
"ACAPY_AUTO_RESPOND_CREDENTIAL_OFFER": "debug.auto_respond_credential_offer",
"ACAPY_AUTO_RESPOND_CREDENTIAL_REQUEST": "debug.auto_respond_credential_request",
"ACAPY_AUTO_VERIFY_PRESENTATION": "debug.auto_verify_presentation",
"ACAPY_NOTIFY_REVOCATION": "revocation.notify",
Expand All @@ -42,10 +42,32 @@
"ACAPY_ENDORSER_ROLE": "endorser.protocol_role",
}

ACAPY_LIFECYCLE_CONFIG_FLAG_ARGS_MAP = {
"log_level": "log.level",
"invite_public": "debug.invite_public",
"public_invites": "public_invites",
"auto_accept_invites": "debug.auto_accept_invites",
"auto_accept_requests": "debug.auto_accept_requests",
"auto_ping_connection": "auto_ping_connection",
"monitor_ping": "debug.monitor_ping",
"auto_respond_messages": "debug.auto_respond_messages",
"auto_respond_credential_offer": "debug.auto_respond_credential_offer",
"auto_respond_credential_request": "debug.auto_respond_credential_request",
"auto_verify_presentation": "debug.auto_verify_presentation",
"notify_revocation": "revocation.notify",
"auto_request_endorsement": "endorser.auto_request",
"auto_write_transactions": "endorser.auto_write",
"auto_create_revocation_transactions": "endorser.auto_create_rev_reg",
"endorser_protocol_role": "endorser.protocol_role",
}

ACAPY_ENDORSER_FLAGS_DEPENDENT_ON_AUTHOR_ROLE = [
"ACAPY_AUTO_REQUEST_ENDORSEMENT",
"ACAPY_AUTO_WRITE_TRANSACTIONS",
"ACAPY_CREATE_REVOCATION_TRANSACTIONS",
"auto_request_endorsement",
"auto_write_transactions",
"auto_create_revocation_transactions",
]


Expand All @@ -64,11 +86,13 @@ def format_wallet_record(wallet_record: WalletRecord):
def get_extra_settings_dict_per_tenant(tenant_settings: dict) -> dict:
"""Get per tenant settings to be applied when creating wallet."""

endorser_role_flag = tenant_settings.get("ACAPY_ENDORSER_ROLE")
endorser_role_flag = tenant_settings.get(
"ACAPY_ENDORSER_ROLE"
) or tenant_settings.get("endorser_protocol_role")
extra_settings = {}
if endorser_role_flag == "author":
if endorser_role_flag and endorser_role_flag == "author":
extra_settings["endorser.author"] = True
elif endorser_role_flag == "endorser":
elif endorser_role_flag and endorser_role_flag == "endorser":
extra_settings["endorser.endorser"] = True
for flag in tenant_settings.keys():
if (
Expand All @@ -79,7 +103,11 @@ def get_extra_settings_dict_per_tenant(tenant_settings: dict) -> dict:
# this setting will be ignored.
continue
if flag != "ACAPY_ENDORSER_ROLE":
map_flag = ACAPY_LIFECYCLE_CONFIG_FLAG_MAP[flag]
map_flag = ACAPY_LIFECYCLE_CONFIG_FLAG_MAP.get(
flag
) or ACAPY_LIFECYCLE_CONFIG_FLAG_ARGS_MAP.get(flag)
if not map_flag:
continue
extra_settings[map_flag] = tenant_settings[flag]
return extra_settings

Expand Down
Empty file.
113 changes: 113 additions & 0 deletions aries_cloudagent/settings/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""Settings routes."""

import logging

from aiohttp import web
from aiohttp_apispec import docs, request_schema, response_schema
from marshmallow import fields

from ..admin.request_context import AdminRequestContext
from ..core.error import BaseError
from ..messaging.models.openapi import OpenAPISchema
from ..multitenant.admin.routes import get_extra_settings_dict_per_tenant

LOGGER = logging.getLogger(__name__)


class UpdateProfileSettingsSchema(OpenAPISchema):
"""Schema to update profile settings."""

extra_settings = fields.Dict(
description="Agent config key-value pairs",
required=False,
example={
"log_level": "INFO",
"ACAPY_INVITE_PUBLIC": True,
"public_invites": False,
},
)


class ProfileSettingsSchema(OpenAPISchema):
"""Profile settings response schema."""

settings = fields.Dict(
description="Profile settings dict",
example={
"log.level": "INFO",
"debug.invite_public": True,
"public_invites": False,
},
)


@docs(
tags=["settings"],
summary="Update settings or config associated with the profile.",
)
@request_schema(UpdateProfileSettingsSchema())
@response_schema(ProfileSettingsSchema(), 200, description="")
async def update_profile_settings(request: web.BaseRequest):
"""
Request handler for updating setting associated with profile.
Args:
request: aiohttp request object
"""
context: AdminRequestContext = request["context"]
try:
body = await request.json()
extra_setting = get_extra_settings_dict_per_tenant(
body.get("extra_settings") or {}
)
context.profile.settings.update(extra_setting)
result = context.profile.settings
except BaseError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
return web.json_response(result.to_dict())


@docs(
tags=["settings"],
summary="Get the settings or config associated with the profile.",
)
@response_schema(ProfileSettingsSchema(), 200, description="")
async def get_profile_settings(request: web.BaseRequest):
"""
Request handler for getting setting associated with profile.
Args:
request: aiohttp request object
"""
context: AdminRequestContext = request["context"]

try:
result = context.profile.settings
except BaseError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
return web.json_response(result.to_dict())


async def register(app: web.Application):
"""Register routes."""

app.add_routes(
[
web.put("/settings", update_profile_settings),
web.get("/settings", get_profile_settings, allow_head=False),
]
)


def post_process_routes(app: web.Application):
"""Amend swagger API."""

# Add top-level tags description
if "tags" not in app._state["swagger_dict"]:
app._state["swagger_dict"]["tags"] = []
app._state["swagger_dict"]["tags"].append(
{
"name": "settings",
"description": "Agent settings interface.",
}
)
Empty file.
Loading

0 comments on commit 9dc9f4d

Please sign in to comment.