use of com.github.ambry.account.Container in project ambry by linkedin.
the class FrontendTestUrlSigningServiceFactory method injectionAccountAndContainerForGetHeadDeleteBlobIdTest.
/**
* Tests injecting target {@link Account} and {@link Container} for GET/HEAD/DELETE blobId string in {@link BlobId#BLOB_ID_V2}.
* The {@link AccountService} is prepopulated with a reference account and {@link InMemAccountService#UNKNOWN_ACCOUNT}. The expected
* behavior should be:
*
* <pre>
* AId in blobId CId in blobId expected Error injected account injected container
* realAId realCId NotFound refAccount refContainer This can succeed if the blob exists in backend.
* realAId UNKNOWN InvalidContainer null null
* realAId nonExistCId InvalidContainer null null
* UNKNOWN realCId InvalidContainer null null
* UNKNOWN UNKNOWN NotFound UNKNOWN UNKNOWN This can succeed if the blob exists in backend.
* UNKNOWN nonExistCId InvalidContainer null null
* nonExistAId realCId InvalidAccount null null
* nonExistAId UNKNOWN InvalidAccount null null
* nonExistAId nonExistCId InvalidAccount null null
* </pre>
*
* @throws Exception
*/
@Test
public void injectionAccountAndContainerForGetHeadDeleteBlobIdTest() throws Exception {
List<Short> blobIdVersions = Arrays.stream(BlobId.getAllValidVersions()).filter(version -> version >= BlobId.BLOB_ID_V2).collect(Collectors.toList());
for (short version : blobIdVersions) {
populateAccountService();
// aid=refAId, cid=refCId
String blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, refAccount.getId(), refContainer.getId(), clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, refAccount, refContainer, RestServiceErrorCode.NotFound);
// aid=refAId, cid=unknownCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, refAccount.getId(), Container.UNKNOWN_CONTAINER_ID, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidContainer);
// aid=refAId, cid=nonExistCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, refAccount.getId(), (short) -1234, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidContainer);
// aid=unknownAId, cid=refCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, refContainer.getId(), clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidContainer);
// aid=unknownAId, cid=unknownCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, InMemAccountService.UNKNOWN_ACCOUNT, Container.UNKNOWN_CONTAINER, RestServiceErrorCode.NotFound);
// aid=unknownAId, cid=nonExistCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, (short) -1234, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidContainer);
// aid=nonExistAId, cid=refCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, (short) -1234, refContainer.getId(), clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidAccount);
// aid=nonExistAId, cid=unknownCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, (short) -1234, Container.UNKNOWN_CONTAINER_ID, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidAccount);
// aid=nonExistAId, cid=nonExistCId
blobId = new BlobId(version, BlobId.BlobIdType.NATIVE, ClusterMap.UNKNOWN_DATACENTER_ID, (short) -1234, (short) -11, clusterMap.getWritablePartitionIds(MockClusterMap.DEFAULT_PARTITION_CLASS).get(0), false, BlobId.BlobDataType.DATACHUNK).getID();
verifyAccountAndContainerFromBlobId(blobId, null, null, RestServiceErrorCode.InvalidAccount);
}
}
use of com.github.ambry.account.Container 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.Container 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.Container 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.Container 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);
}
}
Aggregations