Skip to content

Commit 22d8a83

Browse files
Merge pull request #312 from shwetapurohit/master
new samples for 7032 release
2 parents 8f4e4b0 + 51d9015 commit 22d8a83

13 files changed

+917
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
This directory contains samples for managing the MACHINE SSL certificate and the TRUSTED ROOT CHAINS
2+
3+
The sample were tested against vSphere 7.0+
4+
5+
### TRUSTED ROOT CHAINS Create/List/Delete/Get operations
6+
Sample | Description
7+
----------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8+
trusted_root_chains_create.py | Demonstrates creation of the trusted root chain in vCenter.
9+
trusted_root_chains_list.py | Demonstrates listing of the aliases of the published trusted root chains in vCenter.
10+
trusted_root_chains_delete.py | Demonstrates deletion of the trusted root chain corresponding to the provided alias.
11+
trusted_root_chains_get.py | Demonstrates retrieval of the trusted root chain corresponding to the provided alias.
12+
13+
### Tls certificate Renew/Get/Replace/Replace with VMCA operations
14+
Sample | Description
15+
----------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------
16+
replace_tls_certificate.py | Demonstrates replacement of the machine ssl certificate with a custom certificate signed by a third party CA.
17+
renew_tls_certificate.py | Demonstrates renewal of the machine ssl certificate for the given duration of time.
18+
get_tls_certificate.py | Demonstrates retrieval of the machine ssl certificate along with the X.509 certificate fields.
19+
replace_tls_certificate_with_vmca_signed.py | Demonstrates replacement of the machine ssl certificate with a VMCA signed certificate.
20+
21+
### VMCA ROOT replace operation
22+
Sample | Description
23+
----------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------
24+
replace_vmca_root.py | Demonstrates replacement of the VMCA root certificate and regeneration of all the other certificates.
25+
26+
### Testbed Requirement:
27+
- 1 vCenter Server on version 7.0+
28+
- The username being used to run the sample should have either the CertificateManagement.Manage or
29+
the CertificateManagement.Administer privilege depending on the operation which is intended to be performed.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2020. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
__vcenter_version__ = '7.0+'
18+
19+
import argparse
20+
21+
from vmware.vapi.vsphere.client import create_vsphere_client
22+
import requests
23+
from com.vmware.vcenter.certificate_management.vcenter_client import TlsCsr
24+
from samples.vsphere.common import (sample_cli, sample_util)
25+
26+
"""
27+
Description: Demonstrates the generation of the Certificate Signing request
28+
for the MACHINE SSL certificate
29+
30+
Sample Prerequisites:
31+
- The user invoking the API should have the CertificateManagement.Administer or the
32+
CertificateManagement.Manage privilege.
33+
"""
34+
35+
parser = sample_cli.build_arg_parser()
36+
37+
parser.add_argument('--keysize',
38+
help='Key size used to generate the private key.'
39+
'keysize will take 2048 bits if not modified')
40+
41+
parser.add_argument('--commonname',
42+
help='Common name of the certificate subject field.'
43+
'common name will take the Primary Network Identifier(PNID) if not modified.')
44+
45+
parser.add_argument('--organization',
46+
required=True,
47+
help='Organization field in certificate subject.')
48+
49+
parser.add_argument('--organizationunit',
50+
required=True,
51+
help='Organization unit field in certificate subject')
52+
53+
parser.add_argument('--locality',
54+
required=True,
55+
help='Locality field in the certificate subject')
56+
57+
parser.add_argument('--stateorprovince',
58+
required=True,
59+
help='State field in certificate subject')
60+
61+
parser.add_argument('--country',
62+
required=True,
63+
help='Country field in the certificate subject')
64+
65+
parser.add_argument('--emailaddress',
66+
required=True,
67+
help='Email field in Certificate extensions')
68+
69+
parser.add_argument('--subjectaltname',
70+
help='subjectaltname is list of Dns Names and Ip addresses')
71+
72+
args = sample_util.process_cli_args(parser.parse_args())
73+
74+
session = requests.session()
75+
session.verify = False if args.skipverification else True
76+
77+
# Login to vCenter
78+
vsphere_client = create_vsphere_client(server=args.server,
79+
username=args.username,
80+
password=args.password,
81+
session=session)
82+
83+
common_name = args.commonname
84+
organization = args.organization
85+
organization_unit = args.organizationunit
86+
locality = args.locality
87+
state_or_province = args.stateorprovince
88+
country = args.country
89+
email_address = args.emailaddress
90+
91+
if args.keysize is None:
92+
key_size = args.keysize
93+
else:
94+
key_size = int(args.keysize)
95+
96+
if args.subjectaltname is None:
97+
subject_alt_name = args.subjectaltname
98+
else:
99+
subject_alt_name = args.subjectaltname.split(',')
100+
101+
"""
102+
Create the spec for input to the API
103+
"""
104+
spec = TlsCsr.Spec(key_size=key_size,
105+
common_name=common_name,
106+
organization=organization,
107+
organization_unit=organization_unit,
108+
locality=locality,
109+
state_or_province=state_or_province,
110+
country=country,
111+
email_address=email_address,
112+
subject_alt_name=subject_alt_name)
113+
114+
print('Generating the certificate signing request based on the information provided in the spec ')
115+
print(vsphere_client.vcenter.certificate_management.vcenter.TlsCsr.create(spec))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2020. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
__vcenter_version__ = '7.0+'
18+
19+
import argparse
20+
from samples.vsphere.common import (sample_cli, sample_util)
21+
from vmware.vapi.vsphere.client import create_vsphere_client
22+
import requests
23+
24+
"""
25+
Description: Demonstrates retrieval of the MACHINE SSL certificate from the vCenter
26+
along with the decoded X.509 certificate fields
27+
28+
Sample Prerequisites:
29+
- The user invoking the API should have the System.Read privilege.
30+
"""
31+
32+
parser = sample_cli.build_arg_parser()
33+
34+
args = sample_util.process_cli_args(parser.parse_args())
35+
36+
session = requests.session()
37+
session.verify = False if args.skipverification else True
38+
39+
# Login to vCenter
40+
vsphere_client = create_vsphere_client(server=args.server,
41+
username=args.username,
42+
password=args.password,
43+
session=session)
44+
45+
print('Listing the MACHINE SSL certificate along with the decoded X.509 fields ')
46+
print(vsphere_client.vcenter.certificate_management.vcenter.Tls.get())
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2020. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
__vcenter_version__ = '7.0+'
18+
19+
import argparse
20+
21+
from vmware.vapi.vsphere.client import create_vsphere_client
22+
import requests
23+
from com.vmware.vcenter.certificate_management.vcenter_client import Tls
24+
from samples.vsphere.common import (sample_cli, sample_util)
25+
26+
"""
27+
Description: Demonstrates the renewal of the MACHINE SSL certificate
28+
29+
Sample Prerequisites:
30+
- The user invoking the API should have the CertificateManagement.Administer privilege.
31+
"""
32+
33+
parser = sample_cli.build_arg_parser()
34+
35+
parser.add_argument('--duration',
36+
help='Duration of time specified in number of days for which the '
37+
'MACHINE SSL certificate has to be renewed')
38+
39+
args = sample_util.process_cli_args(parser.parse_args())
40+
41+
session = requests.session()
42+
session.verify = False if args.skipverification else True
43+
44+
# Login to vCenter
45+
vsphere_client = create_vsphere_client(server=args.server,
46+
username=args.username,
47+
password=args.password,
48+
session=session)
49+
50+
if args.duration is None:
51+
print('Renewing the MACHINE SSL certificate for the duration of ' + str(730) + ' days')
52+
duration = args.duration
53+
else:
54+
print('Renewing the MACHINE SSL certificate for the specified duration of ' + args.duration + ' days')
55+
duration = int(args.duration)
56+
57+
vsphere_client.vcenter.certificate_management.vcenter.Tls.renew(duration)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2020. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
__vcenter_version__ = '7.0+'
18+
19+
import argparse
20+
21+
from vmware.vapi.vsphere.client import create_vsphere_client
22+
import requests
23+
from com.vmware.vcenter.certificate_management.vcenter_client import Tls
24+
from samples.vsphere.common import (sample_cli, sample_util)
25+
26+
"""
27+
Description: Demonstrates the replacement of the MACHINE SSL certificate with a custom
28+
certificate signed by an external third party CA.
29+
30+
Sample Prerequisites:
31+
- The user invoking the API should have the CertificateManagement.Administer privilege.
32+
"""
33+
34+
parser = sample_cli.build_arg_parser()
35+
36+
parser.add_argument('--cert',
37+
required=True,
38+
help='Leaf certificate for replace the MACHINE SSL certificate.')
39+
40+
parser.add_argument('--key',
41+
help='The private key.'
42+
'Not required if the gencsr api was used to generated the certificate signing request.')
43+
44+
parser.add_argument('--rootcert',
45+
help='The root certificate and the intermediate root certificates '
46+
'required to establish the chain of trust.'
47+
'Not required if the certificates are already present in the vCenter.')
48+
49+
args = sample_util.process_cli_args(parser.parse_args())
50+
51+
session = requests.session()
52+
session.verify = False if args.skipverification else True
53+
54+
# Login to vCenter
55+
vsphere_client = create_vsphere_client(server=args.server,
56+
username=args.username,
57+
password=args.password,
58+
session=session)
59+
60+
cert = args.cert.encode(encoding='utf-8').decode('unicode_escape')
61+
62+
if args.key is not None:
63+
key = args.encode(encoding='utf-8').key.decode('unicode_escape')
64+
else:
65+
key = args.key
66+
67+
if args.rootcert is not None:
68+
root_cert = args.rootcert.encode(encoding='utf-8').decode('unicode_escape')
69+
else:
70+
root_cert = args.rootcert
71+
72+
"""
73+
Create the spec for input to the API
74+
"""
75+
spec = Tls.Spec(cert=cert,
76+
key=key,
77+
root_cert=root_cert)
78+
79+
80+
print('The MACHINE SSL certificate will be replaced with the custom certificate ')
81+
vsphere_client.vcenter.certificate_management.vcenter.Tls.set(spec)

0 commit comments

Comments
 (0)