Skip to content

Commit

Permalink
SQL Server support for RDS broker (#58)
Browse files Browse the repository at this point in the history
* SQL Server support for RDS broker
- Added SqlServerClient
- Pushed defaults into the RDS clients

* SQL Server support for RDS broker
- removing unused variable
  • Loading branch information
CWDoherty authored and fritzdj committed Dec 8, 2018
1 parent a081886 commit 34c45c8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

import static com.libertymutualgroup.herman.aws.ecs.broker.rds.RdsBroker.pollingIntervalMs;

public class AuroraClient implements RdsClient {
public class AuroraClient extends StandardRdsClient {

public static final String AVAILABLE_STATUS = "available";
public static final String INTERRUPTED_DURING_PROCESS = "Interrupted during process";
Expand All @@ -76,6 +76,7 @@ public class AuroraClient implements RdsClient {

AuroraClient(AmazonRDS client, RdsInstance rds, EcsClusterMetadata clusterMetadata, List<HermanTag> tags,
HermanLogger buildLogger) {
super(client, rds, clusterMetadata, tags, buildLogger);
this.client = client;
this.rds = rds;
this.clusterMetadata = clusterMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@

public class RdsBroker {

public static final String AURORA_ENGINE = "aurora";
public static final String POSTGRES_ENGINE = "postgres";
public static final String MYSQL_ENGINE = "mysql";
public static final String CIPHER_PREFIX = "{cipher}";
private static final String AURORA_ENGINE = "aurora";
private static final String POSTGRES_ENGINE = "postgres";
private static final String MYSQL_ENGINE = "mysql";
private static final String CIPHER_PREFIX = "{cipher}";
private static final int MYSQL_MAXLENGTH = 32;
static int pollingIntervalMs = 10000;
private AmazonRDS client;
Expand Down Expand Up @@ -82,7 +82,6 @@ public RdsBroker(EcsPushContext pushContext, AmazonRDS client, AWSKMS kmsClient,

public RdsInstance brokerDb() {
RdsInstance rds = definition.getDatabase();
rds.setDefaults(targetKeyId);
String instanceId = rds.getDBInstanceIdentifier() != null ? rds.getDBInstanceIdentifier()
: definition.getAppName();
String masterUserPassword = this.generateRandomPassword();
Expand All @@ -102,18 +101,21 @@ public RdsInstance brokerDb() {
tags = TagUtil.mergeTags(tags, definition.getTags());
}


String encryptedPassword;
RdsClient rdsClient;

if (rds.getEngine().contains(AURORA_ENGINE)) {
rdsClient = new AuroraClient(client, rds, clusterMetadata, tags, logger);
} else if (rds.getEngine().contains("oracle")) {
rdsClient = new OracleClient(client, rds, clusterMetadata, tags, logger);
} else if (rds.getEngine().contains("sqlserver")) {
rdsClient = new SqlServerClient(client, rds, clusterMetadata, tags, logger);
} else {
rdsClient = new StandardRdsClient(client, rds, clusterMetadata, tags, logger);
}

rdsClient.setDefaults(targetKeyId);

boolean newDb = !rdsClient.dbExists(instanceId);

if (!newDb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public interface RdsClient {
void setOptionGroup(String instanceId, OptionGroup options);

void createSnapshot(String instanceId, String snapshotId);

void setDefaults(String kmsKeyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,6 @@ public class RdsInstance extends DBInstance {
private Boolean preDeployBackup;
private List<String> extensions; // Postgres-specific (optional)

public void setDefaults(String kmsKeyId) {
this.setInjectNames(getInjectNames() == null ? new RdsInjectConfiguration() : getInjectNames());

injectNames.setDefaults();

this.setMasterUsername(getMasterUsername() == null ? "dbAdmin" : getMasterUsername());
this.setDBName(getDBName() == null ? "appDb" : getDBName());
this.setDBInstanceClass(getDBInstanceClass() == null ? "db.t2.small" : getDBInstanceClass());
this.setAllocatedStorage(getAllocatedStorage() == null ? 5 : getAllocatedStorage());
this.setPubliclyAccessible(getPubliclyAccessible() == null ? false : getPubliclyAccessible());
this.setAutoMinorVersionUpgrade(getAutoMinorVersionUpgrade() == null ? true : getAutoMinorVersionUpgrade());
this.setPreferredBackupWindow(getPreferredBackupWindow() == null ? "01:00-02:00" : getPreferredBackupWindow());
this.setPreferredMaintenanceWindow(
getPreferredMaintenanceWindow() == null ? "sun:20:00-sun:21:00" : getPreferredMaintenanceWindow());
this.setBackupRetentionPeriod(getBackupRetentionPeriod() == null ? 14 : getBackupRetentionPeriod());
this.setStorageType(getStorageType() == null ? "gp2" : getStorageType());
this.setFullUpdate(getFullUpdate() == null ? false : getFullUpdate());
this.setIAMDatabaseAuthenticationEnabled(
getIAMDatabaseAuthenticationEnabled() == null ? false : getIAMDatabaseAuthenticationEnabled());
this.setAvailabilityZones(getAvailabilityZones() == null ? new String[]{"us-east-1a"} : getAvailabilityZones());
this.setPreDeployBackup(getPreDeployBackup() == null ? false : getPreDeployBackup());
this.setExtensions(getExtensions() == null ? new ArrayList<>() : getExtensions());
this.setKmsKeyId(getKmsKeyId() == null ? kmsKeyId : getKmsKeyId());
}

public String getConnectionString() {
String instanceType = this.getEngine().toLowerCase();
String connectionType = instanceType.contains("postgres") ? "postgresql" : instanceType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.libertymutualgroup.herman.aws.ecs.broker.rds;

import com.amazonaws.services.rds.AmazonRDS;
import com.libertymutualgroup.herman.aws.ecs.cluster.EcsClusterMetadata;
import com.libertymutualgroup.herman.aws.tags.HermanTag;
import com.libertymutualgroup.herman.logging.HermanLogger;
import java.util.ArrayList;
import java.util.List;

public class SqlServerClient extends StandardRdsClient {

private AmazonRDS client;
private RdsInstance rds;
private EcsClusterMetadata clusterMetadata;
private List<HermanTag> tags;
private HermanLogger logger;

SqlServerClient(AmazonRDS client, RdsInstance rds, EcsClusterMetadata clusterMetadata, List<HermanTag> tags,
HermanLogger logger) {
super(client, rds, clusterMetadata, tags, logger);
this.client = client;
this.rds = rds;
this.clusterMetadata = clusterMetadata;
this.tags = tags;
this.logger = logger;
}

@Override
public void setDefaults(String kmsKeyId) {
rds.setInjectNames(rds.getInjectNames() == null ? new RdsInjectConfiguration() : rds.getInjectNames());

rds.getInjectNames().setDefaults();

rds.setMasterUsername(rds.getMasterUsername() == null ? "dbAdmin" : rds.getMasterUsername());
rds.setDBInstanceClass(rds.getDBInstanceClass() == null ? "db.m4.large" : rds.getDBInstanceClass());
rds.setAllocatedStorage(rds.getAllocatedStorage() == null ? 200 : rds.getAllocatedStorage());
rds.setPubliclyAccessible(rds.getPubliclyAccessible() == null ? false : rds.getPubliclyAccessible());
rds.setAutoMinorVersionUpgrade(rds.getAutoMinorVersionUpgrade() == null ? true : rds.getAutoMinorVersionUpgrade());
rds.setPreferredBackupWindow(rds.getPreferredBackupWindow() == null ? "01:00-02:00" : rds.getPreferredBackupWindow());
rds.setPreferredMaintenanceWindow(
rds.getPreferredMaintenanceWindow() == null ? "sun:20:00-sun:21:00" : rds.getPreferredMaintenanceWindow());
rds.setBackupRetentionPeriod(rds.getBackupRetentionPeriod() == null ? 14 : rds.getBackupRetentionPeriod());
rds.setStorageType(rds.getStorageType() == null ? "gp2" : rds.getStorageType());
rds.setFullUpdate(rds.getFullUpdate() == null ? false : rds.getFullUpdate());
rds.setIAMDatabaseAuthenticationEnabled(
rds.getIAMDatabaseAuthenticationEnabled() == null ? false : rds.getIAMDatabaseAuthenticationEnabled());
rds.setAvailabilityZones(rds.getAvailabilityZones() == null ? new String[]{"us-east-1a"} : rds.getAvailabilityZones());
rds.setPreDeployBackup(rds.getPreDeployBackup() == null ? false : rds.getPreDeployBackup());
rds.setExtensions(rds.getExtensions() == null ? new ArrayList<>() : rds.getExtensions());
rds.setKmsKeyId(rds.getKmsKeyId() == null ? kmsKeyId : rds.getKmsKeyId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,32 @@ public void createSnapshot(String instanceId, String snapshotId) {
}
}

@Override
public void setDefaults(String kmsKeyId) {
rds.setInjectNames(rds.getInjectNames() == null ? new RdsInjectConfiguration() : rds.getInjectNames());

rds.getInjectNames().setDefaults();

rds.setMasterUsername(rds.getMasterUsername() == null ? "dbAdmin" : rds.getMasterUsername());
rds.setDBName(rds.getDBName() == null ? "appDb" : rds.getDBName());
rds.setDBInstanceClass(rds.getDBInstanceClass() == null ? "db.t2.small" : rds.getDBInstanceClass());
rds.setAllocatedStorage(rds.getAllocatedStorage() == null ? 5 : rds.getAllocatedStorage());
rds.setPubliclyAccessible(rds.getPubliclyAccessible() == null ? false : rds.getPubliclyAccessible());
rds.setAutoMinorVersionUpgrade(rds.getAutoMinorVersionUpgrade() == null ? true : rds.getAutoMinorVersionUpgrade());
rds.setPreferredBackupWindow(rds.getPreferredBackupWindow() == null ? "01:00-02:00" : rds.getPreferredBackupWindow());
rds.setPreferredMaintenanceWindow(
rds.getPreferredMaintenanceWindow() == null ? "sun:20:00-sun:21:00" : rds.getPreferredMaintenanceWindow());
rds.setBackupRetentionPeriod(rds.getBackupRetentionPeriod() == null ? 14 : rds.getBackupRetentionPeriod());
rds.setStorageType(rds.getStorageType() == null ? "gp2" : rds.getStorageType());
rds.setFullUpdate(rds.getFullUpdate() == null ? false : rds.getFullUpdate());
rds.setIAMDatabaseAuthenticationEnabled(
rds.getIAMDatabaseAuthenticationEnabled() == null ? false : rds.getIAMDatabaseAuthenticationEnabled());
rds.setAvailabilityZones(rds.getAvailabilityZones() == null ? new String[]{"us-east-1a"} : rds.getAvailabilityZones());
rds.setPreDeployBackup(rds.getPreDeployBackup() == null ? false : rds.getPreDeployBackup());
rds.setExtensions(rds.getExtensions() == null ? new ArrayList<>() : rds.getExtensions());
rds.setKmsKeyId(rds.getKmsKeyId() == null ? kmsKeyId : rds.getKmsKeyId());
}

private DescribeDBSnapshotsResult getDescribeDBSnapshotsResult(String snapshotId) throws InterruptedException {
String status = "";
DescribeDBSnapshotsResult result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.libertymutualgroup.herman.aws.ecs.broker.rds.RdsCommonTestObjects.initDbInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -86,6 +87,8 @@ private RdsClient initClient(EcsPushDefinition definition, ArrayList<HermanTag>
rdsClient = new AuroraClient(client, definition.getDatabase(), clusterMetadata, tags, logger);
} else if (definition.getDatabase().getEngine().contains("oracle")) {
rdsClient = new OracleClient(client, definition.getDatabase(), clusterMetadata, tags, logger);
} else if (definition.getDatabase().getEngine().contains("sqlserver")) {
rdsClient = new SqlServerClient(client, definition.getDatabase(), clusterMetadata, tags, logger);
} else {
rdsClient = new StandardRdsClient(client, definition.getDatabase(), clusterMetadata, tags, logger);
}
Expand Down Expand Up @@ -202,4 +205,55 @@ public void shouldUseCredPrefixWhenSpecified() {
assertTrue(rdsResult.getAdminEncryptedPassword().startsWith(prefix));
assertTrue(rdsResult.getEncryptedPassword().startsWith(prefix));
}

@Test
public void dbNameNullForSQLServer() {
EcsPushDefinition definition = new EcsPushDefinition();
RdsInstance instance = initSqlServerInstanceDefinition();
definition.setDatabase(instance);

RdsBroker broker = initBroker(definition);
mockEncryptionResult("123");
definition.setUseKms(Boolean.TRUE.toString());

DescribeDBInstancesResult result = new DescribeDBInstancesResult();
result.setDBInstances(Arrays.asList(initDbInstance()));

Mockito.when(client.describeDBInstances(any()))
.thenThrow(new DBInstanceNotFoundException(""))
.thenReturn(result);

RdsInstance rdsResult = broker.brokerDb();
assertNull(rdsResult.getDBName());
}

@Test
public void dbNameNotNullForMySql() {
EcsPushDefinition definition = new EcsPushDefinition();
RdsInstance instance = initInstanceDefinition();
definition.setDatabase(instance);

RdsBroker broker = initBroker(definition);
mockEncryptionResult("123");
definition.setUseKms(Boolean.TRUE.toString());

DescribeDBInstancesResult result = new DescribeDBInstancesResult();
result.setDBInstances(Arrays.asList(initDbInstance()));

Mockito.when(client.describeDBInstances(any()))
.thenThrow(new DBInstanceNotFoundException(""))
.thenReturn(result);

RdsInstance rdsResult = broker.brokerDb();
assertNotNull(rdsResult.getDBName());
assertEquals("appDb", rdsResult.getDBName());
}

private RdsInstance initSqlServerInstanceDefinition() {
RdsInstance instance = new RdsInstance();
instance.setEngine("sqlserver-se");
instance.setEngineVersion("13.00.4466.4.v1");

return instance;
}
}

0 comments on commit 34c45c8

Please sign in to comment.