Search in sources :

Example 11 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class GenerateYamlRegisteredServiceCommand method generateYaml.

/**
 * Validate service.
 *
 * @param file        the file
 * @param destination the destination
 * @return the file
 */
@ShellMethod(key = "generate-yaml", value = "Generate a YAML registered service definition")
public static File generateYaml(@ShellOption(value = { "file", "--file" }, help = "Path to the JSON service definition file") final String file, @ShellOption(value = { "destination", "--destination" }, help = "Path to the destination YAML service definition file") final String destination) {
    val filePath = new File(file);
    val result = StringUtils.isBlank(destination) ? null : new File(destination);
    generate(filePath, result);
    return filePath;
}
Also used : lombok.val(lombok.val) File(java.io.File) ShellMethod(org.springframework.shell.standard.ShellMethod)

Example 12 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class GenerateJwtCommand method generate.

/**
 * Generate.
 *
 * @param signingSecretSize    the signing secret size
 * @param encryptionSecretSize the encryption secret size
 * @param signingAlgorithm     the signing algorithm
 * @param encryptionAlgorithm  the encryption algorithm
 * @param encryptionMethod     the encryption algorithm
 * @param subject              the subject
 */
@ShellMethod(key = "generate-jwt", value = "Generate a JWT with given size and algorithm for signing and encryption.")
public static void generate(@ShellOption(value = { "signingSecretSize", "--signingSecretSize" }, help = "Size of the signing secret", defaultValue = StringUtils.EMPTY + DEFAULT_SIGNING_SECRET_SIZE) final int signingSecretSize, @ShellOption(value = { "encryptionSecretSize", "--encryptionSecretSize" }, help = "Size of the encryption secret", defaultValue = StringUtils.EMPTY + DEFAULT_ENCRYPTION_SECRET_SIZE) final int encryptionSecretSize, @ShellOption(value = { "signingAlgorithm", "--signingAlgorithm" }, help = "Algorithm to use for signing", defaultValue = DEFAULT_SIGNING_ALGORITHM) final String signingAlgorithm, @ShellOption(value = { "encryptionAlgorithm", "--encryptionAlgorithm" }, help = "Algorithm to use for encryption", defaultValue = DEFAULT_ENCRYPTION_ALGORITHM) final String encryptionAlgorithm, @ShellOption(value = { "encryptionMethod", "--encryptionMethod" }, help = "Method to use for encryption", defaultValue = DEFAULT_ENCRYPTION_METHOD) final String encryptionMethod, @ShellOption(value = { "subject", "--subject" }, help = "Subject to use for the JWT") final String subject) {
    val g = new JwtGenerator();
    configureJwtSigning(signingSecretSize, signingAlgorithm, g);
    configureJwtEncryption(encryptionSecretSize, encryptionAlgorithm, encryptionMethod, g);
    val profile = new CommonProfile();
    profile.setId(subject);
    val repeat = "=".repeat(SEP_LENGTH);
    LOGGER.debug(repeat);
    LOGGER.info("\nGenerating JWT for subject [{}] with signing key size [{}], signing algorithm [{}], " + "encryption key size [{}], encryption method [{}] and encryption algorithm [{}]\n", subject, signingSecretSize, signingAlgorithm, encryptionSecretSize, encryptionMethod, encryptionAlgorithm);
    LOGGER.debug(repeat);
    val token = g.generate(profile);
    LOGGER.info("==== JWT ====\n[{}]", token);
}
Also used : lombok.val(lombok.val) JwtGenerator(org.pac4j.jwt.profile.JwtGenerator) CommonProfile(org.pac4j.core.profile.CommonProfile) ShellMethod(org.springframework.shell.standard.ShellMethod)

Example 13 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class AnonymousUsernameAttributeProviderCommand method generateUsername.

/**
 * Generate username.
 *
 * @param username the username
 * @param service  the service
 * @param salt     the salt
 * @return the string
 */
