Skip to content

Commit

Permalink
Merge 1391318 into d0f788d
Browse files Browse the repository at this point in the history
  • Loading branch information
speedlog committed Jun 21, 2018
2 parents d0f788d + 1391318 commit bf98662
Show file tree
Hide file tree
Showing 19 changed files with 115 additions and 62 deletions.
Expand Up @@ -10,6 +10,9 @@
import pl.gov.coi.cascades.server.domain.launchdatabase.UsernameAndPasswordCredentialsGeneratorService;
import pl.gov.coi.cascades.server.domain.loadtemplate.TemplateIdGeneratorService;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
* @author <a href="agnieszka.celuch@coi.gov.pl">Agnieszka Celuch</a>
* @since 02.03.17.
Expand All @@ -33,8 +36,13 @@ DatabaseNameGeneratorService produceDatabaseNameGeneratorService() {
}

@Bean
UsernameAndPasswordCredentialsGeneratorService produceCredentials() {
return new UsernameAndPasswordCredentialsGeneratorService();
UsernameAndPasswordCredentialsGeneratorService produceCredentials(Random random) {
return new UsernameAndPasswordCredentialsGeneratorService(random);
}

@Bean
Random produceRandomGenerator() {
return ThreadLocalRandom.current();
}

@Bean
Expand Down
Expand Up @@ -12,6 +12,10 @@
import pl.gov.coi.cascades.server.domain.launchdatabase.UsernameAndPasswordCredentialsGeneratorService;
import pl.gov.coi.cascades.server.domain.loadtemplate.TemplateIdGeneratorService;

import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -26,11 +30,10 @@ public class PresentationConfigurationTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Test
public void testProduceTemplateIdGeneratorService() throws Exception {
// given
PresentationConfiguration presentationConfiguration = new PresentationConfiguration();
private final PresentationConfiguration presentationConfiguration = new PresentationConfiguration();

@Test
public void testProduceTemplateIdGeneratorService() {
// when
TemplateIdGeneratorService actual = presentationConfiguration.produceTemplateIdGeneratorService();

Expand All @@ -40,10 +43,7 @@ public void testProduceTemplateIdGeneratorService() throws Exception {
}

@Test
public void testProduceDatabaseIdGeneratorService() throws Exception {
// given
PresentationConfiguration presentationConfiguration = new PresentationConfiguration();

public void testProduceDatabaseIdGeneratorService() {
// when
DatabaseIdGeneratorService actual = presentationConfiguration.produceDatabaseIdGeneratorService();

Expand All @@ -53,10 +53,7 @@ public void testProduceDatabaseIdGeneratorService() throws Exception {
}

@Test
public void testProduceDatabaseNameGeneratorService() throws Exception {
// given
PresentationConfiguration presentationConfiguration = new PresentationConfiguration();

public void testProduceDatabaseNameGeneratorService() {
// when
DatabaseNameGeneratorService actual = presentationConfiguration.produceDatabaseNameGeneratorService();

Expand All @@ -66,23 +63,27 @@ public void testProduceDatabaseNameGeneratorService() throws Exception {
}

@Test
public void testProduceCredentials() throws Exception {
// given
PresentationConfiguration presentationConfiguration = new PresentationConfiguration();

public void testProduceCredentials() {
// when
UsernameAndPasswordCredentialsGeneratorService actual = presentationConfiguration.produceCredentials();
UsernameAndPasswordCredentialsGeneratorService actual = presentationConfiguration.produceCredentials(new SecureRandom());

// then
assertThat(actual).isNotNull();
assertThat(actual).isInstanceOf(UsernameAndPasswordCredentialsGeneratorService.class);
}

@Test
public void testProduceDatabaseTypeClassName() throws Exception {
// given
PresentationConfiguration presentationConfiguration = new PresentationConfiguration();
public void testProduceRandomGenerator() {
// when
Random actual = presentationConfiguration.produceRandomGenerator();

// then
assertThat(actual).isNotNull();
assertThat(actual).isInstanceOf(ThreadLocalRandom.class);
}

@Test
public void testProduceDatabaseTypeClassName() {
// when
DatabaseTypeClassNameService actual = presentationConfiguration.produceDatabaseTypeClassName(
osgiBeanLocator
Expand Down
4 changes: 4 additions & 0 deletions cascades-server/domain/logic/pom.xml
Expand Up @@ -75,6 +75,10 @@
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
</dependencies>

</project>
@@ -1,27 +1,27 @@
package pl.gov.coi.cascades.server.domain.launchdatabase;

import org.apache.commons.lang.RandomStringUtils;
import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;

import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;

public class UsernameAndPasswordCredentialsGeneratorService {

private static final int PASSWORD_LENGTH = 24;
private static final int USERNAME_LENGTH = 8;
private static final int USERNAME_LENGTH = 10;
private static final String CHAR_PASSWORD =
"0123456789" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"!@#$%^&*";
private Random rand;
private Random randomGenerator;

/**
* Default constructor.
* @param randomGenerator random generator implementation
*/
public UsernameAndPasswordCredentialsGeneratorService() {
rand = new SecureRandom();
public UsernameAndPasswordCredentialsGeneratorService(Random randomGenerator) {
this.randomGenerator = randomGenerator;
}

/**
Expand All @@ -31,22 +31,21 @@ public UsernameAndPasswordCredentialsGeneratorService() {
*/
public UsernameAndPasswordCredentials generate() {
String username = generateUsername();
char[] password = generatePassword();
char[] password = generateCharArrayPassword();
return new UsernameAndPasswordCredentialsImpl(username, password);
}

private static String generateUsername() {
String uuid = UUID.randomUUID().toString();
return uuid.substring(0, USERNAME_LENGTH);
return RandomStringUtils.randomAlphabetic(USERNAME_LENGTH);
}

private char[] generatePassword() {
StringBuilder stringBuilder = new StringBuilder();
private char[] generateCharArrayPassword() {
char[] password = new char[PASSWORD_LENGTH];
for (int i = 0; i < PASSWORD_LENGTH; i++) {
char c = (char) (rand.nextInt(CHAR_PASSWORD.length()));
stringBuilder.append(c);
char c = CHAR_PASSWORD.charAt(randomGenerator.nextInt(CHAR_PASSWORD.length()));
password[i] = c;
}
return stringBuilder.toString().toCharArray();
return password;
}

}
@@ -1,29 +1,64 @@
package pl.gov.coi.cascades.server.domain.launchdatabase;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.runners.MockitoJUnitRunner;
import pl.gov.coi.cascades.contract.domain.UsernameAndPasswordCredentials;
import pl.gov.coi.cascades.server.domain.launchdatabase.UsernameAndPasswordCredentialsGeneratorService;

import java.security.SecureRandom;
import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
* @author <a href="agnieszka.celuch@coi.gov.pl">Agnieszka Celuch</a>
* @since 07.03.17.
*/
public class UsernameAndPasswordCredentialsGeneratorServiceTest {

private static final int PASSWORD_LENGTH = 24;

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Mock
private Random randomGeneratorMock;

@Test
public void testGenerate() throws Exception {
public void testGenerateRandomCredentials() {
// given
UsernameAndPasswordCredentialsGeneratorService credentials = new UsernameAndPasswordCredentialsGeneratorService();
UsernameAndPasswordCredentialsGeneratorService credentials = new UsernameAndPasswordCredentialsGeneratorService(new SecureRandom());

// when
UsernameAndPasswordCredentials actual = credentials.generate();
UsernameAndPasswordCredentials firstGeneration = credentials.generate();
UsernameAndPasswordCredentials secondGeneration = credentials.generate();

// then
checkGeneratedDataIsRandom(firstGeneration, secondGeneration);
}

@Test
public void testUsePassedGenerator() {
//given
UsernameAndPasswordCredentialsGeneratorService credentials = new UsernameAndPasswordCredentialsGeneratorService(randomGeneratorMock);

//when
credentials.generate();

// then
assertThat(actual).isNotNull();
assertThat(actual.getUsername()).hasSize(8);
assertThat(actual.getPassword()).hasSize(24);
verify(randomGeneratorMock, times(PASSWORD_LENGTH)).nextInt(any(Integer.class));
}

private void checkGeneratedDataIsRandom(UsernameAndPasswordCredentials firstGeneration, UsernameAndPasswordCredentials secondGeneration) {
assertThat(firstGeneration.getUsername()).isNotEqualTo(secondGeneration.getUsername());
assertThat(firstGeneration.getPassword()).isNotEqualTo(secondGeneration.getPassword());
}

}
Expand Up @@ -41,7 +41,7 @@ public DatabaseInstance get() {
db.setCredentials(
Credentials.builder()
.username("yjdr6uyjrjt")
.password("tgjsrt64w6ujstrjrst")
.password("tgjsrt64w6ujstrjrst".toCharArray())
.build()
);
db.setStatus(DatabaseStatus.LAUNCHED);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public DatabaseInstance get() {
db.setCredentials(
Credentials.builder()
.username("dje4njknjkrn")
.password("vfnui34jnghie")
.password("vfnui34jnghie".toCharArray())
.build()
);
db.setReuseTimes(1);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public DatabaseInstance get() {
db.setCredentials(
Credentials.builder()
.username("fdrg5yh545y")
.password("5h5h54jyuyr7za")
.password("5h5h54jyuyr7za".toCharArray())
.build()
);
db.setReuseTimes(1);
Expand Down
Expand Up @@ -41,7 +41,7 @@ public DatabaseInstance get() {
db.setCredentials(
Credentials.builder()
.username("fddfhy56ytj3")
.password("hjsw5y4thaw43t5grw")
.password("hjsw5y4thaw43t5grw".toCharArray())
.build()
);
db.setReuseTimes(1);
Expand Down
Expand Up @@ -8,6 +8,7 @@

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Lob;

/**
* @author <a href="agnieszka.celuch@coi.gov.pl">Agnieszka Celuch</a>
Expand All @@ -25,6 +26,7 @@ public class Credentials {
private String username;

@Column
private String password;
@Lob
private char[] password;

}
Expand Up @@ -20,7 +20,6 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.Arrays;

import static pl.wavesoftware.eid.utils.EidPreconditions.checkNotNull;

Expand Down Expand Up @@ -74,7 +73,7 @@ public DatabaseInstance toHibernateEntity(@Nonnull pl.gov.coi.cascades.server.do
checkNotNull(databaseInstance.getDatabaseType(), "20170414:111248");

Credentials credentials = new Credentials();
credentials.setPassword(Arrays.toString(databaseInstance.getCredentials().getPassword()));
credentials.setPassword(databaseInstance.getCredentials().getPassword());
credentials.setUsername(databaseInstance.getCredentials().getUsername());

NetworkBind networkBind = new NetworkBind();
Expand Down Expand Up @@ -121,7 +120,7 @@ public pl.gov.coi.cascades.server.domain.DatabaseInstance fromHibernateEntity(@N
DatabaseType databaseType = new DtoFetcher(databaseTypeDTO).getDatabaseType();
UsernameAndPasswordCredentials credentials = new UsernameAndPasswordCredentialsImpl(
databaseInstance.getCredentials().getUsername(),
databaseInstance.getCredentials().getPassword().toCharArray()
databaseInstance.getCredentials().getPassword()
);
pl.gov.coi.cascades.contract.domain.NetworkBind networkBind = new NetworkBindImpl(
databaseInstance.getNetworkBind().getHost(),
Expand Down
Expand Up @@ -68,7 +68,7 @@ public class DatabaseIdGatewayImplTest {
public void testFind() throws Exception {
// given
String id = "123456789";
String PASSWORD = "12345678";
char[] PASSWORD = "12345678".toCharArray();
String USERNAME = "Ben Affleck";
String HOST = "db01.lab.internal";
String GENERATED_ID = "fh45j32b";
Expand Down
Expand Up @@ -30,7 +30,7 @@ public void testGet() throws Exception {
String instanceName = "Oracle is extremely hard";
String host = "cascades.example.org";
String username = "yjdr6uyjrjt";
String password = "tgjsrt64w6ujstrjrst";
char[] password = "tgjsrt64w6ujstrjrst".toCharArray();
DatabaseStatus status = DatabaseStatus.LAUNCHED;
Date created = Date.from(Instant.parse("2017-03-28T17:56:11.01Z"));
Ora12e34Supplier ora12e34Supplier = new Ora12e34Supplier();
Expand Down
Expand Up @@ -30,7 +30,7 @@ public void testGet() throws Exception {
String instanceName = "Oracle is *%! hard";
String host = "cascades.example.org";
String username = "dje4njknjkrn";
String password = "vfnui34jnghie";
char[] password = "vfnui34jnghie".toCharArray();
DatabaseStatus status = DatabaseStatus.LAUNCHED;
Date created = Date.from(Instant.parse("2017-03-28T17:56:11.01Z"));
Ora23r45Supplier ora23r45Supplier = new Ora23r45Supplier();
Expand Down
Expand Up @@ -30,7 +30,7 @@ public void testGet() throws Exception {
String instanceName = "Postgres is *%! hard";
String host = "cascades.example.org";
String username = "fdrg5yh545y";
String password = "5h5h54jyuyr7za";
char[] password = "5h5h54jyuyr7za".toCharArray();
DatabaseStatus status = DatabaseStatus.LAUNCHED;
Date created = Date.from(Instant.parse("2017-03-28T17:56:11.01Z"));
Pos34t56Supplier pos34t56Supplier = new Pos34t56Supplier();
Expand Down
Expand Up @@ -30,7 +30,7 @@ public void testGet() throws Exception {
String instanceName = "Postgres is *%! hard";
String host = "cascades.example.org";
String username = "fddfhy56ytj3";
String password = "hjsw5y4thaw43t5grw";
char[] password = "hjsw5y4thaw43t5grw".toCharArray();
DatabaseStatus status = DatabaseStatus.DELETED;
Date created = Date.from(Instant.parse("2017-03-28T17:56:11.01Z"));
Pos45y67Supplier pos45y67Supplier = new Pos45y67Supplier();
Expand Down

0 comments on commit bf98662

Please sign in to comment.