|
2 | 2 |
|
3 | 3 | import com.contrast.labs.ai.mcp.contrast.sdkexstension.SDKExtension;
|
4 | 4 | import com.contrast.labs.ai.mcp.contrast.sdkexstension.SDKHelper;
|
| 5 | +import com.contrast.labs.ai.mcp.contrast.sdkexstension.data.application.Application; |
5 | 6 | import com.contrast.labs.ai.mcp.contrast.sdkexstension.data.routecoverage.Route;
|
| 7 | +import com.contrast.labs.ai.mcp.contrast.sdkexstension.data.routecoverage.RouteCoverageBySessionIDAndMetadataRequestExtended; |
6 | 8 | import com.contrast.labs.ai.mcp.contrast.sdkexstension.data.routecoverage.RouteCoverageResponse;
|
7 | 9 | import com.contrast.labs.ai.mcp.contrast.sdkexstension.data.routecoverage.RouteDetailsResponse;
|
8 |
| -import com.contrastsecurity.models.Application; |
| 10 | +import com.contrast.labs.ai.mcp.contrast.sdkexstension.data.sessionmetadata.SessionMetadataResponse; |
| 11 | +import com.contrastsecurity.models.RouteCoverageBySessionIDAndMetadataRequest; |
| 12 | +import com.contrastsecurity.models.RouteCoverageMetadataLabelValues; |
9 | 13 | import com.contrastsecurity.sdk.ContrastSDK;
|
10 | 14 | import org.slf4j.Logger;
|
11 | 15 | import org.slf4j.LoggerFactory;
|
@@ -52,30 +56,23 @@ public RouteCoverageResponse getRouteCoverage(String app_name) throws IOExceptio
|
52 | 56 | logger.info("Retrieving route coverage for application by name: {}", app_name);
|
53 | 57 | ContrastSDK contrastSDK = SDKHelper.getSDK(hostName, apiKey, serviceKey, userName,httpProxyHost, httpProxyPort);
|
54 | 58 | SDKExtension sdkExtension = new SDKExtension(contrastSDK);
|
55 |
| - Optional<String> appID = Optional.empty(); |
56 | 59 | logger.debug("Searching for application ID matching name: {}", app_name);
|
57 | 60 |
|
58 |
| - for(Application app : SDKHelper.getApplicationsWithCache(orgID, contrastSDK)) { |
59 |
| - if(app.getName().toLowerCase().contains(app_name.toLowerCase())) { |
60 |
| - appID = Optional.of(app.getId()); |
61 |
| - logger.debug("Found matching application with ID: {}", appID.get()); |
62 |
| - break; |
63 |
| - } |
64 |
| - } |
| 61 | + Optional<Application> application = SDKHelper.getApplicationByName(app_name, orgID, contrastSDK); |
65 | 62 |
|
66 |
| - if (!appID.isPresent()) { |
| 63 | + if (!application.isPresent()) { |
67 | 64 | logger.error("Application not found: {}", app_name);
|
68 | 65 | throw new IOException("Application not found: " + app_name);
|
69 | 66 | }
|
70 | 67 |
|
71 |
| - logger.debug("Fetching route coverage data for application ID: {}", appID.get()); |
72 |
| - RouteCoverageResponse response = sdkExtension.getRouteCoverage(orgID, appID.get(), null); |
| 68 | + logger.debug("Fetching route coverage data for application ID: {}", application.get().getAppId()); |
| 69 | + RouteCoverageResponse response = sdkExtension.getRouteCoverage(orgID, application.get().getAppId(), null); |
73 | 70 | logger.debug("Found {} routes for application", response.getRoutes().size());
|
74 | 71 |
|
75 | 72 | logger.debug("Retrieving route details for each route");
|
76 | 73 | for(Route route : response.getRoutes()) {
|
77 | 74 | logger.trace("Fetching details for route: {}", route.getSignature());
|
78 |
| - RouteDetailsResponse routeDetailsResponse = sdkExtension.getRouteDetails(orgID, appID.get(), route.getRouteHash()); |
| 75 | + RouteDetailsResponse routeDetailsResponse = sdkExtension.getRouteDetails(orgID, application.get().getAppId(), route.getRouteHash()); |
79 | 76 | route.setRouteDetailsResponse(routeDetailsResponse);
|
80 | 77 | }
|
81 | 78 |
|
@@ -105,6 +102,86 @@ public RouteCoverageResponse getRouteCoverageByAppID(String app_id) throws IOExc
|
105 | 102 | return response;
|
106 | 103 | }
|
107 | 104 |
|
| 105 | + @Tool(name = "get_application_route_coverage_by_app_name_and_session_metadata", description = "takes a application name and return the route coverage data for that application for the specified session metadata name and value. " + |
| 106 | + "If a route/endpoint is DISCOVERED, it means it has been found by Assess but that route has had no inbound http requests. If it is EXERCISED, it means it has had at least one inbound http request to that route/endpoint.") |
| 107 | + public RouteCoverageResponse getRouteCoverageByAppNameAndSessionMetadata(String app_name, String session_Metadata_Name, String session_Metadata_Value) throws IOException { |
| 108 | + logger.info("Retrieving route coverage for application by Name: {}", app_name); |
| 109 | + ContrastSDK contrastSDK = SDKHelper.getSDK(hostName, apiKey, serviceKey, userName,httpProxyHost, httpProxyPort); |
| 110 | + logger.debug("Searching for application ID matching name: {}", app_name); |
| 111 | + |
| 112 | + Optional<Application> application = SDKHelper.getApplicationByName(app_name, orgID, contrastSDK); |
| 113 | + if (!application.isPresent()) { |
| 114 | + logger.error("Application not found: {}", app_name); |
| 115 | + throw new IOException("Application not found: " + app_name); |
| 116 | + } |
| 117 | + return getRouteCoverageByAppIDAndSessionMetadata(application.get().getAppId(), session_Metadata_Name, session_Metadata_Value); |
| 118 | + } |
| 119 | + |
| 120 | + @Tool(name = "get_application_route_coverage_by_app_id_and_session_metadata", description = "takes a application id and return the route coverage data for that application for the specified session metadata name and value. " + |
| 121 | + "If a route/endpoint is DISCOVERED, it means it has been found by Assess but that route has had no inbound http requests. If it is EXERCISED, it means it has had at least one inbound http request to that route/endpoint.") |
| 122 | + public RouteCoverageResponse getRouteCoverageByAppIDAndSessionMetadata(String app_id, String session_Metadata_Name, String session_Metadata_Value) throws IOException { |
| 123 | + logger.info("Retrieving route coverage for application by ID: {}", app_id); |
| 124 | + ContrastSDK contrastSDK = SDKHelper.getSDK(hostName, apiKey, serviceKey, userName,httpProxyHost, httpProxyPort); |
| 125 | + SDKExtension sdkExtension = new SDKExtension(contrastSDK); |
| 126 | + RouteCoverageBySessionIDAndMetadataRequestExtended requestExtended = new RouteCoverageBySessionIDAndMetadataRequestExtended(); |
| 127 | + RouteCoverageMetadataLabelValues metadataLabelValue = new RouteCoverageMetadataLabelValues(); |
| 128 | + metadataLabelValue.setLabel(session_Metadata_Name); |
| 129 | + metadataLabelValue.getValues().add(String.valueOf(session_Metadata_Value)); |
| 130 | + requestExtended.getValues().add(metadataLabelValue); |
| 131 | + logger.debug("Fetching route coverage data for application ID: {}", app_id); |
| 132 | + RouteCoverageResponse response = sdkExtension.getRouteCoverage(orgID, app_id, requestExtended); |
| 133 | + logger.debug("Found {} routes for application", response.getRoutes().size()); |
| 134 | + |
| 135 | + logger.debug("Retrieving route details for each route"); |
| 136 | + for(Route route : response.getRoutes()) { |
| 137 | + logger.trace("Fetching details for route: {}", route.getSignature()); |
| 138 | + RouteDetailsResponse routeDetailsResponse = sdkExtension.getRouteDetails(orgID, app_id, route.getRouteHash()); |
| 139 | + route.setRouteDetailsResponse(routeDetailsResponse); |
| 140 | + } |
| 141 | + |
| 142 | + logger.info("Successfully retrieved route coverage for application ID: {}", app_id); |
| 143 | + return response; |
| 144 | + } |
| 145 | + |
| 146 | + @Tool(name = "get_application_route_coverage_by_app_name_latest_session", description = "takes a application name and return the route coverage data for that application from the latest session. " + |
| 147 | + "If a route/endpoint is DISCOVERED, it means it has been found by Assess but that route has had no inbound http requests. If it is EXERCISED, it means it has had atleast one inbound http request to that route/endpoint.") |
| 148 | + public RouteCoverageResponse getRouteCoverageByAppNameLatestSession(String app_name) throws IOException { |
| 149 | + logger.info("Retrieving route coverage for application by Name: {}", app_name); |
| 150 | + ContrastSDK contrastSDK = SDKHelper.getSDK(hostName, apiKey, serviceKey, userName, httpProxyHost, httpProxyPort); |
| 151 | + Optional<Application> application = SDKHelper.getApplicationByName(app_name, orgID, contrastSDK); |
| 152 | + if (application.isEmpty()) { |
| 153 | + logger.error("Application not found: {}", app_name); |
| 154 | + throw new IOException("Application not found: " + app_name); |
| 155 | + } |
| 156 | + return getRouteCoverageByAppIDLatestSession(application.get().getAppId()); |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | + @Tool(name = "get_application_route_coverage_by_app_id_latest_session", description = "takes a application id and return the route coverage data for that application from the latest session. " + |
| 161 | + "If a route/endpoint is DISCOVERED, it means it has been found by Assess but that route has had no inbound http requests. If it is EXERCISED, it means it has had atleast one inbound http request to that route/endpoint.") |
| 162 | + public RouteCoverageResponse getRouteCoverageByAppIDLatestSession(String app_id) throws IOException { |
| 163 | + logger.info("Retrieving route coverage for application by ID: {}", app_id); |
| 164 | + ContrastSDK contrastSDK = SDKHelper.getSDK(hostName, apiKey, serviceKey, userName,httpProxyHost, httpProxyPort); |
| 165 | + SDKExtension sdkExtension = new SDKExtension(contrastSDK); |
| 166 | + SDKExtension extension = new SDKExtension(contrastSDK); |
| 167 | + SessionMetadataResponse latest = extension.getLatestSessionMetadata(orgID,app_id); |
| 168 | + RouteCoverageBySessionIDAndMetadataRequestExtended requestExtended = new RouteCoverageBySessionIDAndMetadataRequestExtended(); |
| 169 | + requestExtended.setSessionId(latest.getAgentSession().getAgentSessionId()); |
| 170 | + logger.debug("Fetching route coverage data for application ID: {}", app_id); |
| 171 | + RouteCoverageResponse response = sdkExtension.getRouteCoverage(orgID, app_id, requestExtended); |
| 172 | + logger.debug("Found {} routes for application", response.getRoutes().size()); |
| 173 | + |
| 174 | + logger.debug("Retrieving route details for each route"); |
| 175 | + for(Route route : response.getRoutes()) { |
| 176 | + logger.trace("Fetching details for route: {}", route.getSignature()); |
| 177 | + RouteDetailsResponse routeDetailsResponse = sdkExtension.getRouteDetails(orgID, app_id, route.getRouteHash()); |
| 178 | + route.setRouteDetailsResponse(routeDetailsResponse); |
| 179 | + } |
| 180 | + |
| 181 | + logger.info("Successfully retrieved route coverage for application ID: {}", app_id); |
| 182 | + return response; |
| 183 | + } |
| 184 | + |
108 | 185 |
|
109 | 186 |
|
110 | 187 |
|
|
0 commit comments