ulisesbocchio / jasypt-spring-boot

Jasypt integration for Spring boot
MIT License
2.87k stars 514 forks source link

Gradle Plugin Support #349

Open wayne-truong opened 1 year ago

wayne-truong commented 1 year ago

Gradle is mainly used where I work. Could someone add a gradle plugin equivalent of the maven plugin?

pcbowers commented 1 year ago

While not a plugin, you can add the following at the bottom of your build.gradle file to add an encrypt and decrypt task. You may need to modify the args passed to the two tasks if you apply different defaults (i.e. use a different algorithm or ivGenerator). You may also want to tweak it if you use a different environment variable or want to surface different environment variables:

abstract class JasyptCliTask extends JavaExec {

    @Input
    String input

    @Input
    String password

    @Input
    Boolean isEncryptionCli

    @TaskAction
    @Override
    void exec() {
        def jasyptCliClass = isEncryptionCli ? 'JasyptPBEStringEncryptionCLI' : 'JasyptPBEStringDecryptionCLI'

        project.javaexec {
            classpath = project.sourceSets.main.runtimeClasspath
            mainClass.set('org.jasypt.intf.cli.' + jasyptCliClass)
            args = [
                'input="' + input + '"',
                'password="' + password + '"',
                'algorithm=PBEWITHHMACSHA512ANDAES_256',
                'ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator',
                'verbose=false'
            ]
        }
    }
}

tasks.register('encrypt', JasyptCliTask) {
    group 'jasypt'
    description 'Encrypts a string using the Jasypt CLI'

    input = project.properties['input'] ?: null
    password = project.properties['password'] ?: System.getenv('JASYPT_ENCRYPTOR_PASSWORD') ?: null
    isEncryptionCli = true
}

tasks.register('decrypt', JasyptCliTask) {
    group 'jasypt'
    description 'Decrypts a string using the Jasypt CLI'

    input = project.properties['input'] ?: null
    password = project.properties['password'] ?: System.getenv('JASYPT_ENCRYPTOR_PASSWORD') ?: null
    isEncryptionCli = false
}