Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-17 00:10:07 +00:00
|
|
|
# (c) 2016, Tom Melendez (@supertom) <tom@supertom.com>
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2017-03-17 00:10:07 +00:00
|
|
|
import pytest
|
|
|
|
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
from ansible.compat.tests import mock, unittest
|
2017-03-17 00:10:07 +00:00
|
|
|
from ansible.module_utils.gcp import (_get_gcp_ansible_credentials, _get_gcp_credentials, _get_gcp_environ_var,
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
_get_gcp_libcloud_credentials, _get_gcp_environment_credentials,
|
|
|
|
_validate_credentials_file)
|
|
|
|
|
|
|
|
# Fake data/function used for testing
|
|
|
|
fake_env_data = {'GCE_EMAIL': 'gce-email'}
|
2017-05-18 16:07:16 +00:00
|
|
|
|
|
|
|
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
def fake_get_gcp_environ_var(var_name, default_value):
|
|
|
|
if var_name not in fake_env_data:
|
|
|
|
return default_value
|
|
|
|
else:
|
|
|
|
return fake_env_data[var_name]
|
|
|
|
|
2017-03-17 00:10:07 +00:00
|
|
|
# Fake AnsibleModule for use in tests
|
2017-05-18 16:07:16 +00:00
|
|
|
|
|
|
|
|
2017-03-17 00:10:07 +00:00
|
|
|
class FakeModule(object):
|
|
|
|
class Params():
|
|
|
|
data = {}
|
|
|
|
|
|
|
|
def get(self, key, alt=None):
|
|
|
|
if key in self.data:
|
|
|
|
return self.data[key]
|
|
|
|
else:
|
|
|
|
return alt
|
|
|
|
|
2017-09-12 07:11:13 +00:00
|
|
|
def __init__(self, data=None):
|
|
|
|
data = {} if data is None else data
|
|
|
|
|
2017-03-17 00:10:07 +00:00
|
|
|
self.params = FakeModule.Params()
|
|
|
|
self.params.data = data
|
|
|
|
|
|
|
|
def fail_json(self, **kwargs):
|
|
|
|
raise ValueError("fail_json")
|
|
|
|
|
2017-05-18 16:07:16 +00:00
|
|
|
def deprecate(self, **kwargs):
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
class GCPAuthTestCase(unittest.TestCase):
|
|
|
|
"""Tests to verify different Auth mechanisms."""
|
2017-03-17 00:10:07 +00:00
|
|
|
|
|
|
|
def setup_method(self, method):
|
|
|
|
global fake_env_data
|
|
|
|
fake_env_data = {'GCE_EMAIL': 'gce-email'}
|
|
|
|
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
def test_get_gcp_ansible_credentials(self):
|
|
|
|
input_data = {'service_account_email': 'mysa',
|
|
|
|
'credentials_file': 'path-to-file.json',
|
|
|
|
'project_id': 'my-cool-project'}
|
|
|
|
|
|
|
|
module = FakeModule(input_data)
|
|
|
|
actual = _get_gcp_ansible_credentials(module)
|
|
|
|
expected = tuple(input_data.values())
|
|
|
|
self.assertEqual(sorted(expected), sorted(actual))
|
|
|
|
|
|
|
|
def test_get_gcp_environ_var(self):
|
|
|
|
# Chose not to mock this so we could really verify that it
|
|
|
|
# works as expected.
|
|
|
|
existing_var_name = 'gcp_ansible_auth_test_54321'
|
|
|
|
non_existing_var_name = 'doesnt_exist_gcp_ansible_auth_test_12345'
|
|
|
|
os.environ[existing_var_name] = 'foobar'
|
2017-05-18 16:07:16 +00:00
|
|
|
self.assertEqual('foobar', _get_gcp_environ_var(
|
|
|
|
existing_var_name, None))
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
del os.environ[existing_var_name]
|
|
|
|
self.assertEqual('default_value', _get_gcp_environ_var(
|
|
|
|
non_existing_var_name, 'default_value'))
|
|
|
|
|
|
|
|
def test_get_gcp_libcloud_credentials_no_import(self):
|
|
|
|
"""No secrets imported. Whatever is sent in should come out."""
|
2017-05-18 16:07:16 +00:00
|
|
|
module = FakeModule()
|
|
|
|
actual = _get_gcp_libcloud_credentials(module,
|
|
|
|
service_account_email=None,
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
credentials_file=None,
|
|
|
|
project_id=None)
|
|
|
|
expected = (None, None, None)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
# no libcloud, with values
|
2017-05-18 16:07:16 +00:00
|
|
|
actual = _get_gcp_libcloud_credentials(module,
|
|
|
|
service_account_email='sa-email',
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
credentials_file='creds-file',
|
|
|
|
project_id='proj-id')
|
|
|
|
expected = ('sa-email', 'creds-file', 'proj-id')
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2017-05-18 16:07:16 +00:00
|
|
|
def test_get_gcp_libcloud_credentials_import(self):
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
"""secrets is imported and those values should be used."""
|
|
|
|
# Note: Opted for a real class here rather than MagicMock as
|
|
|
|
# __getitem__ comes for free.
|
|
|
|
class FakeSecrets:
|
|
|
|
def __init__(self):
|
|
|
|
# 2 element list, service account email and creds file
|
|
|
|
self.GCE_PARAMS = ['secrets-sa', 'secrets-file.json']
|
|
|
|
# dictionary with project_id, optionally auth_type
|
|
|
|
self.GCE_KEYWORD_PARAMS = {}
|
|
|
|
self.__file__ = 'THIS_IS_A_FAKEFILE_FOR_TESTING'
|
|
|
|
|
|
|
|
# patch in module
|
|
|
|
fake_secrets = FakeSecrets()
|
2017-05-18 16:07:16 +00:00
|
|
|
patcher = mock.patch.dict(sys.modules, {'secrets': fake_secrets})
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
patcher.start()
|
|
|
|
|
|
|
|
# obtain sa and creds from secrets
|
2017-05-18 16:07:16 +00:00
|
|
|
module = FakeModule()
|
|
|
|
actual = _get_gcp_libcloud_credentials(module,
|
|
|
|
service_account_email=None,
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
credentials_file=None,
|
|
|
|
project_id='proj-id')
|
|
|
|
expected = ('secrets-sa', 'secrets-file.json', 'proj-id')
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
2017-05-18 16:07:16 +00:00
|
|
|
# fetch project id. Current logic requires sa-email or creds to be
|
|
|
|
# set.
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
fake_secrets.GCE_KEYWORD_PARAMS['project'] = 'new-proj-id'
|
|
|
|
fake_secrets.GCE_PARAMS[1] = 'my-creds.json'
|
2017-05-18 16:07:16 +00:00
|
|
|
module = FakeModule()
|
|
|
|
actual = _get_gcp_libcloud_credentials(module,
|
|
|
|
service_account_email='my-sa',
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
credentials_file=None,
|
|
|
|
project_id=None)
|
|
|
|
expected = ('my-sa', 'my-creds.json', 'new-proj-id')
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
# stop patching
|
|
|
|
patcher.stop()
|
|
|
|
|
2017-05-18 16:07:16 +00:00
|
|
|
def test_validate_credentials_file(self):
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
# TODO(supertom): Only dealing with p12 here, check the other states
|
|
|
|
# of this function
|
|
|
|
module = mock.MagicMock()
|
|
|
|
with mock.patch("ansible.module_utils.gcp.open",
|
|
|
|
mock.mock_open(read_data='foobar'), create=True) as m:
|
2017-06-12 06:55:19 +00:00
|
|
|
# pem condition, warning is suppressed with the return_value
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
credentials_file = '/foopath/pem.pem'
|
|
|
|
is_valid = _validate_credentials_file(module,
|
|
|
|
credentials_file=credentials_file,
|
|
|
|
require_valid_json=False,
|
|
|
|
check_libcloud=False)
|
|
|
|
self.assertTrue(is_valid)
|
2017-01-27 23:45:23 +00:00
|
|
|
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
@mock.patch('ansible.module_utils.gcp._get_gcp_environ_var',
|
|
|
|
side_effect=fake_get_gcp_environ_var)
|
|
|
|
def test_get_gcp_environment_credentials(self, mockobj):
|
|
|
|
global fake_env_data
|
|
|
|
|
|
|
|
actual = _get_gcp_environment_credentials(None, None, None)
|
|
|
|
expected = tuple(['gce-email', None, None])
|
|
|
|
self.assertEqual(expected, actual)
|
2017-01-27 23:45:23 +00:00
|
|
|
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
fake_env_data = {'GCE_PEM_FILE_PATH': '/path/to/pem.pem'}
|
|
|
|
expected = tuple([None, '/path/to/pem.pem', None])
|
|
|
|
actual = _get_gcp_environment_credentials(None, None, None)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
# pem and creds are set, expect creds
|
|
|
|
fake_env_data = {'GCE_PEM_FILE_PATH': '/path/to/pem.pem',
|
|
|
|
'GCE_CREDENTIALS_FILE_PATH': '/path/to/creds.json'}
|
|
|
|
expected = tuple([None, '/path/to/creds.json', None])
|
|
|
|
actual = _get_gcp_environment_credentials(None, None, None)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
# expect GOOGLE_APPLICATION_CREDENTIALS over PEM
|
|
|
|
fake_env_data = {'GCE_PEM_FILE_PATH': '/path/to/pem.pem',
|
|
|
|
'GOOGLE_APPLICATION_CREDENTIALS': '/path/to/appcreds.json'}
|
|
|
|
expected = tuple([None, '/path/to/appcreds.json', None])
|
|
|
|
actual = _get_gcp_environment_credentials(None, None, None)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
# project tests
|
|
|
|
fake_env_data = {'GCE_PROJECT': 'my-project'}
|
|
|
|
expected = tuple([None, None, 'my-project'])
|
|
|
|
actual = _get_gcp_environment_credentials(None, None, None)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
fake_env_data = {'GOOGLE_CLOUD_PROJECT': 'my-cloud-project'}
|
|
|
|
expected = tuple([None, None, 'my-cloud-project'])
|
|
|
|
actual = _get_gcp_environment_credentials(None, None, None)
|
|
|
|
self.assertEqual(expected, actual)
|
|
|
|
|
|
|
|
# data passed in, picking up project id only
|
|
|
|
fake_env_data = {'GOOGLE_CLOUD_PROJECT': 'my-project'}
|
|
|
|
expected = tuple(['my-sa-email', '/path/to/creds.json', 'my-project'])
|
2017-05-18 16:07:16 +00:00
|
|
|
actual = _get_gcp_environment_credentials(
|
|
|
|
'my-sa-email', '/path/to/creds.json', None)
|
Adding auth support for google-api-python-client and gcloud-python (#19090)
Support for the Google API and GCloud-Python Clients have been added.
The three libraries:
* GCloud-Python: A new function, get_google_cloud_credentials, should be used. The credentials-object returned can be passed to any gcloud-python client. Using this client library requires in the installation of gcloud-python. This is preferred library for new modules.
* Google API: A new function, gcp_api_auth, should be used to take advantage of services requiring this client. This client library should be used if the desired functionality is not available in GCloud-Python. Using this library requires the installation of google-api-python-client.
* libcloud: Existing function, gcp_connect, should be used. The interface and return values have not changed and existing modules (such as gce, gce_pd and gce_net) should work without modification. Note that the credentials-fetching code has been refactored out of gcp_connect so that can be reused by all connection functions. To use this function, apache-libcloud must be installed.
Import guards have been added and will only be trigger if a user tries to use a function that is missing dependencies.
Credential-specifying mechanisms (i.e, ansible module params, env vars and libcloud secrets.py) have not changed. They have been refactored and unit tests have been added to allow for changes going forward. We are deprecating (and removing in a subsequent release) the ability to specify credentials via the libcloud secrets file. Also, we have deprecated (and also plan to remove in a subsequent release) the ability to use a p12 pem file for a key - the JSON format is strongly preferred. Deprecation warnings have been added for both of these issues (see the Ansible docs on how to disable deprecation warnings).
2016-12-29 17:33:52 +00:00
|
|
|
self.assertEqual(expected, actual)
|
2017-03-17 00:10:07 +00:00
|
|
|
|
|
|
|
@mock.patch('ansible.module_utils.gcp._get_gcp_environ_var',
|
|
|
|
side_effect=fake_get_gcp_environ_var)
|
|
|
|
def test_get_gcp_credentials(self, mockobj):
|
|
|
|
global fake_env_data
|
|
|
|
|
|
|
|
fake_env_data = {}
|
|
|
|
module = FakeModule()
|
|
|
|
module.params.data = {}
|
|
|
|
# Nothing is set, calls fail_json
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
_get_gcp_credentials(module)
|
|
|
|
|
|
|
|
# project_id (only) is set from Ansible params.
|
|
|
|
module.params.data['project_id'] = 'my-project'
|
2017-05-18 16:07:16 +00:00
|
|
|
actual = _get_gcp_credentials(
|
|
|
|
module, require_valid_json=True, check_libcloud=False)
|
2017-03-17 00:10:07 +00:00
|
|
|
expected = {'service_account_email': '',
|
|
|
|
'project_id': 'my-project',
|
|
|
|
'credentials_file': ''}
|
|
|
|
self.assertEqual(expected, actual)
|