From a96fcabd0352fcc33fbd49db4116a5408ba0ff7f Mon Sep 17 00:00:00 2001 From: Kenny Lee Sin Cheong <2530351+kleesc@users.noreply.github.com> Date: Tue, 4 Aug 2020 13:59:05 -0400 Subject: [PATCH] Handle generated ssh keypairs in build triggers as str (#503) The keys are either part of a dict being serialized into json with json.dumps to pass as a requests' body, or passed directly to the client(e.g github). In both cases, the value needs to be a str. --- buildtrigger/bitbuckethandler.py | 6 +++--- buildtrigger/customhandler.py | 4 ++-- buildtrigger/githubhandler.py | 8 +++++--- buildtrigger/gitlabhandler.py | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/buildtrigger/bitbuckethandler.py b/buildtrigger/bitbuckethandler.py index 49a5e4a66..3754cc0a4 100644 --- a/buildtrigger/bitbuckethandler.py +++ b/buildtrigger/bitbuckethandler.py @@ -310,12 +310,12 @@ class BitbucketBuildTrigger(BuildTriggerHandler): # Add a deploy key to the repository. public_key, private_key = generate_ssh_keypair() config["credentials"] = [ - {"name": "SSH Public Key", "value": public_key,}, + {"name": "SSH Public Key", "value": public_key.decode("ascii"),}, ] repository = self._get_repository_client() (result, created_deploykey, err_msg) = repository.deploykeys().create( - app.config["REGISTRY_TITLE"] + " webhook key", public_key + app.config["REGISTRY_TITLE"] + " webhook key", public_key.decode("ascii") ) if not result: @@ -337,7 +337,7 @@ class BitbucketBuildTrigger(BuildTriggerHandler): config["webhook_id"] = created_webhook["uuid"] self.config = config - return config, {"private_key": private_key} + return config, {"private_key": private_key.decode("ascii")} def deactivate(self): config = self.config diff --git a/buildtrigger/customhandler.py b/buildtrigger/customhandler.py index bd4d0450a..930dc2f00 100644 --- a/buildtrigger/customhandler.py +++ b/buildtrigger/customhandler.py @@ -194,11 +194,11 @@ class CustomBuildTrigger(BuildTriggerHandler): config = self.config public_key, private_key = generate_ssh_keypair() config["credentials"] = [ - {"name": "SSH Public Key", "value": public_key,}, + {"name": "SSH Public Key", "value": public_key.decode("ascii"),}, {"name": "Webhook Endpoint URL", "value": standard_webhook_url,}, ] self.config = config - return config, {"private_key": private_key} + return config, {"private_key": private_key.decode("ascii")} def deactivate(self): config = self.config diff --git a/buildtrigger/githubhandler.py b/buildtrigger/githubhandler.py index 48760fef1..27a5b0693 100644 --- a/buildtrigger/githubhandler.py +++ b/buildtrigger/githubhandler.py @@ -199,11 +199,13 @@ class GithubBuildTrigger(BuildTriggerHandler): # Add a deploy key to the GitHub repository. public_key, private_key = generate_ssh_keypair() config["credentials"] = [ - {"name": "SSH Public Key", "value": public_key,}, + {"name": "SSH Public Key", "value": public_key.decode("ascii"),}, ] try: - deploy_key = gh_repo.create_key("%s Builder" % app.config["REGISTRY_TITLE"], public_key) + deploy_key = gh_repo.create_key( + "%s Builder" % app.config["REGISTRY_TITLE"], public_key.decode("ascii") + ) config["deploy_key_id"] = deploy_key.id except GithubException as ghe: default_msg = "Unable to add deploy key to repository: %s" % new_build_source @@ -225,7 +227,7 @@ class GithubBuildTrigger(BuildTriggerHandler): msg = GithubBuildTrigger._get_error_message(ghe, default_msg) raise TriggerActivationException(msg) - return config, {"private_key": private_key} + return config, {"private_key": private_key.decode("ascii")} @_catch_ssl_errors def deactivate(self): diff --git a/buildtrigger/gitlabhandler.py b/buildtrigger/gitlabhandler.py index d375040ee..775a535c7 100644 --- a/buildtrigger/gitlabhandler.py +++ b/buildtrigger/gitlabhandler.py @@ -227,7 +227,7 @@ class GitLabBuildTrigger(BuildTriggerHandler): # Add a deploy key to the repository. public_key, private_key = generate_ssh_keypair() config["credentials"] = [ - {"name": "SSH Public Key", "value": public_key,}, + {"name": "SSH Public Key", "value": public_key.decode("ascii"),}, ] key = gl_project.keys.create( @@ -259,7 +259,7 @@ class GitLabBuildTrigger(BuildTriggerHandler): config["hook_id"] = hook.get_id() self.config = config - return config, {"private_key": private_key} + return config, {"private_key": private_key.decode("ascii")} def deactivate(self): config = self.config