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

fix: support type:secret in config.options #1624

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion charmcraft/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,21 @@ class JujuBooleanOption(_BaseJujuOption, frozen=True):
default: bool | None = None


class JujuSecretOption(_BaseJujuOption, frozen=True):
"""A Juju option field containing a secret ID."""

type: Literal["secret"]
# A secret doesn't really make sense, since it's unlikely
# that anyone would know what the secret ID (specific to
# the deployment in a model) is at the time that they are
# writing the config, but included for completeness.
default: Annotated[
str, pydantic.StringConstraints(pattern=r"^secret:[a-z0-9]{20}$")
] | None = None


JujuOption = Annotated[
JujuStringOption | JujuIntOption | JujuFloatOption | JujuBooleanOption,
JujuStringOption | JujuIntOption | JujuFloatOption | JujuBooleanOption | JujuSecretOption,
pydantic.Field(discriminator="type"),
]

Expand Down
10 changes: 10 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,8 @@ def test_load_config_in_charmcraft_yaml(tmp_path, prepare_charmcraft_yaml):
test-bool:
default: true
type: boolean
test-secret:
default: secret:co1s9mnmp25c762drvtg
"""
)
)
Expand All @@ -1146,6 +1148,7 @@ def test_load_config_in_charmcraft_yaml(tmp_path, prepare_charmcraft_yaml):
"test-string": {"description": "test-2", "type": "string"},
"test-float": {"default": 1.23, "type": "float"},
"test-bool": {"default": True, "type": "boolean"},
"test-secret": {"default": "secret:co1s9mnmp25c762drvtg", "type": "secret"},
},
}

Expand Down Expand Up @@ -1179,6 +1182,9 @@ def test_load_config_in_config_yaml(tmp_path, prepare_charmcraft_yaml, prepare_c
test-bool:
default: true
type: boolean
test-secret:
default: secret:co1s9mnmp25c762drvtg
type: secret
"""
),
)
Expand All @@ -1190,6 +1196,7 @@ def test_load_config_in_config_yaml(tmp_path, prepare_charmcraft_yaml, prepare_c
"test-string": {"description": "test-2", "type": "string"},
"test-float": {"default": 1.23, "type": "float"},
"test-bool": {"default": True, "type": "boolean"},
"test-secret": {"default": "secret:co1s9mnmp25c762drvtg", "type": "secret"},
},
}

Expand Down Expand Up @@ -1248,6 +1255,9 @@ def test_load_bad_config_in_charmcraft_yaml(tmp_path, prepare_charmcraft_yaml):
test-bool:
default: true
type: boolean
test-secret:
default: secret:co1s9mnmp25c762drvtg
type: secret
"""
)
)
Expand Down
Loading