Skip to content

Commit 4427ef0

Browse files
committed
CKS: generate a strong random password for CKS user
1 parent 1948f90 commit 4427ef0

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

api/src/main/java/com/cloud/user/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface User extends OwnedBy, InternalIdentity {
2424

2525
// UNKNOWN and NATIVE can be used interchangeably
2626
public enum Source {
27-
OAUTH2, LDAP, SAML2, SAML2DISABLED, UNKNOWN, NATIVE
27+
OAUTH2, LDAP, SAML2, SAML2DISABLED, UNKNOWN, NATIVE, CKS
2828
}
2929

3030
public static final long UID_SYSTEM = 1;

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImpl.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
import javax.inject.Inject;
4242
import javax.naming.ConfigurationException;
4343

44+
import com.cloud.user.PasswordPolicy;
4445
import com.cloud.uservm.UserVm;
46+
import com.cloud.utils.PasswordGenerator;
4547
import com.cloud.vm.UserVmService;
4648
import org.apache.cloudstack.acl.ControlledEntity;
4749
import org.apache.cloudstack.acl.Role;
@@ -242,6 +244,7 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne
242244
);
243245
private static final String PROJECT_KUBERNETES_ACCOUNT_FIRST_NAME = "Kubernetes";
244246
private static final String PROJECT_KUBERNETES_ACCOUNT_LAST_NAME = "Service User";
247+
private static final int CKS_USER_MIN_PASSWORD_LENGTH = 12;
245248

246249

247250
private static final String DEFAULT_NETWORK_OFFERING_FOR_KUBERNETES_SERVICE_DISPLAY_TEXT = "Network Offering used for CloudStack Kubernetes service";
@@ -1499,6 +1502,14 @@ protected String[] createUserApiKeyAndSecretKey(long userId) {
14991502
}
15001503
}
15011504

1505+
protected String generateRandomUserPassword(Long domainId) {
1506+
Integer passwordPolicyMinimumLength = PasswordPolicy.PasswordPolicyMinimumLength.valueIn(domainId);
1507+
if (passwordPolicyMinimumLength == null || passwordPolicyMinimumLength < CKS_USER_MIN_PASSWORD_LENGTH) {
1508+
passwordPolicyMinimumLength = CKS_USER_MIN_PASSWORD_LENGTH;
1509+
}
1510+
return PasswordGenerator.generateRandomPassword(passwordPolicyMinimumLength);
1511+
}
1512+
15021513
protected String[] getServiceUserKeys(Account owner) {
15031514
String username = owner.getAccountName();
15041515
if (!username.startsWith(KUBEADMIN_ACCOUNT_NAME + "-")) {
@@ -1507,8 +1518,9 @@ protected String[] getServiceUserKeys(Account owner) {
15071518
UserAccount kubeadmin = accountService.getActiveUserAccount(username, owner.getDomainId());
15081519
String[] keys;
15091520
if (kubeadmin == null) {
1510-
User kube = userDao.persist(new UserVO(owner.getAccountId(), username, UUID.randomUUID().toString(), owner.getAccountName(),
1511-
KUBEADMIN_ACCOUNT_NAME, "kubeadmin", null, UUID.randomUUID().toString(), User.Source.UNKNOWN));
1521+
Integer passwordPolicyMinimumLength = PasswordPolicy.PasswordPolicyMinimumLength.valueIn(owner.getDomainId());
1522+
User kube = userDao.persist(new UserVO(owner.getAccountId(), username, generateRandomUserPassword(owner.getDomainId()), owner.getAccountName(),
1523+
KUBEADMIN_ACCOUNT_NAME, "kubeadmin", null, UUID.randomUUID().toString(), User.Source.CKS));
15121524
keys = createUserApiKeyAndSecretKey(kube.getId());
15131525
} else {
15141526
String apiKey = kubeadmin.getApiKey();
@@ -1551,9 +1563,9 @@ protected Account createProjectKubernetesAccount(final Project project, final St
15511563
try {
15521564
Role role = getProjectKubernetesAccountRole();
15531565
UserAccount userAccount = accountService.createUserAccount(accountName,
1554-
UuidUtils.first(UUID.randomUUID().toString()), PROJECT_KUBERNETES_ACCOUNT_FIRST_NAME,
1566+
generateRandomUserPassword(project.getDomainId()), PROJECT_KUBERNETES_ACCOUNT_FIRST_NAME,
15551567
PROJECT_KUBERNETES_ACCOUNT_LAST_NAME, null, null, accountName, Account.Type.NORMAL, role.getId(),
1556-
project.getDomainId(), null, null, null, null, User.Source.NATIVE);
1568+
project.getDomainId(), null, null, null, null, User.Source.CKS);
15571569
projectManager.assignAccountToProject(project, userAccount.getAccountId(), ProjectAccount.Role.Regular,
15581570
userAccount.getId(), null);
15591571
Account account = accountService.getAccount(userAccount.getAccountId());

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2747,7 +2747,9 @@ protected UserVO createUser(long accountId, String userName, String password, St
27472747
logger.debug("Creating user: " + userName + ", accountId: " + accountId + " timezone:" + timezone);
27482748
}
27492749

2750-
passwordPolicy.verifyIfPasswordCompliesWithPasswordPolicies(password, userName, getAccount(accountId).getDomainId());
2750+
if (!User.Source.CKS.equals(source)) {
2751+
passwordPolicy.verifyIfPasswordCompliesWithPasswordPolicies(password, userName, getAccount(accountId).getDomainId());
2752+
}
27512753

27522754
String encodedPassword = null;
27532755
for (UserAuthenticator authenticator : _userPasswordEncoders) {

0 commit comments

Comments
 (0)