All Apps and Add-ons

Use GCE service account

tyron_
Explorer

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?

0 Karma
1 Solution

tyron_
Explorer

After asking, I decided to take a look and implement myself. If anyone needs a similar solution, you need to change 2 files: bin/splunk_ta_gcp/common/credentials.py and bin/splunk_ta_gcp/legacy/common.py.
The diff of the changes (git format) is available below. Too bad Splunk doesn't have an open source repo for this add-on.

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)

View solution in original post

0 Karma

tyron_
Explorer

After asking, I decided to take a look and implement myself. If anyone needs a similar solution, you need to change 2 files: bin/splunk_ta_gcp/common/credentials.py and bin/splunk_ta_gcp/legacy/common.py.
The diff of the changes (git format) is available below. Too bad Splunk doesn't have an open source repo for this add-on.

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)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

@tyron_ If your problem is resolved, please accept the answer to help future readers.

---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...