use of org.springframework.boot.configurationmetadata.ConfigurationMetadataGroup in project cas by apereo.
the class GenerateCryptoKeysCommand method generateKey.
/**
* Generate key.
*
* @param name the name
*/
@CliCommand(value = "generate-key", help = "Generate signing/encryption crypto keys for CAS settings")
public void generateKey(@CliOption(key = { "group" }, help = "Property group that holds the key (i.e. cas.webflow). The group must have a child category of 'crypto'.", mandatory = true, specifiedDefaultValue = "", unspecifiedDefaultValue = "", optionContext = "Property name for that holds the key") final String name) {
/*
Because the command is used both from the shell and CLI,
we need to validate parameters again.
*/
if (StringUtils.isBlank(name)) {
LOGGER.warn("No property/setting name is specified for signing/encryption key generation.");
return;
}
final CasConfigurationMetadataRepository repository = new CasConfigurationMetadataRepository();
final String cryptoGroup = name.concat(".crypto");
repository.getRepository().getAllGroups().entrySet().stream().filter(e -> e.getKey().startsWith(cryptoGroup)).forEach(e -> {
final ConfigurationMetadataGroup grp = e.getValue();
grp.getSources().forEach(Unchecked.biConsumer((k, v) -> {
final Object obj = ClassUtils.getClass(k, true).getDeclaredConstructor().newInstance();
if (obj instanceof EncryptionJwtSigningJwtCryptographyProperties) {
final EncryptionJwtSigningJwtCryptographyProperties crypto = (EncryptionJwtSigningJwtCryptographyProperties) obj;
LOGGER.info(cryptoGroup.concat(".encryption.key=" + EncodingUtils.generateJsonWebKey(crypto.getEncryption().getKeySize())));
LOGGER.info(cryptoGroup.concat(".signing.key=" + EncodingUtils.generateJsonWebKey(crypto.getSigning().getKeySize())));
} else if (obj instanceof EncryptionRandomizedSigningJwtCryptographyProperties) {
final EncryptionRandomizedSigningJwtCryptographyProperties crypto = (EncryptionRandomizedSigningJwtCryptographyProperties) obj;
final String encKey = new Base64RandomStringGenerator(crypto.getEncryption().getKeySize()).getNewString();
LOGGER.info(cryptoGroup.concat(".encryption.key=" + encKey));
LOGGER.info(cryptoGroup.concat(".signing.key=" + EncodingUtils.generateJsonWebKey(crypto.getSigning().getKeySize())));
}
}));
});
}
Aggregations