@ShellMethod(key = "generate-anonymous-user", value = "Generate an anonymous (persistent) username identifier")
public String generateUsername(@ShellOption(value = { "username", "--username" }, help = "Authenticated username") final String username, @ShellOption(value = { "service", "--service" }, help = "Service application URL for which CAS may generate the identifier") final String service, @ShellOption(value = { "salt", "--salt" }, help = "Salt used to generate and encode the anonymous identifier") final String salt) {
    val generator = new ShibbolethCompatiblePersistentIdGenerator(salt);
    val id = generator.generate(username, service);
    LOGGER.info("Generated identifier:\n[{}]", id);
    return id;
}
Also used : lombok.val(lombok.val) ShibbolethCompatiblePersistentIdGenerator(org.apereo.cas.authentication.principal.ShibbolethCompatiblePersistentIdGenerator) ShellMethod(org.springframework.shell.standard.ShellMethod)

Example 14 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class GenerateDdlCommand method generate.

/**
 * Generate.
 *
 * @param file         the file
 * @param dialect      the dialect
 * @param jdbcUrl      the jdbc url
 * @param delimiter    the delimiter
 * @param pretty       the pretty
 * @param dropSchema   the drop schema
 * @param createSchema the create schema
 * @param haltOnError  the halt on error
 * @return the file
 */
@ShellMethod(key = "generate-ddl", value = "Generate database DDL scripts")
public String generate(@ShellOption(value = { "file", "--file" }, help = "DDL file to contain to generated script", defaultValue = "/etc/cas/config/cas-db-schema.sql") final String file, @ShellOption(value = { "dialect", "--dialect" }, help = "Database dialect class", defaultValue = "HSQL") final String dialect, @ShellOption(value = { "url", "--url" }, help = "JDBC database connection URL", defaultValue = "jdbc:hsqldb:mem:cas") final String jdbcUrl, @ShellOption(value = { "delimiter", "--delimiter" }, help = "Delimiter to use for separation of statements when generating SQL", defaultValue = ";") final String delimiter, @ShellOption(value = { "pretty", "--pretty" }, help = "Format DDL scripts and pretty-print the output", defaultValue = "false") final Boolean pretty, @ShellOption(value = { "dropSchema", "--dropSchema" }, help = "Generate DROP SQL statements in the DDL", defaultValue = "false") final Boolean dropSchema, @ShellOption(value = { "createSchema", "--createSchema" }, help = "Generate DROP SQL statements in the DDL", defaultValue = "false") final Boolean createSchema, @ShellOption(value = { "haltOnError", "--haltOnError" }, help = "Halt if an error occurs during the generation process", defaultValue = "false") final Boolean haltOnError) {
    LOGGER.info("Requested database dialect type [{}]", dialect);
    val dialectName = DIALECTS_MAP.getOrDefault(dialect.trim(), dialect);
    LOGGER.info("Using database dialect class [{}]", dialectName);
    if (!dialectName.contains(".")) {
        LOGGER.warn("Dialect name must be a fully qualified class name. Supported dialects by default are [{}] " + "or you may specify the dialect class directly", DIALECTS_MAP.keySet());
        return null;
    }
    val svcRegistry = new StandardServiceRegistryBuilder();
    val settings = new HashMap<String, String>();
    settings.put(AvailableSettings.DIALECT, dialectName);
    settings.put(AvailableSettings.URL, jdbcUrl);
    settings.put(AvailableSettings.HBM2DDL_AUTO, "none");
    settings.put(AvailableSettings.SHOW_SQL, "true");
    svcRegistry.applySettings(settings);
    LOGGER.info("Collecting entity metadata sources...");
    val metadata = new MetadataSources(svcRegistry.build());
    REFLECTIONS.getTypesAnnotatedWith(MappedSuperclass.class).forEach(metadata::addAnnotatedClass);
    REFLECTIONS.getTypesAnnotatedWith(Entity.class).forEach(metadata::addAnnotatedClass);
    val metadataSources = metadata.buildMetadata();
    val export = new SchemaExport();
    export.setDelimiter(delimiter);
    export.setOutputFile(file);
    export.setFormat(BooleanUtils.toBoolean(pretty));
    export.setHaltOnError(BooleanUtils.toBoolean(haltOnError));
    export.setManageNamespaces(true);
    val action = getAction(BooleanUtils.toBoolean(dropSchema), BooleanUtils.toBoolean(createSchema));
    LOGGER.info("Exporting Database DDL to [{}] using dialect [{}] with export type set to [{}]", file, dialect, action);
    export.execute(EnumSet.of(TargetType.SCRIPT, TargetType.STDOUT), SchemaExport.Action.BOTH, metadataSources);
    LOGGER.info("Database DDL is exported to [{}]", file);
    return file;
}
Also used : lombok.val(lombok.val) Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) HashMap(java.util.HashMap) MappedSuperclass(javax.persistence.MappedSuperclass) MetadataSources(org.hibernate.boot.MetadataSources) SchemaExport(org.hibernate.tool.hbm2ddl.SchemaExport) ShellMethod(org.springframework.shell.standard.ShellMethod)

