Search in sources :

Example 1 with ContainerBuilder

use of com.github.ambry.account.ContainerBuilder in project ambry by linkedin.

the class AccountDao method convertContainersResultSet.

/**
 * Convert a query result set to a list of containers.
 * @param resultSet the result set.
 * @return a list of {@link Container}s.
 * @throws SQLException
 */
private List<Container> convertContainersResultSet(ResultSet resultSet) throws SQLException {
    List<Container> containers = new ArrayList<>();
    while (resultSet.next()) {
        int accountId = resultSet.getInt(ACCOUNT_ID);
        String containerJson = resultSet.getString(CONTAINER_INFO);
        Timestamp lastModifiedTime = resultSet.getTimestamp(LAST_MODIFIED_TIME);
        int version = resultSet.getInt(VERSION);
        try {
            Container deserialized = objectMapper.readValue(containerJson, Container.class);
            Container container = new ContainerBuilder(deserialized).setParentAccountId((short) accountId).setLastModifiedTime(lastModifiedTime.getTime()).setSnapshotVersion(version).build();
            containers.add(container);
        } catch (IOException e) {
            throw new SQLException(e);
        }
    }
    return containers;
}
Also used : Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Timestamp(java.sql.Timestamp)

Example 2 with ContainerBuilder

use of com.github.ambry.account.ContainerBuilder in project ambry by linkedin.

the class FrontendIntegrationTest method accountApiTest.

/**
 * Tests for the account/container get/update API.
 */
@Test
public void accountApiTest() throws Exception {
    verifyGetAccountsAndContainer();
    // update and add accounts
    Map<Short, Account> accountsById = ACCOUNT_SERVICE.getAllAccounts().stream().collect(Collectors.toMap(Account::getId, Function.identity()));
    Account editedAccount = accountsById.values().stream().findAny().get();
    Container editedContainer = editedAccount.getAllContainers().stream().findAny().get();
    editedContainer = new ContainerBuilder(editedContainer).setDescription("new description abcdefgh").build();
    editedAccount = new AccountBuilder(editedAccount).addOrUpdateContainer(editedContainer).build();
    updateAccountsAndVerify(ACCOUNT_SERVICE, editedAccount, ACCOUNT_SERVICE.generateRandomAccount());
    verifyGetAccountsAndContainer();
    // Test adding a container to the account
    Container newContainer = ACCOUNT_SERVICE.getRandomContainer(editedAccount.getId());
    updateContainersAndVerify(editedAccount, newContainer);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test)

Example 3 with ContainerBuilder

use of com.github.ambry.account.ContainerBuilder in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method containerMetricsExclusionTest.

/**
 * Tests that container metrics are not generated when the target account is in the excluded list.
 * @throws Exception
 */
@Test
public void containerMetricsExclusionTest() throws Exception {
    short excludedAccountId = Utils.getRandomShort(TestUtils.RANDOM);
    short containerId = 2;
    String containerName = "tenant1";
    Container container = new ContainerBuilder(containerId, containerName, Container.ContainerStatus.ACTIVE, "test", excludedAccountId).build();
    Account excludedAccount = new AccountBuilder(excludedAccountId, excludedAccountName, Account.AccountStatus.ACTIVE).addOrUpdateContainer(container).build();
    accountService.updateAccounts(Collections.singletonList(excludedAccount));
    postBlobAndVerifyWithAccountAndContainer(excludedAccountName, containerName, "serviceId", !container.isCacheable(), excludedAccount, container, null);
}
Also used : Account(com.github.ambry.account.Account) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 4 with ContainerBuilder

use of com.github.ambry.account.ContainerBuilder in project ambry by linkedin.

the class FrontendTestUrlSigningServiceFactory method validateSecurePathTest.

/**
 * Test that the secure path is validated if required by {@link Container}.
 * @throws Exception
 */
