use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class ValidateEndpointCommand method validateEndpoint.
/**
* Validate endpoint.
*
* @param url the url
* @param proxy the proxy
* @param timeout the timeout
* @return true/false
*/
@ShellMethod(key = "validate-endpoint", value = "Test connections to an endpoint to verify connectivity, SSL, etc")
public boolean validateEndpoint(@ShellOption(value = { "url", "--url" }, help = "Endpoint URL to test") final String url, @ShellOption(value = { "proxy", "--proxy" }, help = "Proxy address to use when testing the endpoint url", defaultValue = StringUtils.EMPTY) final String proxy, @ShellOption(value = { "timeout", "--timeout" }, help = "Timeout to use in milliseconds when testing the url", defaultValue = "5000") final int timeout) {
try {
LOGGER.info("Trying to connect to [{}]", url);
val conn = createConnection(url, proxy);
LOGGER.info("Setting connection timeout to [{}]", timeout);
conn.setConnectTimeout(timeout);
try (val reader = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
val in = new BufferedReader(reader)) {
in.readLine();
if (conn instanceof HttpURLConnection) {
val code = ((HttpURLConnection) conn).getResponseCode();
LOGGER.info("Response status code received: [{}]", code);
}
LOGGER.info("Successfully connected to url [{}]", url);
return true;
}
} catch (final Exception e) {
LOGGER.info("Could not connect to the host address [{}]", url);
LOGGER.info("The error is: [{}]", e.getMessage());
LOGGER.info("Here are the details:");
LOGGER.error(consolidateExceptionMessages(e));
testBadTlsConnection(url, proxy);
}
return false;
}
use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class JasyptListProvidersCommand method listProviders.
/**
* List providers you can use Jasypt.
*
* @param includeBC whether to include the BouncyCastle provider
*/
@ShellMethod(key = "jasypt-list-providers", value = "List encryption providers with PBE Ciphers you can use with Jasypt")
public void listProviders(@ShellOption(value = { "includeBC", "--includeBC" }, help = "Include Bouncy Castle provider", defaultValue = "false") final Boolean includeBC) {
if (includeBC) {
Security.addProvider(new BouncyCastleProvider());
} else {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
}
val providers = Security.getProviders();
for (val provider : providers) {
val services = provider.getServices();
val algorithms = services.stream().filter(service -> "Cipher".equals(service.getType()) && service.getAlgorithm().contains("PBE")).map(Provider.Service::getAlgorithm).collect(Collectors.toList());
if (!algorithms.isEmpty()) {
LOGGER.info("Provider: Name: [{}] Class: [{}]", provider.getName(), provider.getClass().getName());
for (val algorithm : algorithms) {
LOGGER.info(" - Algorithm: [{}]", algorithm);
}
}
}
}
use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class AddPropertiesToConfigurationCommand method add.
/**
* Add properties to configuration.
*
* @param file the file
* @param group the group
* @throws Exception the exception
*/
@ShellMethod(key = "add-properties", value = "Add properties associated with a CAS group/module to a Properties/Yaml configuration file.")
public static void add(@ShellOption(value = { "file", "--file" }, help = "Path to the CAS configuration file", defaultValue = "/etc/cas/config/cas.properties") final String file, @ShellOption(value = { "group", "--group" }, help = "Group/module whose associated settings should be added to the CAS configuration file") final String group) throws Exception {
if (StringUtils.isBlank(file)) {
LOGGER.warn("Configuration file must be specified");
return;
}
val filePath = new File(file);
if (filePath.exists() && (filePath.isDirectory() || !filePath.canRead() || !filePath.canWrite())) {
LOGGER.warn("Configuration file [{}] is not readable/writable or is not a path to a file", filePath.getCanonicalPath());
return;
}
val results = findProperties(group);
LOGGER.info("Located [{}] properties matching [{}]", results.size(), group);
switch(FilenameUtils.getExtension(filePath.getName()).toLowerCase()) {
case "properties":
createConfigurationFileIfNeeded(filePath);
val props = loadPropertiesFromConfigurationFile(filePath);
writeConfigurationPropertiesToFile(filePath, results, props);
break;
case "yaml":
case "yml":
createConfigurationFileIfNeeded(filePath);
val yamlProps = CasCoreConfigurationUtils.loadYamlProperties(new FileSystemResource(filePath));
writeYamlConfigurationPropertiesToFile(filePath, results, yamlProps);
break;
default:
LOGGER.warn("Configuration file format [{}] is not recognized", filePath.getCanonicalPath());
}
}
use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class FindPropertiesCommand method find.
/**
* Find property.
*
* @param name the name
* @param strict the strict match
* @param summary the summary
*/
@ShellMethod(key = "find", value = "Look up properties associated with a CAS group/module.")
public void find(@ShellOption(value = { "name", "--name" }, help = "Property name regex pattern", defaultValue = ".+") final String name, @ShellOption(value = { "strict-match", "--strict-match" }, help = "Whether pattern should be done in strict-mode which means " + "the matching engine tries to match the entire region for the query.") final boolean strict, @ShellOption(value = { "summary", "--summary" }, help = "Whether results should be presented in summarized mode") final boolean summary) {
val results = find(strict, RegexUtils.createPattern(name));
if (results.isEmpty()) {
LOGGER.info("Could not find any results matching the criteria");
return;
}
results.forEach((k, v) -> {
if (summary) {
LOGGER.info("[{}]=[{}]", k, v.getDefaultValue());
val value = StringUtils.normalizeSpace(v.getShortDescription());
if (StringUtils.isNotBlank(value)) {
LOGGER.info("[{}]", value);
}
} else {
LOGGER.info("Property: [{}]", k);
LOGGER.info("Group: [{}]", StringUtils.substringBeforeLast(k, "."));
LOGGER.info("Default Value: [{}]", ObjectUtils.defaultIfNull(v.getDefaultValue(), "[blank]"));
LOGGER.info("Type: [{}]", v.getType());
LOGGER.info("Summary: [{}]", StringUtils.normalizeSpace(v.getShortDescription()));
LOGGER.info("Description: [{}]", StringUtils.normalizeSpace(v.getDescription()));
LOGGER.info("Deprecated: [{}]", BooleanUtils.toStringYesNo(v.isDeprecated()));
}
LOGGER.info("-".repeat(SEP_LINE_LENGTH));
});
}
use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class ValidateRegisteredServiceCommand method validateService.
/**
* Validate service.
*
* @param file the file
* @param directory the directory
*/
@ShellMethod(key = "validate-service", value = "Validate a given JSON/YAML service definition by path or directory")
public static void validateService(@ShellOption(value = { "file", "--file" }, help = "Path to the JSON/YAML service definition file", defaultValue = StringUtils.EMPTY) final String file, @ShellOption(value = { "directory", "--directory" }, help = "Path to the JSON/YAML service definitions directory", defaultValue = StringUtils.EMPTY) final String directory) {
if (StringUtils.isNotBlank(file)) {
val filePath = new File(file);
validate(filePath);
return;
}
if (StringUtils.isNotBlank(directory)) {
val directoryPath = new File(directory);
if (directoryPath.isDirectory()) {
FileUtils.listFiles(directoryPath, new String[] { "json", "yml", "yaml" }, false).forEach(ValidateRegisteredServiceCommand::validate);
}
}
}
Aggregations