Example 15 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class StringableCipherExecutorCommand method cipher.

/**
 * Cipher text.
 *
 * @param value                   the value
 * @param secretKeyEncryption     the secret key encryption
 * @param secretKeySigning        the secret key signing
 * @param secretKeyEncryptionSize the secret key encryption size
 * @param secretKeySigningSize    the secret key signing size
 * @param encryptionEnabled       the encryption enabled
 * @param signingEnabled          the signing enabled
 * @return the string
 */
@SneakyThrows
@ShellMethod(key = { "cipher-text", "encode-text" }, value = "Sign and encrypt text data using keys")
public String cipher(@ShellOption(value = { "value", "--value" }, defaultValue = ShellOption.NULL, help = "Value to put through the cipher") final String value, @ShellOption(value = { "encryption-key", "--encryption-key" }, defaultValue = ShellOption.NULL, help = "Encryption key") final String secretKeyEncryption, @ShellOption(value = { "signing-key", "--signing-key" }, defaultValue = ShellOption.NULL, help = "Signing key") final String secretKeySigning, @ShellOption(value = { "encryption-key-size", "--encryption-key-size" }, defaultValue = StringUtils.EMPTY + CipherExecutor.DEFAULT_STRINGABLE_ENCRYPTION_KEY_SIZE, help = "Encryption key size") final int secretKeyEncryptionSize, @ShellOption(value = { "signing-key-size", "--signing-key-size" }, defaultValue = StringUtils.EMPTY + CipherExecutor.DEFAULT_STRINGABLE_SIGNING_KEY_SIZE, help = "Signing key size") final int secretKeySigningSize, @ShellOption(value = { "enable-encryption", "--enable-encryption" }, defaultValue = "true", help = "Whether value should be encrypted") final boolean encryptionEnabled, @ShellOption(value = { "enable-signing", "--enable-signing" }, defaultValue = "true", help = "Whether value should be signed") final boolean signingEnabled) {
    var toEncode = value;
    if (value != null && new File(value).exists()) {
        toEncode = FileUtils.readFileToString(new File(value), StandardCharsets.UTF_8);
    }
    if (StringUtils.isNotBlank(toEncode)) {
        val cipher = new ShellStringCipherExecutor(secretKeyEncryption, secretKeySigning, encryptionEnabled, signingEnabled, secretKeySigningSize, secretKeyEncryptionSize);
        val encoded = cipher.encode(toEncode);
        LOGGER.info("Encoded value: [{}]", encoded);
        return encoded;
    }
    return null;
}
Also used : lombok.val(lombok.val) File(java.io.File) ShellMethod(org.springframework.shell.standard.ShellMethod) SneakyThrows(lombok.SneakyThrows)

Aggregations

lombok.val (lombok.val)18 ShellMethod (org.springframework.shell.standard.ShellMethod)18 File (java.io.File)7 CasConfigurationJasyptCipherExecutor (org.apereo.cas.configuration.support.CasConfigurationJasyptCipherExecutor)3 SneakyThrows (lombok.SneakyThrows)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Writer (java.io.Writer)1 HttpURLConnection (java.net.HttpURLConnection)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Provider (java.security.Provider)1 HashMap (java.util.HashMap)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 Entity (javax.persistence.Entity)1 MappedSuperclass (javax.persistence.MappedSuperclass)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1