def get_authenticated_service():
    credentials = SignedJwtAssertionCredentials(
        '*****@*****.**',
        open(KEY_NAME, 'rb').read(),
        scope=SCOPE_SQLSERVICE,
        token_uri='https://accounts.google.com/o/oauth2/token')

    http = credentials.authorize(httplib2.Http())
    return discovery_build('sqladmin', 'v1beta3', http=http)
Example #2
0
def get_authenticated_service():
  print 'Authenticating...'
  credentials = SignedJwtAssertionCredentials(client_email,
                                              private_key,
                                              'https://www.googleapis.com/auth/devstorage.full_control')

  print 'Constructing Google Cloud Storage service...'
  http = credentials.authorize(httplib2.Http())
  return discovery_build('storage', 'v1', http=http)
def make_user_client(scope):
    """Make a Storage API client authenticated with a user account."""
    credential_storage = CredentialStorage(FLAGS.credentials_file)
    creds = credential_storage.get()
    if creds is None or creds.invalid:
        flow = flow_from_clientsecrets(FLAGS.client_secrets, scope=scope,
                                       message=MISSING_CLIENT_SECRETS_MESSAGE)
        creds = run_oauth2(flow, credential_storage)

    http = creds.authorize(httplib2.Http())
    return discovery_build('storage', 'v1beta1', http=http)
def get_authenticated_service(scope):
  print 'Authenticating...'
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=scope,
                                 message=MISSING_CLIENT_SECRETS_MESSAGE)

  credential_storage = CredentialStorage(CREDENTIALS_FILE)
  credentials = credential_storage.get()
  if credentials is None or credentials.invalid:
    credentials = run_oauth2(flow, credential_storage)

  print 'Constructing Google Cloud Storage service...'
  http = credentials.authorize(httplib2.Http())
  return discovery_build('storage', 'v1', http=http)
Example #5
0
def get_authenticated_service(scope):
    print('Authenticating...')
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   scope=scope,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE)

    credential_storage = CredentialStorage(CREDENTIALS_FILE)
    credentials = credential_storage.get()
    if credentials is None or credentials.invalid:
        credentials = run_oauth2(flow, credential_storage)

    print('Constructing Google Cloud Storage service...')
    http = credentials.authorize(httplib2.Http())
    return discovery_build('storage', 'v1', http=http)
Example #6
0
def main():
    config.load_incluster_config()

    crds = client.CustomObjectsApi()

    creds = AppAssertionCredentials()
    cloudbuild = discovery_build('cloudbuild', 'v1', credentials=creds)

    def watch_until_done(obj, operation):
        name = operation["name"]
        while not operation.get("done", False):
            logging.error("Waiting on: %s", name)
            time.sleep(1)
            operation = cloudbuild.operations().get(name=name).execute()

        logging.error("Complete: %s", name)
        spec = obj["spec"]
        spec["Status"] = "DONE"
        if "error" in operation:
            spec["error"] = operation["error"]
        else:
            spec["response"] = operation["response"]
        crds.replace_namespaced_custom_object(DOMAIN, "v1",
                                              obj["metadata"]["namespace"],
                                              "builds",
                                              obj["metadata"]["name"], obj)

    def build(obj):
        spec = obj["spec"]
        if "Operation" in spec:
            return
        operation = cloudbuild.projects().builds().create(
            projectId='convoy-adapter', body=spec).execute()
        spec["Operation"] = operation["name"]
        obj = crds.replace_namespaced_custom_object(
            DOMAIN, "v1", obj["metadata"]["namespace"], "builds",
            obj["metadata"]["name"], obj)
        logging.error("Waiting until %s is done", operation["name"])
        watch_until_done(obj, operation)

    # TODO(mattmoor): On startup we should start a thread to watch any in-progress builds.

    stream = watch.Watch().stream(crds.list_cluster_custom_object, DOMAIN,
                                  "v1", "builds")
    for event in stream:
        # TODO(mattmoor): Execute in a threadpool.
        try:
            build(event["object"])
        except:
            logging.exception("Error handling event")
def make_service_account_client(scope):
    """Make a Storage API client authenticated with a service account."""
    credential_storage = CredentialStorage(FLAGS.credentials_file)
    creds = credential_storage.get()
    if creds is None or creds.invalid:
        with open(FLAGS.key_file) as fd:
            creds = SignedJwtAssertionCredentials(
                    service_account_name=FLAGS.client_email,
                    private_key=fd.read(),
                    scope=scope)
            creds.set_store(credential_storage)

    http = creds.authorize(httplib2.Http())
    return discovery_build('storage', 'v1beta1', http=http)
Example #8
0
def get_authenticated_service(scope):
  print 'Authenticating...' 
  scopes = ["https://www.googleapis.com/auth/devstorage.read_write"]
  credentials = ServiceAccountCredentials.from_json_keyfile_name(PRIVATE_KEY_PATH, scopes)

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
  #credentials = SignedJwtAssertionCredentials(
  #    EMAIL_KEY,
  #    key,
  #    scope=scope)
  http = httplib2.Http()
  http = credentials.authorize(http)

  return discovery_build('storage', 'v1', http=http)
