-
Notifications
You must be signed in to change notification settings - Fork 2
chore(xtest): Add test of key mgmt create WIP #319
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||
| import random | ||||||||||||||||||||||||||||||||||||||
| import string | ||||||||||||||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||
| import base64 | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -46,12 +48,25 @@ class AttributeRule(enum.IntEnum): | |||||||||||||||||||||||||||||||||||||
| HIERARCHY = 3 | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class SimpleKasPublicKey(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| algorithm: int | ||||||||||||||||||||||||||||||||||||||
| kid: str | ||||||||||||||||||||||||||||||||||||||
| pem: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class SimpleKasKey(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| kas_uri: str | ||||||||||||||||||||||||||||||||||||||
| public_key: SimpleKasPublicKey | ||||||||||||||||||||||||||||||||||||||
| kas_id: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class AttributeValue(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| id: str | ||||||||||||||||||||||||||||||||||||||
| value: str | ||||||||||||||||||||||||||||||||||||||
| fqn: str | None = None | ||||||||||||||||||||||||||||||||||||||
| active: BoolValue | None = None | ||||||||||||||||||||||||||||||||||||||
| metadata: Metadata | None = None | ||||||||||||||||||||||||||||||||||||||
| kas_keys: list[SimpleKasKey] | None = None | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class Attribute(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -206,25 +221,39 @@ class KasPublicKey(BaseModelIgnoreExtra): | |||||||||||||||||||||||||||||||||||||
| algStr: str | None = Field(default=None, exclude=True) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class PrivateKeyCtx(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| key_id: str | ||||||||||||||||||||||||||||||||||||||
| wrapped_key: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class KeyProviderConfig(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| id: str | ||||||||||||||||||||||||||||||||||||||
| name: str | ||||||||||||||||||||||||||||||||||||||
| config_json: bytes | None = None | ||||||||||||||||||||||||||||||||||||||
| metadata: Metadata | None = None | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # Helper model for the structure within key.public_key_ctx in the KAS key creation response | ||||||||||||||||||||||||||||||||||||||
| class KasKeyResponsePublicKeyContext(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| class PublicKeyCtx(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| pem: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # Helper model for the nested "key" object in the KAS key creation response | ||||||||||||||||||||||||||||||||||||||
| class KasKeyResponseKeyDetails(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| class AsymmetricKey(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| id: str | ||||||||||||||||||||||||||||||||||||||
| key_id: str | ||||||||||||||||||||||||||||||||||||||
| key_algorithm: int | ||||||||||||||||||||||||||||||||||||||
| key_status: int | ||||||||||||||||||||||||||||||||||||||
| key_mode: int | ||||||||||||||||||||||||||||||||||||||
| public_key_ctx: KasKeyResponsePublicKeyContext | ||||||||||||||||||||||||||||||||||||||
| public_key_ctx: PublicKeyCtx | ||||||||||||||||||||||||||||||||||||||
| private_key_ctx: PrivateKeyCtx | None = None | ||||||||||||||||||||||||||||||||||||||
| provider_config: KeyProviderConfig | None = None | ||||||||||||||||||||||||||||||||||||||
| metadata: Metadata | None = None | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| class KasKey(BaseModelIgnoreExtra): | ||||||||||||||||||||||||||||||||||||||
| kas_id: str | ||||||||||||||||||||||||||||||||||||||
| key: KasKeyResponseKeyDetails | ||||||||||||||||||||||||||||||||||||||
| key: AsymmetricKey | ||||||||||||||||||||||||||||||||||||||
| kas_uri: str | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
@@ -304,6 +333,32 @@ def kas_registry_create_if_not_present( | |||||||||||||||||||||||||||||||||||||
| return e | ||||||||||||||||||||||||||||||||||||||
| return self.kas_registry_create(uri, key) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def kas_registry_key_create(self, kas: KasEntry, key_id: str | None) -> KasKey: | ||||||||||||||||||||||||||||||||||||||
| cmd = self.otdfctl + "policy kas-registry key create".split() | ||||||||||||||||||||||||||||||||||||||
| if not key_id: | ||||||||||||||||||||||||||||||||||||||
| key_id = "".join(random.choices(string.ascii_lowercase, k=8)) | ||||||||||||||||||||||||||||||||||||||
| cmd += [ | ||||||||||||||||||||||||||||||||||||||
| f"--kas={kas.id}", | ||||||||||||||||||||||||||||||||||||||
| f"--key-id={key_id}", | ||||||||||||||||||||||||||||||||||||||
| "--mode=local", | ||||||||||||||||||||||||||||||||||||||
| "--algorithm=rsa:2048", | ||||||||||||||||||||||||||||||||||||||
| "--wrapping-key-id=wrapping-key-1", | ||||||||||||||||||||||||||||||||||||||
| f"--wrapping-key={os.environ['OT_ROOT_KEY']}", | ||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| logger.info(f"kr-keys-create [{' '.join(cmd)}]") | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| cmd += [f"--wrapping-key={os.environ['OT_ROOT_KEY']}"] | ||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||||||
| process = subprocess.Popen(cmd, stdout=subprocess.PIPE) | ||||||||||||||||||||||||||||||||||||||
| out, err = process.communicate() | ||||||||||||||||||||||||||||||||||||||
| if err: | ||||||||||||||||||||||||||||||||||||||
| print(err, file=sys.stderr) | ||||||||||||||||||||||||||||||||||||||
| raise RuntimeError(f"Error creating KAS key: {err.decode()}") | ||||||||||||||||||||||||||||||||||||||
| if out: | ||||||||||||||||||||||||||||||||||||||
| print(out) | ||||||||||||||||||||||||||||||||||||||
| assert process.returncode == 0 | ||||||||||||||||||||||||||||||||||||||
| return KasKey.model_validate_json(out) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+352
to
+360
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The subprocess execution lacks proper error handling. Capture Style Guide References
Suggested change
Footnotes |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def kas_registry_keys_list(self, kas: KasEntry) -> list[KasKey]: | ||||||||||||||||||||||||||||||||||||||
| cmd = self.otdfctl + "policy kas-registry key list".split() | ||||||||||||||||||||||||||||||||||||||
| cmd += [f"--kas={kas.uri}"] | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Direct access to
os.environ['OT_ROOT_KEY']can raise aKeyErrorif the environment variable is not set. Useos.getenv()with a default value or explicit error handling to prevent this 1.Style Guide References
Footnotes
Using
os.getenvwith a default value or explicit error handling preventsKeyErrorexceptions when environment variables are not set. (link) ↩