diff --git a/server/src/main/java/com/mirth/connect/client/core/api/servlets/ConfigurationServletInterface.java b/server/src/main/java/com/mirth/connect/client/core/api/servlets/ConfigurationServletInterface.java index 2c54864e3..dae96238d 100644 --- a/server/src/main/java/com/mirth/connect/client/core/api/servlets/ConfigurationServletInterface.java +++ b/server/src/main/java/com/mirth/connect/client/core/api/servlets/ConfigurationServletInterface.java @@ -203,7 +203,7 @@ public void setServerSettings(@Param("settings") @RequestBody(description = "The @Path("/property") @Produces(MediaType.TEXT_PLAIN) @Operation(summary = "Returns a property from the configuration table.") - @MirthOperation(name = "getProperty", display = "Get property", auditable = false) + @MirthOperation(name = "getProperty", display = "Get property", permission = Permissions.SERVER_SETTINGS_VIEW, auditable = false) public String getProperty( @Param("group") @Parameter(description = "The property group.", required = true, schema = @Schema(type = "string")) @QueryParam("group") String group, @Param("name") @Parameter(description = "The name of the property.", required = true, schema = @Schema(type = "string")) @QueryParam("name") String name @@ -242,7 +242,7 @@ public ConnectionTestResponse sendTestEmail(@Param("properties") @RequestBody(de @PUT @Path("/updateSettings") @Operation(summary = "Updates the update settings.") - @MirthOperation(name = "setUpdateSettings", display = "Set update settings", auditable = false) + @MirthOperation(name = "setUpdateSettings", display = "Set update settings", permission = Permissions.SERVER_SETTINGS_EDIT, auditable = false) public void setUpdateSettings(@Param("settings") @RequestBody(description = "The UpdateSettings object containing all of the settings to update.", required = true, content = { @Content(mediaType = MediaType.APPLICATION_XML, examples = { @ExampleObject(name = "updateSetings", ref = "../apiexamples/update_settings_xml") }), diff --git a/server/src/main/java/com/mirth/connect/connectors/doc/DocumentConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/doc/DocumentConnectorServletInterface.java index 1a45942aa..1c92a04fd 100644 --- a/server/src/main/java/com/mirth/connect/connectors/doc/DocumentConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/doc/DocumentConnectorServletInterface.java @@ -26,6 +26,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -47,7 +48,7 @@ public interface DocumentConnectorServletInterface extends BaseServletInterface @ExampleObject(name = "connection_test_response_file", ref = "../apiexamples/connection_test_response_file_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_file", ref = "../apiexamples/connection_test_response_file_json") }) }) - @MirthOperation(name = "testWrite", display = "Test Write", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "testWrite", display = "Test Write", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse testWrite(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, diff --git a/server/src/main/java/com/mirth/connect/connectors/file/FileConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/file/FileConnectorServletInterface.java index 51531f8cc..f22e8c555 100644 --- a/server/src/main/java/com/mirth/connect/connectors/file/FileConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/file/FileConnectorServletInterface.java @@ -26,6 +26,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -46,7 +47,7 @@ public interface FileConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "connection_test_response_file", ref = "../apiexamples/connection_test_response_file_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_file", ref = "../apiexamples/connection_test_response_file_json") }) }) - @MirthOperation(name = "testRead", display = "Test Read", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "testRead", display = "Test Read", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse testRead(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, @@ -66,7 +67,7 @@ public ConnectionTestResponse testRead(// @formatter:off @ExampleObject(name = "connection_test_response_file", ref = "../apiexamples/connection_test_response_file_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_file", ref = "../apiexamples/connection_test_response_file_json") }) }) - @MirthOperation(name = "testWrite", display = "Test Write", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "testWrite", display = "Test Write", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse testWrite(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, diff --git a/server/src/main/java/com/mirth/connect/connectors/http/HttpConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/http/HttpConnectorServletInterface.java index 8d5751cdb..76bf3f367 100644 --- a/server/src/main/java/com/mirth/connect/connectors/http/HttpConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/http/HttpConnectorServletInterface.java @@ -26,6 +26,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -46,7 +47,7 @@ public interface HttpConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "connection_test_response_http", ref = "../apiexamples/connection_test_response_http_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_http", ref = "../apiexamples/connection_test_response_http_json") }) }) - @MirthOperation(name = "testConnection", display = "Test HTTP Connection", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "testConnection", display = "Test HTTP Connection", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse testConnection(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, diff --git a/server/src/main/java/com/mirth/connect/connectors/jdbc/DatabaseConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/jdbc/DatabaseConnectorServletInterface.java index ecc8f45e5..28a8afd51 100644 --- a/server/src/main/java/com/mirth/connect/connectors/jdbc/DatabaseConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/jdbc/DatabaseConnectorServletInterface.java @@ -30,6 +30,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -50,7 +51,7 @@ public interface DatabaseConnectorServletInterface extends BaseServletInterface @ExampleObject(name = "table_set", ref = "../apiexamples/table_set_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "table_set", ref = "../apiexamples/table_set_json") }) }) - @MirthOperation(name = "getTables", display = "Get Tables", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "getTables", display = "Get Tables", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public SortedSet getTables(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, diff --git a/server/src/main/java/com/mirth/connect/connectors/jms/JmsConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/jms/JmsConnectorServletInterface.java index e346db5ab..a0a276aaf 100644 --- a/server/src/main/java/com/mirth/connect/connectors/jms/JmsConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/jms/JmsConnectorServletInterface.java @@ -32,6 +32,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -52,7 +53,7 @@ public interface JmsConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "jms_connector_properties_map", ref = "../apiexamples/jms_connector_properties_map_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "jms_connector_properties_map", ref = "../apiexamples/jms_connector_properties_map_json") }) }) - @MirthOperation(name = "getTemplates", display = "Get JMS Templates", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "getTemplates", display = "Get JMS Templates", permission = Permissions.CHANNELS_VIEW, type = ExecuteType.ASYNC, auditable = false) public LinkedHashMap getTemplates() throws ClientException; @GET @@ -63,7 +64,7 @@ public interface JmsConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "jms_connector_properties", ref = "../apiexamples/jms_connector_properties_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "jms_connector_properties", ref = "../apiexamples/jms_connector_properties_json") }) }) - @MirthOperation(name = "getTemplate", display = "Get JMS Template", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "getTemplate", display = "Get JMS Template", permission = Permissions.CHANNELS_VIEW, type = ExecuteType.ASYNC, auditable = false) public JmsConnectorProperties getTemplate(@Param("templateName") @Parameter(description = "The name of the template.", required = true) @PathParam("templateName") String templateName) throws ClientException; @PUT @@ -74,7 +75,7 @@ public interface JmsConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "jms_template_name_set", ref = "../apiexamples/jms_template_name_set_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "jms_template_name_set", ref = "../apiexamples/jms_template_name_set_json") }) }) - @MirthOperation(name = "saveTemplate", display = "Save JMS Template", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "saveTemplate", display = "Save JMS Template", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public Set saveTemplate(// @formatter:off @Param("templateName") @Parameter(description = "The name of the template.", required = true) @PathParam("templateName") String templateName, @Param("properties") @@ -88,12 +89,12 @@ public Set saveTemplate(// @formatter:off @DELETE @Path("/templates/{templateName}") - @Operation(summary="Creates or updates a JMS connector settings template.") + @Operation(summary="Deletes a JMS connector settings template.") @ApiResponse(content = { @Content(mediaType = MediaType.APPLICATION_XML, examples = { @ExampleObject(name = "jms_template_name_set", ref = "../apiexamples/jms_template_name_set_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "jms_template_name_set", ref = "../apiexamples/jms_template_name_set_json") }) }) - @MirthOperation(name = "saveTemplate", display = "Save JMS Template", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "deleteTemplate", display = "Delete JMS Template", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public Set deleteTemplate(@Param("templateName") @Parameter(description = "The name of the template.", required = true) @PathParam("templateName") String templateName) throws ClientException; } \ No newline at end of file diff --git a/server/src/main/java/com/mirth/connect/connectors/smtp/SmtpConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/smtp/SmtpConnectorServletInterface.java index 1d55bc6c9..b5bf97d10 100644 --- a/server/src/main/java/com/mirth/connect/connectors/smtp/SmtpConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/smtp/SmtpConnectorServletInterface.java @@ -26,6 +26,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -46,7 +47,7 @@ public interface SmtpConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "connection_test_response_smtp", ref = "../apiexamples/connection_test_response_smtp_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_smtp", ref = "../apiexamples/connection_test_response_smtp_json") }) }) - @MirthOperation(name = "sendTestEmail", display = "Send Test Email", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "sendTestEmail", display = "Send Test Email", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse sendTestEmail(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, diff --git a/server/src/main/java/com/mirth/connect/connectors/tcp/TcpConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/tcp/TcpConnectorServletInterface.java index 01c2341e1..37e742191 100644 --- a/server/src/main/java/com/mirth/connect/connectors/tcp/TcpConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/tcp/TcpConnectorServletInterface.java @@ -26,6 +26,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -46,7 +47,7 @@ public interface TcpConnectorServletInterface extends BaseServletInterface { @ExampleObject(name = "connection_test_response_tcp", ref = "../apiexamples/connection_test_response_tcp_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_tcp", ref = "../apiexamples/connection_test_response_tcp_json") }) }) - @MirthOperation(name = "testConnection", display = "Test TCP Connection", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "testConnection", display = "Test TCP Connection", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse testConnection(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.", required = true) @QueryParam("channelName") String channelName, diff --git a/server/src/main/java/com/mirth/connect/connectors/ws/WebServiceConnectorServletInterface.java b/server/src/main/java/com/mirth/connect/connectors/ws/WebServiceConnectorServletInterface.java index bd0d63e9a..da51aa77a 100644 --- a/server/src/main/java/com/mirth/connect/connectors/ws/WebServiceConnectorServletInterface.java +++ b/server/src/main/java/com/mirth/connect/connectors/ws/WebServiceConnectorServletInterface.java @@ -28,6 +28,7 @@ import com.mirth.connect.client.core.ClientException; import com.mirth.connect.client.core.Operation.ExecuteType; +import com.mirth.connect.client.core.Permissions; import com.mirth.connect.client.core.api.BaseServletInterface; import com.mirth.connect.client.core.api.MirthOperation; import com.mirth.connect.client.core.api.Param; @@ -48,7 +49,7 @@ public interface WebServiceConnectorServletInterface extends BaseServletInterfac @ExampleObject(name = "cache_wsdl_from_url", ref = "../apiexamples/null_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "cache_wsdl_from_url", ref = "../apiexamples/null_json") }) }) - @MirthOperation(name = "cacheWsdlFromUrl", display = "Download and cache WSDL", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "cacheWsdlFromUrl", display = "Download and cache WSDL", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public Object cacheWsdlFromUrl(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.") @QueryParam("channelName") String channelName, @@ -70,7 +71,7 @@ public Object cacheWsdlFromUrl(// @formatter:off @ExampleObject(name = "is_wsdl_cached", ref = "../apiexamples/boolean_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "is_wsdl_cached", ref = "../apiexamples/boolean_json") }) }) - @MirthOperation(name = "isWsdlCached", display = "Check if WSDL is cached", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "isWsdlCached", display = "Check if WSDL is cached", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public boolean isWsdlCached(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true, schema = @Schema(description = "The ID of the channel.")) @@ -101,7 +102,7 @@ public boolean isWsdlCached(// @formatter:off @ExampleObject(name = "definition_service_map", ref = "../apiexamples/definition_service_map_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "definition_service_map", ref = "../apiexamples/definition_service_map_json") }) }) - @MirthOperation(name = "getDefinition", display = "Get WSDL Definition", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "getDefinition", display = "Get WSDL Definition", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public DefinitionServiceMap getDefinition(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true, schema = @Schema(description = "The ID of the channel.")) @@ -131,7 +132,7 @@ public DefinitionServiceMap getDefinition(// @formatter:off @Operation(summary = "Generate SOAP envelope for a given WSDL operation.") @ApiResponse(content = { @Content(mediaType = MediaType.TEXT_PLAIN, examples = { @ExampleObject(name = "generate_envelope", ref = "../apiexamples/generate_envelope_txt") }) }) - @MirthOperation(name = "generateEnvelope", display = "Generate WSDL operation envelope", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "generateEnvelope", display = "Generate WSDL operation envelope", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public String generateEnvelope(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true, schema = @Schema(description = "The ID of the channel.")) @@ -178,7 +179,7 @@ public String generateEnvelope(// @formatter:off @Operation(summary = "Retrieves the default SOAP Action (URI string) for a given WSDL operation.") @ApiResponse(content = { @Content(mediaType = MediaType.TEXT_PLAIN, examples = { @ExampleObject(value = "SomeAction") }) }) - @MirthOperation(name = "getSoapAction", display = "Get SOAP Action for operation", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "getSoapAction", display = "Get SOAP Action for operation", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public String getSoapAction(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true, schema = @Schema(description = "The ID of the channel.")) @@ -220,7 +221,7 @@ public String getSoapAction(// @formatter:off @ExampleObject(name = "connection_test_response_ws", ref = "../apiexamples/connection_test_response_ws_xml") }), @Content(mediaType = MediaType.APPLICATION_JSON, examples = { @ExampleObject(name = "connection_test_response_ws", ref = "../apiexamples/connection_test_response_ws_json") }) }) - @MirthOperation(name = "testConnection", display = "Test Web Service Connection", type = ExecuteType.ASYNC, auditable = false) + @MirthOperation(name = "testConnection", display = "Test Web Service Connection", permission = Permissions.CHANNELS_MANAGE, type = ExecuteType.ASYNC, auditable = false) public ConnectionTestResponse testConnection(// @formatter:off @Param("channelId") @Parameter(description = "The ID of the channel.", required = true) @QueryParam("channelId") String channelId, @Param("channelName") @Parameter(description = "The name of the channel.") @QueryParam("channelName") String channelName, diff --git a/server/src/test/java/com/mirth/connect/client/core/api/MirthOperationPermissionTest.java b/server/src/test/java/com/mirth/connect/client/core/api/MirthOperationPermissionTest.java new file mode 100644 index 000000000..a9cc456d5 --- /dev/null +++ b/server/src/test/java/com/mirth/connect/client/core/api/MirthOperationPermissionTest.java @@ -0,0 +1,160 @@ +// SPDX-License-Identifier: MPL-2.0 +// SPDX-FileCopyrightText: Open Integration Engine + +package com.mirth.connect.client.core.api; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; + +import org.apache.commons.lang3.StringUtils; +import org.junit.BeforeClass; +import org.junit.Test; +import org.reflections.Reflections; + +/** + * Every API operation declares the permission it needs, so an authorization controller can enforce + * it. An operation that needs none goes in {@link #OPEN_ON_PURPOSE} with the reason. + */ +public class MirthOperationPermissionTest { + + /** Operations that declare no permission on purpose, keyed by interface and method name. */ + private static final Map OPEN_ON_PURPOSE = new TreeMap<>(); + + static { + open("ConfigurationServletInterface.getServerId", "Server identity"); + open("ConfigurationServletInterface.getVersion", "Server version"); + open("ConfigurationServletInterface.getBuildDate", "Server version"); + open("ConfigurationServletInterface.getStatus", "Server status"); + open("ConfigurationServletInterface.getServerTimezone", "Server clock"); + open("ConfigurationServletInterface.getServerTime", "Server clock"); + open("ConfigurationServletInterface.getJVMName", "Server runtime name"); + open("ConfigurationServletInterface.getAvailableCharsetEncodings", "Character sets the server supports"); + open("ConfigurationServletInterface.getPublicServerSettings", "The public subset of the server settings"); + open("ConfigurationServletInterface.getUpdateSettings", "Usage statistics switch and timestamp"); + open("ConfigurationServletInterface.getLicenseInfo", "License information"); + open("ConfigurationServletInterface.getGuid", "Generates a random ID"); + open("ConfigurationServletInterface.getDatabaseDrivers", "Database drivers the server has"); + open("ConfigurationServletInterface.getPasswordRequirements", "Password rules"); + open("ConfigurationServletInterface.getProtocolsAndCipherSuites", "TLS protocols and cipher suites the server supports"); + open("ConfigurationServletInterface.getRhinoLanguageVersion", "JavaScript language version"); + open("ConfigurationServletInterface.getAbout", "Lists every channel; limiting it per channel is separate from a permission"); + open("ConfigurationServletInterface.getChannelDependencies", "Lists every channel; limiting it per channel is separate from a permission"); + open("ConfigurationServletInterface.getChannelMetadata", "Lists every channel; limiting it per channel is separate from a permission"); + open("ConfigurationServletInterface.getEncryptionSettings", "Needed by the Administrator for every user"); + open("ExtensionServletInterface.getExtensionMetaData", "Extension metadata"); + open("ExtensionServletInterface.getConnectorMetaData", "Extension metadata"); + open("ExtensionServletInterface.getPluginMetaData", "Extension metadata"); + open("ExtensionServletInterface.isExtensionEnabled", "Extension metadata"); + open("ExtensionServletInterface.getDisabledExtensions", "Extension metadata"); + open("ExtensionServletInterface.getPluginProperties", "Checks the extension's own permission inside the method"); + open("ExtensionServletInterface.setPluginProperties", "Checks the extension's own permission inside the method"); + open("MessageServletInterface.auditAccessedPHIMessage", "Called by the client to record an action it already performed"); + open("MessageServletInterface.auditQueriedPHIMessage", "Called by the client to record an action it already performed"); + open("MessageServletInterface.auditExportMessages", "Called by the client to record an action it already performed"); + open("MessageServletInterface.auditExportMessagesSuccess", "Called by the client to record an action it already performed"); + open("SystemServletInterface.getInfo", "Server runtime and system information"); + open("SystemServletInterface.getStats", "Server system statistics"); + open("UsageServletInterface.getUsageData", "Usage statistics report"); + open("UserServletInterface.login", "Runs before the user is known"); + open("UserServletInterface.logout", "Ends the caller's own session"); + open("UserServletInterface.inactivityLogout", "Ends the caller's own session"); + open("UserServletInterface.getCurrentUser", "Returns the caller's own user"); + open("UserServletInterface.getUserPreferences", "Every user reads and writes their own preferences"); + open("UserServletInterface.getUserPreference", "Every user reads and writes their own preferences"); + open("UserServletInterface.setUserPreferences", "Every user reads and writes their own preferences"); + open("UserServletInterface.setUserPreference", "Every user reads and writes their own preferences"); + open("UserServletInterface.setUserNotificationAcknowledged", "Every user reads and writes their own preferences"); + open("DirectoryResourceServletInterface.getLibraries", "Libraries of a resource"); + } + + private static void open(String operation, String reason) { + OPEN_ON_PURPOSE.put(operation, reason); + } + + /** "Interface.method" of every @MirthOperation that declares no permission. */ + private static final Set unlabelled = new TreeSet<>(); + /** How many @MirthOperation methods share each "Interface.method" (overloads share one). */ + private static final Map methodsPerKey = new HashMap<>(); + private static int core; + private static int connectors; + private static int plugins; + + @BeforeClass + public static void scan() { + for (Class servletInterface : new Reflections("com.mirth.connect").getSubTypesOf(BaseServletInterface.class)) { + for (Method method : servletInterface.getDeclaredMethods()) { + MirthOperation operation = method.getAnnotation(MirthOperation.class); + if (operation == null) { + continue; + } + String key = servletInterface.getSimpleName() + "." + method.getName(); + methodsPerKey.merge(key, 1, Integer::sum); + String pkg = servletInterface.getPackage().getName(); + if (pkg.startsWith("com.mirth.connect.client.core.api.servlets")) { + core++; + } else if (pkg.startsWith("com.mirth.connect.connectors.")) { + connectors++; + } else if (pkg.startsWith("com.mirth.connect.plugins.")) { + plugins++; + } + if (StringUtils.isBlank(operation.permission())) { + unlabelled.add(key); + } + } + } + } + + @Test + public void scanFindsCoreConnectorAndPluginOperations() { + // An empty or partial scan would let every other check pass with nothing checked. + assertTrue("core API operations found: " + core, core > 100); + assertTrue("connector operations found: " + connectors, connectors >= 17); + assertTrue("bundled plugin operations found: " + plugins, plugins >= 1); + } + + @Test + public void everyOperationDeclaresAPermissionOrIsOpenOnPurpose() { + List missing = new ArrayList<>(); + for (String operation : unlabelled) { + if (!OPEN_ON_PURPOSE.containsKey(operation)) { + missing.add(operation); + } + } + if (!missing.isEmpty()) { + fail("These operations declare no permission. Add one to @MirthOperation, or add the operation to OPEN_ON_PURPOSE in this test with the reason it needs none:\n " + StringUtils.join(missing, "\n ")); + } + } + + @Test + public void openOnPurposeListsOnlyUnlabelledOperations() { + List stale = new ArrayList<>(OPEN_ON_PURPOSE.keySet()); + stale.removeAll(unlabelled); + if (!stale.isEmpty()) { + fail("These OPEN_ON_PURPOSE entries match no operation without a permission (renamed, removed or now labelled); remove them:\n " + StringUtils.join(stale, "\n ")); + } + } + + @Test + public void openOnPurposeEntriesNameOneMethodEach() { + // Overloads share a key, so a new overload of a listed method would otherwise be covered by + // the existing entry without anyone deciding it needs no permission. + List ambiguous = new ArrayList<>(); + for (String operation : OPEN_ON_PURPOSE.keySet()) { + if (methodsPerKey.getOrDefault(operation, 0) > 1) { + ambiguous.add(operation); + } + } + if (!ambiguous.isEmpty()) { + fail("These OPEN_ON_PURPOSE entries match more than one overloaded method; give the new overload a permission or rename it:\n " + StringUtils.join(ambiguous, "\n ")); + } + } +}