-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add support for host certificate deletion, display and installation #483
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 | ||||
---|---|---|---|---|---|---|
|
@@ -44,10 +44,13 @@ | |||||
SECURITY_PROFILE_PATH = ( | ||||||
"data/openconfig-pki:pki/security-profiles/security-profile" | ||||||
) | ||||||
INSTALL_PATH = "operations/openconfig-pki-rpc:crypto-host-cert-install" | ||||||
DELETE_PATH = "operations/openconfig-pki-rpc:crypto-host-cert-delete" | ||||||
|
||||||
PATCH = "patch" | ||||||
DELETE = "delete" | ||||||
PUT = "put" | ||||||
POST = 'post' | ||||||
TEST_KEYS = [ | ||||||
{"security_profiles": {"profile_name": ""}}, | ||||||
{"trust_stores": {"name": ""}}, | ||||||
|
@@ -322,6 +325,20 @@ def _state_merged(self, want, have, diff): | |||||
} | ||||||
) | ||||||
|
||||||
# Handle INSTALL for certificates | ||||||
for cert in commands.get("host_cert") or []: | ||||||
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. Can you explain why the empty list is necessary here? I don't immediately see a function of that or clause here. |
||||||
if isinstance(cert, dict) and cert.get("file_path") and cert.get("key_path"): | ||||||
requests.append( | ||||||
{ | ||||||
"path": INSTALL_PATH, | ||||||
"method": POST, | ||||||
"data": {"openconfig-pki-rpc:input": | ||||||
{"file-path": cert.get('file_path'), | ||||||
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.
Suggested change
A little nitpick but for consistency you should use double quotes here |
||||||
"key-path": cert.get('key_path'), | ||||||
"fips-cert": cert.get('fips_cert')}} | ||||||
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.
Suggested change
A little nitpick but for consistency you should use double quotes here |
||||||
} | ||||||
) | ||||||
|
||||||
if commands and requests: | ||||||
commands = update_states(commands, "merged") | ||||||
else: | ||||||
|
@@ -371,6 +388,22 @@ def _state_deleted(self, want, have, diff): | |||||
if ts.get("name") in current_ts: | ||||||
requests.extend(mk_ts_delete(ts, have)) | ||||||
|
||||||
# Handle DELETE for certificates | ||||||
for cert in commands.get("host_cert") or []: | ||||||
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. Can you explain why the empty list is necessary here? I don't immediately see a function of that or clause here. |
||||||
if isinstance(cert, dict) and cert.get("file_name"): | ||||||
requests.append( | ||||||
{ | ||||||
"path": DELETE_PATH, | ||||||
"method": POST, | ||||||
"data": { | ||||||
"openconfig-pki-rpc:input": { | ||||||
"file-name": cert.get("file_name"), | ||||||
"fips-cert": cert.get("fips_cert", False), | ||||||
} | ||||||
}, | ||||||
} | ||||||
) | ||||||
|
||||||
if commands and requests: | ||||||
commands = update_states([commands], "deleted") | ||||||
else: | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ | |
|
||
pki_path = "data/openconfig-pki:pki/" | ||
security_profiles_path = "data/openconfig-pki:pki/security-profiles" | ||
|
||
cert_get_path = "operations/openconfig-pki-rpc:crypto-host-cert-display" | ||
|
||
class PkiFacts(object): | ||
"""The sonic pki fact class""" | ||
|
@@ -108,6 +108,21 @@ def populate_facts(self, connection, ansible_facts, data=None): | |
|
||
objs["trust_stores"] = rep_conf | ||
|
||
cert_data = self.get_cert() | ||
cert_list = [] | ||
if cert_data and len(cert_data) > 0 and "openconfig-pki-rpc:output" in cert_data[0][1]: | ||
cert_data = cert_data[0][1].get("openconfig-pki-rpc:output", {}) | ||
if cert_data and "filename" in cert_data: | ||
filenames = cert_data.get("filename", []) or [] | ||
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. Does the "or []" do anything in this statement? If the get() does not find anything with the key "filename" it will return an empty list. I am unclear on what the "or []" snippet does here. |
||
fips_certs = cert_data.get("fips-cert", []) or [] | ||
if len(filenames) > 0 and len(filenames) == len(fips_certs): | ||
for file_name, fips_cert in zip(filenames, fips_certs): | ||
cert_list.append({ | ||
"file_name": file_name, | ||
"fips_cert": fips_cert, | ||
}) | ||
objs["host_cert"] = cert_list | ||
|
||
ansible_facts["ansible_network_resources"].pop("pki", None) | ||
facts = {} | ||
if objs: | ||
|
@@ -131,6 +146,22 @@ def get_pki(self): | |
|
||
return response | ||
|
||
def get_cert(self): | ||
request = { | ||
"path": cert_get_path, | ||
"method": "post", | ||
"data": {"openconfig-pki-rpc:input": | ||
{"file-name": "all"}} | ||
} | ||
try: | ||
response = edit_config( | ||
self._module, to_request(self._module, request) | ||
) | ||
except ConnectionError as exc: | ||
self._module.fail_json(msg=str(exc), code=exc.code) | ||
|
||
return response | ||
|
||
def render_config(self, spec, conf): | ||
""" | ||
Render config as dictionary structure and delete keys | ||
|
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.
A little nitpick but for consistency you should use double quotes here