|
49 | 49 | import com.azure.storage.blob.BlobUrlParts; |
50 | 50 | import com.azure.storage.blob.models.BlobItem; |
51 | 51 | import com.azure.storage.common.StorageSharedKeyCredential; |
| 52 | +import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; |
| 53 | +import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; |
52 | 54 | import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; |
53 | 55 | import com.fasterxml.jackson.databind.JsonNode; |
54 | 56 | import com.fasterxml.jackson.databind.ObjectMapper; |
|
60 | 62 | import com.microsoft.azure.vmagent.exceptions.AzureCloudException; |
61 | 63 | import com.microsoft.azure.vmagent.launcher.AzureComputerLauncher; |
62 | 64 | import com.microsoft.azure.vmagent.launcher.AzureSSHLauncher; |
63 | | -import com.microsoft.azure.vmagent.retry.NoRetryStrategy; |
64 | 65 | import com.microsoft.azure.vmagent.util.*; |
65 | 66 | import com.microsoft.jenkins.credentials.AzureResourceManagerCache; |
| 67 | +import com.sshtools.common.publickey.InvalidPassphraseException; |
| 68 | +import com.sshtools.common.ssh.SshException; |
66 | 69 | import hudson.model.Descriptor.FormException; |
| 70 | +import hudson.util.Secret; |
67 | 71 | import io.jenkins.plugins.azuresdk.HttpClientRetriever; |
68 | 72 | import jenkins.model.Jenkins; |
69 | 73 | import jenkins.slaves.JnlpAgentReceiver; |
@@ -579,9 +583,8 @@ public AzureVMDeploymentInfo createDeployment( |
579 | 583 |
|
580 | 584 | putVariable(tmp, "vmSize", template.getVirtualMachineSize()); |
581 | 585 | // Grab the username/pass |
582 | | - StandardUsernamePasswordCredentials creds = template.getVMCredentials(); |
| 586 | + StandardUsernameCredentials creds = template.getVMCredentials(); |
583 | 587 |
|
584 | | - putVariable(tmp, "adminUsername", creds.getUsername()); |
585 | 588 | putVariableIfNotBlank(tmp, "storageAccountName", storageAccountName); |
586 | 589 | putVariableIfNotBlank(tmp, "storageAccountType", storageAccountType); |
587 | 590 | putVariableIfNotBlank(tmp, "blobEndpointSuffix", blobEndpointSuffix); |
@@ -618,8 +621,22 @@ public AzureVMDeploymentInfo createDeployment( |
618 | 621 |
|
619 | 622 | final ObjectNode parameters = MAPPER.createObjectNode(); |
620 | 623 |
|
621 | | - defineParameter(tmp, "adminPassword", "secureString"); |
622 | | - putParameter(parameters, "adminPassword", creds.getPassword().getPlainText()); |
| 624 | + defineParameter(tmp, "adminUsername", "string"); |
| 625 | + putParameter(parameters, "adminUsername", creds.getUsername()); |
| 626 | + |
| 627 | + defineParameter(tmp, "authenticationType", "string"); |
| 628 | + defineParameter(tmp, "adminPasswordOrKey", "secureString"); |
| 629 | + if (creds instanceof StandardUsernamePasswordCredentials) { |
| 630 | + StandardUsernamePasswordCredentials passwordCredentials = (StandardUsernamePasswordCredentials) creds; |
| 631 | + putParameter(parameters, "adminPasswordOrKey", passwordCredentials.getPassword().getPlainText()); |
| 632 | + putParameter(parameters, "authenticationType", "password"); |
| 633 | + } else { |
| 634 | + SSHUserPrivateKey sshCredentials = (SSHUserPrivateKey) creds; |
| 635 | + String privateKey = sshCredentials.getPrivateKeys().get(0); |
| 636 | + String rsaPublicKey = KeyDecoder.getPublicKey(privateKey, Secret.toString(sshCredentials.getPassphrase())); |
| 637 | + putParameter(parameters, "adminPasswordOrKey", rsaPublicKey); |
| 638 | + putParameter(parameters, "authenticationType", "key"); |
| 639 | + } |
623 | 640 |
|
624 | 641 | // Register the deployment for cleanup |
625 | 642 | deploymentRegistrar.registerDeployment( |
@@ -2014,18 +2031,23 @@ public List<String> verifyTemplate( |
2014 | 2031 | } |
2015 | 2032 |
|
2016 | 2033 | //verify password |
2017 | | - String adminPassword = ""; |
2018 | | - try { |
2019 | | - StandardUsernamePasswordCredentials creds = AzureUtil.getCredentials(credentialsId); |
2020 | | - adminPassword = creds.getPassword().getPlainText(); |
2021 | | - } catch (AzureCloudException e) { |
2022 | | - LOGGER.log(Level.SEVERE, "Could not load the VM credentials", e); |
2023 | | - } |
| 2034 | + String adminPassword; |
| 2035 | + StandardUsernameCredentials creds = AzureUtil.getCredentials(credentialsId); |
| 2036 | + if (creds instanceof StandardUsernamePasswordCredentials) { |
| 2037 | + adminPassword = ((StandardUsernamePasswordCredentials) creds).getPassword().getPlainText(); |
2024 | 2038 |
|
2025 | | - validationResult = verifyAdminPassword(adminPassword); |
2026 | | - addValidationResultIfFailed(validationResult, errors); |
2027 | | - if (returnOnSingleError && !errors.isEmpty()) { |
2028 | | - return errors; |
| 2039 | + validationResult = verifyAdminPassword(adminPassword); |
| 2040 | + addValidationResultIfFailed(validationResult, errors); |
| 2041 | + if (returnOnSingleError && !errors.isEmpty()) { |
| 2042 | + return errors; |
| 2043 | + } |
| 2044 | + } else { |
| 2045 | + SSHUserPrivateKey sshCredentials = (SSHUserPrivateKey) creds; |
| 2046 | + validationResult = verifySSHKey(sshCredentials.getPrivateKeys(), sshCredentials.getPassphrase()); |
| 2047 | + addValidationResultIfFailed(validationResult, errors); |
| 2048 | + if (returnOnSingleError && !errors.isEmpty()) { |
| 2049 | + return errors; |
| 2050 | + } |
2029 | 2051 | } |
2030 | 2052 |
|
2031 | 2053 | //verify JVM Options |
@@ -2394,6 +2416,26 @@ private static String verifyAdminPassword(String adminPassword) { |
2394 | 2416 | } |
2395 | 2417 | } |
2396 | 2418 |
|
| 2419 | + private static String verifySSHKey(List<String> sshKeys, Secret passphrase) throws RuntimeException { |
| 2420 | + if (sshKeys == null || sshKeys.isEmpty()) { |
| 2421 | + return Messages.AzureVMManagementServiceDelegate_SSH_Missing_Key(); |
| 2422 | + } |
| 2423 | + |
| 2424 | + if (sshKeys.size() > 1) { |
| 2425 | + return Messages.AzureVMManagementServiceDelegate_SSH_Multiple_Keys_Found(); |
| 2426 | + } |
| 2427 | + |
| 2428 | + String sshKey = sshKeys.get(0); |
| 2429 | + try { |
| 2430 | + KeyDecoder.getPublicKey(sshKey, Secret.toString(passphrase)); |
| 2431 | + } catch (IOException | InvalidPassphraseException | SshException e) { |
| 2432 | + LOGGER.log(Level.INFO, "Failed to validate SSH key", e); |
| 2433 | + return Messages.AzureVMManagementServiceDelegate_SSH_Invalid_Key_Format(); |
| 2434 | + } |
| 2435 | + |
| 2436 | + return Constants.OP_SUCCESS; |
| 2437 | + } |
| 2438 | + |
2397 | 2439 | private static String verifyJvmOptions(String jvmOptions) { |
2398 | 2440 | if (StringUtils.isBlank(jvmOptions) || AzureUtil.isValidJvmOption(jvmOptions)) { |
2399 | 2441 | return Constants.OP_SUCCESS; |
|
0 commit comments