-
Notifications
You must be signed in to change notification settings - Fork 1.3k
KVM: Improvements on upload direct download certificates #2995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f0d898
Improvements on upload direct download certificates
nvazquez 547b008
Move upload direct download certificate logic to KVM plugin
nvazquez fc9e153
Extend unit test certificate expiration days
nvazquez efe35d3
Add marvin tests and command to revoke certificates
nvazquez f953268
Review comments
nvazquez 49b24ee
Do not include revoke certificates API
nvazquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...udstack/api/command/admin/direct/download/RevokeTemplateDirectDownloadCertificateCmd.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // 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.apache.cloudstack.api.command.admin.direct.download; | ||
|
|
||
| import com.cloud.exception.ConcurrentOperationException; | ||
| import com.cloud.exception.InsufficientCapacityException; | ||
| import com.cloud.exception.NetworkRuleConflictException; | ||
| import com.cloud.exception.ResourceAllocationException; | ||
| import com.cloud.exception.ResourceUnavailableException; | ||
| import org.apache.cloudstack.acl.RoleType; | ||
| import org.apache.cloudstack.api.APICommand; | ||
| import org.apache.cloudstack.api.ApiConstants; | ||
| import org.apache.cloudstack.api.ApiErrorCode; | ||
| import org.apache.cloudstack.api.BaseCmd; | ||
| import org.apache.cloudstack.api.Parameter; | ||
| import org.apache.cloudstack.api.ServerApiException; | ||
| import org.apache.cloudstack.api.response.SuccessResponse; | ||
| import org.apache.cloudstack.context.CallContext; | ||
| import org.apache.cloudstack.direct.download.DirectDownloadManager; | ||
| import org.apache.log4j.Logger; | ||
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| @APICommand(name = RevokeTemplateDirectDownloadCertificateCmd.APINAME, | ||
| description = "Revoke a certificate alias from a KVM host", | ||
| responseObject = SuccessResponse.class, | ||
| requestHasSensitiveInfo = true, | ||
| responseHasSensitiveInfo = true, | ||
| since = "4.11.3", | ||
| authorized = {RoleType.Admin}) | ||
| public class RevokeTemplateDirectDownloadCertificateCmd extends BaseCmd { | ||
|
|
||
| @Inject | ||
| DirectDownloadManager directDownloadManager; | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(RevokeTemplateDirectDownloadCertificateCmd.class); | ||
| public static final String APINAME = "revokeTemplateDirectDownloadCertificate"; | ||
|
|
||
| @Parameter(name = ApiConstants.NAME, type = BaseCmd.CommandType.STRING, required = true, | ||
| description = "alias of the SSL certificate") | ||
| private String certificateAlias; | ||
|
|
||
| @Parameter(name = ApiConstants.HYPERVISOR, type = BaseCmd.CommandType.STRING, required = true, | ||
| description = "Hypervisor type") | ||
| private String hypervisor; | ||
|
|
||
| @Override | ||
| public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException { | ||
| if (!hypervisor.equalsIgnoreCase("kvm")) { | ||
| throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Currently supporting KVM hosts only"); | ||
| } | ||
| SuccessResponse response = new SuccessResponse(getCommandName()); | ||
| try { | ||
| LOG.debug("Revoking certificate " + certificateAlias + " from " + hypervisor + " hosts"); | ||
| boolean result = directDownloadManager.revokeCertificateAlias(certificateAlias, hypervisor); | ||
| response.setSuccess(result); | ||
| setResponseObject(response); | ||
| } catch (Exception e) { | ||
| throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getCommandName() { | ||
| return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX; | ||
| } | ||
|
|
||
| @Override | ||
| public long getEntityOwnerId() { | ||
| return CallContext.current().getCallingAccount().getId(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...rc/org/apache/cloudstack/agent/directdownload/RevokeDirectDownloadCertificateCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // | ||
| // 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.apache.cloudstack.agent.directdownload; | ||
|
|
||
| import com.cloud.agent.api.Command; | ||
|
|
||
| public class RevokeDirectDownloadCertificateCommand extends Command { | ||
|
|
||
| private String certificateAlias; | ||
|
|
||
| public RevokeDirectDownloadCertificateCommand(final String alias) { | ||
| this.certificateAlias = alias; | ||
| } | ||
|
|
||
| public String getCertificateAlias() { | ||
| return certificateAlias; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean executeInSequence() { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
.../cloud/hypervisor/kvm/resource/wrapper/LibvirtRevokeDirectDownloadCertificateWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // | ||
| // 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 com.cloud.hypervisor.kvm.resource.wrapper; | ||
|
|
||
| import com.cloud.agent.api.Answer; | ||
| import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource; | ||
| import com.cloud.resource.CommandWrapper; | ||
| import com.cloud.resource.ResourceWrapper; | ||
| import com.cloud.utils.PropertiesUtil; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import com.cloud.utils.script.Script; | ||
| import org.apache.cloudstack.agent.directdownload.RevokeDirectDownloadCertificateCommand; | ||
| import org.apache.cloudstack.utils.security.KeyStoreUtils; | ||
| import org.apache.log4j.Logger; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
|
|
||
| import static org.apache.commons.lang.StringUtils.isBlank; | ||
|
|
||
| @ResourceWrapper(handles = RevokeDirectDownloadCertificateCommand.class) | ||
| public class LibvirtRevokeDirectDownloadCertificateWrapper extends CommandWrapper<RevokeDirectDownloadCertificateCommand, Answer, LibvirtComputingResource> { | ||
|
|
||
| private static final Logger s_logger = Logger.getLogger(LibvirtRevokeDirectDownloadCertificateWrapper.class); | ||
|
|
||
| /** | ||
| * Retrieve agent.properties file | ||
| */ | ||
| private File getAgentPropertiesFile() throws FileNotFoundException { | ||
| final File agentFile = PropertiesUtil.findConfigFile("agent.properties"); | ||
| if (agentFile == null) { | ||
| throw new FileNotFoundException("Failed to find agent.properties file"); | ||
| } | ||
| return agentFile; | ||
| } | ||
|
|
||
| /** | ||
| * Get the property 'keystore.passphrase' value from agent.properties file | ||
| */ | ||
| private String getKeystorePassword(File agentFile) { | ||
| String pass = null; | ||
| if (agentFile != null) { | ||
| try { | ||
| pass = PropertiesUtil.loadFromFile(agentFile).getProperty(KeyStoreUtils.KS_PASSPHRASE_PROPERTY); | ||
| } catch (IOException e) { | ||
| s_logger.error("Could not get 'keystore.passphrase' property value due to: " + e.getMessage()); | ||
| } | ||
| } | ||
| return pass; | ||
| } | ||
|
|
||
| /** | ||
| * Get keystore path | ||
| */ | ||
| private String getKeyStoreFilePath(File agentFile) { | ||
| return agentFile.getParent() + "/" + KeyStoreUtils.KS_FILENAME; | ||
| } | ||
|
|
||
| @Override | ||
| public Answer execute(RevokeDirectDownloadCertificateCommand command, LibvirtComputingResource serverResource) { | ||
| String certificateAlias = command.getCertificateAlias(); | ||
| try { | ||
| File agentFile = getAgentPropertiesFile(); | ||
| String privatePassword = getKeystorePassword(agentFile); | ||
| if (isBlank(privatePassword)) { | ||
| return new Answer(command, false, "No password found for keystore: " + KeyStoreUtils.KS_FILENAME); | ||
| } | ||
|
|
||
| final String keyStoreFile = getKeyStoreFilePath(agentFile); | ||
|
|
||
| String checkCmd = String.format("keytool -list -alias %s -keystore %s -storepass %s", | ||
| certificateAlias, keyStoreFile, privatePassword); | ||
| int existsCmdResult = Script.runSimpleBashScriptForExitValue(checkCmd); | ||
| if (existsCmdResult == 1) { | ||
| s_logger.error("Certificate alias " + certificateAlias + " does not exist, no need to revoke it"); | ||
| } else { | ||
| String revokeCmd = String.format("keytool -delete -alias %s -keystore %s -storepass %s", | ||
| certificateAlias, keyStoreFile, privatePassword); | ||
| s_logger.debug("Revoking certificate alias " + certificateAlias + " from keystore " + keyStoreFile); | ||
| Script.runSimpleBashScriptForExitValue(revokeCmd); | ||
| } | ||
| } catch (FileNotFoundException | CloudRuntimeException e) { | ||
| s_logger.error("Error while setting up certificate " + certificateAlias, e); | ||
| return new Answer(command, false, e.getMessage()); | ||
| } | ||
| return new Answer(command); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, this is not used anymore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but moved to specific KVM code. Let me update the description