From eebcc0017fbf7b1f1d0d2920cd8f891e1ec36e38 Mon Sep 17 00:00:00 2001 From: Ignasi Barrera Date: Thu, 4 Jan 2018 01:44:14 +0100 Subject: [PATCH] JCLOUDS-1362: Proper password generation with custom constraints for each cloud --- .../config/AzureComputeServiceContextModule.java | 16 ++++++++++++++++ .../strategy/CreateResourcesThenCreateNodes.java | 9 ++++++--- .../CreateResourcesThenCreateNodesTest.java | 3 ++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureComputeServiceContextModule.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureComputeServiceContextModule.java index de33fdf143..0a81ecb180 100644 --- a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureComputeServiceContextModule.java +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/config/AzureComputeServiceContextModule.java @@ -53,6 +53,7 @@ import org.jclouds.compute.functions.NodeAndTemplateOptionsToStatementWithoutPub import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.strategy.CreateNodesInGroupThenAddToSet; import org.jclouds.net.domain.IpPermission; +import org.jclouds.util.PasswordGenerator; import com.google.common.base.Function; import com.google.common.cache.CacheBuilder; @@ -102,6 +103,21 @@ public class AzureComputeServiceContextModule extends bind(new TypeLiteral() { }).to(AzureComputeSecurityGroupExtension.class); } + + @Provides + @Singleton + protected PasswordGenerator.Config providePasswordGenerator() { + // Guest passwords must be between 6-72 characters long. + // Must contain an upper case character. + // Must contain a lower case character. + // Must contain a numeric digit. + // Must contain a special character. Control characters are not allowed. + return new PasswordGenerator() + .lower().min(2).max(10) + .upper().min(2).max(10) + .numbers().min(2).max(10) + .symbols().min(2).max(10); + } @Provides @Singleton diff --git a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodes.java b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodes.java index e5d38fbe5f..511d5315d3 100644 --- a/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodes.java +++ b/providers/azurecompute-arm/src/main/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodes.java @@ -52,7 +52,7 @@ import org.jclouds.compute.strategy.ListNodesStrategy; import org.jclouds.compute.strategy.impl.CreateNodesWithGroupEncodedIntoNameThenAddToSet; import org.jclouds.domain.Location; import org.jclouds.logging.Logger; -import org.jclouds.util.Passwords; +import org.jclouds.util.PasswordGenerator; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; @@ -84,6 +84,7 @@ public class CreateResourcesThenCreateNodes extends CreateNodesWithGroupEncodedI private final String defaultVnetAddressPrefix; private final String defaultSubnetAddressPrefix; private final TemplateToAvailabilitySet templateToAvailabilitySet; + private final PasswordGenerator.Config passwordGenerator; @Inject protected CreateResourcesThenCreateNodes( @@ -95,7 +96,8 @@ public class CreateResourcesThenCreateNodes extends CreateNodesWithGroupEncodedI AzureComputeApi api, @Named(DEFAULT_VNET_ADDRESS_SPACE_PREFIX) String defaultVnetAddressPrefix, @Named(DEFAULT_SUBNET_ADDRESS_PREFIX) String defaultSubnetAddressPrefix, LoadingCache securityGroupMap, - TemplateToAvailabilitySet templateToAvailabilitySet) { + TemplateToAvailabilitySet templateToAvailabilitySet, + PasswordGenerator.Config passwordGenerator) { super(addNodeWithGroupStrategy, listNodesStrategy, namingConvention, userExecutor, customizeNodeAndAddToGoodMapOrPutExceptionIntoBadMapFactory); this.api = api; @@ -103,6 +105,7 @@ public class CreateResourcesThenCreateNodes extends CreateNodesWithGroupEncodedI this.defaultVnetAddressPrefix = defaultVnetAddressPrefix; this.defaultSubnetAddressPrefix = defaultSubnetAddressPrefix; this.templateToAvailabilitySet = templateToAvailabilitySet; + this.passwordGenerator = passwordGenerator; } @Override @@ -141,7 +144,7 @@ public class CreateResourcesThenCreateNodes extends CreateNodesWithGroupEncodedI TemplateOptions options = template.getOptions(); if (options.getLoginPassword() == null) { Optional passwordOptional = template.getImage().getDefaultCredentials().getOptionalPassword(); - options.overrideLoginPassword(passwordOptional.or(Passwords.generate())); + options.overrideLoginPassword(passwordOptional.or(passwordGenerator.generate())); } } diff --git a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodesTest.java b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodesTest.java index 51a45af484..45c56a317c 100644 --- a/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodesTest.java +++ b/providers/azurecompute-arm/src/test/java/org/jclouds/azurecompute/arm/compute/strategy/CreateResourcesThenCreateNodesTest.java @@ -24,6 +24,7 @@ import org.jclouds.azurecompute.arm.domain.PublicIPAddressProperties; import org.jclouds.azurecompute.arm.domain.Subnet; import org.jclouds.azurecompute.arm.features.PublicIPAddressApi; import org.jclouds.azurecompute.arm.features.SubnetApi; +import org.jclouds.util.PasswordGenerator; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; @@ -101,7 +102,7 @@ public class CreateResourcesThenCreateNodesTest { } private static CreateResourcesThenCreateNodes strategy(AzureComputeApi api) { - return new CreateResourcesThenCreateNodes(null, null, null, null, null, api, null, null, null, null); + return new CreateResourcesThenCreateNodes(null, null, null, null, null, api, null, null, null, null, new PasswordGenerator().lower()); } private static String netResource(String resource) {