-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ApiServices: Use retry support for most GET, PUT, POST and DELETE api…
… calls Useful for retrying any api calls that fail for infra reasons. Added base class for Ssl actions. Added support for saving a cert to a pem file. Testing Done: Manual testing. Bug Number: none Reviewed by: trivial Run Pipeline: NO Former-commit-id: 245b298
- Loading branch information
1 parent
471745b
commit e48927c
Showing
24 changed files
with
307 additions
and
149 deletions.
There are no files selected for viewing
This file contains 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 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
63 changes: 63 additions & 0 deletions
63
core/src/main/java/com/vmware/action/base/BaseSslAction.java
This file contains 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,63 @@ | ||
package com.vmware.action.base; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.Provider; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.vmware.action.BaseAction; | ||
import com.vmware.config.WorkflowConfig; | ||
import com.vmware.util.StringUtils; | ||
import com.vmware.util.exception.FatalException; | ||
|
||
public abstract class BaseSslAction extends BaseAction { | ||
public BaseSslAction(WorkflowConfig config) { | ||
super(config); | ||
} | ||
|
||
protected KeyStore loadKeyStore(File keystoreFile) { | ||
List<String> keystoreTypes = new ArrayList<>(Arrays.asList("JCEKS", "PKCS12", "JKS")); | ||
if (sslConfig.additionalKeystoreTypes != null) { | ||
keystoreTypes.addAll(sslConfig.additionalKeystoreTypes.keySet()); | ||
} | ||
return keystoreTypes.stream().map(keystoreType -> { | ||
try { | ||
KeyStore privateKS = createKeyStore(keystoreType); | ||
privateKS.load(new FileInputStream(keystoreFile), sslConfig.keystorePassword.toCharArray()); | ||
log.info("Loaded keystore {} of type {}", keystoreFile.getAbsolutePath(), keystoreType); | ||
return privateKS; | ||
} catch (Throwable t) { | ||
log.debug("Failed with keystore type {}\n{}", keystoreType, StringUtils.exceptionAsString(t)); | ||
return null; | ||
} | ||
}).filter(Objects::nonNull).findFirst().orElseThrow(() -> new FatalException("Unable to load keystore file {}", | ||
keystoreFile.getPath(), sslConfig.keystorePassword)); | ||
} | ||
|
||
protected KeyStore createKeyStore(String keystoreType) | ||
throws MalformedURLException, ClassNotFoundException, KeyStoreException, IllegalAccessException, InstantiationException { | ||
if (sslConfig.additionalKeystoreTypes == null || !sslConfig.additionalKeystoreTypes.containsKey(keystoreType)) { | ||
return KeyStore.getInstance(keystoreType); | ||
} | ||
List<String> providerInfo = sslConfig.additionalKeystoreTypes.get(keystoreType); | ||
|
||
failIfTrue(providerInfo == null || providerInfo.size() != 2, "Invalid value " + providerInfo + " for keystore " + keystoreType); | ||
String filePath = replacementVariables.replaceVariablesInValue(providerInfo.get(1)); | ||
File providerJarFile = new File(filePath); | ||
failIfTrue(!providerJarFile.exists(), "file " + providerJarFile.getAbsolutePath() + " path not set"); | ||
|
||
log.debug("Using jar file {} for provider {}", providerJarFile.getAbsolutePath(), providerInfo.get(0)); | ||
URL jarUrl = new URL("jar:file:" + providerJarFile.getAbsolutePath() + "!/"); | ||
URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] {jarUrl}, getClass().getClassLoader()); | ||
Provider provider = (Provider) classLoader.loadClass(providerInfo.get(0)).newInstance(); | ||
return KeyStore.getInstance(keystoreType, provider); | ||
} | ||
} |
This file contains 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
55 changes: 55 additions & 0 deletions
55
core/src/main/java/com/vmware/action/filesystem/LoadCertFromKeystore.java
This file contains 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,55 @@ | ||
package com.vmware.action.filesystem; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.security.Key; | ||
import java.security.KeyPair; | ||
import java.security.KeyStore; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.vmware.action.BaseAction; | ||
import com.vmware.action.base.BaseSslAction; | ||
import com.vmware.config.ActionDescription; | ||
import com.vmware.config.WorkflowConfig; | ||
import com.vmware.util.IOUtils; | ||
import com.vmware.util.StringUtils; | ||
import com.vmware.util.exception.FatalException; | ||
|
||
import static com.vmware.util.StringUtils.LINE_SEPARATOR; | ||
|
||
@ActionDescription("Loads a certificate from a keystore") | ||
public class LoadCertFromKeystore extends BaseSslAction { | ||
public LoadCertFromKeystore(WorkflowConfig config) { | ||
super(config); | ||
super.addFailWorkflowIfBlankProperties("sourceFile", "keystoreAlias", "keystoreAliasPassword", "keystorePassword"); | ||
} | ||
|
||
@Override | ||
protected void failWorkflowIfConditionNotMet() { | ||
super.failWorkflowIfConditionNotMet(); | ||
super.failIfTrue(!new File(fileSystemConfig.sourceFile).exists(), "Source file " + fileSystemConfig.sourceFile + " does not exist"); | ||
} | ||
|
||
@Override | ||
public void process() { | ||
try { | ||
log.info("Loading alias {} from keystore {}", sslConfig.keystoreAlias, fileSystemConfig.sourceFile); | ||
KeyStore keystore = loadKeyStore(new File(fileSystemConfig.sourceFile)); | ||
Key privateKey = keystore.getKey(sslConfig.keystoreAlias, sslConfig.keystoreAliasPassword.toCharArray()); | ||
Certificate cert = keystore.getCertificate(sslConfig.keystoreAlias); | ||
|
||
fileSystemConfig.fileData = StringUtils.convertToPem(privateKey) + LINE_SEPARATOR + StringUtils.convertToPem(cert); | ||
} catch (Exception e) { | ||
if (e instanceof RuntimeException) { | ||
throw (RuntimeException) e; | ||
} else { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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,45 @@ | ||
package com.vmware.action.vcd; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
import com.vmware.action.BaseAction; | ||
import com.vmware.config.ActionDescription; | ||
import com.vmware.config.WorkflowConfig; | ||
import com.vmware.util.CollectionUtils; | ||
import com.vmware.util.input.InputUtils; | ||
import com.vmware.vcd.Vcd; | ||
import com.vmware.vcd.domain.QueryResultVMType; | ||
import com.vmware.vcd.domain.QueryResultVMsType; | ||
import com.vmware.vcd.domain.TaskType; | ||
|
||
@ActionDescription("Action for bulk deleting of VMs") | ||
public class DeleteVMs extends BaseAction { | ||
public DeleteVMs(WorkflowConfig config) { | ||
super(config); | ||
} | ||
|
||
@Override | ||
public void process() { | ||
Vcd vcd = serviceLocator.getVcd(); | ||
QueryResultVMsType vmRecords = vcd.queryVms(vcdConfig.queryFilters()); | ||
if (CollectionUtils.isEmpty(vmRecords.record)) { | ||
log.info("No VMs found"); | ||
return; | ||
} | ||
List<String> choices = vmRecords.record.stream().map(QueryResultVMType::getLabel).collect(Collectors.toList()); | ||
List<Integer> vmIndexes = InputUtils.readSelections(choices, "Select VMs to delete", false); | ||
|
||
String vmsToDelete = vmIndexes.stream().map(choices::get).collect(Collectors.joining(",")); | ||
log.info("VMs {} will be deleted", vmsToDelete); | ||
String confirmation = InputUtils.readValueUntilNotBlank("Delete (Y/N)"); | ||
if ("Y".equalsIgnoreCase(confirmation)) { | ||
vmIndexes.stream().map(i -> vmRecords.record.get(i)).forEach(vm -> { | ||
log.info("Deleting VM {}", vm.getLabel()); | ||
TaskType deleteTask = serviceLocator.getVcd().deleteResource(vm.getSelfLink(), true); | ||
vcd.waitForTaskToComplete(deleteTask.href, 1, TimeUnit.MINUTES); | ||
}); | ||
} | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.