Skip to content
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

Added validation of UUIDs in path parameters in the admin API so that clearer errors are reported when non UUIDs are provided or item isn't found #2347

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
*/
package com.github.tomakehurst.wiremock.admin;

import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.admin.tasks.AbstractSingleServeEventTask;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import java.util.UUID;

public class RemoveServeEventTask implements AdminTask {
public class RemoveServeEventTask extends AbstractSingleServeEventTask {

@Override
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
String idString = pathParams.get("id");
UUID id = UUID.fromString(idString);
protected ResponseDefinition processServeEvent(Admin admin, ServeEvent adminServeEvent, UUID id) {
admin.removeServeEvent(id);
return ResponseDefinition.okEmptyJson();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tomakehurst.wiremock.admin.tasks;

import com.github.tomakehurst.wiremock.admin.AdminTask;
import com.github.tomakehurst.wiremock.common.Errors;
import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import java.util.UUID;

public abstract class AbstractSingleServeEventTask implements AdminTask {

@Override
public ResponseDefinition execute(
Admin admin, ServeEvent adminServeEvent, PathParams pathParams) {
String idString = pathParams.get("id");
UUID id;
try {
id = UUID.fromString(idString);
} catch (IllegalArgumentException e) {
return ResponseDefinition.badRequest(Errors.single(10, idString + " is not a valid UUID"));
}

return processServeEvent(admin, adminServeEvent, id);
}

protected abstract ResponseDefinition processServeEvent(
Admin admin, ServeEvent adminServeEvent, UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tomakehurst.wiremock.admin.tasks;

import com.github.tomakehurst.wiremock.admin.AdminTask;
import com.github.tomakehurst.wiremock.admin.model.SingleStubMappingResult;
import com.github.tomakehurst.wiremock.common.Errors;
import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import java.util.UUID;

public abstract class AbstractSingleStubTask implements AdminTask {

@Override
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
String idString = pathParams.get("id");
UUID id;
try {
id = UUID.fromString(idString);
} catch (IllegalArgumentException e) {
return ResponseDefinition.badRequest(Errors.single(10, idString + " is not a valid UUID"));
}

final SingleStubMappingResult result = admin.getStubMapping(id);
return result.isPresent()
? processStubMapping(admin, serveEvent, result.getItem())
: ResponseDefinition.notFound();
}

protected abstract ResponseDefinition processStubMapping(
Admin admin, ServeEvent serveEvent, StubMapping stubMapping);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,18 @@
*/
package com.github.tomakehurst.wiremock.admin.tasks;

import com.github.tomakehurst.wiremock.admin.AdminTask;
import com.github.tomakehurst.wiremock.admin.model.SingleStubMappingResult;
import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import java.util.UUID;

public class EditStubMappingTask implements AdminTask {
public class EditStubMappingTask extends AbstractSingleStubTask {

@Override
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
protected ResponseDefinition processStubMapping(
Admin admin, ServeEvent serveEvent, StubMapping stubMapping) {
StubMapping newStubMapping = StubMapping.buildFrom(serveEvent.getRequest().getBodyAsString());
UUID id = UUID.fromString(pathParams.get("id"));
SingleStubMappingResult stubMappingResult = admin.getStubMapping(id);
if (!stubMappingResult.isPresent()) {
return ResponseDefinition.notFound();
}

newStubMapping.setId(id);

newStubMapping.setId(stubMapping.getId());
admin.editStubMapping(newStubMapping);
return ResponseDefinition.okForJson(newStubMapping);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@
*/
package com.github.tomakehurst.wiremock.admin.tasks;

import com.github.tomakehurst.wiremock.admin.AdminTask;
import com.github.tomakehurst.wiremock.admin.model.SingleServedStubResult;
import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import java.util.UUID;

public class GetServedStubTask implements AdminTask {
public class GetServedStubTask extends AbstractSingleServeEventTask {

@Override
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
String idString = pathParams.get("id");
UUID id = UUID.fromString(idString);

SingleServedStubResult result = admin.getServedStub(id);
protected ResponseDefinition processServeEvent(Admin admin, ServeEvent adminServeEvent, UUID id) {
final SingleServedStubResult result = admin.getServedStub(id);
return result.isPresent()
? ResponseDefinition.okForJson(result.getItem())
: ResponseDefinition.notFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,16 @@
*/
package com.github.tomakehurst.wiremock.admin.tasks;

import com.github.tomakehurst.wiremock.admin.AdminTask;
import com.github.tomakehurst.wiremock.admin.model.SingleStubMappingResult;
import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import java.util.UUID;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;

public class GetStubMappingTask implements AdminTask {
public class GetStubMappingTask extends AbstractSingleStubTask {

@Override
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
String idString = pathParams.get("id");
UUID id = UUID.fromString(idString);

SingleStubMappingResult stubMappingResult = admin.getStubMapping(id);
return stubMappingResult.isPresent()
? ResponseDefinition.okForJson(stubMappingResult.getItem())
: ResponseDefinition.notFound();
protected ResponseDefinition processStubMapping(
Admin admin, ServeEvent serveEvent, StubMapping stubMapping) {
return ResponseDefinition.okForJson(stubMapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,17 @@
*/
package com.github.tomakehurst.wiremock.admin.tasks;

import com.github.tomakehurst.wiremock.admin.AdminTask;
import com.github.tomakehurst.wiremock.admin.model.SingleStubMappingResult;
import com.github.tomakehurst.wiremock.common.url.PathParams;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import java.util.UUID;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;

public class RemoveStubMappingByIdTask implements AdminTask {
public class RemoveStubMappingByIdTask extends AbstractSingleStubTask {

@Override
public ResponseDefinition execute(Admin admin, ServeEvent serveEvent, PathParams pathParams) {
SingleStubMappingResult stubMappingResult =
admin.getStubMapping(UUID.fromString(pathParams.get("id")));

if (!stubMappingResult.isPresent()) {
return ResponseDefinition.notFound();
}

admin.removeStubMapping(stubMappingResult.getItem());
protected ResponseDefinition processStubMapping(
Admin admin, ServeEvent serveEvent, StubMapping stubMapping) {
admin.removeStubMapping(stubMapping);
return ResponseDefinition.okEmptyJson();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void getLoggedRequestsWithLimitLargerThanResults() throws Exception {
}

@Test
public void getLoggedRequestById() throws Exception {
public void getLoggedRequestById() {
for (int i = 1; i <= 3; i++) {
testClient.get("/received-request/" + i);
}
Expand Down Expand Up @@ -1282,6 +1282,57 @@ void returnsDefaultStubMappingInServeEventWhenRequestNotMatched() {
assertThat(data, jsonPartEquals("requests[0].stubMapping.response.status", 404));
}

@Test
void returnsBadRequestWhenAttemptingToGetByNonUuid() {
WireMockResponse response = testClient.get("/__admin/mappings/not-a-uuid");
assertThat(response.statusCode(), is(400));
assertThat(
response.content(), jsonPartEquals("errors[0].title", "not-a-uuid is not a valid UUID"));
}

@Test
void returnsNotFoundWhenAttemptingToGetNonExistentStub() {
assertThat(testClient.get("/__admin/mappings/" + UUID.randomUUID()).statusCode(), is(404));
}

@Test
void returnsBadRequestWhenAttemptingToEditByNonUuid() {
assertThat(testClient.putJson("/__admin/mappings/not-a-uuid", "{}").statusCode(), is(400));
}

@Test
void returnsNotFoundWhenAttemptingToEditNonExistentStub() {
assertThat(testClient.put("/__admin/mappings/" + UUID.randomUUID()).statusCode(), is(404));
}

@Test
void returnsBadRequestWhenAttemptingToRemoveByNonUuid() {
assertThat(testClient.delete("/__admin/mappings/not-a-uuid").statusCode(), is(400));
}

@Test
void returnsNotFoundWhenAttemptingToRemoveNonExistentStub() {
assertThat(testClient.put("/__admin/mappings/" + UUID.randomUUID()).statusCode(), is(404));
}

@Test
void returnsBadRequestWhenAttemptingToGetServeEventByNonUuid() {
WireMockResponse response = testClient.get("/__admin/requests/not-a-uuid");
assertThat(response.statusCode(), is(400));
assertThat(
response.content(), jsonPartEquals("errors[0].title", "not-a-uuid is not a valid UUID"));
}

@Test
void returnsNotFoundWhenAttemptingToGetServeEventByNonExistentId() {
assertThat(testClient.get("/__admin/requests/" + UUID.randomUUID()).statusCode(), is(404));
}

@Test
void returnsBadRequestWhenAttemptingToRemoveServeEventByNonUuid() {
assertThat(testClient.delete("/__admin/requests/not-a-uuid").statusCode(), is(400));
}

public static class TestExtendedSettingsData {
public String name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public WireMockResponse postJson(String url, String body, TestHttpHeader... head
return executeMethodAndConvertExceptions(httpPost, headers);
}

public WireMockResponse putJson(String url, String body, TestHttpHeader... headers) {
return putWithBody(url, body, APPLICATION_JSON.getMimeType(), headers);
}

public WireMockResponse postXml(String url, String body, TestHttpHeader... headers) {
HttpPost httpPost = new HttpPost(mockServiceUrlFor(url));
httpPost.setEntity(new StringEntity(body, APPLICATION_XML));
Expand Down
Loading