3535import org .apache .cloudstack .ldap .LdapManager ;
3636import org .apache .cloudstack .ldap .LdapUser ;
3737import org .apache .log4j .Logger ;
38+ import org .bouncycastle .util .encoders .Base64 ;
3839
3940import com .cloud .user .Account ;
4041import com .cloud .user .AccountService ;
4142import com .cloud .user .UserAccount ;
4243
4344@ APICommand (name = "ldapCreateAccount" , description = "Creates an account from an LDAP user" , responseObject = AccountResponse .class , since = "4.2.0" )
4445public class LdapCreateAccountCmd extends BaseCmd {
45- public static final Logger s_logger = Logger
46- .getLogger (LdapCreateAccountCmd .class .getName ());
47- private static final String s_name = "createaccountresponse" ;
48-
49- @ Inject
50- private LdapManager _ldapManager ;
51-
52- @ Parameter (name = ApiConstants .ACCOUNT , type = CommandType .STRING , description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name." )
53- private String accountName ;
54-
55- @ Parameter (name = ApiConstants .ACCOUNT_TYPE , type = CommandType .SHORT , required = true , description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin" )
56- private Short accountType ;
57-
58- @ Parameter (name = ApiConstants .DOMAIN_ID , type = CommandType .UUID , entityType = DomainResponse .class , description = "Creates the user under the specified domain." )
59- private Long domainId ;
60-
61- @ Parameter (name = ApiConstants .TIMEZONE , type = CommandType .STRING , description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format." )
62- private String timezone ;
63-
64- @ Parameter (name = ApiConstants .USERNAME , type = CommandType .STRING , required = true , description = "Unique username." )
65- private String username ;
66-
67- @ Parameter (name = ApiConstants .NETWORK_DOMAIN , type = CommandType .STRING , description = "Network domain for the account's networks" )
68- private String networkDomain ;
69-
70- @ Parameter (name = ApiConstants .ACCOUNT_DETAILS , type = CommandType .MAP , description = "details for account used to store specific parameters" )
71- private Map <String , String > details ;
72-
73- @ Parameter (name = ApiConstants .ACCOUNT_ID , type = CommandType .STRING , description = "Account UUID, required for adding account from external provisioning system" )
74- private String accountUUID ;
75-
76- @ Parameter (name = ApiConstants .USER_ID , type = CommandType .STRING , description = "User UUID, required for adding account from external provisioning system" )
77- private String userUUID ;
78-
79- public LdapCreateAccountCmd () {
80- super ();
81- }
82-
83- public LdapCreateAccountCmd (final LdapManager ldapManager ,
84- final AccountService accountService ) {
85- super ();
86- _ldapManager = ldapManager ;
87- _accountService = accountService ;
88- }
89-
90- UserAccount createCloudstackUserAccount (final LdapUser user ) {
91- return _accountService .createUserAccount (username , generatePassword (),
92- user .getFirstname (), user .getLastname (), user .getEmail (),
93- timezone , accountName , accountType , domainId , networkDomain ,
94- details , accountUUID , userUUID );
95- }
96-
97- @ Override
98- public void execute () throws ServerApiException {
99- final CallContext callContext = getCurrentContext ();
100- callContext .setEventDetails ("Account Name: " + accountName
101- + ", Domain Id:" + domainId );
102- try {
103- final LdapUser user = _ldapManager .getUser (username );
104- validateUser (user );
105- final UserAccount userAccount = createCloudstackUserAccount (user );
106- if (userAccount != null ) {
107- final AccountResponse response = _responseGenerator
108- .createUserAccountResponse (userAccount );
109- response .setResponseName (getCommandName ());
110- setResponseObject (response );
111- } else {
112- throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
113- "Failed to create a user account" );
114- }
115- } catch (final NamingException e ) {
116- throw new ServerApiException (
117- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR ,
118- "No LDAP user exists with the username of " + username );
119- }
120- }
121-
122- private String generatePassword () throws ServerApiException {
123- final SecureRandom random = new SecureRandom ();
124- final int length = 20 ;
125- final String characters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789!@£$%^&*()_+=" ;
126-
127- String password = "" ;
128- for (int i = 0 ; i < length ; i ++) {
129- int index = (int ) (random .nextDouble () * characters .length ());
130- password += characters .charAt (index );
131- }
132- return password ;
133- }
134-
135- @ Override
136- public String getCommandName () {
137- return s_name ;
138- }
139-
140- CallContext getCurrentContext () {
141- return CallContext .current ();
142- }
143-
144- @ Override
145- public long getEntityOwnerId () {
146- return Account .ACCOUNT_ID_SYSTEM ;
147- }
148-
149- private boolean validateUser (final LdapUser user ) throws ServerApiException {
150- if (user .getEmail () == null ) {
151- throw new ServerApiException (
152- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
153- + " has no email address set within LDAP" );
154- }
155- if (user .getFirstname () == null ) {
156- throw new ServerApiException (
157- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
158- + " has no firstname set within LDAP" );
159- }
160- if (user .getLastname () == null ) {
161- throw new ServerApiException (
162- ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
163- + " has no lastname set within LDAP" );
164- }
165- return true ;
166- }
46+ public static final Logger s_logger = Logger
47+ .getLogger (LdapCreateAccountCmd .class .getName ());
48+ private static final String s_name = "createaccountresponse" ;
49+
50+ @ Inject
51+ private LdapManager _ldapManager ;
52+
53+ @ Parameter (name = ApiConstants .ACCOUNT , type = CommandType .STRING , description = "Creates the user under the specified account. If no account is specified, the username will be used as the account name." )
54+ private String accountName ;
55+
56+ @ Parameter (name = ApiConstants .ACCOUNT_TYPE , type = CommandType .SHORT , required = true , description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin" )
57+ private Short accountType ;
58+
59+ @ Parameter (name = ApiConstants .DOMAIN_ID , type = CommandType .UUID , entityType = DomainResponse .class , description = "Creates the user under the specified domain." )
60+ private Long domainId ;
61+
62+ @ Parameter (name = ApiConstants .TIMEZONE , type = CommandType .STRING , description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format." )
63+ private String timezone ;
64+
65+ @ Parameter (name = ApiConstants .USERNAME , type = CommandType .STRING , required = true , description = "Unique username." )
66+ private String username ;
67+
68+ @ Parameter (name = ApiConstants .NETWORK_DOMAIN , type = CommandType .STRING , description = "Network domain for the account's networks" )
69+ private String networkDomain ;
70+
71+ @ Parameter (name = ApiConstants .ACCOUNT_DETAILS , type = CommandType .MAP , description = "details for account used to store specific parameters" )
72+ private Map <String , String > details ;
73+
74+ @ Parameter (name = ApiConstants .ACCOUNT_ID , type = CommandType .STRING , description = "Account UUID, required for adding account from external provisioning system" )
75+ private String accountUUID ;
76+
77+ @ Parameter (name = ApiConstants .USER_ID , type = CommandType .STRING , description = "User UUID, required for adding account from external provisioning system" )
78+ private String userUUID ;
79+
80+ public LdapCreateAccountCmd () {
81+ super ();
82+ }
83+
84+ public LdapCreateAccountCmd (final LdapManager ldapManager ,
85+ final AccountService accountService ) {
86+ super ();
87+ _ldapManager = ldapManager ;
88+ _accountService = accountService ;
89+ }
90+
91+ UserAccount createCloudstackUserAccount (final LdapUser user ) {
92+ return _accountService .createUserAccount (username , generatePassword (),
93+ user .getFirstname (), user .getLastname (), user .getEmail (),
94+ timezone , accountName , accountType , domainId , networkDomain ,
95+ details , accountUUID , userUUID );
96+ }
97+
98+ @ Override
99+ public void execute () throws ServerApiException {
100+ final CallContext callContext = getCurrentContext ();
101+ callContext .setEventDetails ("Account Name: " + accountName
102+ + ", Domain Id:" + domainId );
103+ try {
104+ final LdapUser user = _ldapManager .getUser (username );
105+ validateUser (user );
106+ final UserAccount userAccount = createCloudstackUserAccount (user );
107+ if (userAccount != null ) {
108+ final AccountResponse response = _responseGenerator
109+ .createUserAccountResponse (userAccount );
110+ response .setResponseName (getCommandName ());
111+ setResponseObject (response );
112+ } else {
113+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
114+ "Failed to create a user account" );
115+ }
116+ } catch (final NamingException e ) {
117+ throw new ServerApiException (
118+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR ,
119+ "No LDAP user exists with the username of " + username );
120+ }
121+ }
122+
123+ private String generatePassword () throws ServerApiException {
124+ try {
125+ final SecureRandom randomGen = SecureRandom .getInstance ("SHA1PRNG" );
126+ final byte bytes [] = new byte [20 ];
127+ randomGen .nextBytes (bytes );
128+ return Base64 .encode (bytes ).toString ();
129+ } catch (final NoSuchAlgorithmException e ) {
130+ throw new ServerApiException (ApiErrorCode .INTERNAL_ERROR ,
131+ "Failed to generate random password" );
132+ }
133+ }
134+
135+ @ Override
136+ public String getCommandName () {
137+ return s_name ;
138+ }
139+
140+ CallContext getCurrentContext () {
141+ return CallContext .current ();
142+ }
143+
144+ @ Override
145+ public long getEntityOwnerId () {
146+ return Account .ACCOUNT_ID_SYSTEM ;
147+ }
148+
149+ private boolean validateUser (final LdapUser user ) throws ServerApiException {
150+ if (user .getEmail () == null ) {
151+ throw new ServerApiException (
152+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
153+ + " has no email address set within LDAP" );
154+ }
155+ if (user .getFirstname () == null ) {
156+ throw new ServerApiException (
157+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
158+ + " has no firstname set within LDAP" );
159+ }
160+ if (user .getLastname () == null ) {
161+ throw new ServerApiException (
162+ ApiErrorCode .RESOURCE_UNAVAILABLE_ERROR , username
163+ + " has no lastname set within LDAP" );
164+ }
165+ return true ;
166+ }
167167}
0 commit comments