Skip to content

Commit 2ce6c06

Browse files
authored
added set encrypted value methods (#65)
added methods to add encrypted values by router and group
1 parent 577bb48 commit 2ce6c06

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

ncm/ncm/ncm.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import sys
107107
import os
108108
import json
109+
import uuid
109110
from typing import Union, Optional, Dict, Any
110111

111112

@@ -2348,6 +2349,120 @@ def set_ncm_api_keys_by_group(self, group_id=None, group_name=None, x_ecm_api_id
23482349
result = self._return_handler(ncm.status_code, ncm.text, call_type)
23492350
return result
23502351

2352+
def set_encrypted_value_by_router(self, router_id=None, router_name=None, name: str = None, key: str = None):
2353+
"""
2354+
This method sets an encrypted value using the router's certificate management configuration
2355+
:param router_id: ID of router to update (optional if router_name is provided)
2356+
:param router_name: Name of router to update (optional if router_id is provided)
2357+
:param name: Name of the encrypted value
2358+
:param key: Key value to encrypt
2359+
:return:
2360+
"""
2361+
call_type = 'Set Encrypted Value'
2362+
2363+
if not router_id and not router_name:
2364+
raise Exception("Either router_id or router_name must be provided")
2365+
2366+
if not name or not key:
2367+
raise Exception("Both name and key parameters must be provided")
2368+
2369+
if router_name and not router_id:
2370+
try:
2371+
router_id = self.get_router_by_name(router_name)['id']
2372+
except Exception as e:
2373+
raise Exception(f"Router with name '{router_name}' not found: {str(e)}")
2374+
2375+
response = self.session.get(
2376+
'{0}/configuration_managers/?router.id={1}&fields=id,configuration'.format(
2377+
self.base_url,
2378+
str(router_id))) # Get Configuration Managers ID
2379+
response = json.loads(response.content.decode(
2380+
"utf-8")) # Decode the response and make it a dictionary
2381+
config_man_id = response['data'][0][
2382+
'id'] # get the Configuration Managers ID from response
2383+
2384+
x509 = "-----BEGIN CERTIFICATE-----\nMIIB0jCCATugAwIBAgIUIF7Bygk4C0l0ikNv00u98unXZ9kwDQYJKoZIhvcNAQEL\nBQAwFzEVMBMGA1UEAwwMTmV0Q2xvdWQgQVBJMB4XDTI1MDYwNDA5MjYzNloXDTM1\nMDYwMzA5MjYzNlowFzEVMBMGA1UEAwwMTmV0Q2xvdWQgQVBJMIGfMA0GCSqGSIb3\nDQEBAQUAA4GNADCBiQKBgQDHWAtI42kixQBU9yZdiTmakxlj1OGfXlYGYDTMr/Q7\neFRZHLxJwIwrfV4UjJSvXkeo9ui1JNXzfQzDwZXdJKEdFM0fBpu9TD/cyetz9lCs\nh5YL1aC0IcH/liZwGt/z2X4snqe3KADHjy8Dl/5ib16vTC/FuRm02Bf8wVJ0c/sr\nhwIDAQABoxswGTAJBgNVHREEAjAAMAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEL\nBQADgYEAB5UavmWqkT7MXnt2/RE2qdtoTw4PfWIo+I2O7FAwJmHISubp3LW1vCn0\nRIsnyscH+BZmQkZOk3AYhLikgSky64HRHK32HXrLr79ku4as0drJzxuVOOKJn1+6\nDiNWTpAhzT55WU3fZ9H6FRvfEls0ZtLia/yiZ60rH01RO0lo2bs=\n-----END CERTIFICATE-----\n"
2385+
2386+
# Generate a unique ID for the certificate
2387+
cert_id = str(uuid.uuid4())
2388+
2389+
payload = {
2390+
"configuration": [
2391+
{
2392+
"certmgmt": {
2393+
"certs": {
2394+
cert_id: {
2395+
"_id_": cert_id,
2396+
"key": key,
2397+
"name": name,
2398+
"x509": x509
2399+
}
2400+
}
2401+
}
2402+
},
2403+
[]
2404+
]
2405+
}
2406+
2407+
ncm = self.session.patch(
2408+
'{0}/configuration_managers/{1}/'.format(self.base_url,
2409+
str(config_man_id)),
2410+
data=json.dumps(payload)) # Patch indie config with new values
2411+
result = self._return_handler(ncm.status_code, ncm.text, call_type)
2412+
return result
2413+
2414+
def set_encrypted_value_by_group(self, group_id=None, group_name=None, name: str = None, key: str = None):
2415+
"""
2416+
This method sets an encrypted value using the group's certificate management configuration
2417+
:param group_id: ID of group to update (optional if group_name is provided)
2418+
:param group_name: Name of group to update (optional if group_id is provided)
2419+
:param name: Name of the encrypted value
2420+
:param key: Key value to encrypt
2421+
:return:
2422+
"""
2423+
call_type = 'Set Encrypted Value'
2424+
2425+
if not group_id and not group_name:
2426+
raise Exception("Either group_id or group_name must be provided")
2427+
2428+
if not name or not key:
2429+
raise Exception("Both name and key parameters must be provided")
2430+
2431+
if group_name and not group_id:
2432+
try:
2433+
group_id = self.get_group_by_name(group_name)['id']
2434+
except Exception as e:
2435+
raise Exception(f"Group with name '{group_name}' not found: {str(e)}")
2436+
2437+
x509 = "-----BEGIN CERTIFICATE-----\nMIIB0jCCATugAwIBAgIUIF7Bygk4C0l0ikNv00u98unXZ9kwDQYJKoZIhvcNAQEL\nBQAwFzEVMBMGA1UEAwwMTmV0Q2xvdWQgQVBJMB4XDTI1MDYwNDA5MjYzNloXDTM1\nMDYwMzA5MjYzNlowFzEVMBMGA1UEAwwMTmV0Q2xvdWQgQVBJMIGfMA0GCSqGSIb3\nDQEBAQUAA4GNADCBiQKBgQDHWAtI42kixQBU9yZdiTmakxlj1OGfXlYGYDTMr/Q7\neFRZHLxJwIwrfV4UjJSvXkeo9ui1JNXzfQzDwZXdJKEdFM0fBpu9TD/cyetz9lCs\nh5YL1aC0IcH/liZwGt/z2X4snqe3KADHjy8Dl/5ib16vTC/FuRm02Bf8wVJ0c/sr\nhwIDAQABoxswGTAJBgNVHREEAjAAMAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEL\nBQADgYEAB5UavmWqkT7MXnt2/RE2qdtoTw4PfWIo+I2O7FAwJmHISubp3LW1vCn0\nRIsnyscH+BZmQkZOk3AYhLikgSky64HRHK32HXrLr79ku4as0drJzxuVOOKJn1+6\nDiNWTpAhzT55WU3fZ9H6FRvfEls0ZtLia/yiZ60rH01RO0lo2bs=\n-----END CERTIFICATE-----\n"
2438+
2439+
# Generate a unique ID for the certificate
2440+
cert_id = str(uuid.uuid4())
2441+
2442+
payload = {
2443+
"configuration": [
2444+
{
2445+
"certmgmt": {
2446+
"certs": {
2447+
cert_id: {
2448+
"_id_": cert_id,
2449+
"key": key,
2450+
"name": name,
2451+
"x509": x509
2452+
}
2453+
}
2454+
}
2455+
},
2456+
[]
2457+
]
2458+
}
2459+
2460+
ncm = self.session.patch(
2461+
'{0}/groups/{1}/'.format(self.base_url, str(group_id)),
2462+
data=json.dumps(payload)) # Patch group config with new values
2463+
result = self._return_handler(ncm.status_code, ncm.text, call_type)
2464+
return result
2465+
23512466
def set_router_fields(self, router_id: int, name: str = None, description: str = None, asset_id: str = None, custom1: str = None, custom2: str = None):
23522467
"""
23532468
This method sets multiple fields for a router.

0 commit comments

Comments
 (0)