Exemplo n.º 1
0
 def test_grant_password_request_with_login_hint(self):
     requests = FakeRequests()
     session = MockSession()
     with patch("oauth2_client.credentials_manager.requests", new=requests), patch(
         "cloudfoundry_client.client.requests", new=requests
     ):
         requests.Session.return_value = session
         self._mock_info_calls(requests)
         requests.post.return_value = MockResponse(
             "%s/oauth/token" % self.AUTHORIZATION_ENDPOINT,
             status_code=HTTPStatus.OK.value,
             text=json.dumps(dict(access_token="access-token", refresh_token="refresh-token")),
         )
         client = CloudFoundryClient(
             self.TARGET_ENDPOINT, login_hint=quote(json.dumps(dict(origin="uaa"), separators=(",", ":")))
         )
         client.init_with_user_credentials("somebody", "p@s$w0rd")
         self.assertEqual("Bearer access-token", session.headers.get("Authorization"))
         requests.post.assert_called_with(
             requests.post.return_value.url,
             data=dict(
                 grant_type="password",
                 username="******",
                 scope="",
                 password="******",
                 login_hint="%7B%22origin%22%3A%22uaa%22%7D",
             ),
             headers=dict(Accept="application/json", Authorization="Basic Y2Y6"),
             proxies=dict(http="", https=""),
             verify=True,
         )
def cf_get_client(username, password, endpoint, http_proxy="", https_proxy=""):
    target_endpoint = endpoint
    proxy = dict(http=http_proxy, https=https_proxy)
    client = CloudFoundryClient(target_endpoint, proxy=proxy)
    client.init_with_user_credentials(username, password)

    return client
Exemplo n.º 3
0
 def test_grant_password_request_with_token_format_opaque(self):
     requests = FakeRequests()
     session = MockSession()
     with patch('oauth2_client.credentials_manager.requests', new=requests), \
          patch('cloudfoundry_client.client.requests', new=requests):
         requests.Session.return_value = session
         requests.get.return_value = MockResponse(
             '%s/v2/info' % TARGET_ENDPOINT,
             status_code=OK,
             text=json.dumps(
                 dict(api_version='2.1',
                      authorization_endpoint=TARGET_ENDPOINT)))
         requests.post.return_value = MockResponse(
             '%s/oauth/token' % TARGET_ENDPOINT,
             status_code=OK,
             text=json.dumps(
                 dict(access_token='access-token',
                      refresh_token='refresh-token')))
         client = CloudFoundryClient(TARGET_ENDPOINT, token_format='opaque')
         client.init_with_user_credentials('somebody', 'p@s$w0rd')
         self.assertEqual('Bearer access-token',
                          session.headers.get('Authorization'))
         requests.post.assert_called_with(requests.post.return_value.url,
                                          data=dict(grant_type='password',
                                                    username='******',
                                                    scope='',
                                                    password='******',
                                                    token_format='opaque'),
                                          headers=dict(
                                              Accept='application/json',
                                              Authorization='Basic Y2Y6'),
                                          proxies=dict(http='', https=''),
                                          verify=True)
Exemplo n.º 4
0
 def test_grant_password_request_with_login_hint(self):
     requests = FakeRequests()
     session = MockSession()
     with patch('oauth2_client.credentials_manager.requests', new=requests), \
          patch('cloudfoundry_client.client.requests', new=requests):
         requests.Session.return_value = session
         self._mock_info_calls(requests)
         requests.post.return_value = MockResponse(
             '%s/oauth/token' % self.AUTHORIZATION_ENDPOINT,
             status_code=HTTPStatus.OK.value,
             text=json.dumps(
                 dict(access_token='access-token',
                      refresh_token='refresh-token')))
         client = CloudFoundryClient(self.TARGET_ENDPOINT,
                                     login_hint=quote(
                                         json.dumps(dict(origin='uaa'),
                                                    separators=(',', ':'))))
         client.init_with_user_credentials('somebody', 'p@s$w0rd')
         self.assertEqual('Bearer access-token',
                          session.headers.get('Authorization'))
         requests.post.assert_called_with(
             requests.post.return_value.url,
             data=dict(grant_type='password',
                       username='******',
                       scope='',
                       password='******',
                       login_hint='%7B%22origin%22%3A%22uaa%22%7D'),
             headers=dict(Accept='application/json',
                          Authorization='Basic Y2Y6'),
             proxies=dict(http='', https=''),
             verify=True)
Exemplo n.º 5
0
def build_client_from_configuration():
    global _client
    if _client is None:
        _init_logging()
        cfg = ConfigParser()
        cfg.read(get_resource('test.properties'))
        proxy = None
        try:
            http = cfg.get('proxy', 'http')
            https = cfg.get('proxy', 'https')
            proxy = dict(http=http, https=https)
        except (NoSectionError, NoOptionError):
            pass
        skip_verification = False
        try:
            skip_verification_str = cfg.get('service', 'skip_ssl_verification')
            skip_verification = skip_verification_str.lower() == 'true'
        except (NoSectionError, NoOptionError):
            pass
        client = CloudFoundryClient(cfg.get('service', 'target_endpoint'), proxy=proxy,
                                    skip_verification=skip_verification)
        client.init_with_user_credentials(cfg.get('authentification', 'login'),
                                                              cfg.get('authentification', 'password'))
        client.org_guid = cfg.get('test_data', 'org_guid')
        client.space_guid = cfg.get('test_data', 'space_guid')
        client.app_guid = cfg.get('test_data', 'app_guid')
        client.log_app_guid = cfg.get('test_data', 'log_app_guid')
        client.service_guid = cfg.get('test_data', 'service_guid')
        client.service_name = cfg.get('test_data', 'service_name')
        client.plan_guid = cfg.get('test_data', 'plan_guid')
        client.creation_parameters = eval(cfg.get('test_data', 'creation_parameters'))
        client.update_parameters = eval(cfg.get('test_data', 'update_parameters'))
        _client = client

    return _client