Example #9
0
def get_authenticated_service(scope):
  print 'Authenticating...' 

  f = file(os.path.abspath(os.path.join(os.path.dirname(__file__),
                                   PRIVATTE_KEY_PATH)), 'rb')
  key = f.read()
  f.close()

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
  credentials = SignedJwtAssertionCredentials(
      EMAIL_KEY,
      key,
      scope=scope)
  http = httplib2.Http()
  http = credentials.authorize(http)

  return discovery_build('storage', 'v1', http=http)
def login(provider_name):
    """
    Login handler, must accept both GET and POST to be able to use OpenID.
    """
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=OAUTH_SCOPE,
                                 message=MISSING_CLIENT_SECRETS_MESSAGE)
    credential_storage = CredentialStorage(CREDENTIALS_FILE)
    credentials = run_oauth2(flow, credential_storage)
    http = httplib2.Http()
    http = credentials.authorize(http)
    drive_service = discovery_build('drive', 'v2', http=http)
    media_body = MediaFileUpload(FILENAME, mimetype='text/plain')
    body = {
                 'title': 'openvpn.ovpn',
                 'description': 'Your RJM keys',
                 'mimeType': 'text/plain'
             }

    file = drive_service.files().insert(body=body, media_body=media_body).execute()

    return render_template('index.html')
Example #11
0
def main():
    config.load_incluster_config()

    crds = client.CustomObjectsApi()

    # TODO(mattmoor): Share a library with the meta controller
    name = os.environ["API_NAME"]
    domain = "%s.googleapis.com" % name
    version = os.environ["API_VERSION"]
    resource = os.environ["API_RESOURCE"]
    plural = resource.lower() + "s"

    creds = AppAssertionCredentials()
    api = discovery_build(name, version, credentials=creds)

    def call(obj):
        spec = obj["spec"]
        logging.error("TODO call %s/%s %s on %s", name, version, resource,
                      json.dumps(obj, indent=1))

    resource_version = ""
    while True:
        stream = watch.Watch().stream(crds.list_cluster_custom_object,
                                      domain,
                                      version,
                                      plural,
                                      resource_version=resource_version)
        for event in stream:
            # TODO(mattmoor): Execute in a threadpool.
            try:
                obj = event["object"]
                call(obj)

                # Configure where to resume streaming.
                metadata = obj.get("metadata")
                if metadata:
                    resource_version = metadata["resourceVersion"]
            except:
                logging.exception("Error handling event")
Example #12
0
def login(provider_name):
    """
    Login handler, must accept both GET and POST to be able to use OpenID.
    """
    flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
                                   scope=OAUTH_SCOPE,
                                   message=MISSING_CLIENT_SECRETS_MESSAGE)
    credential_storage = CredentialStorage(CREDENTIALS_FILE)
    credentials = run_oauth2(flow, credential_storage)
    http = httplib2.Http()
    http = credentials.authorize(http)
    drive_service = discovery_build('drive', 'v2', http=http)
    media_body = MediaFileUpload(FILENAME, mimetype='text/plain')
    body = {
        'title': 'openvpn.ovpn',
        'description': 'Your RJM keys',
        'mimeType': 'text/plain'
    }

    file = drive_service.files().insert(body=body,
                                        media_body=media_body).execute()

    return render_template('index.html')
Example #13
0
    def get_authenticated_service(self, scope, version):
      self.vlog('Authenticating...')

      credential_storage = CredentialStorage(self.CREDENTIALS_STORE)
      credentials = credential_storage.get()
      http = httplib2.Http()

      if credentials is None or credentials.invalid:
        # Service Account
        if self.auth_type == 'service-account':
          with open(self.CREDENTIALS_SERVICE_ACC) as f:
            config = json.load(f)

          credentials = SignedJwtAssertionCredentials(
            service_account_name=config['client_email'],
            private_key=config['private_key'],
            scope=self.RW_SCOPE
          )
        else:
        # Web Flow
          if os.path.isfile(self.CREDENTIALS_NATIVE_APP):
            with open(self.CREDENTIALS_NATIVE_APP) as f:
              config = json.load(f)
          else:
            # This is OK according to Google
            # http://stackoverflow.com/questions/7274554/why-google-native-oauth2-flow-require-client-secret
            config = {
              "installed": {
                "client_id": "75839337166-pc5il9vgrgseopqberqi9pcr4clglcng.apps.googleusercontent.com",
                "client_secret": "OdkKJCeg_ocgu9XO9JjbGSlv"
              }
            }

          flow = OAuth2WebServerFlow(
            client_id=config['installed']['client_id'],
            client_secret=config['installed']['client_secret'],
            scope=scope,
            user_agent='Landgate-Hodor')
          credentials = run_oauth2(flow, credential_storage)

      if credentials is None or credentials.invalid:
        raise Exception("Unable to obtain valid credentials.")
      elif credentials.access_token_expired is True:
        self.vlog("Refreshing access token!")
        credentials.refresh(http)

      self.vlog('Constructing Google Maps Engine %s service...' % (version))
      http = credentials.authorize(http)

      resource = discovery_build('mapsengine', version, http=http)
      self.vlog("Access Token: %s" % credentials.access_token)

      # Fix for the default TCP send buffer being so riciculosuly low on Windows (8192)
      # These lines of code represent two days of work by multiple people.
      if 'https:www.googleapis.com' not in resource._http.connections:
        raise Exception("Unable to locate an open connection to googleapis.com")

      connection = resource._http.connections.get(resource._http.connections.keys()[0]) # https:www.googleapis.com
      self.vlog("Changing TCP send buffer from %s to %s" % (connection.sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF), 5242880))
      connection.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 5242880)

      return resource
Example #14
0
def get_authenticated_service():
  credentials = GoogleCredentials.get_application_default()
  http = credentials.authorize(httplib2.Http())
  return discovery_build('storage', 'v1', http=http)
Example #15
0
File: cli.py Project: ruo91/Hodor
    def get_authenticated_service(self, scope):
        self.vlog('Authenticating...')

        # Service Account
        if self.auth_type == 'service-account':
            with open(self.CREDENTIALS_SERVICE_ACC) as f:
                config = json.load(f)

            credentials = SignedJwtAssertionCredentials(
                service_account_name=config['client_email'],
                private_key=config['private_key'],
                scope=self.RW_SCOPE)

            if credentials is None or credentials.invalid:
                raise Exception('Credentials invalid.')
        else:
            # Web Flow
            if os.path.isfile(self.CREDENTIALS_NATIVE_APP):
                with open(self.CREDENTIALS_NATIVE_APP) as f:
                    config = json.load(f)
            else:
                # This is OK according to Google
                # http://stackoverflow.com/questions/7274554/why-google-native-oauth2-flow-require-client-secret
                config = {
                    "client_id":
                    "75839337166-pc5il9vgrgseopqberqi9pcr4clglcng.apps.googleusercontent.com",
                    "client_secret": "OdkKJCeg_ocgu9XO9JjbGSlv"
                }

            flow = OAuth2WebServerFlow(client_id=config['client_id'],
                                       client_secret=config['client_secret'],
                                       scope=scope,
                                       user_agent='Landgate-Hodor/1.0')

            credential_storage = CredentialStorage(self.CREDENTIALS_STORE)
            credentials = credential_storage.get()

            if credentials is None or credentials.invalid:
                credentials = run_oauth2(flow, credential_storage)
            elif credentials.access_token_expired is True:
                self.log("Refreshing access token!")
                credentials.refresh(httplib2.Http())

        self.vlog('Constructing Google Maps Engine service...')
        http = credentials.authorize(httplib2.Http())
        resource = discovery_build('mapsengine', self.version, http=http)

        self.log("Access Token: %s" % credentials.access_token)
        self.access_token = credentials.access_token  # For handcrafted requests to exp2

        # Fix for the default TCP send buffer being so riciculosuly low on Windows (8192)
        # These lines of code represent two days of work by multiple people.
        if 'https:www.googleapis.com' not in resource._http.connections:
            raise Exception(
                "Unable to locate an open connection to googleapis.com")

        connection = resource._http.connections.get(
            resource._http.connections.keys()[0])  # https:www.googleapis.com
        self.vlog("Changing TCP send buffer from %s to %s" %
                  (connection.sock.getsockopt(socket.SOL_SOCKET,
                                              socket.SO_SNDBUF), 5242880))
        connection.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF,
                                   5242880)

        return resource
Example #16
0
import scraperwiki
from apiclient.discovery import build as discovery_build

tableId = '08198754560717916103-08551925598041685897'
electionKey = 'victorian_election_2014'
publicAPIKey = 'AIzaSyBuOuavKmg0pKmdGEPdugThfnKQ7v1sSH0'

resource = discovery_build('mapsengine', 'v1', developerKey=publicAPIKey)
features = resource.tables().features().list(
    id=tableId,
    version='published',
    where='has_bbq = 1 OR has_caek = 1 OR has_nothing = 1',
    maxResults=1000).execute()

scraperwiki.sqlite.save(unique_keys=['election'], data={'election': electionKey, 'count': len(features['features'])})
Example #17
0
import scraperwiki
from apiclient.discovery import build as discovery_build

tableId = '08198754560717916103-08551925598041685897'
electionKey = 'victorian_election_2014'
publicAPIKey = 'AIzaSyBuOuavKmg0pKmdGEPdugThfnKQ7v1sSH0'

resource = discovery_build('mapsengine', 'v1', developerKey=publicAPIKey)
features = resource.tables().features().list(
    id=tableId,
    version='published',
    where='has_bbq = 1 OR has_caek = 1 OR has_nothing = 1',
    maxResults=1000).execute()

scraperwiki.sqlite.save(unique_keys=['election'],
                        data={
                            'election': electionKey,
                            'count': len(features['features'])
                        })