Skip to content

Commit feedbf7

Browse files
committed
Added methods for instance certificate management changes in UAG
1 parent c240bb8 commit feedbf7

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

kepconfig/ua_gateway/client.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"""
1212

1313
from typing import Union
14+
15+
from kepconfig.structures import KepServiceResponse
1416
from ..connection import server
1517
from ..error import KepError, KepHTTPError
16-
from ..ua_gateway.common import _INTER_TYPE, _change_cert_trust, _create_url_cert, _create_url_client, _delete_cert_truststore
18+
from ..ua_gateway.common import _INTER_TYPE, _change_cert_trust, _create_url_cert, _create_url_client, _delete_cert_truststore, _create_url_inst_cert
19+
20+
CLIENT_INSTANCE_CERTIFICATE = "Client Instance Certificate"
1721

1822
def get_ua_client_connection(server: server, ua_client_connection: str) -> dict:
1923
'''Returns the properties of the UAG client connection object.
@@ -192,4 +196,37 @@ def delete_certificate(server: server, certificate: str) -> bool:
192196
:raises KepHTTPError: If urllib provides an HTTPError
193197
:raises KepURLError: If urllib provides an URLError
194198
'''
195-
return _delete_cert_truststore(server, _INTER_TYPE.CLIENT, certificate)
199+
return _delete_cert_truststore(server, _INTER_TYPE.CLIENT, certificate)
200+
201+
def get_instance_certificate(server: server) -> dict:
202+
'''Returns the properties of the UAG client instance certificate object in the UAG certificate store.
203+
These are UAG instance certificates that are used by UAG for trust purposes in the UA security model.
204+
205+
:param server: instance of the `server` class
206+
207+
:return: Dict of properties for the certificate requested
208+
209+
:raises KepHTTPError: If urllib provides an HTTPError
210+
:raises KepURLError: If urllib provides an URLError
211+
'''
212+
r = server._config_get(server.url + _create_url_inst_cert(_INTER_TYPE.CLIENT, CLIENT_INSTANCE_CERTIFICATE))
213+
return r.payload
214+
215+
def reissue_self_signed_instance_certificate(server: server, job_ttl: int = None) -> KepServiceResponse:
216+
'''Deletes and reissues a self-signed UAG server instance certificate object in the UAG certificate store.
217+
This is the UAG instance certificate that are used by UAG for trust purposes in the UA security model.
218+
219+
:param server: instance of the `server` class
220+
:param job_ttl: *(optional)* Determines the number of seconds a job instance will exist following completion.
221+
222+
:return: `KepServiceResponse` instance with job information
223+
224+
:raises KepHTTPError: If urllib provides an HTTPError
225+
:raises KepURLError: If urllib provides an URLError
226+
'''
227+
url = server.url + _create_url_inst_cert(_INTER_TYPE.CLIENT, CLIENT_INSTANCE_CERTIFICATE) + '/services/ReIssueInstanceCertificate'
228+
try:
229+
job = server._kep_service_execute(url, TTL= job_ttl)
230+
return job
231+
except Exception as err:
232+
raise err

kepconfig/ua_gateway/common.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616
CLIENT_ROOT = f'{UA_GATEWAY_ROOT}/ua_client_interfaces/Client Interface'
1717
CONN_ROOT = f'{CLIENT_ROOT}/ua_client_connections'
1818
CLIENT_CERT_ROOT = f'{CLIENT_ROOT}/certificates'
19+
CLIENT_INST_CERT_ROOT = f'{CLIENT_ROOT}/client_instance_certificates'
1920
SERVER_ROOT = f'{UA_GATEWAY_ROOT}/ua_server_interfaces/Server Interface'
2021
ENDPOINT_ROOT = f'{SERVER_ROOT}/ua_server_endpoints'
2122
SERVER_CERT_ROOT = f'{SERVER_ROOT}/certificates'
23+
SERVER_INST_CERT_ROOT = f'{SERVER_ROOT}/server_instance_certificates'
2224

2325

2426
class _INTER_TYPE(Enum):
2527
SERVER = 0
2628
CLIENT = 1
2729
CERTS = 2
2830

31+
# TODO: DEPRECATED: This constant is deprecated and will be removed in a future release.
2932
INSTANCE_CERTIFICATE = "Instance Certificate"
3033

3134
def _create_url_cert(interface, certificate = None):
3235
'''Creates url object for the "certificate" branch of Kepware's UA Gateway. Used
3336
to build a part of Kepware Configuration API URL structure
34-
35-
Returns the UA Gateway client interfaces specific certificate url when a value is passed as the certificate name.
3637
'''
3738
if interface == _INTER_TYPE.SERVER:
3839
if certificate == None:
@@ -44,6 +45,27 @@ def _create_url_cert(interface, certificate = None):
4445
return CLIENT_CERT_ROOT
4546
else:
4647
return f'{CLIENT_CERT_ROOT}/{_url_parse_object(certificate)}'
48+
# TODO: DEPRECATED: This interface type is deprecated and will be removed in a future release.
49+
else:
50+
if certificate == None:
51+
return CERT_ROOT
52+
else:
53+
return '{}/{}'.format(CERT_ROOT,_url_parse_object(certificate))
54+
55+
def _create_url_inst_cert(interface, certificate = None):
56+
'''Creates url object for the "instance certificate" branch of Kepware's UA Gateway interfaces. Used
57+
to build a part of Kepware Configuration API URL structure
58+
'''
59+
if interface == _INTER_TYPE.SERVER:
60+
if certificate == None:
61+
return SERVER_INST_CERT_ROOT
62+
else:
63+
return f'{SERVER_INST_CERT_ROOT}/{_url_parse_object(certificate)}'
64+
elif interface == _INTER_TYPE.CLIENT:
65+
if certificate == None:
66+
return CLIENT_INST_CERT_ROOT
67+
else:
68+
return f'{CLIENT_INST_CERT_ROOT}/{_url_parse_object(certificate)}'
4769
else:
4870
if certificate == None:
4971
return CERT_ROOT

kepconfig/ua_gateway/server.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"""
1212

