Variables and Secrets
When writing scripts, you may want to reuse variables, or safely pass secrets to scripts. You can do that with Variables
. getOperate has user-defined variables and contextual variables.
Variables are dynamic values that have a key associated to them and can be retrieved during the execution of a Script or Flow.
All Variables (not just secrets) are encrypted with a workspace specific symmetric key to avoid leakage.
There are two types of Variables in getOperate: user-defined and contextual.
User-defined Variables
User-defined Variables is essentially a key-value store where every user can read, set and share values at given keys as long as they have the privilege to do so.
They are editable in the UI and also readable if they are not Secret Variables.
Inside the Scripts, one would use the getOperate client to interact with the user-defined Variables.
Note that there is a similar API for getting and setting Resources which are simply Variables that can contain any JSON values, not just a string and that are labeled with a Resource Type to be automatically discriminated in the auto-generated form to be of the proper type (e.g. a parameter in TypeScript of type pg: Postgresql is only going to offer a selection over the resources of type postgresql in the auto-generated UI).
There is also a concept of state to share values across script executions.
Contextual variables
Contextual Variables are environment variables automatically set by getOperate. This is how the Deno and Python clients get their implicit credentials to interact with the platform.
See the Contextual
tab on the Variable page for the list of reserved variables and what they are used for.
You can use them in a Script by clicking on “+Context Var”:
Name | Value |
---|---|
GOPRT_WORKSPACE | Workspace id of the current script |
GOPRT_TOKEN | Token ephemeral to the current script with equal permission to the permission of the run (Usable as a bearer token) |
GOPRT_EMAIL | Email of the user that executed the current script |
GOPRT_USERNAME | Username of the user that executed the current script |
GOPRT_BASE_URL | Base URL of this instance |
GOPRT_JOB_ID | Job id of the current script |
GOPRT_JOB_PATH | Path of the script or flow being run if any |
GOPRT_FLOW_JOB_ID | Job id of the encapsulating flow if the job is a flow step |
GOPRT_FLOW_PATH | Path of the encapsulating flow if the job is a flow step |
GOPRT_SCHEDULE_PATH | Path of the schedule if the job of the step or encapsulating step has been triggered by a schedule |
GOPRT_PERMISSIONED_AS | Fully Qualified (u/g) owner name of executor of the job |
GOPRT_STATE_PATH | State resource path unique to a script and its trigger |
Secrets
Secrets are encrypted when stored on getOperate. From a usage standpoint, secrets are kept safe in three different ways:
- Secrets can only be accessed by users with the right permissions, as defined by their path. In addition, secrets can be explicitly shared with users or groups. A secret in
u/alice/secret
will only be accessible byalice
, unless explicitly shared. A secret inf/devops/secret
will be accessible by anyone with read access tof/devops
. - Secrets cannot be viewed outside of scripts. Note that a user could still
print
a secret if they have access to it from a script. - Accessing secrets generates
variables.decrypt_secret
event that ends up in the Audit Logs. It means that you can audit who accesses secrets. Additionally you can audit results, logs and script code for every script run.
Add a Variable or Secret
You can define variables from the Variables page. Like all objects in getOperate, variable ownership is defined by the path - see ownership path prefix.
Variables also have a name, generated from the path, and names are used to access variables from scripts.
A variable can be made secret. In this case, its value will not be visible outside of a script.
Accessing a variable from a script
Add Resources and Variables to Code Editor
You can directly access Variables and Resources from the Code Editor.
Accessing Contextual Variables from a script
See the Contextual tab on the Variable page for the list of reserved variables and what they are used for.
You can use them in a Script by clicking on “+Context Var”:
Reserved variables are passed to the job as environment variables. For example, the ephemeral token is passed as GOPRT_TOKEN
.
Accessing User-defined Variables from a script
There are 2 main ways variables are used within scripts:
Passing variables as parameters to scripts
Variables can be passed as parameters of the script, using the UI based variable picker. Underneath, the variable is passed as a string of the form: $var:<variable_path>
and replaced by the worker at time of execution of the script by fetching the value with the job’s permissions. So the job will fail if the job’s permissions inherited from the caller do not allow access to the variable. This is the same mechanism used for resource, but they use $res:
instead of $var:
.
Fetching them from within a script by using the getOperate client in the respective language
By clicking on + Variable
, you’ll get to pick a variable from your workspace and be able to fetch it from within the script.
Python:
Typescript:
Go:
Bash:
PowerShell:
The last examples in bash and PowerShell showcase well how it works under the hood: It fetches the secret from the API using the job’s permissions through the ephemeral token passed as an environment variable to the job.