<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Use GCE service account in All Apps and Add-ons</title>
    <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491036#M60455</link>
    <description>&lt;P&gt;After asking, I decided to take a look and implement myself. If anyone needs a similar solution, you need to change 2 files: &lt;CODE&gt;bin/splunk_ta_gcp/common/credentials.py&lt;/CODE&gt; and &lt;CODE&gt;bin/splunk_ta_gcp/legacy/common.py&lt;/CODE&gt;.&lt;BR /&gt;
The diff of the changes (git format) is available below. Too bad Splunk doesn't have an open source repo for this add-on.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;diff --git a/bin/splunk_ta_gcp/common/credentials.py b/bin/splunk_ta_gcp/common/credentials.py
index 3bacb9f..c763fb8 100644
--- a/bin/splunk_ta_gcp/common/credentials.py
+++ b/bin/splunk_ta_gcp/common/credentials.py
@@ -1,6 +1,6 @@
 import json
 from google.oauth2 import service_account
-
+import google.auth

 class CredentialFactory(object):
     def __init__(self, config):
@@ -11,4 +11,9 @@ def load(self, profile, scopes):
         content = self._config.load(collection, stanza=profile, virtual=True)
         key = content['google_credentials']
         info = json.loads(key)
+
+        if not bool(info): # bool(dict) evaluates to False if empty
+            credentials, _ = google.auth.default()
+            return credentials
+
         return service_account.Credentials.from_service_account_info(info, scopes=scopes)
diff --git a/bin/splunk_ta_gcp/legacy/common.py b/bin/splunk_ta_gcp/legacy/common.py
index 2d7b601..f23f301 100755
--- a/bin/splunk_ta_gcp/legacy/common.py
+++ b/bin/splunk_ta_gcp/legacy/common.py
@@ -192,16 +192,19 @@ def create_google_client(config):
     }
     """

-    if config.get("google_credentials"):
+    scopes = config.get("scopes")
+
+    if config.get("google_credentials") and bool(config["google_credentials"]):
         credentials = service_account.Credentials.from_service_account_info(
             config["google_credentials"]
         )
+        if scopes:
+            credentials = credentials.with_scopes(scopes)
     else:
-        credentials, project = google.auth.default()
-
-    scopes = config.get("scopes")
-    if scopes:
-        credentials = credentials.with_scopes(scopes)
+        if scopes:
+            credentials, project = google.auth.default(scopes=scopes)
+        else:
+            credentials, project = google.auth.default()

     http = sr.build_http_connection(config, timeout=config.get("pulling_interval", 30))
     http = AuthorizedHttp(credentials, http=http)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 28 Sep 2019 21:17:38 GMT</pubDate>
    <dc:creator>tyron_</dc:creator>
    <dc:date>2019-09-28T21:17:38Z</dc:date>
    <item>
      <title>Use GCE service account</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491035#M60454</link>
      <description>&lt;P&gt;I am running Splunk on top of GCE instances, and I'd like to use GCE service account for the pubsub subscription instead of providing a JSON file. Is that possible? What would be the effort to implement this?&lt;/P&gt;</description>
      <pubDate>Sat, 28 Sep 2019 14:07:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491035#M60454</guid>
      <dc:creator>tyron_</dc:creator>
      <dc:date>2019-09-28T14:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: Use GCE service account</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491036#M60455</link>
      <description>&lt;P&gt;After asking, I decided to take a look and implement myself. If anyone needs a similar solution, you need to change 2 files: &lt;CODE&gt;bin/splunk_ta_gcp/common/credentials.py&lt;/CODE&gt; and &lt;CODE&gt;bin/splunk_ta_gcp/legacy/common.py&lt;/CODE&gt;.&lt;BR /&gt;
The diff of the changes (git format) is available below. Too bad Splunk doesn't have an open source repo for this add-on.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;diff --git a/bin/splunk_ta_gcp/common/credentials.py b/bin/splunk_ta_gcp/common/credentials.py
index 3bacb9f..c763fb8 100644
--- a/bin/splunk_ta_gcp/common/credentials.py
+++ b/bin/splunk_ta_gcp/common/credentials.py
@@ -1,6 +1,6 @@
 import json
 from google.oauth2 import service_account
-
+import google.auth

 class CredentialFactory(object):
     def __init__(self, config):
@@ -11,4 +11,9 @@ def load(self, profile, scopes):
         content = self._config.load(collection, stanza=profile, virtual=True)
         key = content['google_credentials']
         info = json.loads(key)
+
+        if not bool(info): # bool(dict) evaluates to False if empty
+            credentials, _ = google.auth.default()
+            return credentials
+
         return service_account.Credentials.from_service_account_info(info, scopes=scopes)
diff --git a/bin/splunk_ta_gcp/legacy/common.py b/bin/splunk_ta_gcp/legacy/common.py
index 2d7b601..f23f301 100755
--- a/bin/splunk_ta_gcp/legacy/common.py
+++ b/bin/splunk_ta_gcp/legacy/common.py
@@ -192,16 +192,19 @@ def create_google_client(config):
     }
     """

-    if config.get("google_credentials"):
+    scopes = config.get("scopes")
+
+    if config.get("google_credentials") and bool(config["google_credentials"]):
         credentials = service_account.Credentials.from_service_account_info(
             config["google_credentials"]
         )
+        if scopes:
+            credentials = credentials.with_scopes(scopes)
     else:
-        credentials, project = google.auth.default()
-
-    scopes = config.get("scopes")
-    if scopes:
-        credentials = credentials.with_scopes(scopes)
+        if scopes:
+            credentials, project = google.auth.default(scopes=scopes)
+        else:
+            credentials, project = google.auth.default()

     http = sr.build_http_connection(config, timeout=config.get("pulling_interval", 30))
     http = AuthorizedHttp(credentials, http=http)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 28 Sep 2019 21:17:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491036#M60455</guid>
      <dc:creator>tyron_</dc:creator>
      <dc:date>2019-09-28T21:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Use GCE service account</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491037#M60456</link>
      <description>&lt;P&gt;@tyron_ If your problem is resolved, please accept the answer to help future readers.&lt;/P&gt;</description>
      <pubDate>Sat, 28 Sep 2019 22:57:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/Use-GCE-service-account/m-p/491037#M60456</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2019-09-28T22:57:32Z</dc:date>
    </item>
  </channel>
</rss>