Exemplo n.º 6
0
def build_client_from_configuration():
    global _client
    if _client is None:
        _init_logging()
        cfg = ConfigParser()
        cfg.read(get_resource('test.properties'))
        proxy = None
        try:
            http = cfg.get('proxy', 'http')
            https = cfg.get('proxy', 'https')
            proxy = dict(http=http, https=https)
        except (NoSectionError, NoOptionError):
            pass
        skip_verification = False
        try:
            skip_verification_str = cfg.get('service', 'skip_ssl_verification')
            skip_verification = skip_verification_str.lower() == 'true'
        except (NoSectionError, NoOptionError):
            pass
        client = CloudFoundryClient(cfg.get('service', 'target_endpoint'), proxy=proxy,
                                    skip_verification=skip_verification)
        client.init_with_user_credentials(cfg.get('authentification', 'login'),
                                                              cfg.get('authentification', 'password'))
        client.org_guid = cfg.get('test_data', 'org_guid')
        client.space_guid = cfg.get('test_data', 'space_guid')
        client.app_guid = cfg.get('test_data', 'app_guid')
        client.log_app_guid = cfg.get('test_data', 'log_app_guid')
        client.service_guid = cfg.get('test_data', 'service_guid')
        client.service_name = cfg.get('test_data', 'service_name')
        client.plan_guid = cfg.get('test_data', 'plan_guid')
        client.creation_parameters = eval(cfg.get('test_data', 'creation_parameters'))
        client.update_parameters = eval(cfg.get('test_data', 'update_parameters'))
        _client = client

    return _client
def connect_to_cloud(endpoint_credentials):
    """
    Connect to cloud foundry target api using the credentials
    """
    client_cf = None
    try:

        logging.info(': Using API Endpoint: %s', endpoint_credentials[0])
        proxy = {
            'http':
            'http://cis-india-pitc-bangalorez.proxy.corporate.ge.com:80',
            'https':
            'http://cis-india-pitc-bangalorez.proxy.corporate.ge.com:80'
        }
        client_cf = CloudFoundryClient(target_endpoint=endpoint_credentials[0],
                                       proxy=proxy,
                                       skip_verification=True)
        client_cf.init_with_user_credentials(endpoint_credentials[1],
                                             endpoint_credentials[2])
        logging.info(': %s user logged into CloudFoundry',
                     endpoint_credentials[1])
    except (ConnectionResetError, ConnectionRefusedError,
            ConnectionAbortedError, OAuthError) as error_x:
        logging.info('In Except block of method cloud_connect:::: %s', error_x)
        exit_program()
    return client_cf
Exemplo n.º 8
0
def loginCF(config):
    #cf login
    cf_endpoint = config.get('pcf_conf', 'endpoint')
    cf_user = config.get('pcf_conf', 'user')
    cf_passwd = config.get('pcf_conf', 'password')
    proxy = dict(http = os.environ.get('HTTP_PROXY', ''), https = os.environ.get('HTTPS_PROXY', ''))
    client = CloudFoundryClient(cf_endpoint, proxy = proxy, skip_verification = True)
    client.init_with_user_credentials(cf_user, cf_passwd)
    return client, client.service_information
Exemplo n.º 9
0
 def __init__(self, target_endpoint, username, password, organization_name,
              space_name):
     client = CloudFoundryClient(target_endpoint, skip_verification=False)
     client.init_with_user_credentials(username, password)
     self.client = client
     self.target_endpoint = target_endpoint
     self.organization_name = organization_name
     self.space_name = space_name
     self.space_guid = self.get_space_guid(self.organization_name,
                                           self.space_name)
Exemplo n.º 10
0
def build_client_from_configuration(previous_configuration=None):
    dir_conf = os.path.join(os.path.expanduser("~"))
    if not os.path.isdir(dir_conf):
        if os.path.exists(dir_conf):
            raise IOError("%s exists but is not a directory")
        os.mkdir(dir_conf)
    config_file = os.path.join(dir_conf, ".cf_client_python.json")
    if not os.path.isfile(config_file):
        target_endpoint = _read_value_from_user(
            "Please enter a target endpoint",
            "Url must starts with http:// or https://",
            lambda s: s.startswith("http://") or s.startswith("https://"),
            default="" if previous_configuration is None else previous_configuration.get("target_endpoint", ""),
        )
        skip_ssl_verification = _read_value_from_user(
            "Skip ssl verification (true/false)",
            "Enter either true or false",
            lambda s: s == "true" or s == "false",
            default="false"
            if previous_configuration is None
            else json.dumps(previous_configuration.get("skip_ssl_verification", False)),
        )
        login = _read_value_from_user("Please enter your login")
        password = _read_value_from_user("Please enter your password")
        client = CloudFoundryClient(target_endpoint, skip_verification=(skip_ssl_verification == "true"))
        client.init_with_user_credentials(login, password)
        with open(config_file, "w") as f:
            f.write(
                json.dumps(
                    dict(
                        target_endpoint=target_endpoint,
                        skip_ssl_verification=(skip_ssl_verification == "true"),
                        refresh_token=client.refresh_token,
                    ),
                    indent=2,
                )
            )
        return client
    else:
        try:
            configuration = None
            with open(config_file, "r") as f:
                configuration = json.load(f)
                client = CloudFoundryClient(
                    configuration["target_endpoint"], skip_verification=configuration["skip_ssl_verification"]
                )
                client.init_with_token(configuration["refresh_token"])
                return client
        except Exception as ex:
            if type(ex) == ConnectionError:
                raise
            else:
                _logger.exception("Could not restore configuration. Cleaning and recreating")
                os.remove(config_file)
                return build_client_from_configuration(configuration)
Exemplo n.º 11
0
def get_cf_client(config):
    # "why is this a function, and the rest of these are static?"
    # good question. The __init__ on this immediately probes the
    # api endpoint, which means we need to stub it for testing.
    # and having to add that stub to every test that _might_
    # `import extensions` would be bonkers. As a function, we should
    # only need to stub when we're actually thinking about CF
    logger.debug("getting cf client")
    client = CloudFoundryClient(config.CF_API_ENDPOINT)
    client.init_with_user_credentials(config.CF_USERNAME, config.CF_PASSWORD)
    return client
Exemplo n.º 12
0
def dbname_get_cf_info(postgres_certification, cf_user, cf_passwd):
    client = CloudFoundryClient(cf_endpoint, proxy = proxy, skip_verification = True)
    client.init_with_user_credentials(cf_user, cf_passwd)
    sql_dbnames = 'SELECT datname FROM pg_database WHERE datistemplate = false;'
    conn = psycopg2.connect(postgres_certification)
    cur = conn.cursor()
    cur.execute(sql_dbnames)
    dbnames = cur.fetchall()
    conn.commit()
    for i in dbnames:
        if i[0] != 'template0' and i[0] != 'template1' and i[0] != 'postgres':
            getServiceInfo(client, i[0])
Exemplo n.º 13
0
def build_client_from_configuration(previous_configuration=None):
    dir_conf = os.path.join(os.path.expanduser('~'))
    if not os.path.isdir(dir_conf):
        if os.path.exists(dir_conf):
            raise IOError('%s exists but is not a directory')
        os.mkdir(dir_conf)
    config_file = os.path.join(dir_conf, '.cf_client_python.json')
    if not os.path.isfile(config_file):
        target_endpoint = _read_value_from_user(
            'Please enter a target endpoint',
            'Url must starts with http:// or https://',
            lambda s: s.startswith('http://') or s.startswith('https://'),
            default='' if previous_configuration is None else
            previous_configuration.get('target_endpoint', ''))
        skip_ssl_verification = _read_value_from_user(
            'Skip ssl verification (true/false)',
            'Enter either true or false',
            lambda s: s == 'true' or s == 'false',
            default='false' if previous_configuration is None else json.dumps(
                previous_configuration.get('skip_ssl_verification', False)))
        login = _read_value_from_user('Please enter your login')
        password = _read_value_from_user('Please enter your password')
        client = CloudFoundryClient(
            target_endpoint,
            skip_verification=(skip_ssl_verification == 'true'))
        client.init_with_user_credentials(login, password)
        with open(config_file, 'w') as f:
            f.write(
                json.dumps(dict(
                    target_endpoint=target_endpoint,
                    skip_ssl_verification=(skip_ssl_verification == 'true'),
                    refresh_token=client.refresh_token),
                           indent=2))
        return client
    else:
        try:
            configuration = None
            with open(config_file, 'r') as f:
                configuration = json.load(f)
                client = CloudFoundryClient(
                    configuration['target_endpoint'],
                    skip_verification=configuration['skip_ssl_verification'])
                client.init_with_token(configuration['refresh_token'])
                return client
        except Exception as ex:
            if type(ex) == ConnectionError:
                raise
            else:
                _logger.exception(
                    "Could not restore configuration. Cleaning and recreating")
                os.remove(config_file)
                return build_client_from_configuration(configuration)
def connect_to_cloud(endpoint_credentials):
    """
    Connect to cloud foundry target api using the credentials
    """
    client_cf = None


    logging.info(': Using API Endpoint: %s', endpoint_credentials[0])
    proxy = {'http': 'http://cis-india-pitc-bangalorez.proxy.corporate.ge.com:80',
             'https': 'http://cis-india-pitc-bangalorez.proxy.corporate.ge.com:80'}
    client_cf = CloudFoundryClient(target_endpoint=endpoint_credentials[0],
                                   proxy=proxy, skip_verification=True)
    client_cf.init_with_user_credentials(endpoint_credentials[1], endpoint_credentials[2])
    logging.info(': %s user logged into CloudFoundry', endpoint_credentials[1])
    return client_cf
    def get_cloudfoundry_client(self):
        if self.client is None:
            proxy = dict(http=os.environ.get('HTTP_PROXY', ''),
                         https=os.environ.get('HTTPS_PROXY', ''))
            client = CloudFoundryClient(self.api_url, proxy=proxy)
            try:
                client.init_with_user_credentials(self.username, self.password)
                self.client = client
            except BaseException as e:
                msg = 'Failed to authenticate: {}, waiting 5 minutes and exiting'.format(
                    str(e))
                logging.error(msg)
                # The sleep is added to avoid automatically banning the user for too many failed login attempts
                time.sleep(5 * 60)

        return self.client
Exemplo n.º 16
0
def build_client_from_configuration(
        previous_configuration: dict = None) -> CloudFoundryClient:
    config_file = get_config_file()
    if not os.path.isfile(config_file):
        target_endpoint = _read_value_from_user(
            "Please enter a target endpoint",
            "Url must starts with http:// or https://",
            lambda s: s.startswith("http://") or s.startswith("https://"),
            default="" if previous_configuration is None else
            previous_configuration.get("target_endpoint", ""),
        )
        verify = _read_value_from_user(
            "Verify ssl (true/false)",
            "Enter either true or false",
            lambda s: s == "true" or s == "false",
            default="true" if previous_configuration is None else json.dumps(
                previous_configuration.get("verify", True)),
        )
        login = _read_value_from_user("Please enter your login")
        password = _read_value_from_user("Please enter your password")
        client = CloudFoundryClient(target_endpoint, verify=(verify == "true"))
        client.init_with_user_credentials(login, password)
        with open(config_file, "w") as f:
            f.write(
                json.dumps(dict(target_endpoint=target_endpoint,
                                verify=(verify == "true"),
                                refresh_token=client.refresh_token),
                           indent=2))
        return client
    else:
        try:
            configuration = None
            with open(config_file, "r") as f:
                configuration = json.load(f)
                client = CloudFoundryClient(configuration["target_endpoint"],
                                            verify=configuration["verify"])
                client.init_with_token(configuration["refresh_token"])
                return client
        except Exception as ex:
            if type(ex) == ConnectionError:
                raise
            else:
                _logger.exception(
                    "Could not restore configuration. Cleaning and recreating")
                os.remove(config_file)
                return build_client_from_configuration(configuration)
Exemplo n.º 17
0
def build_client_from_configuration(previous_configuration=None):
    dir_conf = os.path.join(os.path.expanduser('~'))
    if not os.path.isdir(dir_conf):
        if os.path.exists(dir_conf):
            raise IOError('%s exists but is not a directory')
        os.mkdir(dir_conf)
    config_file = os.path.join(dir_conf, '.cf_client_python.json')
    if not os.path.isfile(config_file):
        target_endpoint = _read_value_from_user('Please enter a target endpoint',
                                                'Url must starts with http:// or https://',
                                                lambda s: s.startswith('http://') or s.startswith('https://'),
                                                default='' if previous_configuration is None else
                                                previous_configuration.get('target_endpoint', ''))
        skip_ssl_verification = _read_value_from_user('Skip ssl verification (true/false)',
                                                      'Enter either true or false',
                                                      lambda s: s == 'true' or s == 'false',
                                                      default='false' if previous_configuration is None else
                                                      json.dumps(
                                                          previous_configuration.get('skip_ssl_verification', False)))
        login = _read_value_from_user('Please enter your login')
        password = _read_value_from_user('Please enter your password')
        client = CloudFoundryClient(target_endpoint, skip_verification=(skip_ssl_verification == 'true'))
        client.init_with_user_credentials(login, password)
        with open(config_file, 'w') as f:
            f.write(json.dumps(dict(target_endpoint=target_endpoint,
                                    skip_ssl_verification=(skip_ssl_verification == 'true'),
                                    refresh_token=client.refresh_token), indent=2))
        return client
    else:
        try:
            configuration = None
            with open(config_file, 'r') as f:
                configuration = json.load(f)
                client = CloudFoundryClient(configuration['target_endpoint'],
                                            skip_verification=configuration['skip_ssl_verification'])
                client.init_with_token(configuration['refresh_token'])
                return client
        except Exception, ex:
            if type(ex) == ConnectionError:
                raise
            else:
                _logger.exception("Could not restore configuration. Cleaning and recreating")
                os.remove(config_file)
                return build_client_from_configuration(configuration)
Exemplo n.º 18
0
def build_client_from_configuration(
        previous_configuration: dict = None) -> CloudFoundryClient:
    config_file = get_config_file()
    if not os.path.isfile(config_file):
        target_endpoint = _read_value_from_user(
            'Please enter a target endpoint',
            'Url must starts with http:// or https://',
            lambda s: s.startswith('http://') or s.startswith('https://'),
            default='' if previous_configuration is None else
            previous_configuration.get('target_endpoint', ''))
        verify = _read_value_from_user(
            'Verify ssl (true/false)',
            'Enter either true or false',
            lambda s: s == 'true' or s == 'false',
            default='true' if previous_configuration is None else json.dumps(
                previous_configuration.get('verify', True)))
        login = _read_value_from_user('Please enter your login')
        password = _read_value_from_user('Please enter your password')
        client = CloudFoundryClient(target_endpoint, verify=(verify == 'true'))
        client.init_with_user_credentials(login, password)
        with open(config_file, 'w') as f:
            f.write(
                json.dumps(dict(target_endpoint=target_endpoint,
                                verify=(verify == 'true'),
                                refresh_token=client.refresh_token),
                           indent=2))
        return client
    else:
        try:
            configuration = None
            with open(config_file, 'r') as f:
                configuration = json.load(f)
                client = CloudFoundryClient(configuration['target_endpoint'],
                                            verify=configuration['verify'])
                client.init_with_token(configuration['refresh_token'])
                return client
        except Exception as ex:
            if type(ex) == ConnectionError:
                raise
            else:
                _logger.exception(
                    "Could not restore configuration. Cleaning and recreating")
                os.remove(config_file)
                return build_client_from_configuration(configuration)
def build_client_from_configuration():
    global _client
    if _client is None:
        _init_logging()
        cfg = ConfigParser()
        cfg.read(get_resource("test.properties"))
        proxy = None
        try:
            http = cfg.get("proxy", "http")
            https = cfg.get("proxy", "https")
            proxy = dict(http=http, https=https)
        except (NoSectionError, NoOptionError):
            pass
        skip_verification = False
        try:
            skip_verification_str = cfg.get("service", "skip_ssl_verification")
            skip_verification = skip_verification_str.lower() == "true"
        except (NoSectionError, NoOptionError):
            pass
        client = CloudFoundryClient(cfg.get("service", "target_endpoint"),
                                    proxy=proxy,
                                    skip_verification=skip_verification)
        client.init_with_user_credentials(
            cfg.get("authentification", "login"),
            cfg.get("authentification", "password"))
        client.org_guid = cfg.get("test_data", "org_guid")
        client.space_guid = cfg.get("test_data", "space_guid")
        client.app_guid = cfg.get("test_data", "app_guid")
        client.log_app_guid = cfg.get("test_data", "log_app_guid")
        client.service_guid = cfg.get("test_data", "service_guid")
        client.service_name = cfg.get("test_data", "service_name")
        client.plan_guid = cfg.get("test_data", "plan_guid")
        client.creation_parameters = eval(
            cfg.get("test_data", "creation_parameters"))
        client.update_parameters = eval(
            cfg.get("test_data", "update_parameters"))
        _client = client

    return _client
Exemplo n.º 20
0
#! /usr/bin/env python3

import os
from cloudfoundry_client.client import CloudFoundryClient

target_endpoint = 'https://api.dev.cfdev.sh'
proxy = dict(http=os.environ.get('HTTP_PROXY', ''),
             https=os.environ.get('HTTPS_PROXY', ''))

# print('1')
client = CloudFoundryClient(target_endpoint, verify=False)
# print('1.5')
# init with user credentials
client.init_with_user_credentials('user', 'pass')
# print('2')
# init with refresh token (that will retrieve a fresh access token)
# client.init_with_token('refresh-token')
# init with access and refresh token (if the above method is not convenient)
#client.refresh_token = 'refresh-token'
#client._access_token = 'access-token'
# print('3')
print('#--- organization ---#')
for organization in client.v2.organizations:
    print(organization['metadata']['guid'])
print('#--- ---#')
Exemplo n.º 21
0
import os
from cloudfoundry_client.client import CloudFoundryClient

target_endpoint = 'https://somewhere.org'
proxy = dict(http=os.environ.get('HTTP_PROXY', ''),
             https=os.environ.get('HTTPS_PROXY', ''))
client = CloudFoundryClient(target_endpoint,
                            proxy=proxy,
                            skip_verification=True)
client.init_with_user_credentials('login', 'password')

for organization in client.organizations:
    print organization['metadata']['guid']
