Search in sources :

Example 6 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509RecordNullableColumns.

@Test
public void testUpdateX509RecordNullableColumns() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date();
    X509CertRecord certRecord = getRecordNonNullableColumns(now);
    certRecord.setLastNotifiedTime(null);
    certRecord.setLastNotifiedServer(null);
    certRecord.setExpiryTime(now);
    certRecord.setHostName(null);
    certRecord.setSvcDataUpdateTime(now);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.provider:cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("provider").put(certRecord.getProvider()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("currentSerial").put(certRecord.getCurrentSerial()), new AttributeUpdate("currentIP").put(certRecord.getCurrentIP()), new AttributeUpdate("currentTime").put(certRecord.getCurrentTime().getTime()), new AttributeUpdate("currentDate").put(DynamoDBUtils.getIso8601FromDate(certRecord.getCurrentTime())), new AttributeUpdate("prevSerial").put(certRecord.getPrevSerial()), new AttributeUpdate("prevIP").put(certRecord.getPrevIP()), new AttributeUpdate("prevTime").put(certRecord.getPrevTime().getTime()), new AttributeUpdate("clientCert").put(certRecord.getClientCert()), new AttributeUpdate("ttl").put(certRecord.getCurrentTime().getTime() / 1000L + 3660 * 720), new AttributeUpdate("svcDataUpdateTime").put(certRecord.getSvcDataUpdateTime().getTime()), new AttributeUpdate("expiryTime").put(null));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
    assertTrue(requestSuccess);
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Example 7 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509RecordNoHostName.

