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

adding support to encrypt values using GoCD API #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions gomatic/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def get(self, path):
return FakeResponse('{{"version": "{}"}}'.format(self.version))
raise RuntimeError("not expecting to be asked for anything else")

def post(self, path, data, headers=None):
expected_headers = {'Accept': 'application/vnd.go.cd.v1+json', 'Content-Type': 'application/json'}
if path == "/go/api/admin/encrypt" and headers == expected_headers:
value = json.loads(data)['value']
return FakeResponse('{{"encrypted_value": "{}"}}'.format(value))
raise RuntimeError("given this garbage: {} {} {}".format(path, data, headers))

def load_file(config_name):
with codecs.open('test-data/' + config_name + '.xml', encoding='utf-8') as xml_file:
Expand Down
5 changes: 5 additions & 0 deletions gomatic/go_cd_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import requests

from gomatic.gocd.config_repos import ConfigRepos
from gomatic.gocd.encryption import Encryption
from gomatic.gocd.security import Security
from gomatic.gocd.elastic import Elastic
from gomatic.gocd.agents import Agent
Expand Down Expand Up @@ -151,6 +152,9 @@ def pipeline_groups(self):
def config_repos(self):
return ConfigRepos(self.__xml_root.find('config-repos'), self)

def encryption(self):
return Encryption(self.server_version, self.__host_rest_client)

def ensure_pipeline_group(self, group_name):
pipeline_group_element = Ensurance(self.__xml_root).ensure_child_with_attribute("pipelines", "group", group_name)
return PipelineGroup(pipeline_group_element.element, self)
Expand Down Expand Up @@ -327,6 +331,7 @@ def post(self, path, data, headers=None):
raise RuntimeError("Could not post config to Go server (%s) [status code=%s]:\n%s" % (url, result.status_code, message))
except ValueError:
raise RuntimeError("Could not post config to Go server (%s) [status code=%s] (and result was not json):\n%s" % (url, result.status_code, result))
return result


def main(args):
Expand Down
23 changes: 23 additions & 0 deletions gomatic/gocd/encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from gomatic.mixins import CommonEqualityMixin
from distutils.version import LooseVersion as Version

API_PATH = '/go/api/admin/encrypt'
ENDPOINT_INTRODUCED_VERSION = Version('17.1.0')

class Encryption(CommonEqualityMixin):
def __init__(self, version, client):
self.version = version
self.client = client

def __supported(self):
return Version(self.version) >= ENDPOINT_INTRODUCED_VERSION

def encrypt(self, value):
if not self.__supported():
raise RuntimeError("Your server does not support the encrypt endpoint, which was introduced in 17.1")

headers = {'Accept': 'application/vnd.go.cd.v1+json',
'Content-Type': 'application/json'}
data = '{{"value": "{}"}}'.format(value)
response = self.client.post(API_PATH, data, headers)
return response.json()['encrypted_value']
10 changes: 10 additions & 0 deletions tests/go_cd_configurator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,16 @@ def test_can_reverse_engineer_pipeline(self):
"""
self.assertEqual(simplified(expected), simplified(actual))

class TestEncryption(unittest.TestCase):
def test_gets_encryped_value_from_api_call(self):
configurator = GoCdConfigurator(FakeHostRestClient(empty_config_xml, version='17.1.0'))
# expected == input because the stub does ROT26 encryption
expected = 'aValue'
actual = configurator.encryption().encrypt(expected)
self.assertEqual(expected, actual)
def test_aborts_on_invalid_version(self):
configurator = GoCdConfigurator(FakeHostRestClient(empty_config_xml, version='16.12.0'))
self.assertRaises(RuntimeError, configurator.encryption().encrypt, 'aValue')

class TestXmlFormatting(unittest.TestCase):
def test_can_format_simple_xml(self):
Expand Down