@Test
public void validateSecurePathTest() throws Exception {
    short refAccountId = Utils.getRandomShort(TestUtils.RANDOM);
    String refAccountName = TestUtils.getRandomString(10);
    short[] refContainerIds = new short[] { 2, 3 };
    String[] refContainerNames = new String[] { "SecurePathValidation", "NoValidation" };
    Container signedPathRequiredContainer = new ContainerBuilder(refContainerIds[0], refContainerNames[0], Container.ContainerStatus.ACTIVE, "validate secure path", refAccountId).setSecurePathRequired(true).build();
    Container noValidationContainer = new ContainerBuilder(refContainerIds[1], refContainerNames[1], Container.ContainerStatus.ACTIVE, "no validation on secure path", refAccountId).setSecurePathRequired(false).build();
    Account account = new AccountBuilder(refAccountId, refAccountName, Account.AccountStatus.ACTIVE).addOrUpdateContainer(signedPathRequiredContainer).addOrUpdateContainer(noValidationContainer).build();
    accountService.updateAccounts(Collections.singletonList(account));
    ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(CONTENT_LENGTH));
    String contentType = "application/octet-stream";
    String ownerId = "SecurePathValidationTest";
    JSONObject headers = new JSONObject();
    setAmbryHeadersForPut(headers, TTL_SECS, false, refAccountName, contentType, ownerId, refAccountName, refContainerNames[0], null);
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX + "key1", "value1");
    RestUtilsTest.setUserMetadataHeaders(headers, userMetadata);
    String blobId = postBlobAndVerify(headers, content, account, signedPathRequiredContainer);
    headers.put(RestUtils.Headers.BLOB_SIZE, (long) CONTENT_LENGTH);
    // test that secure path validation succeeded
    String testUri = "/" + frontendConfig.securePathPrefix + blobId;
    getBlobAndVerify(testUri, null, null, headers, content, account, signedPathRequiredContainer);
    // test that no secure path should fail (return AccessDenied)
    try {
        getBlobAndVerify(blobId, null, null, headers, content, account, signedPathRequiredContainer);
        fail("get blob should fail because secure path is missing");
    } catch (RestServiceException e) {
        assertEquals("Mismatch in error code", RestServiceErrorCode.AccessDenied, e.getErrorCode());
    }
    // test that secure path equals other prefix should fail (return AccessDenied)
    try {
        getBlobAndVerify("/media" + blobId, null, null, headers, content, account, signedPathRequiredContainer);
        fail("get blob should fail because secure path equals other prefix and doesn't match expected one");
    } catch (RestServiceException e) {
        assertEquals("Mismatch in error code", RestServiceErrorCode.AccessDenied, e.getErrorCode());
    }
    // test that incorrect path should fail (return BadRequest)
    try {
        getBlobAndVerify("/incorrect-path" + blobId, null, null, headers, content, account, signedPathRequiredContainer);
        fail("get blob should fail because secure path is incorrect");
    } catch (RestServiceException e) {
        assertEquals("Mismatch in error code", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    // test container with no validation
    setAmbryHeadersForPut(headers, TTL_SECS, false, refAccountName, contentType, ownerId, refAccountName, refContainerNames[1], null);
    content = ByteBuffer.wrap(TestUtils.getRandomBytes(CONTENT_LENGTH));
    blobId = postBlobAndVerify(headers, content, account, noValidationContainer);
    // test container with no validation should fail if there is invalid path in URI
    try {
        getBlobAndVerify("/incorrect-path" + blobId, null, null, headers, content, account, noValidationContainer);
        fail("get blob should fail because there is invalid path in uri");
    } catch (RestServiceException e) {
        assertEquals("Mismatch in error code", RestServiceErrorCode.BadRequest, e.getErrorCode());
    }
    // test container with no validation should succeed if URI is correct
    getBlobAndVerify(blobId, null, null, headers, content, account, noValidationContainer);
}
Also used : Account(com.github.ambry.account.Account) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) RestServiceException(com.github.ambry.rest.RestServiceException) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) JSONObject(org.json.JSONObject) AccountBuilder(com.github.ambry.account.AccountBuilder) Test(org.junit.Test) RestUtilsTest(com.github.ambry.rest.RestUtilsTest) StorageStatsUtilTest(com.github.ambry.server.StorageStatsUtilTest)