1313
from typing import Union
14+
15+
from kepconfig.structures import KepServiceResponse
1416
from ..connection import server
1517
from ..error import KepError, KepHTTPError
16-
from ..ua_gateway.common import _INTER_TYPE, _change_cert_trust, _create_url_cert, _create_url_server, _delete_cert_truststore, SERVER_ROOT
18+
from ..ua_gateway.common import _INTER_TYPE, _change_cert_trust, _create_url_cert, _create_url_server, _delete_cert_truststore, SERVER_ROOT, _create_url_inst_cert
19+
20+
SERVER_INSTANCE_CERTIFICATE = 'Server Instance Certificate'
1721

1822
def get_uag_server_interface_properties(server: server) -> dict:
1923
''' Get the UAG Server Interface Properties of the Kepware instance. These properties expose User Identify
@@ -224,4 +228,37 @@ def delete_certificate(server: server, certificate: str) -> bool:
224228
:raises KepHTTPError: If urllib provides an HTTPError
225229
:raises KepURLError: If urllib provides an URLError
226230
'''
227-
return _delete_cert_truststore(server, _INTER_TYPE.SERVER, certificate)
231+
return _delete_cert_truststore(server, _INTER_TYPE.SERVER, certificate)
232+
233+
def get_instance_certificate(server: server) -> dict:
234+
'''Returns the properties of the UAG server instance certificate object in the UAG certificate store.
235+
These are UAG instance certificates that are used by UAG for trust purposes in the UA security model.
236+
237+
:param server: instance of the `server` class
238+
239+
:return: Dict of properties for the certificate requested
240+
241+
:raises KepHTTPError: If urllib provides an HTTPError
242+
:raises KepURLError: If urllib provides an URLError
243+
'''
244+
r = server._config_get(server.url + _create_url_inst_cert(_INTER_TYPE.SERVER, SERVER_INSTANCE_CERTIFICATE))
245+
return r.payload
246+
247+
def reissue_self_signed_instance_certificate(server: server, job_ttl: int = None) -> KepServiceResponse:
248+
'''Deletes and reissues a self-signed UAG server instance certificate object in the UAG certificate store.
249+
This is the UAG instance certificate that are used by UAG for trust purposes in the UA security model.
250+
251+
:param server: instance of the `server` class
252+
:param job_ttl: *(optional)* Determines the number of seconds a job instance will exist following completion.
253+
254+
:return: `KepServiceResponse` instance with job information
255+
256+
:raises KepHTTPError: If urllib provides an HTTPError
257+
:raises KepURLError: If urllib provides an URLError
258+
'''
259+
url = server.url + _create_url_inst_cert(_INTER_TYPE.SERVER, SERVER_INSTANCE_CERTIFICATE) + '/services/ReIssueInstanceCertificate'
260+
try:
261+
job = server._kep_service_execute(url, TTL= job_ttl)
262+
return job
263+
except Exception as err:
264+
raise err

0 commit comments

Comments
 (0)