Exemplo n.º 1
0
def _get_tls_cert_and_key():
  """Get the TLS cert from instance metadata."""
  # TODO(ochang): Implement a fake metadata server for testing.
  local_cert_location = environment.get_value('UNTRUSTED_TLS_CERT_FOR_TESTING')
  local_key_location = environment.get_value('UNTRUSTED_TLS_KEY_FOR_TESTING')

  if local_cert_location and local_key_location:
    with open(local_cert_location) as f:
      cert_contents = f.read()

    with open(local_key_location) as f:
      key_contents = f.read()

    return cert_contents, key_contents

  return (compute_metadata.get('instance/attributes/tls-cert'),
          compute_metadata.get('instance/attributes/tls-key'))
Exemplo n.º 2
0
def _get_tls_cert_and_key():
    """Get the TLS cert from instance metadata."""
    # TODO(ochang): Implement a fake metadata server for testing.
    local_cert_location = environment.get_value(
        'UNTRUSTED_TLS_CERT_FOR_TESTING')
    local_key_location = environment.get_value('UNTRUSTED_TLS_KEY_FOR_TESTING')

    if local_cert_location and local_key_location:
        with open(local_cert_location) as f:
            cert_contents = f.read()

        with open(local_key_location) as f:
            key_contents = f.read()

        return cert_contents, key_contents

    # TODO(mbarbella): Remove this after migrating to Python 3. The grpc library
    # has explicit type checks against str.
    cert_contents = str(compute_metadata.get('instance/attributes/tls-cert'))
    cert_contents = utils.newstr_to_native_str(cert_contents)
    key_contents = str(compute_metadata.get('instance/attributes/tls-key'))
    key_contents = utils.newstr_to_native_str(key_contents)
    return cert_contents, key_contents
Exemplo n.º 3
0
def _initialize_monitored_resource():
  """Monitored resources."""
  global _monitored_resource
  _monitored_resource = monitoring_v3.types.MonitoredResource()

  # TODO(ochang): Use generic_node when that is available.
  _monitored_resource.type = 'gce_instance'

  # The project ID must be the same as the one we write metrics to, not the ID
  # where the instance lives.
  _monitored_resource.labels['project_id'] = utils.get_application_id()

  # Use bot name here instance as that's more useful to us.
  _monitored_resource.labels['instance_id'] = environment.get_value('BOT_NAME')

  if compute_metadata.is_gce():
    # Returned in the form projects/{id}/zones/{zone}
    zone = compute_metadata.get('instance/zone').split('/')[-1]
    _monitored_resource.labels['zone'] = zone
  else:
    # Default zone for instances not on GCE.
    _monitored_resource.labels['zone'] = 'us-central1-f'