Repository: jclouds Updated Branches: refs/heads/master 78104938e -> 7e496723a
Move Passwords implementation to jclouds-core to be reused by providers Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/7e496723 Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/7e496723 Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/7e496723 Branch: refs/heads/master Commit: 7e496723abfcecb988f316324a85236904207d53 Parents: 7810493 Author: Svetoslav Neykov <[email protected]> Authored: Fri Jul 21 09:27:02 2017 +0300 Committer: Svetoslav Neykov <[email protected]> Committed: Thu Jul 27 20:54:31 2017 +0300 ---------------------------------------------------------------------- .../main/java/org/jclouds/util/Passwords.java | 64 ++++++++++++++++++++ .../java/org/jclouds/util/PasswordsTest.java | 55 +++++++++++++++++ .../ProfitBricksComputeServiceAdapter.java | 2 +- .../jclouds/profitbricks/util/Passwords.java | 64 -------------------- .../profitbricks/util/Preconditions.java | 2 +- .../profitbricks/util/PasswordsTest.java | 53 ---------------- 6 files changed, 121 insertions(+), 119 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds/blob/7e496723/core/src/main/java/org/jclouds/util/Passwords.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/jclouds/util/Passwords.java b/core/src/main/java/org/jclouds/util/Passwords.java new file mode 100644 index 0000000..e1d3b46 --- /dev/null +++ b/core/src/main/java/org/jclouds/util/Passwords.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.util; + +import java.util.Random; +import java.util.regex.Pattern; + +import com.google.common.collect.ImmutableSet; + +public class Passwords { + + private static final Random random = new Random(); + + private static final int MIN_CHAR = 8; + private static final int MAX_CHAR = 50; + private static final String PASSWORD_FORMAT = String.format( + "[a-zA-Z0-9][^iIloOwWyYzZ10]{%d,%d}", MIN_CHAR - 1, MAX_CHAR); + private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_FORMAT); + + private static final ImmutableSet<Character> INVALID_CHARS = ImmutableSet.<Character>of( + 'i', 'I', 'l', 'o', 'O', 'w', 'W', 'y', 'Y', 'z', 'Z', '1', '0'); + + public static boolean isValidPassword(String password) { + return PASSWORD_PATTERN.matcher(password).matches(); + } + + public static String generate() { + int count = random.nextInt(MAX_CHAR - MIN_CHAR) + MIN_CHAR; + + final char[] buffer = new char[count]; + + final int start = 'A'; + final int end = 'z'; + final int gap = end - start + 1; + + while (count-- != 0) { + char ch = (char) (random.nextInt(gap) + start); + if ((isBetween(ch, start, 'Z') || isBetween(ch, 'a', end)) + && !INVALID_CHARS.contains(ch)) + buffer[count] = ch; + else + count++; + } + return new String(buffer); + } + + private static boolean isBetween(char ch, int start, int end) { + return ch >= start && ch <= end; + } +} http://git-wip-us.apache.org/repos/asf/jclouds/blob/7e496723/core/src/test/java/org/jclouds/util/PasswordsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/jclouds/util/PasswordsTest.java b/core/src/test/java/org/jclouds/util/PasswordsTest.java new file mode 100644 index 0000000..200bed9 --- /dev/null +++ b/core/src/test/java/org/jclouds/util/PasswordsTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jclouds.util; + +import com.google.common.collect.ImmutableList; +import org.testng.annotations.Test; + +import java.util.List; + +import static org.jclouds.util.Passwords.isValidPassword; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +@Test(groups = "unit", testName = "PasswordsTest") +public class PasswordsTest { + + private final List<String> validPasswords = ImmutableList.of( + "fKVasTnNm", "84625894", "QQQQQQQQ", "qqqqqqqq", "asdfghjk" + ); + private final List<String> invalidPasswords = ImmutableList.of( + "", "apachejclouds", "s0merand0mpassw0rd" + ); + + @Test + public void testPasswordValidation() { + for (String pwd : validPasswords) + assertTrue(isValidPassword(pwd), "Should've been valid: " + pwd); + + for (String pwd : invalidPasswords) + assertFalse(isValidPassword(pwd), "Should've been invalid: " + pwd); + } + + @Test + public void testGeneratorGeneratesValidPassword() { + final int times = 50; + for (int i = 0; i < times; i++) { + String pwd = Passwords.generate(); + assertTrue(isValidPassword(pwd), "Failed with: " + pwd); + } + } +} http://git-wip-us.apache.org/repos/asf/jclouds/blob/7e496723/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapter.java ---------------------------------------------------------------------- diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapter.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapter.java index 766b360..c6fd08b 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapter.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/compute/ProfitBricksComputeServiceAdapter.java @@ -59,7 +59,6 @@ import org.jclouds.profitbricks.domain.Server; import org.jclouds.profitbricks.domain.Snapshot; import org.jclouds.profitbricks.domain.Storage; import org.jclouds.profitbricks.features.ServerApi; -import org.jclouds.profitbricks.util.Passwords; import org.jclouds.rest.ResourceNotFoundException; import com.google.common.base.Function; @@ -72,6 +71,7 @@ import com.google.common.collect.Lists; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.inject.Inject; +import org.jclouds.util.Passwords; @Singleton public class ProfitBricksComputeServiceAdapter implements ComputeServiceAdapter<Server, Hardware, Provisionable, Location> { http://git-wip-us.apache.org/repos/asf/jclouds/blob/7e496723/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Passwords.java ---------------------------------------------------------------------- diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Passwords.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Passwords.java deleted file mode 100644 index 338f064..0000000 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Passwords.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jclouds.profitbricks.util; - -import java.util.Random; -import java.util.regex.Pattern; - -import com.google.common.collect.ImmutableSet; - -public class Passwords { - - private static final Random random = new Random(); - - private static final int MIN_CHAR = 8; - private static final int MAX_CHAR = 50; - private static final String PASSWORD_FORMAT = String.format( - "[a-zA-Z0-9][^iIloOwWyYzZ10]{%d,%d}", MIN_CHAR - 1, MAX_CHAR); - private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_FORMAT); - - private static final ImmutableSet<Character> INVALID_CHARS = ImmutableSet.<Character>of( - 'i', 'I', 'l', 'o', 'O', 'w', 'W', 'y', 'Y', 'z', 'Z', '1', '0'); - - public static boolean isValidPassword(String password) { - return PASSWORD_PATTERN.matcher(password).matches(); - } - - public static String generate() { - int count = random.nextInt(MAX_CHAR - MIN_CHAR) + MIN_CHAR; - - final char[] buffer = new char[count]; - - final int start = 'A'; - final int end = 'z'; - final int gap = end - start + 1; - - while (count-- != 0) { - char ch = (char) (random.nextInt(gap) + start); - if ((isBetween(ch, start, 'Z') || isBetween(ch, 'a', end)) - && !INVALID_CHARS.contains(ch)) - buffer[count] = ch; - else - count++; - } - return new String(buffer); - } - - private static boolean isBetween(char ch, int start, int end) { - return ch >= start && ch <= end; - } -} http://git-wip-us.apache.org/repos/asf/jclouds/blob/7e496723/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Preconditions.java ---------------------------------------------------------------------- diff --git a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Preconditions.java b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Preconditions.java index b15689d..b0013c4 100644 --- a/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Preconditions.java +++ b/providers/profitbricks/src/main/java/org/jclouds/profitbricks/util/Preconditions.java @@ -21,7 +21,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.net.InetAddresses.isInetAddress; import static org.jclouds.profitbricks.util.MacAddresses.isMacAddress; -import static org.jclouds.profitbricks.util.Passwords.isValidPassword; +import static org.jclouds.util.Passwords.isValidPassword; import java.util.List; import java.util.regex.Pattern; http://git-wip-us.apache.org/repos/asf/jclouds/blob/7e496723/providers/profitbricks/src/test/java/org/jclouds/profitbricks/util/PasswordsTest.java ---------------------------------------------------------------------- diff --git a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/util/PasswordsTest.java b/providers/profitbricks/src/test/java/org/jclouds/profitbricks/util/PasswordsTest.java deleted file mode 100644 index fc7be1f..0000000 --- a/providers/profitbricks/src/test/java/org/jclouds/profitbricks/util/PasswordsTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jclouds.profitbricks.util; - -import com.google.common.collect.ImmutableList; -import java.util.List; -import static org.jclouds.profitbricks.util.Passwords.isValidPassword; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; -import org.testng.annotations.Test; - -@Test(groups = "unit", testName = "PasswordsTest") -public class PasswordsTest { - - private final List<String> validPasswords = ImmutableList.of( - "fKVasTnNm", "84625894", "QQQQQQQQ", "qqqqqqqq", "asdfghjk" - ); - private final List<String> invalidPasswords = ImmutableList.of( - "", "apachejclouds", "s0merand0mpassw0rd" - ); - - @Test - public void testPasswordValidation() { - for (String pwd : validPasswords) - assertTrue(isValidPassword(pwd), "Should've been valid: " + pwd); - - for (String pwd : invalidPasswords) - assertFalse(isValidPassword(pwd), "Should've been invalid: " + pwd); - } - - @Test - public void testGeneratorGeneratesValidPassword() { - final int times = 50; - for (int i = 0; i < times; i++) { - String pwd = Passwords.generate(); - assertTrue(isValidPassword(pwd), "Failed with: " + pwd); - } - } -}
