Skip to content

Commit d46b658

Browse files
committed
Fixed CLOUDSTACK-8551 Findbugs warning in LdapCreateAccountCmd.java
byte[].toString() would give reference to the array (ex: [B@6c521576 ) but not the original string. used new String() to get the text.
1 parent 7ba2f22 commit d46b658

2 files changed

Lines changed: 111 additions & 99 deletions

File tree

‎plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapCreateAccountCmd.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command;
1818

19+
import java.io.UnsupportedEncodingException;
1920
import java.security.NoSuchAlgorithmException;
2021
import java.security.SecureRandom;
2122
import java.util.Map;
@@ -143,8 +144,8 @@ private String generatePassword() throws ServerApiException {
143144
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
144145
final byte bytes[] = new byte[20];
145146
randomGen.nextBytes(bytes);
146-
return Base64.encode(bytes).toString();
147-
} catch (final NoSuchAlgorithmException e) {
147+
return new String(Base64.encode(bytes), "UTF-8");
148+
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
148149
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
149150
}
150151
}

‎plugins/user-authenticators/ldap/test/groovy/org/apache/cloudstack/ldap/LdapCreateAccountCmdSpec.groovy‎

Lines changed: 108 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -37,120 +37,131 @@ import javax.naming.NamingException
3737
class LdapCreateAccountCmdSpec extends spock.lang.Specification {
3838

3939
def "Test failure to retrive LDAP user"() {
40-
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd and LDAP user that doesn't exist"
41-
LdapManager ldapManager = Mock(LdapManager)
42-
ldapManager.getUser(_) >> { throw new NoLdapUserMatchingQueryException() }
43-
AccountService accountService = Mock(AccountService)
44-
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
45-
ldapCreateAccountCmd.getCurrentContext() >> Mock(CallContext)
46-
CallContext context = ldapCreateAccountCmd.getCurrentContext()
47-
when: "An an account is created"
48-
ldapCreateAccountCmd.execute()
49-
then: "It fails and an exception is thrown"
50-
thrown ServerApiException
40+
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd and LDAP user that doesn't exist"
41+
LdapManager ldapManager = Mock(LdapManager)
42+
ldapManager.getUser(_) >> { throw new NoLdapUserMatchingQueryException() }
43+
AccountService accountService = Mock(AccountService)
44+
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
45+
ldapCreateAccountCmd.getCurrentContext() >> Mock(CallContext)
46+
CallContext context = ldapCreateAccountCmd.getCurrentContext()
47+
when: "An an account is created"
48+
ldapCreateAccountCmd.execute()
49+
then: "It fails and an exception is thrown"
50+
thrown ServerApiException
5151
}
5252

53-
def "Test failed creation due to a null response from cloudstack account creater"() {
54-
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
55-
LdapManager ldapManager = Mock(LdapManager)
56-
ldapManager.getUser(_) >> new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering")
57-
AccountService accountService = Mock(AccountService)
58-
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
59-
ldapCreateAccountCmd.getCurrentContext() >> Mock(CallContext)
60-
ldapCreateAccountCmd.createCloudstackUserAccount(_) >> null
61-
when: "Cloudstack fail to create the user"
62-
ldapCreateAccountCmd.execute()
63-
then: "An exception is thrown"
64-
thrown ServerApiException
65-
}
53+
def "Test failed creation due to a null response from cloudstack account creater"() {
54+
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
55+
LdapManager ldapManager = Mock(LdapManager)
56+
ldapManager.getUser(_) >> new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering")
57+
AccountService accountService = Mock(AccountService)
58+
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
59+
ldapCreateAccountCmd.getCurrentContext() >> Mock(CallContext)
60+
ldapCreateAccountCmd.createCloudstackUserAccount(_) >> null
61+
when: "Cloudstack fail to create the user"
62+
ldapCreateAccountCmd.execute()
63+
then: "An exception is thrown"
64+
thrown ServerApiException
65+
}
6666

6767
def "Test command name"() {
68-
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
69-
LdapManager ldapManager = Mock(LdapManager)
70-
AccountService accountService = Mock(AccountService)
71-
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
72-
when: "Get command name is called"
73-
def result = ldapCreateAccountCmd.getCommandName()
74-
then: "createaccountresponse is returned"
75-
result == "createaccountresponse"
68+
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
69+
LdapManager ldapManager = Mock(LdapManager)
70+
AccountService accountService = Mock(AccountService)
71+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
72+
when: "Get command name is called"
73+
def result = ldapCreateAccountCmd.getCommandName()
74+
then: "createaccountresponse is returned"
75+
result == "createaccountresponse"
7676
}
7777

7878
def "Test getEntityOwnerId is 1"() {
79-
given: "We have an LdapManager, AccountService andL dapCreateAccount"
80-
LdapManager ldapManager = Mock(LdapManager)
81-
AccountService accountService = Mock(AccountService)
82-
83-
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
84-
when: "Get entity owner id is called"
85-
long ownerId = ldapCreateAccountCmd.getEntityOwnerId()
86-
then: "1 is returned"
87-
ownerId == 1
79+
given: "We have an LdapManager, AccountService andL dapCreateAccount"
80+
LdapManager ldapManager = Mock(LdapManager)
81+
AccountService accountService = Mock(AccountService)
82+
83+
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
84+
when: "Get entity owner id is called"
85+
long ownerId = ldapCreateAccountCmd.getEntityOwnerId()
86+
then: "1 is returned"
87+
ownerId == 1
8888
}
8989

9090
def "Test password generation"() {
91-
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
92-
LdapManager ldapManager = Mock(LdapManager)
93-
AccountService accountService = Mock(AccountService)
94-
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
95-
when: "A password is generated"
96-
def result = ldapCreateAccountCmd.generatePassword()
97-
then: "The result shouldn't be null or empty"
98-
result != ""
99-
result != null
100-
}
91+
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
92+
LdapManager ldapManager = Mock(LdapManager)
93+
AccountService accountService = Mock(AccountService)
94+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
95+
when: "A password is generated"
96+
def result = ldapCreateAccountCmd.generatePassword()
97+
then: "The result shouldn't be null or empty"
98+
result != ""
99+
result != null
100+
}
101101