@Test
public void testUpdateX509RecordNoHostName() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date();
    X509CertRecord certRecord = getRecordNonNullableColumns(now);
    certRecord.setLastNotifiedTime(now);
    certRecord.setLastNotifiedServer("last-notified-server");
    certRecord.setExpiryTime(now);
    certRecord.setSvcDataUpdateTime(now);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.provider:cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("provider").put(certRecord.getProvider()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("currentSerial").put(certRecord.getCurrentSerial()), new AttributeUpdate("currentIP").put(certRecord.getCurrentIP()), new AttributeUpdate("currentTime").put(certRecord.getCurrentTime().getTime()), new AttributeUpdate("currentDate").put(DynamoDBUtils.getIso8601FromDate(certRecord.getCurrentTime())), new AttributeUpdate("prevSerial").put(certRecord.getPrevSerial()), new AttributeUpdate("prevIP").put(certRecord.getPrevIP()), new AttributeUpdate("prevTime").put(certRecord.getPrevTime().getTime()), new AttributeUpdate("clientCert").put(certRecord.getClientCert()), new AttributeUpdate("ttl").put(certRecord.getCurrentTime().getTime() / 1000L + 3660 * 720), new AttributeUpdate("svcDataUpdateTime").put(certRecord.getSvcDataUpdateTime().getTime()), new AttributeUpdate("expiryTime").put(certRecord.getExpiryTime().getTime()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
    assertTrue(requestSuccess);
    ArgumentCaptor<UpdateItemSpec> itemCaptor = ArgumentCaptor.forClass(UpdateItemSpec.class);
    Mockito.verify(table, times(1)).updateItem(itemCaptor.capture());
    List<UpdateItemSpec> allValues = itemCaptor.getAllValues();
    assertEquals(1, allValues.size());
    UpdateItemSpec capturedItem = allValues.get(0);
    assertEquals(capturedItem.getKeyComponents().toArray()[0].toString(), item.getKeyComponents().toArray()[0].toString());
    List<AttributeUpdate> capturedAttributes = capturedItem.getAttributeUpdate();
    List<AttributeUpdate> expectedAttributes = item.getAttributeUpdate();
    // Check everyone except the hostname (it will be filled with the primaryKey value as the hostName index doesn't allow nulls)
    for (int i = 0; i < capturedAttributes.size() - 1; ++i) {
        System.out.println("expected attr: " + expectedAttributes.get(i).getAttributeName() + ", value: " + expectedAttributes.get(i).getValue());
        assertEquals(capturedAttributes.get(i).getAttributeName(), expectedAttributes.get(i).getAttributeName());
        assertEquals(capturedAttributes.get(i).getValue(), expectedAttributes.get(i).getValue());
    }
    // Make sure hostName received the value of the primaryKey
    System.out.println("expected attr: hostName, value: athenz.provider:cn:1234");
    assertEquals(capturedAttributes.get(capturedAttributes.size() - 1).getAttributeName(), "hostName");
    assertEquals(capturedAttributes.get(capturedAttributes.size() - 1).getValue(), "athenz.provider:cn:1234");
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Example 8 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBSSHRecordStoreConnectionTest method testUpdateSSHRecord.

@Test
public void testUpdateSSHRecord() {
    DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
    SSHCertRecord certRecord = new SSHCertRecord();
    certRecord.setInstanceId("1234");
    certRecord.setService("cn");
    certRecord.setPrincipals("host1,host2");
    certRecord.setClientIP("10.10.10.11");
    certRecord.setPrivateIP("10.10.10.12");
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("principals").put(certRecord.getPrincipals()), new AttributeUpdate("clientIP").put(certRecord.getClientIP()), new AttributeUpdate("privateIP").put(certRecord.getPrivateIP()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateSSHCertRecord(certRecord);
    assertTrue(requestSuccess);
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) SSHCertRecord(com.yahoo.athenz.common.server.ssh.SSHCertRecord) Test(org.testng.annotations.Test)

Example 9 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509Record.

@Test
public void testUpdateX509Record() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date();
    X509CertRecord certRecord = getRecordNonNullableColumns(now);
    certRecord.setLastNotifiedTime(now);
    certRecord.setLastNotifiedServer("last-notified-server");
    certRecord.setExpiryTime(now);
    certRecord.setHostName("hostname");
    certRecord.setSvcDataUpdateTime(now);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.provider:cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("provider").put(certRecord.getProvider()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("currentSerial").put(certRecord.getCurrentSerial()), new AttributeUpdate("currentIP").put(certRecord.getCurrentIP()), new AttributeUpdate("currentTime").put(certRecord.getCurrentTime().getTime()), new AttributeUpdate("currentDate").put(DynamoDBUtils.getIso8601FromDate(certRecord.getCurrentTime())), new AttributeUpdate("prevSerial").put(certRecord.getPrevSerial()), new AttributeUpdate("prevIP").put(certRecord.getPrevIP()), new AttributeUpdate("prevTime").put(certRecord.getPrevTime().getTime()), new AttributeUpdate("clientCert").put(certRecord.getClientCert()), new AttributeUpdate("ttl").put(certRecord.getCurrentTime().getTime() / 1000L + 3660 * 720), new AttributeUpdate("svcDataUpdateTime").put(certRecord.getSvcDataUpdateTime().getTime()), new AttributeUpdate("expiryTime").put(certRecord.getExpiryTime().getTime()), new AttributeUpdate("hostName").put(certRecord.getHostName()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
    assertTrue(requestSuccess);
    ArgumentCaptor<UpdateItemSpec> itemCaptor = ArgumentCaptor.forClass(UpdateItemSpec.class);
    Mockito.verify(table, times(1)).updateItem(itemCaptor.capture());
    List<UpdateItemSpec> allValues = itemCaptor.getAllValues();
    assertEquals(1, allValues.size());
    UpdateItemSpec capturedItem = allValues.get(0);
    assertEquals(capturedItem.getKeyComponents().toArray()[0].toString(), item.getKeyComponents().toArray()[0].toString());
    List<AttributeUpdate> capturedAttributes = capturedItem.getAttributeUpdate();
    List<AttributeUpdate> expectedAttributes = item.getAttributeUpdate();
    for (int i = 0; i < expectedAttributes.size(); ++i) {
        System.out.println("expected attr: " + expectedAttributes.get(i).getAttributeName() + ", value: " + expectedAttributes.get(i).getValue());
        assertEquals(capturedAttributes.get(i).getAttributeName(), expectedAttributes.get(i).getAttributeName());
        assertEquals(capturedAttributes.get(i).getValue(), expectedAttributes.get(i).getValue());
    }
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Example 10 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBWorkloadRecordStoreConnection method updateWorkloadRecord.

@Override
public boolean updateWorkloadRecord(WorkloadRecord workloadRecord) {
    // updateItem does not fail on absence of primaryKey, and behaves as insert. So we should set all attributes with update too.
    try {
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(KEY_PRIMARY, getPrimaryKey(workloadRecord.getService(), workloadRecord.getInstanceId(), workloadRecord.getIp())).withAttributeUpdate(new AttributeUpdate(KEY_SERVICE).put(workloadRecord.getService()), new AttributeUpdate(KEY_PROVIDER).put(workloadRecord.getProvider()), new AttributeUpdate(KEY_IP).put(workloadRecord.getIp()), new AttributeUpdate(KEY_INSTANCE_ID).put(workloadRecord.getInstanceId()), new AttributeUpdate(KEY_CREATION_TIME).put(DynamoDBUtils.getLongFromDate(workloadRecord.getCreationTime())), new AttributeUpdate(KEY_UPDATE_TIME).put(DynamoDBUtils.getLongFromDate(workloadRecord.getUpdateTime())), new AttributeUpdate(KEY_EXPIRY_TIME).put(DynamoDBUtils.getLongFromDate(workloadRecord.getCertExpiryTime())), new AttributeUpdate(KEY_HOSTNAME).put(workloadRecord.getHostname()), new AttributeUpdate(KEY_TTL).put(workloadRecord.getUpdateTime().getTime() / 1000L + expiryTime));
        updateItemRetryDynamoDBCommand.run(() -> table.updateItem(updateItemSpec));
        return true;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB Workload update Error={}: {}/{}", workloadRecord, ex.getClass(), ex.getMessage());
        return false;
    }
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)

Aggregations

UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)10 Test (org.testng.annotations.Test)5 AttributeUpdate (com.amazonaws.services.dynamodbv2.document.AttributeUpdate)3 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)3 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)3 Expected (com.amazonaws.services.dynamodbv2.document.Expected)2 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)1 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)1 Date (java.util.Date)1