Skip to content

Instantly share code, notes, and snippets.

@Java4all
Created September 23, 2025 12:02
Show Gist options
  • Select an option

  • Save Java4all/1ed47e79fd3a8bd49f93d32470eca26f to your computer and use it in GitHub Desktop.

Select an option

Save Java4all/1ed47e79fd3a8bd49f93d32470eca26f to your computer and use it in GitHub Desktop.
get_cert_venafi
#!/usr/bin/env python3
import yaml
from pathlib import Path
# Custom type to force PyYAML to use a literal block style (|) for multiline strings
class LiteralStr(str):
pass
def represent_literal_str(dumper, data):
# Use the YAML string tag and force literal style
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|')
yaml.add_representer(LiteralStr, represent_literal_str)
def build_yaml(cert_path: str, key_path: str, output_path: str):
cert_pem = Path(cert_path).read_text(encoding='utf-8').strip()
key_pem = Path(key_path).read_text(encoding='utf-8').strip()
# cert: literal block
cert_value = LiteralStr(cert_pem)
# key: quoted single line with \n escapes (as in your example)
key_value = key_pem.replace('\n', '\\n')
data = {
"_format_version": "3.0",
"certificates": [
{
"cert": cert_value,
"key": key_value,
}
]
}
# safe_dump with wide width to avoid line-wrapping, flow disabled for readability
yaml_str = yaml.safe_dump(
data,
default_flow_style=False,
sort_keys=False,
width=4096
)
Path(output_path).write_text(yaml_str, encoding='utf-8')
print(f"Wrote YAML to {output_path}")
if __name__ == "__main__":
# Update these paths as needed
build_yaml("cert.pem", "key.pem", "output.yaml")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment