Ejemplo n.º 1
0
    def __init__(self,
                 project=None,
                 credentials=None,
                 read_only=False,
                 admin=False,
                 user_agent=DEFAULT_USER_AGENT,
                 timeout_seconds=DEFAULT_TIMEOUT_SECONDS):
        _ClientProjectMixin.__init__(self, project=project)
        if credentials is None:
            credentials = get_credentials()

        if read_only and admin:
            raise ValueError('A read-only client cannot also perform'
                             'administrative actions.')

        scopes = []
        if read_only:
            scopes.append(READ_ONLY_SCOPE)
        else:
            scopes.append(DATA_SCOPE)

        if admin:
            scopes.append(ADMIN_SCOPE)

        self._admin = bool(admin)
        self._credentials = credentials.create_scoped(scopes)
        self.user_agent = user_agent
        self.timeout_seconds = timeout_seconds
Ejemplo n.º 2
0
    def __init__(self, project=None, credentials=None,
                 read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT,
                 timeout_seconds=DEFAULT_TIMEOUT_SECONDS):
        _ClientProjectMixin.__init__(self, project=project)
        if credentials is None:
            credentials = get_credentials()

        if read_only and admin:
            raise ValueError('A read-only client cannot also perform'
                             'administrative actions.')

        scopes = []
        if read_only:
            scopes.append(READ_ONLY_SCOPE)
        else:
            scopes.append(DATA_SCOPE)

        if admin:
            scopes.append(ADMIN_SCOPE)

        self._admin = bool(admin)
        try:
            credentials = credentials.create_scoped(scopes)
        except AttributeError:
            pass
        self._credentials = credentials
        self.user_agent = user_agent
        self.timeout_seconds = timeout_seconds

        # These will be set in start().
        self._data_stub_internal = None
        self._cluster_stub_internal = None
        self._operations_stub_internal = None
        self._table_stub_internal = None
Ejemplo n.º 3
0
def make_channel(host, port):
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)
    creds = get_credentials().create_scoped(args.speech_scope)
    auth_header = ("authorization", "Bearer " + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(lambda _, func: func([auth_header], None), name="google_creds")
    composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin)
    return implementations.secure_channel(host, port, composite_channel)
Ejemplo n.º 4
0
    def __init__(self, project=None, credentials=None,
                 read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT):
        _ClientProjectMixin.__init__(self, project=project)
        if credentials is None:
            credentials = get_credentials()

        if read_only and admin:
            raise ValueError('A read-only client cannot also perform'
                             'administrative actions.')

        scopes = []
        if read_only:
            scopes.append(READ_ONLY_SCOPE)
        else:
            scopes.append(DATA_SCOPE)

        if admin:
            scopes.append(ADMIN_SCOPE)

        self._admin = bool(admin)
        try:
            credentials = credentials.create_scoped(scopes)
        except AttributeError:
            pass
        self._credentials = credentials
        self.user_agent = user_agent

        # Create gRPC stubs for making requests.
        self._data_stub = _make_data_stub(self)
        if self._admin:
            self._instance_stub_internal = _make_instance_stub(self)
            self._operations_stub_internal = _make_operations_stub(self)
            self._table_stub_internal = _make_table_stub(self)
Ejemplo n.º 5
0
    def __init__(self, project=None, credentials=None,
                 read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT,
                 timeout_seconds=DEFAULT_TIMEOUT_SECONDS):
        _ClientProjectMixin.__init__(self, project=project)
        if credentials is None:
            credentials = get_credentials()

        if read_only and admin:
            raise ValueError('A read-only client cannot also perform'
                             'administrative actions.')

        scopes = []
        if read_only:
            scopes.append(READ_ONLY_SCOPE)
        else:
            scopes.append(DATA_SCOPE)

        if admin:
            scopes.append(ADMIN_SCOPE)

        self._admin = bool(admin)
        try:
            credentials = credentials.create_scoped(scopes)
        except AttributeError:
            pass
        self._credentials = credentials
        self.user_agent = user_agent
        self.timeout_seconds = timeout_seconds

        # These will be set in start().
        self._data_stub_internal = None
        self._instance_stub_internal = None
        self._operations_stub_internal = None
        self._table_stub_internal = None
Ejemplo n.º 6
0
def connect(creds):
    """Construct a connection value to Google Storage API

    The credentials are retrieved using get_credentials that checks
    the environment for the correct values.

    """
    credentials = get_credentials()
    return storage.Client(credentials=credentials,
                          http=ThreadSafeHttp(credentials))
Ejemplo n.º 7
0
    def make_channel(self, host, port):
        ssl_channel = implementations.ssl_channel_credentials(None, None, None)
        creds = get_credentials().create_scoped([SPEECH_SCOPE])

        auth_header = ('Authorization', 'Bearer ' + creds.get_access_token().access_token)
        auth_plugin = implementations.metadata_call_credentials(
            lambda _, cb: cb([auth_header], None),
            name='google_creds')

        composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin)
        return implementations.secure_channel(host, port, composite_channel)
Ejemplo n.º 8
0
    def __init__(self, project=None, credentials=None, http=None):
        if project is None:
            project = _get_production_project()
        if project is None:
            raise ValueError('Project was not passed and could not be '
                             'determined from the environment.')
        self.project = project

        if credentials is None and http is None:
            credentials = get_credentials()
        self.connection = Connection(credentials=credentials, http=http)
Ejemplo n.º 9
0
    def from_environment(cls, *args, **kwargs):
        """Factory to retrieve implicit credentials while creating connection.

        :rtype: :class:`gcloud.connection.Connection`
        :returns: The connection created with the retrieved implicit
                  credentials.
        """
        credentials = get_credentials()
        if 'credentials' in kwargs:
            raise TypeError('credentials must not be in keyword arguments')
        kwargs['credentials'] = credentials
        return cls(*args, **kwargs)
def make_channel(host, port):
    """Creates an SSL channel with auth credentials from the environment."""
    # In order to make an https call, use an ssl channel with defaults
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)

    # Grab application default credentials from the environment
    creds = get_credentials().create_scoped([SPEECH_SCOPE])
    # Add a plugin to inject the creds into the header
    auth_header = ("Authorization", "Bearer " + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(lambda _, cb: cb([auth_header], None), name="google_creds")

    # compose the two together for both ssl and google auth
    composite_channel = implementations.composite_channel_credentials(ssl_channel, auth_plugin)

    return implementations.secure_channel(host, port, composite_channel)
Ejemplo n.º 11
0
def get_scoped_connection(klass, scopes):
    """Create a scoped connection to GCloud.

    :type klass: subclass of :class:`gcloud.connection.Connection`
    :param klass: the specific ``Connection`` class to instantiate.

    :type scopes: list of URLs
    :param scopes: the effective service auth scopes for the connection.

    :rtype: instance of ``klass``
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(scopes)
    return klass(credentials=scoped_credentials)
Ejemplo n.º 12
0
def get_connection():
    """Shortcut method to establish a connection to the Cloud Datastore.

    Use this if you are going to access several datasets
    with the same set of credentials (unlikely):

    >>> from gcloud import datastore
    >>> connection = datastore.get_connection()
    >>> dataset1 = connection.dataset('dataset1')
    >>> dataset2 = connection.dataset('dataset2')

    :rtype: :class:`gcloud.datastore.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = credentials.get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(SCOPE)
    return Connection(credentials=scoped_credentials)
Ejemplo n.º 13
0
def get_connection():
    """Shortcut method to establish a connection to the Cloud Datastore.

    Use this if you are going to access several datasets
    with the same set of credentials (unlikely):

    >>> from gcloud import datastore
    >>> connection = datastore.get_connection()
    >>> dataset1 = connection.dataset('dataset1')
    >>> dataset2 = connection.dataset('dataset2')

    :rtype: :class:`gcloud.datastore.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = credentials.get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(SCOPE)
    return Connection(credentials=scoped_credentials)
Ejemplo n.º 14
0
def make_channel(host, port):
    """Creates an SSL channel with auth credentials from the environment."""
    # In order to make an https call, use an ssl channel with defaults
    ssl_channel = implementations.ssl_channel_credentials(None, None, None)

    # Grab application default credentials from the environment
    creds = get_credentials().create_scoped([SPEECH_SCOPE])
    # Add a plugin to inject the creds into the header
    auth_header = ('Authorization',
                   'Bearer ' + creds.get_access_token().access_token)
    auth_plugin = implementations.metadata_call_credentials(
        lambda _, cb: cb([auth_header], None), name='google_creds')

    # compose the two together for both ssl and google auth
    composite_channel = implementations.composite_channel_credentials(
        ssl_channel, auth_plugin)

    return implementations.secure_channel(host, port, composite_channel)
Ejemplo n.º 15
0
def get_connection(project):
    """Shortcut method to establish a connection to Cloud Storage.

    Use this if you are going to access several buckets with the same
    set of credentials:

    >>> from gcloud import storage
    >>> connection = storage.get_connection(project)
    >>> bucket1 = connection.get_bucket('bucket1')
    >>> bucket2 = connection.get_bucket('bucket2')

    :type project: string
    :param project: The name of the project to connect to.

    :rtype: :class:`gcloud.storage.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = credentials.get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(SCOPE)
    return Connection(project=project, credentials=scoped_credentials)
Ejemplo n.º 16
0
    def from_environment(cls, *args, **kwargs):
        """Factory to retrieve implicit credentials while creating connection.

        :type args: tuple
        :param args: Remaining positional arguments to pass to constructor.

        :type kwargs: dictionary
        :param kwargs: Remaining keyword arguments to pass to constructor.

        :rtype: :class:`gcloud.connection.Connection`
        :returns: The connection created with the retrieved implicit
                  credentials.
        :raises: class:`TypeError` if there is a conflict with the kwargs
                 and the credentials created by the factory.
        """
        if 'credentials' in kwargs:
            raise TypeError('credentials must not be in keyword arguments')
        credentials = get_credentials()
        kwargs['credentials'] = credentials
        return cls(*args, **kwargs)
Ejemplo n.º 17
0
def get_connection(project):
    """Shortcut method to establish a connection to Cloud Storage.

    Use this if you are going to access several buckets with the same
    set of credentials:

    >>> from gcloud import storage
    >>> connection = storage.get_connection(project)
    >>> bucket1 = connection.get_bucket('bucket1')
    >>> bucket2 = connection.get_bucket('bucket2')

    :type project: string
    :param project: The name of the project to connect to.

    :rtype: :class:`gcloud.storage.connection.Connection`
    :returns: A connection defined with the proper credentials.
    """
    implicit_credentials = credentials.get_credentials()
    scoped_credentials = implicit_credentials.create_scoped(SCOPE)
    return Connection(project=project, credentials=scoped_credentials)
Ejemplo n.º 18
0
    def __init__(self, project=None, credentials=None,
                 read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT,
                 timeout_seconds=DEFAULT_TIMEOUT_SECONDS):
        _ClientProjectMixin.__init__(self, project=project)
        if credentials is None:
            credentials = get_credentials()

        if read_only and admin:
            raise ValueError('A read-only client cannot also perform'
                             'administrative actions.')

        scopes = []
        if read_only:
            scopes.append(READ_ONLY_SCOPE)
        else:
            scopes.append(DATA_SCOPE)

        if admin:
            scopes.append(ADMIN_SCOPE)

        self._admin = bool(admin)
        self._credentials = credentials.create_scoped(scopes)
        self.user_agent = user_agent
        self.timeout_seconds = timeout_seconds
Ejemplo n.º 19
0
 def __init__(self, credentials=None, http=None):
     if credentials is None and http is None:
         credentials = get_credentials()
     self.connection = self._connection_class(credentials=credentials,
                                              http=http)
Ejemplo n.º 20
0
 def __init__(self, credentials=None, http=None):
     if credentials is None and http is None:
         credentials = get_credentials()
     self.connection = self._connection_class(credentials=credentials, http=http)
Ejemplo n.º 21
0
 def _callFUT(self):
     from gcloud import credentials
     return credentials.get_credentials()
Ejemplo n.º 22
0
def init_colab():
    """
    Setting the configurations in Colab (Google)
    """
    # Login the user
    from google.colab import auth
    from google.colab import drive
    auth.authenticate_user()
    import requests
    from gcloud import credentials
    logging.basicConfig(level=logging.DEBUG)
    access_token = credentials.get_credentials().get_access_token(
    ).access_token
    gcloud_tokeninfo = requests.get(
        'https://www.googleapis.com/oauth2/v3/userinfo?access_token=' +
        access_token).json()
    email = gcloud_tokeninfo['email']
    last_name = ''
    first_name = email.split('@')[0]

    # Mount the drive
    drive.mount('/content/drive')

    # Configure the database
    home = '/content/drive/My Drive/.norm/'
    if not os.path.exists(home):
        os.mkdir(home)
        logging.info("Directory " + home + " created ")
    else:
        logging.info("Directory " + home + " already exists")

    from norm.config import Session
    from norm import config, security

    config.NORM_HOME = home

    config.DATA_STORAGE_ROOT = os.path.join(home, 'data')
    if not os.path.exists(config.DATA_STORAGE_ROOT):
        os.mkdir(config.DATA_STORAGE_ROOT)
        logging.info("Directory " + config.DATA_STORAGE_ROOT + " created ")
    else:
        logging.info("Directory " + config.DATA_STORAGE_ROOT +
                     " already exists")

    db_path = os.path.join(home, 'db')
    if not os.path.exists(db_path):
        os.mkdir(db_path)
        logging.info("Directory " + db_path + " created ")
    else:
        logging.info("Directory " + db_path + " already exists")

    config.DB_PATH = os.path.join(home, 'db/norm.db')
    if not os.path.exists(config.DB_PATH):
        orig_db_file = os.path.join(os.path.dirname(__file__), 'db/norm.db')
        from shutil import copyfile
        copyfile(orig_db_file, config.DB_PATH)
        logging.info("File " + config.DB_PATH + " copied")
    else:
        logging.info("File " + config.DB_PATH + " already exists")

    config.engine = create_engine('sqlite:///{}'.format(config.DB_PATH),
                                  poolclass=StaticPool)
    Session.configure(bind=config.engine)
    config.session = Session()
    config.context_id = str(datetime.utcnow().strftime('%m%d%Y.%H%M%S'))

    from norm.security import login
    user = login({
        'first_name': first_name,
        'last_name': last_name,
        'username': email,
        'email': email
    })

    logging.info(user.username + ' logged in')

    global context
    from norm.engine import NormCompiler, NormError
    context = NormCompiler(config.context_id, user, config.session)
Ejemplo n.º 23
0
    def _callFUT(self):
        from gcloud import credentials

        return credentials.get_credentials()