Repository: PrefectHQ/prefect

Issue:
--env option in Agent CLI cannot parse values with '='
https://github.com/PrefectHQ/prefect/blob/559330bb549b9c92c413948b615509b6c6d33620/src/prefect/cli/agent.py#L100

I'm trying to set a connection string via the --env option. I.e.

start --env CONN=Driver=foo;host=sername;user=myuser

.split("=", 2) returns an array with 3 values which breaks the dict. I believe the intent was to set the max param to 1 resulting in an array with 2 values.


Discussion:
Ah yeah this is going to happen when a value has a `=` in it. We should implement a different/separate way for allowing env vars without reserved characters. Going to mark this as a `bug`.

In the meantime you can also set env vars through the config with:

```
export PREFECT__CLOUD__AGENT__ENV_VARS__YOURVAR=something
```

or through the `~/.prefect/config.toml`

```
[cloud.agent.env_vars]
YOUR_VAR = "something"
```

Resolving patch:
diff --git a/src/prefect/cli/agent.py b/src/prefect/cli/agent.py
--- a/src/prefect/cli/agent.py
+++ b/src/prefect/cli/agent.py
@@ -97,7 +97,7 @@ def decorator(func):
 
 def start_agent(agent_cls, token, api, label, env, log_level, **kwargs):
     labels = sorted(set(label))
-    env_vars = dict(e.split("=", 2) for e in env)
+    env_vars = dict(e.split("=", 1) for e in env)
 
     tmp_config = {
         "cloud.agent.auth_token": token or config.cloud.agent.auth_token,
@@ -177,7 +177,7 @@ def install(label, env, import_paths, **kwargs):
 
     conf = LocalAgent.generate_supervisor_conf(
         labels=sorted(set(label)),
-        env_vars=dict(e.split("=", 2) for e in env),
+        env_vars=dict(e.split("=", 1) for e in env),
         import_paths=list(import_paths),
         **kwargs,
     )
@@ -313,7 +313,7 @@ def install(label, env, **kwargs):
     from prefect.agent.kubernetes import KubernetesAgent
 
     deployment = KubernetesAgent.generate_deployment_yaml(
-        labels=sorted(set(label)), env_vars=dict(e.split("=", 2) for e in env), **kwargs
+        labels=sorted(set(label)), env_vars=dict(e.split("=", 1) for e in env), **kwargs
     )
     click.echo(deployment)
 
@@ -354,7 +354,7 @@ def start(ctx, **kwargs):
     warn_fargate_deprecated()
 
     for item in ctx.args:
-        k, v = item.replace("--", "").split("=", 2)
+        k, v = item.replace("--", "").split("=", 1)
         kwargs[k] = v
 
     start_agent(FargateAgent, _called_from_cli=True, **kwargs)
@@ -637,7 +637,7 @@ def start(
     kwargs = dict()
     for item in ctx.args:
         item = item.replace("--", "")
-        kwargs.update([item.split("=")])
+        kwargs.update([item.split("=", 1)])
 
     tmp_config = {
         "cloud.agent.auth_token": token or config.cloud.agent.auth_token,
@@ -668,7 +668,7 @@ def start(
 
         env_vars = dict()
         for env_var in env:
-            k, v = env_var.split("=")
+            k, v = env_var.split("=", 1)
             env_vars[k] = v
 
         labels = sorted(set(label))
@@ -921,7 +921,7 @@ def install(
 
     env_vars = dict()
     for env_var in env:
-        k, v = env_var.split("=")
+        k, v = env_var.split("=", 1)
         env_vars[k] = v
 
     labels = sorted(set(label))


Test patch:
diff --git a/tests/cli/test_agent.py b/tests/cli/test_agent.py
--- a/tests/cli/test_agent.py
+++ b/tests/cli/test_agent.py
@@ -139,7 +139,7 @@ def test_agent_start(
         (
             "--token TEST-TOKEN --api TEST-API --agent-config-id TEST-AGENT-CONFIG-ID "
             "--name TEST-NAME -l label1 -l label2 -e KEY1=VALUE1 -e KEY2=VALUE2 "
-            "--max-polls 10 --agent-address 127.0.0.1:8080"
+            "-e KEY3=VALUE=WITH=EQUALS --max-polls 10 --agent-address 127.0.0.1:8080"
         ).split()
     )
     if deprecated:
@@ -161,7 +161,7 @@ def test_agent_start(
         "agent_config_id": "TEST-AGENT-CONFIG-ID",
         "name": "TEST-NAME",
         "labels": ["label1", "label2"],
-        "env_vars": {"KEY1": "VALUE1", "KEY2": "VALUE2"},
+        "env_vars": {"KEY1": "VALUE1", "KEY2": "VALUE2", "KEY3": "VALUE=WITH=EQUALS"},
         "max_polls": 10,
         "agent_address": "127.0.0.1:8080",
         "no_cloud_logs": False,