Exemplo n.º 22
0
    def deploy_cf(self, mtar, api, org, space, user, password):
        """
        Deploy mtar archive

        Parameters
        ----------
        mtar : str
            Path to the mtar archive
        mta_builder : str
            Mtar builder jar location
        api : str
            Cloud Foundry api url
        org : str
            Cloud Foundry org to deploy to.
        space : str
            Cloud Foundry space to deploy to.
        user : str
            Cloud Foudry user to deploy with.
        password : str
            Cloud Foundry user password to deploy with.
        """
        if not os.path.isabs(mtar):
            file_name = os.path.basename(mtar)
            mtar = os.path.join(os.getcwd(), file_name)

        if os.path.exists(mtar):
            # Verifying required data is provided
            if api == None or org == None or space == None or user == None or password == None:
                logger.error('Missing cl arguments. Minimal mtar, api, org, space, user, password required.')
                exit(1)

            # Setup cloud foundry client for validating org and space availability
            proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
            client = CloudFoundryClient(api, proxy=proxy, verify=False)

            # Login to api validating login credentials provided
            # CF Client does not properly allow for handling raised exception. It exits beforehand so try / except does not do much. Left now for reference. 
            try:
                client.init_with_user_credentials(user, password)
            except BaseException as ex:
                error_message = ''
                if type(ex).__name__ == 'OAuthError' or type(ex).__name__ == 'oauth2_client.credentials_manager.OAuthError':
                    # Credentials are not correct log accordingly
                    error_message = 'Provided username or password incorrect.'
                else:
                    error_message = 'Error {} occured while trying to authenticate with user {} against api {}.'.format(type(ex).__name__, user, api)
                logger.error(error_message)
                exit(1)

            # Get org and space guid
            org_guid = None
            space_guid = None
            for organization in client.v2.organizations.list(**{'name': [org]}):
                org_guid = organization['metadata']['guid']
                for org_space in organization.spaces(**{'name': [space]}): # perform a GET on spaces_url attribute
                    space_guid = org_space['metadata']['guid']

            if org_guid == None:
                logger.error('Provided orgnization {} is not available on the provided api url {}.'.format(org, api))
                exit(1)
            
            if space_guid == None:
                logger.error('Provided space {} is not available in the provided organization {}.'.format(space, org))
                exit(1)

            logger.info('Start deployment of: {}'.format(mtar))
            
            # Populate cli arguments for cf login
            cf_cli_args = ['cf', 'login']
            # Api
            cf_cli_args.extend(['-a', api])
            # Username
            cf_cli_args.extend(['-u', user])
            # Password
            cf_cli_args.extend(['-p', password])
            # Organization
            cf_cli_args.extend(['-o', org])
            # Space
            cf_cli_args.extend(['-s', space])

            # Call cf cli to login to correct api / org / space
            proc = subprocess.Popen(cf_cli_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            (out, err) = proc.communicate()
            result = out.decode('utf-8')
            if err == b'' and out.decode('utf-8').find('FAILED') == -1:
                # Login via cf cli is succesfull
                # Populate cli deploy arguments
                cf_cli_args = ['cf', 'deploy', mtar, '-f'] # @TODO: We are forcing deployment using the -f argument. Check if this can cause unwanted behaviour.
                proc = subprocess.Popen(cf_cli_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

                process_failed = False
                err=None

                for line in proc.stdout: 
                    line_decoded = line.decode()
                    if process_failed and err == None:
                        err=line_decoded
                    if line_decoded.find('FAILED') > -1:
                        process_failed = True    
                    logger.info('CF_OUTPUT: {}'.format(line_decoded))

                if process_failed:
                    logger.error('Deployment not succesfull. Error: {}'.format(err))
                else:
                    logger.info('Deployment operation was succesfull. Please check output for other issues.')
            else:
                logger.error('Deployment not succesfull. Error: {}'.format(result))
                
            logger.info('MTAR Deployment process done')
        else:
            logger.error('Deployment could not be started as deployment file not found: {}'.format(mtar))
Exemplo n.º 23
0
    exit()

cf_user = os.getenv('CF_USER', None)

if not cf_user:
    print('You need to set the CF_USER environment variable')
    exit()

cf_pass = os.getenv('CF_PASS', None)

if not cf_pass:
    print('You need to set the CF_PASS environment variable')
    exit()

client = CloudFoundryClient(cf_api, skip_verification=True)
client.init_with_user_credentials(cf_user, cf_pass)

for organization in client.organizations:
    for space in organization.spaces():
        space_name = space['entity']['name']

        if space_name != argv[1]:
            continue
        for app in space.apps():

            app_name = app['entity']['name']

            if app_name != argv[2]:

                continue
Exemplo n.º 24
0
class Cloudfoundry(object):
    ROBOT_LIBRARY_SCOPE = "GLOBAL"

    def __init__(self, target_endpoint, skip_verification, login, password,
                 organization_name, space_name, application_name,
                 service_broker_endpoint, service_broker_name,
                 service_broker_auth_user, service_broker_auth_password,
                 instance_name, default_create_instance_parameters):
        Cloudfoundry._check_parameters(default_create_instance_parameters)
        self.proxies = dict(http=os.environ.get('HTTP_PROXY', ''),
                            https=os.environ.get('HTTPS_PROXY', ''))
        self.client = CloudFoundryClient(target_endpoint,
                                         skip_verification=skip_verification,
                                         proxy=self.proxies)
        self.client.init_with_user_credentials(login, password)
        organization = self.client.organizations.get_first(
            name=organization_name)
        if organization is None:
            raise AssertionError('Unknown organization %s' % organization_name)
        space = self.client.spaces.get_first(
            organization_guid=organization['metadata']['guid'],
            name=space_name)
        if space is None:
            raise AssertionError('Unknown space %s' % space_name)
        self.space_guid = space['metadata']['guid']
        application = self.client.apps.get_first(space_guid=self.space_guid,
                                                 name=application_name)
        if application is None:
            raise AssertionError('Unknown application %s in space %s' %
                                 (application_name, space_name))
        self.application_guid = application['metadata']['guid']
        self.service_broker_name = service_broker_name
        self.service_broker_endpoint = service_broker_endpoint
        self.service_broker_auth_user = service_broker_auth_user
        self.service_broker_auth_password = service_broker_auth_password
        self.instance_name = instance_name
        self.default_create_instance_parameters = default_create_instance_parameters

        self.instance_guid = None
        self.binding_guid = None
        self.broker_guid = None
        for service_broker in self.client.service_brokers.list(
                space_guid=self.space_guid):
            if service_broker['entity']['name'] == self.service_broker_name \
                    and service_broker['entity']['broker_url'] == self.service_broker_endpoint:
                self.broker_guid = service_broker['metadata']['guid']
        self.plan_guid = None
        self._set_plan_from_broker()

    def clean_all_service_data(self):
        logging.info('clean_all_service_data - %s - %s - %s', self.space_guid,
                     self.instance_name, self.application_guid)
        instance = self.client.service_instances.get_first(
            space_guid=self.space_guid, name=self.instance_name)
        if instance is not None:
            logging.info('clean_all_service_data instance got - %s',
                         instance['metadata']['guid'])
            cleaned = False
            while not cleaned:
                for binding in self.client.service_bindings.list(
                        service_instance_guid=instance['metadata']['guid']):
                    logging.info('clean_all_service_data binding - %s',
                                 binding['metadata']['guid'])
                    self.client.service_bindings.remove(
                        binding['metadata']['guid'])
                    logging.info('clean_all_service_data - binding deleted')
                try:
                    self.client.service_instances.remove(
                        instance['metadata']['guid'])
                    cleaned = True
                except InvalidStatusCode, ex:
                    if ex.status_code == httplib.BAD_REQUEST and type(ex.body) == dict and \
                                    ex.body['error_code'] == 'CF-AssociationNotEmpty':
                        logging.debug(
                            'some binding appeared in the meantime. looping again'
                        )
                        pass
                    elif ex.status_code == httplib.BAD_GATEWAY and type(ex.body) == dict and \
                                    " can't be deleted during forced enrollment" in ex.body['description']:
                        logging.info(
                            '%s is in forced mode. Updating it as standard' %
                            instance['metadata']['guid'])
                        parameters = dict()
                        parameters['auto-enrollment'] = 'standard'
                        parameters[
                            'secret'] = self.service_broker_auth_password
                        self.client.service_instances.update(
                            instance['metadata']['guid'],
                            parameters=parameters)
                    else:
                        raise
            logging.info('clean_all_service_data - instance deleted')
        self.instance_guid = None
        self.binding_guid = None
Exemplo n.º 25
0
class PCFClass:

    def __init__(self):
        self.__spaces = []
        self.__apps = []
        self.__scaler_conf = {}
        api_endpoint = os.getenv("api_endpoint")
        username = os.getenv("user")
        password = os.getenv("password")
        proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
        print("Connecting to Api:{}".format(api_endpoint))
        self.__client = CloudFoundryClient(api_endpoint, proxy=proxy, verify=False)
        print("Authenticating")
        self.__client.init_with_user_credentials(username, password)
        print("Initializing PCF spaces")
        self.get_spaces()
        print("Initializing PCF apps")
        self.get_apps()
        self.init_scaling_confs()

    def get_apps(self):
        self.__apps.clear()
        for item in self.__client.v2.apps:
            self.__apps.append(item["entity"])
        return self.__apps

    def get_spaces(self):
        self.__spaces.clear()
        for item in self.__client.v2.spaces:
            self.__spaces.append(item["entity"])
        return self.__spaces

    def get_space(self, space_name):
        for item in self.__spaces:
            if item["name"] == space_name:
                return item

    def get_space_guid(self, space_name):
        for space in self.__spaces:
            if space_name == space["name"]:
                return space["apps_url"].split("/")[3]

    def get_app_guid(self, space_name, app_name):
        space_guid = self.get_space_guid(space_name)
        for app in self.__apps:
            if app_name == app["name"] and app["space_guid"] == space_guid:
                return app["service_bindings_url"].split("/")[3]

    def get_org_guid(self, org_name):
        org = self.get_org(org_name)[0]
        return org["spaces_url"].split("/")[-2]

    def get_org(self, org_name):
        org_list = []
        query = {}
        if org_name is not None:
            query["name"] = org_name
        for org in self.__client.v2.organizations.list(**query):
            org_list.append(org["entity"])
        return org_list

    def apps(self, space_name=None, app_name=None):
        space_guid = None
        app_list = []
        if space_name:
            space_guid = self.get_space_guid(space_name)
        for item in self.__apps:
            if not app_name or item["name"] == app_name:
                if not space_name or item["space_guid"] == space_guid:
                    app_list.append(item)
        return app_list

    def services(self):
        service_list = []
        for item in self.__client.v2.services:
            service_list.append(item)
        return service_list

    def service_keys(self):
        service_keys = []
        for item in self.__client.v2.service_keys:
            service_keys.append(item)
        return service_keys

    def get_buildpack(self, space_name, app_name):
        app_summary = self.app_summary(app_name,space_name)
        buildpack = {
            "name":  app_summary["buildpack"],
            "buildpack_guid": app_summary["detected_buildpack_guid"],
            "type": app_summary["detected_buildpack"]
        }
        return buildpack

    def get_buildpacks(self):
        buildpacks = []
        for item in self.__client.v2.buildpacks:
            buildpacks.append(item["entity"])
        return buildpacks

    def space_service_instances(self, space_name, service_name):
        service_instances = []
        space_guid = self.get_space_guid(space_name)
        query = {
            'space_guid': space_guid
        }
        if service_name is not None:
            query['name'] = service_name
        for item in self.__client.v2.service_instances.list(**query):
            service_instances.append(item['entity'])
        return service_instances

    def app_service_bindings(self, space_name, app_name, service_name):
        service_bindings = []
        service_guid = self.get_service_guid(space_name, app_name, service_name)
        app_guid = self.get_app_guid(space_name, app_name)
        query = {
            'app_guid': app_guid
        }
        if service_guid is not None:
            query['service_instance_guid'] = service_guid
        for item in self.__client.v2.service_bindings.list(**query):
            service_bindings.append(item["entity"])
        return service_bindings

    def remove_app_service_binding(self, space_name, app_name, service_name):
        service_guid = self.get_service_binding_id(space_name, app_name, service_name)
        self.__client.v2.service_bindings.remove(service_guid)

    def remove_space_service_instances(self, space_name, service_name):
        print("service name {}".format(service_name))
        service_guid = self.space_service_instances(space_name, service_name)[0]['service_bindings_url'].split("/")[-2]
        print("Received service guid {}".format(service_guid))
        self.__client.v2.service_instances.remove(service_guid)

    def get_service_guid(self, space_name, app_name, service_name):
        app_summary = self.app_summary(app_name, space_name)
        for service in app_summary["services"]:
            if service_name == service["name"]:
                return service["guid"]

    def get_service_binding_id(self, space_name, app_name, service_name):
        service_guid = self.get_service_guid(space_name, app_name, service_name)
        app_guid = self.get_app_guid(space_name, app_name)
        query = {
            'app_guid': app_guid,
            'service_instance_guid': service_guid
        }
        for item in self.__client.v2.service_bindings.list(**query):
            return item["entity"]["service_binding_parameters_url"].split("/")[-2]

    def app_stats(self, app_name, space_name):
        space_guid = self.get_space_guid(space_name)
        for item in self.__client.v2.apps.list(**{'name': app_name, 'space_guid': space_guid}):
            return item.stats()

    def app_summary(self, app_name, space_name):
        space_guid = self.get_space_guid(space_name)
        for item in self.__client.v2.apps.list(**{'name': app_name, 'space_guid': space_guid}):
            return item.summary()

    def get_url(self, url):
        return self.__client.get(url)

    def init_scaling_confs(self):
        for space in self.__spaces:
            scale_service_binding = self.space_service_instances(space["name"], "autoscale")
            if len(scale_service_binding) != 0:
                dashboard_url = (scale_service_binding[0]["dashboard_url"]) \
                                .replace("dashboard", "api") + "/bindings"
                try:
                    response = self.get_url(dashboard_url)
                    self.__scaler_conf[space["name"]] = json.loads(response.content)["resources"]
                except Exception as err:
                    self.__scaler_conf[space["name"]] = None
                    print('Exception occurred auto scale service url:', err)

    def get_scaling_conf(self, space_name, app_name):
        if space_name not in self.__scaler_conf:
            return {"max_instances": 0, "enabled": "false"}
        else:
            for item in self.__scaler_conf[space_name] or []:
                if item["app_name"] == app_name:
                    return item
            return {"max_instances": 0, "enabled": "false"}
Exemplo n.º 26
0
def cf_connection(cf_user, cf_passwd):
    client = CloudFoundryClient(cf_endpoint, proxy = proxy, skip_verification = True)
    client.init_with_user_credentials(cf_user, cf_passwd)
Exemplo n.º 27
0
    def run(self):
        """
		Regenerate our datapoints by querying all the /info endpoints
		"""
        global matrix, spaces, scanning

        client = CloudFoundryClient(self._auth.get('gate'),
                                    skip_verification=True)
        client.init_with_user_credentials(self._auth.get('user'),
                                          self._auth.get('pass'))

        for organization in client.organizations:
            org_name = organization['entity']['name']
            for space in organization.spaces():
                space_name = space['entity']['name']
                if space_name not in spaces:
                    spaces[space_name] = 0
                for app in space.apps():
                    name = app['entity']['name']
                    if name.split('-')[-1] not in spaces:
                        continue
                    app_name = '-'.join(app['entity']['name'].split('-')[:-1])
                    route = app.summary()['routes']
                    if not len(route):
                        continue
                    domain = route[0]['domain']['name']
                    host = route[0]['host']
                    url = 'http://{}.{}/info'.format(host, domain)
                    response = requests.get(url)
                    try:
                        if app_name not in matrix:
                            matrix[app_name] = {}
                            urls[app_name] = {}
                        urls[app_name][space_name] = url
                        matrix[app_name][space_name] = response.json()
                        spaces[space_name] += 1
                    except Exception as e:
                        pass

        if scanning:
            return

        scanning = True

        while True:
            time.sleep(SLEEP_INTERVAL)
            count = 0
            immutable_matrix = list(matrix)
            for app in immutable_matrix:
                immutable_spaces = list(spaces)
                for space in immutable_spaces:
                    if space in urls[app]:
                        url = urls[app][space]
                        response = requests.get(url)
                        if 'gateway-test' in url:
                            print(url, response)
                        count += 1
                        try:
                            json = response.json()
                        except Exception as e:
                            json = {
                                'branch': 'ERROR',
                                'version': str(response.status_code)
                            }

                        matrix[app][space] = json

            print('Finished pass, scanned "{}" urls'.format(count))
servicesTotalName = namespace_prefix + 'total-services'
spacesName      = namespace_prefix + 'spaces'
orgName         = namespace_prefix + 'orgs'
appsName        = namespace_prefix + 'apps'
appServicesName = namespace_prefix + 'appservices'
servicesName    = namespace_prefix + 'services'
orgTotalName    = namespace_prefix + 'total-orgs '
spacesTotalName = namespace_prefix + 'total-spaces '
servicesBindingsName = namespace_prefix + 'service-bindings'
servicesBindingsTotalName = namespace_prefix + 'total-service-bindings'
servicesInstancesName = namespace_prefix + 'service-instances'
servicesInstancesTotalName = namespace_prefix + 'total-service-instances'

# Login can also use a token
client = CloudFoundryClient(cloud_controller, proxy=proxy, verify=verify_ssl)
client.init_with_user_credentials(username, password)

# Netcat dependency in the container
def sendMetric(metric):
    entireMetric = metric + '| nc -q0 ' + proxy_host + ' ' + proxy_port
    if debug==True:
        print(entireMetric)
    os.popen(entireMetric)
    

# List all organizations
orgs = client.v2.organizations
orgCounter = 0
for org in orgs:
    orgGUID = org['metadata']['guid']
    orgName = org['entity']['name']
        cfg.read(get_resource('test.properties'))
        proxy = None
        try:
            http = cfg.get('proxy', 'http')
            https = cfg.get('proxy', 'https')
            proxy = dict(http=http, https=https)
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError), _:
            pass
        skip_verification = False
        try:
            skip_verification_str = cfg.get('service', 'skip_ssl_verification')
            skip_verification = skip_verification_str.lower() == 'true'
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError), _:
            pass
        client = CloudFoundryClient(cfg.get('service', 'target_endpoint'), proxy=proxy,
                                    skip_verification=skip_verification)
        client.init_with_user_credentials(cfg.get('authentification', 'login'),
                                                              cfg.get('authentification', 'password'))
        client.org_guid = cfg.get('test_data', 'org_guid')
        client.space_guid = cfg.get('test_data', 'space_guid')
        client.app_guid = cfg.get('test_data', 'app_guid')
        client.log_app_guid = cfg.get('test_data', 'log_app_guid')
        client.service_guid = cfg.get('test_data', 'service_guid')
        client.service_name = cfg.get('test_data', 'service_name')
        client.plan_guid = cfg.get('test_data', 'plan_guid')
        client.creation_parameters = eval(cfg.get('test_data', 'creation_parameters'))
        client.update_parameters = eval(cfg.get('test_data', 'update_parameters'))
        _client = client

    return _client