Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow secrets to be used in pipeline script #4171

Merged
merged 3 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The above snippet access the secrets `MY_ACCESS_KEY` and `MY_SECRET_KEY` previou
Secrets **cannot** be assigned to pipeline parameters.
:::

## Process secrets
## Process directive

Secrets can be access by pipeline processes by using the `secret` directive. For example:

Expand All @@ -71,3 +71,20 @@ The secrets are made available in the process context running the command script
:::{note}
This feature is only available when using the local or grid executors (Slurm, Grid Engine, etc). The AWS Batch executor allows the use of secrets when deploying the pipeline execution via [Nextflow Tower](https://seqera.io/blog/pipeline-secrets-secure-handling-of-sensitive-information-in-tower/).
:::

## Pipeline script

:::{versionadded} 23.09.0-edge
:::

Secrets can be accessed in the pipeline script using the `secrets` variable. For example:

```groovy
workflow.onComplete {
println("The secret is: ${secrets.MY_SECRET}")
}
```

:::{note}
This feature is only available when using the local or grid executors (Slurm, Grid Engine, etc). The AWS Batch executor allows the use of secrets when deploying the pipeline execution via [Nextflow Tower](https://seqera.io/blog/pipeline-secrets-secure-handling-of-sensitive-information-in-tower/).
pditommaso marked this conversation as resolved.
Show resolved Hide resolved
:::
21 changes: 19 additions & 2 deletions modules/nextflow/src/main/groovy/nextflow/script/BaseScript.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import nextflow.NF
import nextflow.NextflowMeta
import nextflow.Session
import nextflow.exception.AbortOperationException
import nextflow.secret.SecretsLoader
import nextflow.secret.SecretsProvider
/**
* Any user defined script will extends this class, it provides the base execution context
*
Expand Down Expand Up @@ -85,8 +87,23 @@ abstract class BaseScript extends Script implements ExecutionContext {
binding.setVariable( 'workDir', session.workDir )
binding.setVariable( 'workflow', session.workflowMetadata )
binding.setVariable( 'nextflow', NextflowMeta.instance )
binding.setVariable('launchDir', Paths.get('./').toRealPath())
binding.setVariable('moduleDir', meta.moduleDir )
binding.setVariable( 'launchDir', Paths.get('./').toRealPath() )
binding.setVariable( 'moduleDir', meta.moduleDir )
binding.setVariable( 'secrets', makeSecretsContext() )
}

def makeSecretsContext() {
if( !SecretsLoader.isEnabled() )
return null

return new Object() {
private SecretsProvider provider = SecretsLoader.instance.load()

@Override
def getProperty(String name) {
provider.getSecret(name)?.value
}
}
}

protected process( String name, Closure<BodyDef> body ) {
Expand Down
Loading