Repository navigation
feat(audit_log): let ExApps write to the admin audit log #1219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3cb52bd
b397fb1
9d0ad96
6ca304b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
|
|
||
| namespace OCA\AppAPI\AuditLog; | ||
|
|
||
| use InvalidArgumentException; | ||
| use OCP\EventDispatcher\IEventDispatcher; | ||
| use OCP\Log\Audit\CriticalActionPerformedEvent; | ||
|
|
||
|
|
@@ -26,9 +27,55 @@ public function __construct( | |
| * @param array<string, mixed> $parameters named values for the placeholders of $message, in order, optionally followed by more context | ||
| */ | ||
| public function log(Operation $operation, string $message, array $parameters = []): void { | ||
| // The logger replaces `{placeholders}` in the message and admin_audit formats it again, so values are escaped here | ||
| $values = array_map(static fn (mixed $value): mixed => is_string($value) ? strtr($value, ['{' => '(', '}' => ')']) : $value, array_values($parameters)); | ||
| $message = str_replace('%', '%%', vsprintf($message, $values)); | ||
| $this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent($message, $parameters, false, $operation->value)); | ||
| $this->dispatch($operation->value, $message, $parameters); | ||
| } | ||
|
|
||
| /** | ||
| * Writes an entry on behalf of an ExApp, whose ID is added as the `appId` parameter | ||
| * | ||
| * @param string $operation operation in the form `<appId>.entity.action`, the first part being the ID of the ExApp | ||
| * @param string $message message with a `%s` placeholder for each of the first parameters; `%%` writes a `%`, other directives are rejected | ||
| * @param array<string, scalar|null> $parameters named values for the placeholders of $message, in order, optionally followed by more context | ||
| * @throws InvalidArgumentException if the app ID is empty, the operation does not start with the app ID or is not in the form `<appId>.entity.action`, a parameter is not a scalar or null, or the message has a directive other than `%s` and `%%` or more placeholders than parameters | ||
| */ | ||
| public function logForExApp(string $appId, string $operation, string $message, array $parameters = []): void { | ||
| if ($appId === '') { | ||
| throw new InvalidArgumentException('App ID must not be empty'); | ||
| } | ||
| if (!str_starts_with($operation, $appId . '.')) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ExApp IDs may contain
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, nice catch! |
||
| throw new InvalidArgumentException(sprintf('Operation must be in the form %s.entity.action', $appId)); | ||
| } | ||
| if (array_key_exists('appId', $parameters)) { | ||
| throw new InvalidArgumentException('Parameter appId is reserved'); | ||
| } | ||
| foreach ($parameters as $name => $value) { | ||
| if (!is_string($name) || ($value !== null && !is_scalar($value))) { | ||
| throw new InvalidArgumentException('Parameters must be named and be scalars or null'); | ||
| } | ||
| } | ||
| // Directives like `%c` or `%1073741824s` would write braces past the escaping or exhaust the memory | ||
| $directives = str_replace('%%', '', $message); | ||
| if (preg_match('/%(?!s)/', $directives)) { | ||
| throw new InvalidArgumentException('Message must only contain %s and %% directives'); | ||
| } | ||
| // Counted before appId is added, which would otherwise fill a missing placeholder | ||
| if (substr_count($directives, '%s') > count($parameters)) { | ||
| throw new InvalidArgumentException('Message has more placeholders than parameters'); | ||
| } | ||
| $parameters['appId'] = $appId; | ||
| $this->dispatch($operation, $message, $parameters); | ||
| } | ||
|
|
||
| private function dispatch(string $operation, string $message, array $parameters): void { | ||
| $message = self::escape(vsprintf($message, array_values($parameters))); | ||
| $this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent($message, $parameters, false, $operation)); | ||
| } | ||
|
|
||
| /** | ||
| * The logger replaces `{placeholders}` in the message, so braces become parentheses, and admin_audit | ||
| * formats the message again, so values are escaped here | ||
| */ | ||
| private static function escape(string $value): string { | ||
| return strtr($value, ['{' => '(', '}' => ')', '%' => '%%']); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4470,6 +4470,150 @@ | |
| } | ||
| } | ||
| }, | ||
| "/ocs/v2.php/apps/app_api/api/v1/audit_log": { | ||
| "post": { | ||
| "operationId": "ocs_api-audit-log", | ||
| "summary": "Write an entry to the admin audit log on behalf of an ExApp", | ||
| "description": "The entry carries the ID of the ExApp as the `appId` parameter.", | ||
| "tags": [ | ||
| "ocs_api" | ||
| ], | ||
| "security": [ | ||
| {}, | ||
| { | ||
| "bearer_auth": [] | ||
| }, | ||
| { | ||
| "basic_auth": [] | ||
| } | ||
| ], | ||
| "requestBody": { | ||
| "required": true, | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "required": [ | ||
| "operation", | ||
| "message" | ||
| ], | ||
| "properties": { | ||
| "operation": { | ||
| "type": "string", | ||
| "description": "Stable identifier of the action in the form `<appId>.entity.action`, starting with the ID of the ExApp, e.g. `my_app.item.created`" | ||
| }, | ||
| "message": { | ||
| "type": "string", | ||
| "description": "Message with a `%s` placeholder for each of the first parameters, in order" | ||
| }, | ||
| "parameters": { | ||
| "type": "object", | ||
| "default": {}, | ||
| "description": "Named values for the placeholders of the message, optionally followed by more context", | ||
| "additionalProperties": { | ||
| "nullable": true, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make null valid in a typed schema branch. The logger accepts Move |
||
| "anyOf": [ | ||
| { | ||
| "type": "string" | ||
| }, | ||
| { | ||
| "type": "integer", | ||
| "format": "int64" | ||
| }, | ||
| { | ||
| "type": "number", | ||
| "format": "double" | ||
| }, | ||
| { | ||
| "type": "boolean" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "parameters": [ | ||
| { | ||
| "name": "ex-app-id", | ||
| "in": "header", | ||
| "schema": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| { | ||
| "name": "OCS-APIRequest", | ||
| "in": "header", | ||
| "description": "Required to be true for the API request to pass", | ||
| "required": true, | ||
| "schema": { | ||
| "type": "boolean", | ||
| "default": true | ||
| } | ||
| } | ||
| ], | ||
| "responses": { | ||
| "200": { | ||
| "description": "Entry written to the audit log", | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "required": [ | ||
| "ocs" | ||
| ], | ||
| "properties": { | ||
| "ocs": { | ||
| "type": "object", | ||
| "required": [ | ||
| "meta", | ||
| "data" | ||
| ], | ||
| "properties": { | ||
| "meta": { | ||
| "$ref": "#/components/schemas/OCSMeta" | ||
| }, | ||
| "data": {} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "400": { | ||
| "description": "Invalid operation, parameters or message", | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "required": [ | ||
| "ocs" | ||
| ], | ||
| "properties": { | ||
| "ocs": { | ||
| "type": "object", | ||
| "required": [ | ||
| "meta", | ||
| "data" | ||
| ], | ||
| "properties": { | ||
| "meta": { | ||
| "$ref": "#/components/schemas/OCSMeta" | ||
| }, | ||
| "data": {} | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "/ocs/v2.php/apps/app_api/api/v1/users": { | ||
| "get": { | ||
| "operationId": "ocs_api-getnc-users-list", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.