use of com.github.ambry.account.Account in project ambry by linkedin.
the class AmbrySecurityServiceTest method testGetSubResource.
/**
* Tests GET of sub-resources.
* @param subResource the {@link RestUtils.SubResource} to test.
* @throws Exception
*/
private void testGetSubResource(BlobInfo blobInfo, RestUtils.SubResource subResource) throws Exception {
MockRestResponseChannel restResponseChannel = new MockRestResponseChannel();
RestRequest restRequest = createRestRequest(RestMethod.GET, "/sampleId/" + subResource, null);
Pair<Account, Container> accountAndContainer = getAccountAndContainer(blobInfo.getBlobProperties());
insertAccountAndContainer(restRequest, accountAndContainer.getFirst(), accountAndContainer.getSecond());
securityService.processResponse(restRequest, restResponseChannel, blobInfo).get();
Assert.assertEquals("ProcessResponse status should have been set ", ResponseStatus.Ok, restResponseChannel.getStatus());
Assert.assertNotNull("Date has not been set", restResponseChannel.getHeader(RestUtils.Headers.DATE));
Assert.assertEquals("Last Modified does not match creation time", RestUtils.toSecondsPrecisionInMs(blobInfo.getBlobProperties().getCreationTimeInMs()), RestUtils.getTimeFromDateString(restResponseChannel.getHeader(RestUtils.Headers.LAST_MODIFIED)).longValue());
if (subResource.equals(RestUtils.SubResource.BlobInfo)) {
verifyBlobPropertiesHeaders(blobInfo.getBlobProperties(), restResponseChannel);
verifyAccountAndContainerHeaders(restResponseChannel, accountAndContainer.getFirst(), accountAndContainer.getSecond());
Assert.assertEquals("LifeVersion mismatch", Short.toString(blobInfo.getLifeVersion()), restResponseChannel.getHeader(RestUtils.Headers.LIFE_VERSION));
} else {
verifyAbsenceOfHeaders(restResponseChannel, RestUtils.Headers.PRIVATE, RestUtils.Headers.TTL, RestUtils.Headers.SERVICE_ID, RestUtils.Headers.OWNER_ID, RestUtils.Headers.AMBRY_CONTENT_TYPE, RestUtils.Headers.CREATION_TIME, RestUtils.Headers.BLOB_SIZE, RestUtils.Headers.ACCEPT_RANGES, RestUtils.Headers.CONTENT_RANGE, RestUtils.Headers.LIFE_VERSION);
}
Map<String, String> userMetadata = blobInfo.getUserMetadata() != null ? RestUtils.buildUserMetadata(blobInfo.getUserMetadata()) : null;
if (userMetadata == null && !(blobInfo.getUserMetadata().length == 0)) {
Assert.assertTrue("Internal key " + RestUtils.InternalKeys.SEND_USER_METADATA_AS_RESPONSE_BODY + " should be set", (Boolean) restRequest.getArgs().get(RestUtils.InternalKeys.SEND_USER_METADATA_AS_RESPONSE_BODY));
} else if (!(blobInfo.getUserMetadata().length == 0)) {
USER_METADATA.forEach((key, value) -> Assert.assertEquals("Value of " + key + " not as expected", value, restResponseChannel.getHeader(key)));
}
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class AmbrySecurityServiceTest method getAccountAndContainer.
/**
* @param blobProperties the {@link BlobProperties} to read.
* @return the {@link Account} and {@link Container} objects corresponding to the IDs in the {@link BlobProperties}.
*/
private Pair<Account, Container> getAccountAndContainer(BlobProperties blobProperties) {
Account account = ACCOUNT_SERVICE.getAccountById(blobProperties.getAccountId());
Container container = account.getContainerById(blobProperties.getContainerId());
return new Pair<>(account, container);
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class FrontendIntegrationTest method getAndUseSignedUrlTest.
/**
* Tests the handling of {@link Operations#GET_SIGNED_URL} requests.
* @throws Exception
*/
@Test
public void getAndUseSignedUrlTest() throws Exception {
Account account = ACCOUNT_SERVICE.createAndAddRandomAccount();
Container container = account.getContainerById(Container.DEFAULT_PRIVATE_CONTAINER_ID);
// setup
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(10));
String serviceId = "getAndUseSignedUrlTest";
String contentType = "application/octet-stream";
String ownerId = "getAndUseSignedUrlTest";
HttpHeaders headers = new DefaultHttpHeaders();
headers.add(RestUtils.Headers.URL_TYPE, RestMethod.POST.name());
setAmbryHeadersForPut(headers, TTL_SECS, !container.isCacheable(), serviceId, contentType, ownerId, account.getName(), container.getName());
headers.add(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key1", "value1");
headers.add(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key2", "value2");
// POST
// Get signed URL
FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, Operations.GET_SIGNED_URL, headers, null);
ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
HttpResponse response = getHttpResponse(responseParts);
verifyTrackingHeaders(response);
assertNotNull("There should be a response from the server", response);
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
String signedPostUrl = response.headers().get(RestUtils.Headers.SIGNED_URL);
assertNotNull("Did not get a signed POST URL", signedPostUrl);
assertNoContent(responseParts.queue, 1);
// Use signed URL to POST
URI uri = new URI(signedPostUrl);
httpRequest = buildRequest(HttpMethod.POST, uri.getPath() + "?" + uri.getQuery(), null, content);
responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
String blobId = verifyPostAndReturnBlobId(responseParts, content.capacity(), false);
// verify POST
headers.add(RestUtils.Headers.BLOB_SIZE, content.capacity());
headers.add(RestUtils.Headers.LIFE_VERSION, "0");
getBlobAndVerify(blobId, null, GetOption.None, false, headers, !container.isCacheable(), content, account.getName(), container.getName());
getBlobInfoAndVerify(blobId, GetOption.None, headers, !container.isCacheable(), account.getName(), container.getName(), null);
// GET
// Get signed URL
HttpHeaders getHeaders = new DefaultHttpHeaders();
getHeaders.add(RestUtils.Headers.URL_TYPE, RestMethod.GET.name());
getHeaders.add(RestUtils.Headers.BLOB_ID, addClusterPrefix ? "/" + CLUSTER_NAME + blobId : blobId);
httpRequest = buildRequest(HttpMethod.GET, Operations.GET_SIGNED_URL, getHeaders, null);
responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
response = getHttpResponse(responseParts);
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
verifyTrackingHeaders(response);
String signedGetUrl = response.headers().get(RestUtils.Headers.SIGNED_URL);
assertNotNull("Did not get a signed GET URL", signedGetUrl);
assertNoContent(responseParts.queue, 1);
// Use URL to GET blob
uri = new URI(signedGetUrl);
httpRequest = buildRequest(HttpMethod.GET, uri.getPath() + "?" + uri.getQuery(), null, null);
responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
verifyGetBlobResponse(responseParts, null, false, headers, !container.isCacheable(), content, account.getName(), container.getName());
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class FrontendIntegrationTest method postGetHeadUpdateDeleteUndeleteTest.
/**
* Tests blob POST, GET, HEAD, TTL update and DELETE operations.
* @throws Exception
*/
@Test
public void postGetHeadUpdateDeleteUndeleteTest() throws Exception {
// add some accounts
Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
Container publicContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
Container privateContainer = refAccount.getContainerById(Container.DEFAULT_PRIVATE_CONTAINER_ID);
int refContentSize = (int) FRONTEND_CONFIG.chunkedGetResponseThresholdInBytes * 3;
// with valid account and containers
for (int i = 0; i < 2; i++) {
Account account = ACCOUNT_SERVICE.createAndAddRandomAccount();
for (Container container : account.getAllContainers()) {
doPostGetHeadUpdateDeleteUndeleteTest(refContentSize, account, container, account.getName(), !container.isCacheable(), account.getName(), container.getName(), false);
}
}
// valid account and container names but only serviceId passed as part of POST
doPostGetHeadUpdateDeleteUndeleteTest(refContentSize, null, null, refAccount.getName(), false, refAccount.getName(), publicContainer.getName(), false);
doPostGetHeadUpdateDeleteUndeleteTest(refContentSize, null, null, refAccount.getName(), true, refAccount.getName(), privateContainer.getName(), false);
// unrecognized serviceId
doPostGetHeadUpdateDeleteUndeleteTest(refContentSize, null, null, "unknown_service_id", false, null, null, false);
doPostGetHeadUpdateDeleteUndeleteTest(refContentSize, null, null, "unknown_service_id", true, null, null, false);
// different sizes
for (int contentSize : new int[] { 0, (int) FRONTEND_CONFIG.chunkedGetResponseThresholdInBytes - 1, (int) FRONTEND_CONFIG.chunkedGetResponseThresholdInBytes, refContentSize }) {
doPostGetHeadUpdateDeleteUndeleteTest(contentSize, refAccount, publicContainer, refAccount.getName(), !publicContainer.isCacheable(), refAccount.getName(), publicContainer.getName(), false);
}
}
use of com.github.ambry.account.Account in project ambry by linkedin.
the class FrontendIntegrationTest method emptyBlobRangeHandlingTest.
@Test
public void emptyBlobRangeHandlingTest() throws Exception {
Account refAccount = ACCOUNT_SERVICE.createAndAddRandomAccount();
Container refContainer = refAccount.getContainerById(Container.DEFAULT_PUBLIC_CONTAINER_ID);
HttpHeaders headers = new DefaultHttpHeaders();
setAmbryHeadersForPut(headers, TTL_SECS, !refContainer.isCacheable(), refAccount.getName(), "application/octet-stream", null, refAccount.getName(), refContainer.getName());
ByteBuffer content = ByteBuffer.allocate(0);
String blobId = postBlobAndVerify(headers, content, content.capacity());
ByteRange range = ByteRanges.fromOffsetRange(0, 50);
headers.add(RestUtils.Headers.BLOB_SIZE, content.capacity());
headers.add(RestUtils.Headers.LIFE_VERSION, "0");
// Should get a 416 if x-ambry-resolve-range-on-empty-blob is not set
HttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, new DefaultHttpHeaders().add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range)), null);
ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
HttpResponse response = getHttpResponse(responseParts);
assertEquals("Unexpected response status", HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE, response.status());
// Should get a 206 if the header is set.
getBlobAndVerify(blobId, range, null, true, headers, !refContainer.isCacheable(), content, refAccount.getName(), refContainer.getName());
}
Aggregations