11import logging
2+ from pathlib import Path
3+
24
35from .base_command import BaseCommand
46from ..common import Object
57from ..core .enum import Severity , Mode , IPProtocol
68from ..exceptions import CTERAException
9+ from ..lib import X509Certificate , PrivateKey
10+ from ..clients .common import MultipartForm
711
812
913class Syslog (BaseCommand ):
1014 """
1115 Portal Syslog Management APIs
1216 """
1317
14- # TODO: upload_ca_certificate # pylint: disable=W0511
15- # TODO: upload_client_certificate # pylint: disable=W0511
18+ def import_ca (self , certificate ):
19+ """
20+ Import the Syslog Server CA certificate
21+
22+ :param str certificate: Path to the PEM-encoded CA certificate.
23+ """
24+ X509Certificate .load_certificate (certificate )
25+ logging .getLogger ('cterasdk.edge' ).info ("Uploading syslog server CA certificate." )
26+ self ._import_secret ('/settings/logSettings/syslogConfig/caCertificateUpload' , certificate )
27+ logging .getLogger ('cterasdk.edge' ).info ("Uploaded syslog server CA certificate." )
28+
29+ def import_client_certificate (self , private_key , certificate ):
30+ """
31+ Import the Syslog Server CA certificate
32+
33+ :param str private_key: Path to the PEM-encoded private key.
34+ :param str certificate: Path to the PEM-encoded client certificate.
35+ """
36+ PrivateKey .load_private_key (private_key )
37+ logging .getLogger ('cterasdk.edge' ).info ("Uploading syslog server private key." )
38+ self ._import_secret ('/settings/logSettings/syslogConfig/clientPrivateKeyUpload' , private_key )
39+ logging .getLogger ('cterasdk.edge' ).info ("Uploaded syslog server private key." )
40+
41+ X509Certificate .load_certificate (certificate )
42+ logging .getLogger ('cterasdk.edge' ).info ("Uploading syslog server client certificate." )
43+ self ._import_secret ('/settings/logSettings/syslogConfig/clientCertificateUpload' , certificate )
44+ logging .getLogger ('cterasdk.edge' ).info ("Uploaded syslog server client certificate." )
45+
46+ def _import_secret (self , path , file ):
47+ """
48+ Import a Syslog Certificate or Private Key
49+
50+ :param str path: URL Path
51+ :param str file: File Path
52+ """
53+ handle = Path (file )
54+ with handle .open ('rb' ) as fd :
55+ form = MultipartForm ()
56+ form .add ('name' , handle .name )
57+ form .add ('firmware_path' , fd , handle .name )
58+ self ._core .api .multipart (path , form )
1659
1760 def is_enabled (self ):
1861 """
@@ -26,14 +69,15 @@ def get_configuration(self):
2669 """
2770 return self ._core .api .get ('/settings/logsSettings/syslogConfig' )
2871
29- def enable (self , server , port = 514 , protocol = IPProtocol .UDP , min_severity = Severity .INFO ):
72+ def enable (self , server , port = 514 , protocol = IPProtocol .UDP , min_severity = Severity .INFO , ca_cert = None ):
3073 """
3174 Enable Syslog
3275
3376 :param str server: Syslog server address
3477 :param int,optional port: Syslog server port
3578 :param cterasdk.core.enum.IPProtocol,optional protocol: Syslog server IP protocol
36- :param cterasdk.core.enum.Severity,optional min_severity: Minimum log severity to forward
79+ :param cterasdk.core.enum.Severity,optional min_severity: Minimum Log Severity
80+ :param str,optional ca_cert: Path to the PEM-encoded CA certificate.
3781 """
3882 param = Object ()
3983 param ._classname = 'PortalSyslogConfig' # pylint: disable=protected-access
@@ -43,6 +87,8 @@ def enable(self, server, port=514, protocol=IPProtocol.UDP, min_severity=Severit
4387 param .port = port
4488 param .protocol = protocol
4589 param .useClientCertificate = False
90+ if protocol == IPProtocol .TCP and ca_cert is not None :
91+ self .import_ca (ca_cert )
4692 logging .getLogger ('cterasdk.core' ).info ('Enabling syslog.' )
4793 response = self ._core .api .put ('/settings/logsSettings/syslogConfig' , param )
4894 logging .getLogger ('cterasdk.core' ).info ('Syslog enabled.' )
0 commit comments