102102
def "Test validate User"() {
103-
given: "We have an LdapManager, AccountService andL dapCreateAccount"
104-
LdapManager ldapManager = Mock(LdapManager)
105-
AccountService accountService = Mock(AccountService)
106-
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService);
107-
when: "a user with an username, email, firstname and lastname is validated"
108-
def result = ldapCreateAccountCmd.validateUser(new LdapUser("username","email","firstname","lastname","principal","domain"))
109-
then: "the result is true"
110-
result == true
111-
}
103+
given: "We have an LdapManager, AccountService andL dapCreateAccount"
104+
LdapManager ldapManager = Mock(LdapManager)
105+
AccountService accountService = Mock(AccountService)
106+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService);
107+
when: "a user with an username, email, firstname and lastname is validated"
108+
def result = ldapCreateAccountCmd.validateUser(new LdapUser("username", "email", "firstname", "lastname", "principal", "domain"))
109+
then: "the result is true"
110+
result == true
111+
}
112112

113113
def "Test validate User empty email"() {
114-
given: "We have an LdapManager, AccountService andL dapCreateAccount"
115-
LdapManager ldapManager = Mock(LdapManager)
116-
AccountService accountService = Mock(AccountService)
117-
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
118-
when: "A user with no email address attempts to validate"
119-
ldapCreateAccountCmd.validateUser(new LdapUser("username",null,"firstname","lastname","principal","domain"))
120-
then: "An exception is thrown"
121-
thrown Exception
122-
}
114+
given: "We have an LdapManager, AccountService andL dapCreateAccount"
115+
LdapManager ldapManager = Mock(LdapManager)
116+
AccountService accountService = Mock(AccountService)
117+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
118+
when: "A user with no email address attempts to validate"
119+
ldapCreateAccountCmd.validateUser(new LdapUser("username", null, "firstname", "lastname", "principal", "domain"))
120+
then: "An exception is thrown"
121+
thrown Exception
122+
}
123123