Example 5 with ContainerBuilder

use of com.github.ambry.account.ContainerBuilder in project ambry by linkedin.

the class RouterUtilsTest method testGetAccountContainer.

/**
 * Test {@link RouterUtils#getAccountContainer(AccountService, short, short)}.
 */
@Test
public void testGetAccountContainer() throws AccountServiceException {
    AccountService accountService = new InMemAccountService(false, false);
    // Both accountId and containerId are not tracked by AccountService.
    Pair<Account, Container> accountContainer = RouterUtils.getAccountContainer(accountService, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID);
    Assert.assertEquals("Account should be null", null, accountContainer.getFirst());
    Assert.assertEquals("Container should be null", null, accountContainer.getSecond());
    accountContainer = RouterUtils.getAccountContainer(accountService, Utils.getRandomShort(random), Utils.getRandomShort(random));
    Assert.assertEquals("Account should be null", null, accountContainer.getFirst());
    Assert.assertEquals("Container should be null", null, accountContainer.getSecond());
    // accountId is tracked by AccountService but containerId not.
    short accountId = Utils.getRandomShort(random);
    short containerId = Utils.getRandomShort(random);
    Account account = new AccountBuilder(accountId, "AccountNameOf" + accountId, Account.AccountStatus.ACTIVE).build();
    accountService.updateAccounts(Arrays.asList(account));
    accountContainer = RouterUtils.getAccountContainer(accountService, accountId, containerId);
    Assert.assertEquals("Account doesn't match", account, accountContainer.getFirst());
    Assert.assertEquals("Container should be null", null, accountContainer.getSecond());
    // Both accountId and containerId are tracked by AccountService.
    Container container = new ContainerBuilder(containerId, "ContainerNameOf" + containerId, Container.ContainerStatus.ACTIVE, "description", accountId).build();
    account = new AccountBuilder(accountId, "AccountNameOf" + accountId, Account.AccountStatus.ACTIVE).addOrUpdateContainer(container).build();
    accountService.updateAccounts(Arrays.asList(account));
    accountContainer = RouterUtils.getAccountContainer(accountService, accountId, containerId);
    Assert.assertEquals("Account doesn't match", account, accountContainer.getFirst());
    Assert.assertEquals("Container doesn't match", container, accountContainer.getSecond());
}
Also used : Account(com.github.ambry.account.Account) InMemAccountService(com.github.ambry.account.InMemAccountService) Container(com.github.ambry.account.Container) ContainerBuilder(com.github.ambry.account.ContainerBuilder) AccountBuilder(com.github.ambry.account.AccountBuilder) AccountService(com.github.ambry.account.AccountService) InMemAccountService(com.github.ambry.account.InMemAccountService) Test(org.junit.Test)

Aggregations

ContainerBuilder (com.github.ambry.account.ContainerBuilder)19 Container (com.github.ambry.account.Container)13 AccountBuilder (com.github.ambry.account.AccountBuilder)12 Account (com.github.ambry.account.Account)11 Test (org.junit.Test)11 InMemAccountService (com.github.ambry.account.InMemAccountService)6 ArrayList (java.util.ArrayList)5 VerifiableProperties (com.github.ambry.config.VerifiableProperties)4 QuotaResourceType (com.github.ambry.quota.QuotaResourceType)4 HashMap (java.util.HashMap)4 FrontendConfig (com.github.ambry.config.FrontendConfig)3 QuotaResource (com.github.ambry.quota.QuotaResource)3 MockRestRequest (com.github.ambry.rest.MockRestRequest)2 RestRequest (com.github.ambry.rest.RestRequest)2 RestServiceException (com.github.ambry.rest.RestServiceException)2 RestUtilsTest (com.github.ambry.rest.RestUtilsTest)2 StorageStatsUtilTest (com.github.ambry.server.StorageStatsUtilTest)2 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2