Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update to use new library version #34

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ outputs:
description: 'The action stores the retrieved secrets in output variables defined by the end user. The <output_id> must be a unique identifier within the outputs object. The <output_id> must start with a letter or _ and contain only alphanumeric characters, -, or _.'
runs:
using: 'docker'
image: 'docker://beyondtrust/secrets-github-action:1.0.0'
image: 'docker://beyondtrust/secrets-github-action:1.0.1'
args:
- ${{ inputs.client_id }}
- ${{ inputs.client_secret }}
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pyOpenSSL==23.2.0
pyOpenSSL==24.0.0
retry-requests==2.0.0
github_action_utils==1.1.0
coverage==7.3.1
Expand Down
76 changes: 42 additions & 34 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import logging
import json
import secrets_safe_library
import requests
from retry_requests import retry

from secrets_safe_library import secrets_safe, authentication, utils, managed_account
from github_action_utils import error
Expand Down Expand Up @@ -39,15 +41,11 @@
)

logger = logging.getLogger(LOGGER_NAME)

TIMEOUT_CONNECTION_SECONDS = 30
TIMEOUT_REQUEST_SECONDS = 30
CERTIFICATE = env['CERTIFICATE'].replace(r'\n', '\n') if 'CERTIFICATE' in env else None
CERTIFICATE_KEY = env['CERTIFICATE_KEY'].replace(r'\n', '\n') if 'CERTIFICATE_KEY' in env else None

if CERTIFICATE:
CERTIFICATE = f"{CERTIFICATE}\n"
if CERTIFICATE_KEY:
CERTIFICATE_KEY = f"{CERTIFICATE_KEY}\n"

COMMAND_MARKER: str = "::"

def append_output(name, value):
Expand Down Expand Up @@ -140,35 +138,45 @@ def get_secrets(secret_obj, secrets):


def main():
try:
authentication_obj = authentication.Authentication(API_URL,
CLIENT_ID,
CLIENT_SECRET,
CERTIFICATE,
CERTIFICATE_KEY,
VERIFY_CA,
logger)

get_api_access_response = authentication_obj.get_api_access()

utils.print_log(logger, f"{secrets_safe_library.__library_name__} version: {secrets_safe_library.__version__}", logging.DEBUG)
try:
with requests.Session() as session:
req = retry(session, retries=3, backoff_factor=0.2, status_to_retry=(400,408,500,502,503,504))

certificate, certificate_key = utils.prepare_certificate_info(CERTIFICATE, CERTIFICATE_KEY)

authentication_obj = authentication.Authentication(
req,
TIMEOUT_CONNECTION_SECONDS,
TIMEOUT_REQUEST_SECONDS,
API_URL,
CLIENT_ID,
CLIENT_SECRET,
certificate,
certificate_key,
VERIFY_CA,
logger)

get_api_access_response = authentication_obj.get_api_access()

if get_api_access_response.status_code != 200:
error_message = f"Please check credentials, error {get_api_access_response.text}"
show_error(error_message)

if not SECRET_PATH and not MANAGED_ACCOUNT_PATH:
error_message = f"Nothing to do, SECRET and MANAGED_ACCOUNT parameters are empty"
show_error(error_message)

if SECRET_PATH:
secrets_safe_obj = secrets_safe.SecretsSafe(authentication=authentication_obj, logger=logger, separator=PATH_SEPARATOR)
get_secrets(secrets_safe_obj, SECRET_PATH)

if MANAGED_ACCOUNT_PATH:
managed_account_obj = managed_account.ManagedAccount(authentication=authentication_obj, logger=logger, separator=PATH_SEPARATOR)
get_secrets(managed_account_obj, MANAGED_ACCOUNT_PATH)

utils.print_log(logger, f"{secrets_safe_library.__library_name__} version: {secrets_safe_library.__version__}", logging.DEBUG)

if get_api_access_response.status_code != 200:
error_message = f"Please check credentials, error {get_api_access_response.text}"
show_error(error_message)

if not SECRET_PATH and not MANAGED_ACCOUNT_PATH:
error_message = f"Nothing to do, SECRET and MANAGED_ACCOUNT parameters are empty"
show_error(error_message)

if SECRET_PATH:
secrets_safe_obj = secrets_safe.SecretsSafe(authentication=authentication_obj, logger=logger, separator=PATH_SEPARATOR)
get_secrets(secrets_safe_obj, SECRET_PATH)

if MANAGED_ACCOUNT_PATH:
managed_account_obj = managed_account.ManagedAccount(authentication=authentication_obj, logger=logger, separator=PATH_SEPARATOR)
get_secrets(managed_account_obj, MANAGED_ACCOUNT_PATH)

authentication_obj.sign_app_out()

except Exception as e:
show_error(e)