124124
def "Test validate User empty firstname"() {
125-
given: "We have an LdapManager, AccountService andL dapCreateAccount"
126-
LdapManager ldapManager = Mock(LdapManager)
127-
AccountService accountService = Mock(AccountService)
128-
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
129-
when: "A user with no firstname attempts to validate"
130-
ldapCreateAccountCmd.validateUser(new LdapUser("username","email",null,"lastname","principal"))
131-
then: "An exception is thrown"
132-
thrown Exception
133-
}
134-
135-
def "Test validate User empty lastname"() {
136-
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
137-
LdapManager ldapManager = Mock(LdapManager)
138-
AccountService accountService = Mock(AccountService)
139-
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
140-
when: "A user with no lastname attempts to validate"
141-
ldapCreateAccountCmd.validateUser(new LdapUser("username","email","firstname",null,"principal","domain"))
142-
then: "An exception is thown"
143-
thrown Exception
144-
}
125+
given: "We have an LdapManager, AccountService andL dapCreateAccount"
126+
LdapManager ldapManager = Mock(LdapManager)
127+
AccountService accountService = Mock(AccountService)
128+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
129+
when: "A user with no firstname attempts to validate"
130+
ldapCreateAccountCmd.validateUser(new LdapUser("username", "email", null, "lastname", "principal"))
131+
then: "An exception is thrown"
132+
thrown Exception
133+
}
134+
135+
def "Test validate User empty lastname"() {
136+
given: "We have an LdapManager, AccountService and LdapCreateAccountCmd"
137+
LdapManager ldapManager = Mock(LdapManager)
138+
AccountService accountService = Mock(AccountService)
139+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
140+
when: "A user with no lastname attempts to validate"
141+
ldapCreateAccountCmd.validateUser(new LdapUser("username", "email", "firstname", null, "principal", "domain"))
142+
then: "An exception is thown"
143+
thrown Exception
144+
}
145145

146146
def "Test validation of a user"() {
147-
given: "We have an LdapManager, AccountService andL dapCreateAccount"
148-
LdapManager ldapManager = Mock(LdapManager)
149-
AccountService accountService = Mock(AccountService)
150-
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
151-
when: "Get command name is called"
152-
def commandName = ldapCreateAccountCmd.getCommandName()
153-
then: "createaccountresponse is returned"
154-
commandName == "createaccountresponse"
147+
given: "We have an LdapManager, AccountService andL dapCreateAccount"
148+
LdapManager ldapManager = Mock(LdapManager)
149+
AccountService accountService = Mock(AccountService)
150+
def ldapCreateAccountCmd = Spy(LdapCreateAccountCmd, constructorArgs: [ldapManager, accountService])
151+
when: "Get command name is called"
152+
def commandName = ldapCreateAccountCmd.getCommandName()
153+
then: "createaccountresponse is returned"
154+
commandName == "createaccountresponse"
155+
}
156+
157+
def "Test generate password"() {
158+
given: "We have an LdapManager, AccountService and LdapCreateAccount"
159+
LdapManager ldapManager = Mock(LdapManager)
160+
AccountService accountService = Mock(AccountService)
161+
def ldapCreateAccountCmd = new LdapCreateAccountCmd(ldapManager, accountService)
162+
when: "A random password is generated for a new account"
163+
String password = ldapCreateAccountCmd.generatePassword()
164+
then: "password should not be the array address but an actual encoded string. verifying length > 20 as the byte array size is 20"
165+
password.length() > 20
155166
}
156167
}

0 commit comments

Comments
 (0)