Skip to content

Commit

Permalink
changed the way certificates are uploaded. We are now uploading the c…
Browse files Browse the repository at this point in the history
…ertificate and private key as PEM files
  • Loading branch information
ryana-ccbill committed Jul 19, 2024
1 parent 85f227e commit 32fe482
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
14 changes: 9 additions & 5 deletions automation/service/apigee_tls_keystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,19 @@ def create_keystore(session: Session, org_name: str, env_name: str, keystore_nam
return Keystore(keystore_name)


def create_aliases(session: Session, org_name: str, env_name: str, keystore_name: str, alias_name: str, path_name: str) -> Alias:
def create_aliases(session: Session, org_name: str, env_name: str, keystore_name: str, alias_name: str,
cert_file: str, key_file: str) -> Alias:
"""Creates an alias in tls keystores Apigee with the given name"""
url = (APIGEE_API_URL + '/keystores/{}/aliases?alias={}&format={}').format(org_name, env_name, keystore_name,
alias_name, "pkcs12")
alias_name, "keycertfile")

file_name = {"file": open(path_name, 'rb')}
header = {'Content-Type': 'multipart/form-data'}
files = {
'certFile': ('fullchain.pem', open(cert_file, 'rb')),
'keyFile': ('key.pem', open(key_file, 'rb')),
}

response = session.post(url, headers=header, files=file_name)
header = {'Content-Type': 'multipart/form-data'}
response = session.post(url, headers=header, files=files)

if response.status_code != 201:
raise Exception(print_error(response))
Expand Down
14 changes: 3 additions & 11 deletions automation/upload_portal_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ def spec_exists(org_name: str, spec_name: str) -> bool:

def documentation_exists(
spec_name: str,
portal_name: str,
page_size: int) -> ApiDoc:
portal_name: str, pagesize) -> ApiDoc:
"""Retrieves a list of API Products exposed on a portal and checks if
there is an existing one using the given spec name."""

response = REQUEST.get(
'https://apigee.com/portals/api/sites/{}/apidocs?pageSize={}'.format(portal_name, page_size))
'https://apigee.com/portals/api/sites/{}/apidocs?pageSize={}'.format(portal_name, pagesize))

if response.status_code != 200:
raise RestException(utils.print_error(response))
Expand Down Expand Up @@ -157,10 +156,6 @@ def parse_args():
'-rt',
'--refresh_token',
help='apigee refresh token')
req_grp.add_argument(
'-pgs',
'--page_size',
help='page size for API calls that use paging - default is 100')

parsed = parser.parse_args()

Expand All @@ -182,9 +177,6 @@ def main():
password = args.password
refresh_token = args.refresh_token

# Some API calls make use of paging, if the page size is not defined, we default it to 100.
page_size = 100 if args.page_size is None else args.page_size

data = open(doc_path, 'r', encoding='utf8').read()
doc = json.loads(data)

Expand Down Expand Up @@ -218,7 +210,7 @@ def main():
# the spec ID dynamically.
doc.update({'specContent': spec_id})

api_doc = documentation_exists(spec_name, current_portal.id, page_size)
api_doc = documentation_exists(spec_name, current_portal.id)
if api_doc:
print("API Doc already exists.")
try:
Expand Down
17 changes: 12 additions & 5 deletions automation/upload_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ def parse_args():
help='name of the alias',
required=True)
req_grp.add_argument(
'-f',
'--file',
help='local file path of the pck12 certificate',
'-cf',
'--cert_file',
help='local file path of the pem certificate file',
required=True)
req_grp.add_argument(
'-kf',
'--key_file',
help='local file path of the pem private key file',
required=True)
req_grp.add_argument(
'-u',
Expand Down Expand Up @@ -94,7 +99,8 @@ def main():
keystore_name = args.keystore + "-" + datetime.datetime.today().strftime('%Y%m%d')
portal_keystore_name = env_name + "-" + datetime.datetime.today().strftime('%Y%m%d')
alias_name = args.alias
cert_file = args.file
cert_file = args.cert_file
key_file = args.key_file
username = args.username
password = args.password
refresh_token = args.refresh_token
Expand Down Expand Up @@ -124,7 +130,8 @@ def main():
# Create a new alias for the keystore on Apigee if there isn't an existing one.
if alias_name not in alias_list:
print('Alias does not exist - creating it on Apigee.')
alias = apigee_tls_keystore.create_aliases(REQUEST, org_name, env_name, keystore_name, alias_name, cert_file)
alias = apigee_tls_keystore.create_aliases(REQUEST, org_name, env_name, keystore_name, alias_name,
cert_file, key_file)
print(f'Alias is successfully created and certificate uploaded! {alias}')
else:
print('Certification can not be updated!')
Expand Down
17 changes: 12 additions & 5 deletions automation/upload_tls_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ def parse_args():
help='name of the alias',
required=True)
req_grp.add_argument(
'-f',
'--file',
help='local file path of the pck12 certificate',
'-cf',
'--cert_file',
help='local file path of the pem certificate file',
required=True)
req_grp.add_argument(
'-kf',
'--key_file',
help='local file path of the pem private key file',
required=True)
req_grp.add_argument(
'-u',
Expand Down Expand Up @@ -68,7 +73,8 @@ def main():
env_name = args.env
keystore_name = args.keystore
alias_name = args.alias
cert_file = args.file
cert_file = args.cert_file
key_file = args.key_file
username = args.username
password = args.password
refresh_token = args.refresh_token
Expand All @@ -95,7 +101,8 @@ def main():
# Create a new alias for the keystore on Apigee if there isn't an existing one.
if alias_name not in alias_list:
print('Alias does not exist - creating it on Apigee.')
alias = apigee_tls_keystore.create_aliases(REQUEST, org_name, env_name, keystore_name, alias_name, cert_file)
alias = apigee_tls_keystore.create_aliases(REQUEST, org_name, env_name, keystore_name,
alias_name, cert_file, key_file)
print(f'Alias is successfully created and certificate uploaded! {alias}')
else:
print('Certificate updated!')
Expand Down

0 comments on commit 32fe482

Please sign in to comment.