Blame SOURCES/bz1685814-fence_gce-add-serviceaccount-file-support.patch

2107d2
diff --color -uNr a/agents/gce/fence_gce.py b/agents/gce/fence_gce.py
2107d2
--- a/agents/gce/fence_gce.py	2021-06-11 14:28:37.751959830 +0200
2107d2
+++ b/agents/gce/fence_gce.py	2021-06-11 14:54:03.638926494 +0200
2107d2
@@ -15,9 +15,15 @@
2107d2
   import urllib2 as urlrequest
2107d2
 sys.path.append("@FENCEAGENTSLIBDIR@")
2107d2
 
2107d2
-import googleapiclient.discovery
2107d2
 from fencing import fail_usage, run_delay, all_opt, atexit_handler, check_input, process_input, show_docs, fence_action
2107d2
-
2107d2
+try:
2107d2
+  import googleapiclient.discovery
2107d2
+  try:
2107d2
+    from google.oauth2.credentials import Credentials as GoogleCredentials
2107d2
+  except:
2107d2
+    from oauth2client.client import GoogleCredentials
2107d2
+except:
2107d2
+  pass
2107d2
 
2107d2
 METADATA_SERVER = 'http://metadata.google.internal/computeMetadata/v1/'
2107d2
 METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
2107d2
@@ -175,12 +181,21 @@
2107d2
 		"required" : "0",
2107d2
 		"order" : 4
2107d2
 	}
2107d2
+	all_opt["serviceaccount"] = {
2107d2
+		"getopt" : ":",
2107d2
+		"longopt" : "serviceaccount",
2107d2
+		"help" : "--serviceaccount=[filename]    Service account json file location e.g. serviceaccount=/somedir/service_account.json",
2107d2
+		"shortdesc" : "Service Account to use for authentication to the google cloud APIs.",
2107d2
+		"required" : "0",
2107d2
+		"order" : 9
2107d2
+	}
2107d2
 
2107d2
 
2107d2
 def main():
2107d2
 	conn = None
2107d2
 
2107d2
-	device_opt = ["port", "no_password", "zone", "project", "stackdriver-logging", "method"]
2107d2
+	device_opt = ["port", "no_password", "zone", "project", "stackdriver-logging",
2107d2
+		"method", "serviceaccount"]
2107d2
 
2107d2
 	atexit.register(atexit_handler)
2107d2
 
2107d2
@@ -226,10 +241,24 @@
2107d2
 
2107d2
 	# Prepare cli
2107d2
 	try:
2107d2
-		credentials = None
2107d2
-		if tuple(googleapiclient.__version__) < tuple("1.6.0"):
2107d2
-			import oauth2client.client
2107d2
-			credentials = oauth2client.client.GoogleCredentials.get_application_default()
2107d2
+		serviceaccount = options.get("--serviceaccount")
2107d2
+		if serviceaccount:
2107d2
+			scope = ['https://www.googleapis.com/auth/cloud-platform']
2107d2
+			logging.debug("using credentials from service account")
2107d2
+			try:
2107d2
+				from google.oauth2.service_account import Credentials as ServiceAccountCredentials
2107d2
+				credentials = ServiceAccountCredentials.from_service_account_file(filename=serviceaccount, scopes=scope)
2107d2
+			except ImportError:
2107d2
+				from oauth2client.service_account import ServiceAccountCredentials
2107d2
+				credentials = ServiceAccountCredentials.from_json_keyfile_name(serviceaccount, scope)
2107d2
+		else:
2107d2
+			try:
2107d2
+				from googleapiclient import _auth
2107d2
+				credentials = _auth.default_credentials();
2107d2
+			except:
2107d2
+				credentials = GoogleCredentials.get_application_default()
2107d2
+			logging.debug("using application default credentials")
2107d2
+
2107d2
 		conn = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
2107d2
 	except Exception as err:
2107d2
 		fail_usage("Failed: Create GCE compute v1 connection: {}".format(str(err)))