-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconnection_cloud.py
More file actions
1123 lines (990 loc) · 47.4 KB
/
Copy pathconnection_cloud.py
File metadata and controls
1123 lines (990 loc) · 47.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright Venafi, Inc. and CyberArk Software Ltd. ("CyberArk")
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import base64
import re
import time
import requests
import urllib.parse as urlparse
from nacl.public import SealedBox
from .common import (ZoneConfig, CertificateRequest, CommonConnection, Policy, get_ip_address, log_errors, MIME_JSON,
MIME_TEXT, MIME_ANY, CertField, KeyType, DEFAULT_TIMEOUT,
CSR_ORIGIN_SERVICE, CHAIN_OPTION_FIRST, CHAIN_OPTION_LAST)
from .errors import (VenafiConnectionError, ServerUnexptedBehavior, ClientBadData, CertificateRequestError,
CertificateRenewError, VenafiError, RetrieveCertificateTimeoutError)
from .http_status import HTTPStatus
from .logger import get_child
from .pem import parse_pem, Certificate
from .policy import PolicySpecification
from .policy.pm_cloud import (build_policy_spec, validate_policy_spec, AccountDetails, build_cit_request, build_user,
UserDetails, build_company, build_apikey, build_app_update_request, get_ca_info,
CertificateAuthorityDetails, CertificateAuthorityInfo, build_account_details,
build_app_create_request, build_team, get_cit_data_from_response, build_owner_json,
supported_rsa_key_sizes, supported_elliptic_curves)
from .vaas_utils import AppDetails, OwnerIdsAndTypes, RecommendedSettings, EdgeEncryptionKey, zip_to_pem, value_matches_regex
TOKEN_HEADER_NAME = "tppl-api-key" # nosec
APPLICATION_SERVER_TYPE_ID = "784938d1-ef0d-11eb-9461-7bb533ba575b"
MSG_VALUE_NOT_MATCH_POLICY = "Error while requesting certificate using service generated CSR on CyberArk Certificate Manager, SaaS. " \
"Request {} does not match CIT valid {}:\n\tRequest value: {},\n\tCIT values: {}"
CSR_ATTR_CN = 'commonName'
CSR_ATTR_ORG = 'organization'
CSR_ATTR_ORG_UNIT = 'organizationalUnits'
CSR_ATTR_LOCALITY = 'locality'
CSR_ATTR_PROVINCE = 'state'
CSR_ATTR_COUNTRY = 'country'
CSR_ATTR_SANS_BY_TYPE = 'subjectAlternativeNamesByType'
CSR_ATTR_SANS_DNS = 'dnsNames'
CSR_ATTR_SANS_IP_ADDR = 'ipAddresses'
CSR_ATTR_SANS_EMAIL_ADDR = 'rfc822Names'
CSR_ATTR_SANS_URIS = 'uniformResourceIdentifiers'
CSR_ATTR_KEY_TYPE_PARAMS = 'keyTypeParameters'
CSR_ATTR_KEY_TYPE = 'keyType'
CSR_ATTR_KEY_LENGTH = 'keyLength'
CSR_ATTR_KEY_CURVE = 'keyCurve'
OWNER_TYPE_USER = "USER"
OWNER_TYPE_TEAM = "TEAM"
log = get_child("connection-vaas")
class CertStatuses:
REQUESTED = 'REQUESTED'
PENDING = 'PENDING'
FAILED = 'FAILED'
ISSUED = 'ISSUED'
class URLS:
def __init__(self):
pass
API_BASE_URL = "https://api.venafi.cloud/"
API_VERSION = "v1/"
API_BASE_PATH = f"outagedetection/{API_VERSION}"
POLICIES_BY_ID = API_BASE_PATH + "certificatepolicies/{}"
CERTIFICATE_REQUESTS = API_BASE_PATH + "certificaterequests"
CERTIFICATE_STATUS = CERTIFICATE_REQUESTS + "/{}"
CERTIFICATE_RETRIEVE = API_BASE_PATH + "certificates/{}/contents"
CERTIFICATE_SEARCH = API_BASE_PATH + "certificatesearch"
CERTIFICATE_RETIRE = API_BASE_PATH + "certificates/retirement"
APPLICATIONS = API_BASE_PATH + "applications"
APP_BY_ID = APPLICATIONS + "/{}"
CERTIFICATE_TEMPLATE_BY_ID = APP_BY_ID + "/certificateissuingtemplates/{}"
APP_DETAILS_BY_NAME = APPLICATIONS + "/name/{}"
CERTIFICATE_BY_ID = API_BASE_PATH + "certificates/{}"
CERTIFICATE_KEYSTORE_BY_ID = CERTIFICATE_BY_ID + "/keystore"
CA_ACCOUNTS = API_VERSION + "certificateauthorities/{}/accounts"
CA_ACCOUNT_DETAILS = CA_ACCOUNTS + "/{}"
ISSUING_TEMPLATES = API_VERSION + "certificateissuingtemplates"
ISSUING_TEMPLATES_UPDATE = ISSUING_TEMPLATES + "/{}"
USER_ACCOUNTS = API_VERSION + "useraccounts"
DEK_PUBLIC_KEY = API_VERSION + "edgeencryptionkeys/{}"
USERS_USERNAME = API_VERSION + "users/username/{}"
USERS_ID = API_VERSION + "users/{}"
TEAMS_ID = API_VERSION + "teams"
class CondorChainOptions:
ROOT_FIRST = "ROOT_FIRST"
ROOT_LAST = "EE_FIRST"
class CertificateStatusResponse:
def __init__(self, d):
self.status = d.get('status') or d.get("certificateStatus")
self.subject = d.get('subjectDN') or d.get('subjectCN')[0]
self.applicationId = d.get('applicationId')
self.citId = d.get('certificateIssuingTemplateId')
self.certificateIds = d.get('certificateIds') or [d.get('id')]
self.csrId = d.get('certificateRequestId')
def _parse_zone(zone):
if not zone:
log.error("Invalid Zone. It is empty")
raise ClientBadData("You need to specify a zone")
segments = zone.split("\\")
if len(segments) < 2 or len(segments) > 2:
log.error("Invalid zone. Incorrect format")
raise ClientBadData(f"Invalid Zone [{zone}]. The zone format is incorrect")
app_name = segments[0]
cit_alias = segments[1]
return app_name, cit_alias
def create_owner(owner_type, owner_id):
owner = OwnerIdsAndTypes(owner_type, owner_id)
return owner
def resolve_apikey_owner(user_details):
owner = create_owner(OWNER_TYPE_USER, user_details.user.user_id)
return owner
class CloudConnection(CommonConnection):
def __init__(self, token, url=None, http_request_kwargs=None):
super().__init__()
self._base_url = url or URLS.API_BASE_URL
self._token = token
self._normalize_and_verify_base_url()
if http_request_kwargs is None:
http_request_kwargs = {'timeout': 180}
elif 'timeout' not in http_request_kwargs:
http_request_kwargs['timeout'] = 180
self._http_request_kwargs = http_request_kwargs
if self._http_request_kwargs.get('verify') is False:
log.warning("TLS certificate verification is DISABLED; credentials and private keys will be transmitted over unverified connections. This configuration is only appropriate for isolated test environments.")
def __str__(self):
return f"[Cloud] {self._base_url}"
def _get(self, url, params=None):
"""
:param url:
:param params:
:rtype: str or dict
"""
headers = {
TOKEN_HEADER_NAME: self._token,
'accept': MIME_ANY,
'cache-control': "no-cache"
}
r = requests.get(self._base_url + url, params=params, headers=headers, **self._http_request_kwargs) # nosec B113
return self.process_server_response(r)
def _post(self, url, data=None):
"""
:param url:
:param data:
:rtype: str or dict
"""
headers = {
TOKEN_HEADER_NAME: self._token,
'accept': MIME_JSON,
'cache-control': "no-cache"
}
if isinstance(data, dict):
r = requests.post(self._base_url + url, json=data, headers=headers, **self._http_request_kwargs) # nosec B113
else:
log.error(f"Unexpected client data type: {type(data)} for {url}")
raise ClientBadData
return self.process_server_response(r)
def _put(self, url, data=None):
"""
:param url:
:param data:
:rtype:str or dict
"""
headers = {
TOKEN_HEADER_NAME: self._token,
'cache-control': "no-cache",
'accept': MIME_JSON
}
if isinstance(data, dict):
r = requests.put(self._base_url + url, json=data, headers=headers, **self._http_request_kwargs) # nosec B113
else:
log.error(f"Unexpected client data type: {type(data)} for {url}")
raise ClientBadData
return self.process_server_response(r)
def _normalize_and_verify_base_url(self):
u = self._base_url
if u.startswith('http://'):
u = f"https://{u[7:]}"
elif not u.startswith('https://'):
u = f"https://{u}"
if not u.endswith("/"):
u += "/"
if not re.match(r"^https://[a-z\d]+[-a-z\d.]+[a-z\d][:\d]*/$", u):
raise ClientBadData
self._base_url = u
@staticmethod
def _process_server_response(r):
if r.status_code not in (HTTPStatus.OK, HTTPStatus.CREATED, HTTPStatus.ACCEPTED):
raise VenafiConnectionError(f"Server status: {r.status_code}, {r.request.url}")
content_type = r.headers.get('content-type')
if content_type == MIME_TEXT:
log.debug(r.text)
return r.status_code, r.text
elif content_type == MIME_JSON:
log.debug(r.content.decode())
return r.status_code, r.json()
else:
log.error(f"unexpected content type: {content_type} for request {r.request.url}")
raise ServerUnexptedBehavior
def _get_cert_status(self, request):
status, data = self._get(URLS.CERTIFICATE_STATUS.format(request.id))
if status == HTTPStatus.OK:
request_status = CertificateStatusResponse(data)
return request_status
else:
raise ServerUnexptedBehavior
@staticmethod
def _parse_policy_response_to_object(d):
policy = Policy(
d['id'] if 'id' in d else None,
d['companyId'] if 'companyId' in d else None,
d['name'] if 'name' in d else None,
d['systemGenerated'] if 'systemGenerated' in d else None,
d['creationDate'] if 'creationDate' in d else None,
d['subjectCNRegexes'] if 'subjectCNRegexes' in d else None,
d['subjectORegexes'] if 'subjectORegexes' in d else None,
d['subjectOURegexes'] if 'subjectOURegexes' in d else None,
d['subjectSTRegexes'] if 'subjectSTRegexes' in d else None,
d['subjectLRegexes'] if 'subjectLRegexes' in d else None,
d['subjectCValues'] if 'subjectCValues' in d else None,
d['sanRegexes'] if 'sanRegexes' in d else None,
[],
d['keyReuse'] if 'keyReuse' in d else None,
d['certificateAuthority'] if 'certificateAuthority' in d else None,
d['certificateAuthorityAccountId'] if 'certificateAuthorityAccountId' in d else None,
d['certificateAuthorityProductOptionId'] if 'certificateAuthorityProductOptionId' in d else None,
d['priority'] if 'priority' in d else None,
d['modificationDate'] if 'modificationDate' in d else None,
d['status'] if 'status' in d else None,
d['reason'] if 'reason' in d else None,
d['validityPeriod'] if 'validityPeriod' in d else None,
None,
d['csrUploadAllowed'] if 'csrUploadAllowed' in d else None,
d['keyGeneratedByVenafiAllowed'] if 'keyGeneratedByVenafiAllowed' in d else None
)
policy.email_regexes = d['sanRfc822NameRegexes'] if 'sanRfc822NameRegexes' in d else None
policy.ip_constraints_regexes = d['sanIpAddressRegexes'] if 'sanIpAddressRegexes' in d else None
policy.uri_regexes = d['sanUniformResourceIdentifierRegexes'] if 'sanUniformResourceIdentifierRegexes' in d \
else None
for kt in d.get('keyTypes', []):
key_type = kt['keyType'].lower()
if key_type == KeyType.RSA:
for s in kt['keyLengths']:
policy.key_types.append(KeyType(key_type, s))
elif key_type == KeyType.ECDSA:
for s in kt["keyCurves"]:
policy.key_types.append(KeyType(key_type, s))
else:
log.error(f"Unknown key type: {kt['keyType']}")
raise ServerUnexptedBehavior
rs = CloudConnection._parse_recommended_settings_to_object(d)
if rs:
policy.recommended_settings = rs
return policy
@staticmethod
def _parse_recommended_settings_to_object(d):
if 'recommendedSettings' in d:
rs = d['recommendedSettings']
settings = RecommendedSettings(
rs['subjectOValue'] if 'subjectOValue' in rs else None,
rs['subjectOUValue'] if 'subjectOUValue' in rs else None,
rs['subjectLValue'] if 'subjectLValue' in rs else None,
rs['subjectSTValue'] if 'subjectSTValue' in rs else None,
rs['subjectCValue'] if 'subjectCValue' in rs else None,
None,
rs['keyReuse'] if 'keyReuse' in rs else None
)
if 'key' in rs:
key = rs['key']
k_type = key['type']
kl = key['length'] if 'length' in key else None
kc = key['curve'] if 'curve' in key else None
kt = KeyType(k_type, kl or kc)
settings.keyType = kt
return settings
def _get_template_by_id(self, zone):
"""
Returns the Certificate Issuing Template details
:rtype: Policy
"""
app_name, cit_alias = _parse_zone(zone)
status, data = self._get(URLS.CERTIFICATE_TEMPLATE_BY_ID.format(urlparse.quote(app_name),
urlparse.quote(cit_alias)))
if status != HTTPStatus.OK:
log.error(f"Invalid status {status} while retrieving policy [{zone}]")
return None
return self._parse_policy_response_to_object(data)
def auth(self):
status, data = self._get(URLS.USER_ACCOUNTS)
if status == HTTPStatus.OK:
return data
def get_version(self):
raise NotImplementedError
def _get_app_details_by_name(self, app_name):
"""
:param str app_name:
:rtype: AppDetails
"""
if not app_name:
raise ClientBadData("You need to specify the application name")
try:
status, data = self._get(URLS.APP_DETAILS_BY_NAME.format(urlparse.quote(app_name)))
except VenafiConnectionError:
return None
if status == HTTPStatus.OK:
return AppDetails(data['id'] if 'id' in data else None,
data['certificateIssuingTemplateAliasIdMap'] if 'certificateIssuingTemplateAliasIdMap'
in data else None,
data['companyId'] if 'companyId' in data else None,
data['name'] if 'name' in data else None,
data['description'] if 'description' in data else None,
data['ownerIdsAndTypes'] if 'ownerIdsAndTypes' in data else None,
data['fqDns'] if 'fqDns' in data else None,
data['internalFqDns'] if 'internalFqDns' in data else None,
data['externalIpRanges'] if 'externalIpRanges' in data else None,
data['internalIpRanges'] if 'internalIpRanges' in data else None,
data['internalPorts'] if 'internalPorts' in data else None,
data['fullyQualifiedDomainNames'] if 'fullyQualifiedDomainNames' in data else None,
data['ipRanges'] if 'ipRanges' in data else None,
data['ports'] if 'ports' in data else None
)
elif status in (HTTPStatus.BAD_REQUEST, HTTPStatus.NOT_FOUND, HTTPStatus.PRECONDITION_FAILED):
log_errors(data)
def request_cert(self, request, zone):
app_name, cit_alias = _parse_zone(zone)
details = self._get_app_details_by_name(app_name)
cit_id = details.cit_alias_id_map.get(cit_alias)
ip_address = get_ip_address()
request_data = {
'applicationId': details.app_id,
'certificateIssuingTemplateId': cit_id,
'apiClientInformation': {
'type': request.origin,
'identifier': ip_address
}
}
zone_config = self.read_zone_conf(zone)
request.update_from_zone_config(zone_config)
if request.csr_origin != CSR_ORIGIN_SERVICE:
if not request.csr:
request.build_csr()
request_data['certificateSigningRequest'] = request.csr
else:
request_data['isVaaSGenerated'] = True
request_data['applicationServerTypeId'] = APPLICATION_SERVER_TYPE_ID
request_data['csrAttributes'] = self._get_service_generated_csr_attr(request, zone)
if request.validity_hours is not None:
request_data['validityPeriod'] = f"PT{request.validity_hours}H"
status, data = self._post(URLS.CERTIFICATE_REQUESTS, data=request_data)
if status == HTTPStatus.CREATED:
request.id = data['certificateRequests'][0]['id']
if 'certificateIds' in data['certificateRequests'][0] \
and len(data['certificateRequests'][0]['certificateIds']) > 0:
request.cert_guid = data['certificateRequests'][0]['certificateIds'][0]
return True
else:
log.error(f"unexpected server response {status}: {data}")
raise CertificateRequestError
def retrieve_cert(self, request):
cert_status = self._get_cert_status(request)
if cert_status.status == CertStatuses.PENDING or cert_status.status == CertStatuses.REQUESTED:
log.info(f"Certificate status is {cert_status.status}")
# Time in seconds
time_start = time.time()
while True:
log.debug("Waiting for certificate...")
time.sleep(3)
cert_status = self._get_cert_status(request)
if cert_status.status == CertStatuses.ISSUED:
log.info(f"Certificate status is {cert_status.status}")
break
elif (time.time() - time_start) < request.timeout:
continue
else:
raise RetrieveCertificateTimeoutError(f"Operation timed out at {request.timeout} seconds "
f"while waiting for certificate with id {request.id} to be ISSUED")
if cert_status.status == CertStatuses.FAILED:
log.debug(f"Certificate status is {cert_status.status}. Returning data for debug")
return "Certificate FAILED"
if cert_status.status == CertStatuses.ISSUED:
request.cert_guid = cert_status.certificateIds[0]
dek_info = self._get_dek_hash(request.cert_guid)
if dek_info and dek_info.public_key:
return self._retrieve_service_generated_cert(request, dek_info)
url = URLS.CERTIFICATE_RETRIEVE.format(request.cert_guid)
if request.chain_option == CHAIN_OPTION_FIRST:
url += f"?chainOrder={CondorChainOptions.ROOT_FIRST}&format=PEM"
elif request.chain_option == CHAIN_OPTION_LAST:
url += f"?chainOrder={CondorChainOptions.ROOT_LAST}&format=PEM"
else:
log.error(f"chain option {request.chain_option} is not valid")
raise ClientBadData
# Time in seconds
time_start = time.time()
while True:
try:
status, data = self._get(url)
except VenafiError as e:
log.debug(f"Certificate with id {request.id} not found")
status = 0
if status == HTTPStatus.OK:
log.debug("Certificate found, parsing response...")
cert_response = parse_pem(data, request.chain_option)
if cert_response.key is None and request.private_key is not None:
log.debug("Adding local private key to response...")
cert_response.key = request.private_key_pem
return cert_response
elif (time.time() - time_start) < request.timeout:
log.debug("Waiting for certificate...")
time.sleep(2)
else:
raise RetrieveCertificateTimeoutError(f"Operation timed out at {request.timeout} seconds "
f"while retrieving certificate with id {request.id}")
else:
raise ServerUnexptedBehavior
def revoke_cert(self, request):
# not supported in Venafi Cloud
raise NotImplementedError
def retire_cert(self, request):
cert_id = None
if not request.id and not request.thumbprint:
log.error("id or thumbprint must be specified for retiring certificate")
raise ClientBadData
if request.id:
cert_id = request.id
elif request.thumbprint:
response = self.search_by_thumbprint(request.thumbprint)
cert_ids = response.certificateIds
if len(cert_ids) > 1:
log.error(f"multiple certificates matching thumbprint found")
raise VenafiError
cert_id = cert_ids[0]
retire_data = {
'certificateIds': [
cert_id
]
}
status, data = self._post(URLS.CERTIFICATE_RETIRE, retire_data)
if status == HTTPStatus.OK:
if len(data) == 0:
log.error(f"certificate retirement was not successful for {cert_id}")
raise VenafiError
else:
return True
elif status == HTTPStatus.BAD_REQUEST or status == HTTPStatus.PRECONDITION_FAILED:
log.error("bad request for certificate retirement")
raise ClientBadData
else:
log.error("unexpected status returned")
raise ServerUnexptedBehavior
def renew_cert(self, request, reuse_key=False):
cert_request_id = None
if not request.id and not request.thumbprint:
log.error("prev_cert_id or thumbprint or manage_id must be specified for renewing certificate")
raise ClientBadData
if request.thumbprint:
response = self.search_by_thumbprint(request.thumbprint)
cert_request_id = response.csrId
if request.id:
cert_request_id = request.id
prev_request = self._get_cert_status(CertificateRequest(cert_id=cert_request_id))
certificate_id = prev_request.certificateIds[0]
app_id = prev_request.applicationId
cit_id = prev_request.citId
if not certificate_id or not app_id or not cit_id:
log.error("Can't find certificate_id")
raise ClientBadData
status, data = self._get(URLS.CERTIFICATE_BY_ID.format(certificate_id))
if status == HTTPStatus.OK:
request.id = data['certificateRequestId']
else:
raise ServerUnexptedBehavior
ip_address = get_ip_address()
d = {'existingCertificateId': certificate_id,
'applicationId': app_id,
'certificateIssuingTemplateId': cit_id,
'apiClientInformation': {
'type': request.origin,
'identifier': ip_address
}}
if reuse_key:
if request.csr:
d['certificateSigningRequest'] = request.csr
d['reuseCSR'] = False
else:
log.error("Certificate renew by reusing the CSR is not supported right now. "
"Set [reuse_key] to False or just remove it")
raise VenafiError
else:
c = data
if c.get('subjectCN'):
request.common_name = c['subjectCN'][0]
if c.get('subjectC'):
request.country = c['subjectC']
if c.get('subjectO'):
request.organization = c['subjectO']
if c.get('subjectOU'):
request.organizational_unit = c['subjectOU']
if c.get('subjectL'):
request.locality = c['subjectL']
if c.get('subjectAlternativeNameDns'):
request.san_dns = c['subjectAlternativeNameDns']
request.key_type = KeyType(KeyType.RSA, c['keyStrength'])
request.build_csr()
d['certificateSigningRequest'] = request.csr
d['reuseCSR'] = False
status, data = self._post(URLS.CERTIFICATE_REQUESTS, data=d)
if status == HTTPStatus.CREATED:
request.id = data['certificateRequests'][0]['id']
return True
else:
log.error(f"server unexpected status {status}")
raise CertificateRenewError
def search_by_thumbprint(self, thumbprint, timeout=DEFAULT_TIMEOUT):
"""
:param str thumbprint:
:param int timeout:
:rtype CertificateStatusResponse
"""
log.info("Searching certificate by thumbprint...")
thumbprint = re.sub(r'[^\dabcdefABCDEF]', "", thumbprint)
thumbprint = thumbprint.upper()
time_start = time.time()
while True:
status, data = self._post(URLS.CERTIFICATE_SEARCH, data={
'expression': {
'operands': [{
'field': "fingerprint",
'operator': "MATCH",
'value': thumbprint
}]
}
})
if status != HTTPStatus.OK:
raise ServerUnexptedBehavior
elif not data.get('count'):
if (time.time() - time_start) < timeout:
log.debug("Waiting for certificate...")
time.sleep(2)
else:
raise RetrieveCertificateTimeoutError(f'Operation timed out at {timeout} seconds while retrieving '
f'certificate with thumbprint {thumbprint}')
else:
log.debug("Certificate found, returning...")
return CertificateStatusResponse(data['certificates'][0])
def read_zone_conf(self, zone):
policy = self._get_template_by_id(zone)
rs = policy.recommended_settings
org = CertField("")
org_unit = CertField("")
locality = CertField("")
state = CertField("")
country = CertField("")
if rs:
org = CertField(rs.subjectOValue)
org_unit = CertField(rs.subjectOUValue)
locality = CertField(rs.subjectLValue)
state = CertField(rs.subjectSTValue)
country = CertField(rs.subjectCValue)
z = ZoneConfig(
organization=org,
organizational_unit=org_unit,
country=country,
province=state,
locality=locality,
policy=policy,
key_type=policy.key_types[0] if policy.key_types else None,
)
return z
def import_cert(self, request):
# not supported in Cloud
raise NotImplementedError
def get_policy(self, zone):
return self._get_policy(zone=zone, subject_cn_to_str=True)
def _policy_exists(self, zone):
"""
:param str zone:
:rtype: bool
"""
try:
cit = self._get_template_by_id(zone)
except VenafiConnectionError:
cit = None
return False if cit is None else True
def set_policy(self, zone, policy_spec):
validate_policy_spec(policy_spec)
app_name, cit_alias = _parse_zone(zone)
if not policy_spec.policy.certificate_authority:
raise VenafiError("Certificate Authority is required")
ca_details = self._get_ca_details(policy_spec.policy.certificate_authority)
if not ca_details:
raise VenafiError(f"CA [{policy_spec.policy.certificate_authority}] not found in CyberArk Certificate Manager, SaaS")
# CA valid. Create request dictionary
request = build_cit_request(policy_spec, ca_details)
request['name'] = cit_alias
cit_data = self._get_cit(cit_alias)
resp_cit_data = None
if cit_data:
# Issuing Template exists. Update
status, resp_cit_data = self._put(URLS.ISSUING_TEMPLATES_UPDATE.format(cit_data['id']), request)
if status != HTTPStatus.OK:
raise VenafiError(f"Failed to update issuing template [{cit_data['id']}] for zone [{zone}]")
else:
# Issuing Template does not exist. Create one
status, resp_cit_data = self._post(URLS.ISSUING_TEMPLATES, request)
if status != HTTPStatus.CREATED:
raise VenafiError(f"Failed to create issuing template for zone [{zone}]")
# Validate Application existence in Venafi Cloud.
user_details = self._get_user_details()
if not user_details:
raise VenafiError('User Details not found')
app_details = self._get_app_details_by_name(app_name)
for_update, owners_list = self.resolve_owners(policy_spec.users, user_details)
if app_details:
# Application exists. Update with cit and owners
if for_update and len(policy_spec.users) == 0:
log.debug("No users provided in the policy specification")
else:
owner_list = build_owner_json(owners_list)
app_details.owner_ids_and_types = owner_list
if app_details.cit_alias_id_map:
# Only link cit with Application when cit is not already associated with Application
cit_map = app_details.cit_alias_id_map
cit_id, cit_name = get_cit_data_from_response(resp_cit_data)
cit_map[cit_name] = cit_id
app_req = build_app_update_request(app_details, cit_map)
status, data = self._put(URLS.APP_BY_ID.format(app_details.app_id), app_req)
if status != HTTPStatus.OK:
raise VenafiError(f"Could not update Application [{app_name}] with cit [{resp_cit_data}]")
else:
# Application does not exist. Create one
app_req = build_app_create_request(app_name, owners_list, resp_cit_data)
status, data = self._post(URLS.APPLICATIONS, app_req)
if status != HTTPStatus.CREATED:
raise VenafiError(f"Could not create application [{app_name}]")
return
def resolve_owners(self, users_list, user_details):
owners_list = list()
for_update = False
if not users_list:
# When no users are provided on the list, adds apikey user as owner
api_owner = resolve_apikey_owner(user_details)
owners_list.append(api_owner)
for_update = True
else:
# Resolving the usernames list
# Creating a higher level Teams object to cache the response
teams_list = list()
users_list = list(set(users_list))
for username in users_list:
cloud_owner = self.resolve_user_to_cloud_owner(username)
if cloud_owner:
owners_list.append(cloud_owner)
else:
team_owner = self.resolve_user_to_cloud_team(username)
if not team_owner:
raise VenafiError(f"Unable to find identity [{username}]")
owners_list.append(team_owner)
return for_update, owners_list
def resolve_user_to_cloud_owner(self, username):
user = self.get_vaas_identity(username)
if not user:
return None
owner = create_owner(OWNER_TYPE_USER, user.user_id)
return owner
def resolve_user_to_cloud_team(self, username):
teams = list()
data_teams = None
try:
status, response = self._get(URLS.TEAMS_ID)
data_teams = response['teams']
except VenafiError as err:
log.debug(f"Error while getting team [{username}]: {err.args[0]}")
# date_teams will never be empty, we are always expecting at least one team
for data in data_teams:
team = build_team(data)
teams.append(team)
if len(teams) > 0:
for team in teams:
if team.name == username:
owner = create_owner(OWNER_TYPE_TEAM, team.team_id)
return owner
return None
def get_vaas_identity(self, username):
if not username or username == "":
raise VenafiError("Username cannot be empty")
url = URLS.USERS_USERNAME.format(username)
try:
status, response = self._get(url)
if status == HTTPStatus.NOT_FOUND:
return None
identities = response['users'][0]
identity = build_user(identities)
return identity
except VenafiError as err:
log.debug(f"Unable to find identity [{username}] of type USER: {err.args[0]}")
def resolve_cloud_owners_names(self, zone):
app_name, cit_alias = _parse_zone(zone)
app_details = self._get_app_details_by_name(app_name)
users_list = list()
teams_response = list()
for owner in app_details.owner_ids_and_types:
if owner['ownerType'] == OWNER_TYPE_USER:
status, data = self._get(URLS.USERS_ID.format(owner['ownerId']))
user = build_user(data)
users_list.append(user.username)
elif owner['ownerType'] == OWNER_TYPE_TEAM:
if not teams_response:
status, data = self._get(URLS.TEAMS_ID)
data_team = data['teams']
for t in data_team:
team = build_team(t)
teams_response.append(team)
if teams_response:
for team in teams_response:
if team.team_id == owner['ownerId']:
users_list.append(team.name)
return users_list
def _get_ca_details(self, ca_name):
"""
:param str ca_name:
:rtype: CertificateAuthorityDetails
"""
accounts, info = self._get_accounts(ca_name)
for acc in accounts:
if acc.account.key == info.ca_account_key:
for po in acc.product_options:
if po.product_name == info.vendor_name:
return CertificateAuthorityDetails(po.product_id, po.details.product_template.organization_id,)
def _get_accounts(self, ca_name):
"""
:param str ca_name:
:rtype: tuple[list[AccountDetails],CertificateAuthorityInfo]
"""
details = get_ca_info(ca_name)
ca_type = urlparse.quote(details.ca_type)
url = URLS.CA_ACCOUNTS.format(ca_type)
status, data = self._get(url)
if status != HTTPStatus.OK:
raise ServerUnexptedBehavior
if 'accounts' not in data:
raise VenafiError("Response error. Accounts not found")
acc_list = []
for d in data['accounts']:
ad = build_account_details(d)
acc_list.append(ad)
return acc_list, details
def _get_cit(self, cit_name):
"""
:param str cit_name:
:rtype: dict
"""
status, data = self._get(URLS.ISSUING_TEMPLATES)
if status != HTTPStatus.OK:
raise VenafiError("Could not retrieve Certificate Issuing Templates")
if 'certificateIssuingTemplates' in data:
for cit_data in data['certificateIssuingTemplates']:
if cit_data['name'] == cit_name:
return cit_data
return None
def _get_user_details(self):
"""
:rtype: UserDetails
"""
status, data = self._get(URLS.USER_ACCOUNTS)
if status != HTTPStatus.OK:
raise VenafiError(f"Failed to retrieve user accounts. Error {data}")
user = build_user(data['user']) if 'user' in data else None
company = build_company(data['company']) if 'company' in data else None
apikey = build_apikey(data['apiKey']) if 'apiKey' in data else None
return UserDetails(user, company, apikey)
def _get_ca_info(self, name, account_id, product_option_id):
"""
:param str name:
:param str account_id:
:param str product_option_id:
:rtype: CertificateAuthorityInfo
"""
ca_name = urlparse.quote(name)
url = URLS.CA_ACCOUNT_DETAILS.format(ca_name, account_id)
status, data = self._get(url)
if status != HTTPStatus.OK:
raise ServerUnexptedBehavior
account_details = build_account_details(data)
info = CertificateAuthorityInfo(account_details.account.certificate_authority, account_details.account.key)
for po in account_details.product_options:
if po.product_id == product_option_id:
info.vendor_name = po.product_name
return info
def _get_service_generated_csr_attr(self, request, zone):
"""
:param CertificateRequest request:
:param str zone:
:rtype: dict[str, Any]
"""
ps = self._get_policy(zone=zone, subject_cn_to_str=False)
csr_attr_map = {}
if request.common_name:
if ps.policy:
policy_domains = ps.policy.domains
valid = value_matches_regex(value=request.common_name, pattern_list=policy_domains)
if not valid:
log.error(MSG_VALUE_NOT_MATCH_POLICY.format("Common Name", "domains", request.common_name,
ps.policy.domains))
raise ClientBadData()
csr_attr_map[CSR_ATTR_CN] = request.common_name
if request.organization:
if ps.policy and ps.policy.subject:
policy_orgs = ps.policy.subject.orgs
valid = value_matches_regex(value=request.organization, pattern_list=policy_orgs)
if not valid:
org_str = "Organization"
log.error(MSG_VALUE_NOT_MATCH_POLICY.format(org_str, f"{org_str}s", request.organization,
policy_orgs))
raise ClientBadData
csr_attr_map[CSR_ATTR_ORG] = request.organization
elif ps.defaults and ps.defaults.subject and ps.defaults.subject.org:
csr_attr_map[CSR_ATTR_ORG] = ps.defaults.subject.org
if request.organizational_unit:
if isinstance(request.organizational_unit, str):
org_units = [request.organizational_unit]
else:
org_units = request.organizational_unit
if ps.policy and ps.policy.subject:
policy_ous = ps.policy.subject.org_units
valid = all(
value_matches_regex(value=ou, pattern_list=policy_ous) for ou in org_units
)
if not valid:
ou_str = "Organizational Unit"
log.error(MSG_VALUE_NOT_MATCH_POLICY.format(ou_str, f"{ou_str}s", request.organizational_unit,
policy_ous))
raise ClientBadData
csr_attr_map[CSR_ATTR_ORG_UNIT] = org_units
elif ps.defaults and ps.defaults.subject and ps.defaults.subject.org_units:
csr_attr_map[CSR_ATTR_ORG_UNIT] = ps.defaults.subject.org_units
if request.locality:
if ps.policy and ps.policy.subject:
policy_localities = ps.policy.subject.localities
valid = value_matches_regex(value=request.locality, pattern_list=policy_localities)
if not valid:
locality_str = "Localit"
log.error(MSG_VALUE_NOT_MATCH_POLICY.format(f"{locality_str}y", f"{locality_str}ies",
request.locality, policy_localities))
raise ClientBadData
csr_attr_map[CSR_ATTR_LOCALITY] = request.locality
elif ps.defaults and ps.defaults.subject and ps.defaults.subject.locality:
csr_attr_map[CSR_ATTR_LOCALITY] = ps.defaults.subject.locality
if request.province:
if ps.policy and ps.policy.subject:
policy_provinces = ps.policy.subject.states
valid = value_matches_regex(value=request.province, pattern_list=policy_provinces)
if not valid:
province_str = "Province"
log.error(MSG_VALUE_NOT_MATCH_POLICY.format(province_str, f"{province_str}s", request.province,
policy_provinces))
raise ClientBadData
csr_attr_map[CSR_ATTR_PROVINCE] = request.province
elif ps.defaults and ps.defaults.subject and ps.defaults.subject.state:
csr_attr_map[CSR_ATTR_PROVINCE] = ps.defaults.subject.state
if request.country:
if ps.policy and ps.policy.subject:
policy_countries = ps.policy.subject.countries
valid = value_matches_regex(value=request.country, pattern_list=policy_countries)
if not valid:
country_str = "Countr"
log.error(MSG_VALUE_NOT_MATCH_POLICY.format(f"{country_str}y", f"{country_str}ies", request.country,
policy_countries))
raise ClientBadData
csr_attr_map[CSR_ATTR_COUNTRY] = request.country
elif ps.defaults and ps.defaults.subject and ps.defaults.subject.country:
csr_attr_map[CSR_ATTR_COUNTRY] = ps.defaults.subject.country
if len(request.san_dns) > 0:
sans = dict()
if request.san_dns and len(request.san_dns) > 0:
sans[CSR_ATTR_SANS_DNS] = request.san_dns
if request.ip_addresses and len(request.ip_addresses) > 0:
sans[CSR_ATTR_SANS_IP_ADDR] = request.ip_addresses
if request.email_addresses and len(request.email_addresses) > 0: