You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2022/01/28 22:44:49 UTC
[libcloud] 07/08: Add a test case for scenario where the PEM key is invalid.
This is an automated email from the ASF dual-hosted git repository.
tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git
commit 1e2a039a9459fe24ae701b924dfa40ccff98ff9c
Author: Tomaz Muraus <to...@tomaz.me>
AuthorDate: Fri Jan 28 23:41:42 2022 +0100
Add a test case for scenario where the PEM key is invalid.
---
libcloud/test/common/test_google.py | 82 +++++++++++++++++++++++++++----------
1 file changed, 60 insertions(+), 22 deletions(-)
diff --git a/libcloud/test/common/test_google.py b/libcloud/test/common/test_google.py
index d10fbcb..b613067 100644
--- a/libcloud/test/common/test_google.py
+++ b/libcloud/test/common/test_google.py
@@ -52,6 +52,7 @@ except ImportError:
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
PEM_KEY = os.path.join(SCRIPT_PATH, "fixtures", "google", "pkey.pem")
JSON_KEY = os.path.join(SCRIPT_PATH, "fixtures", "google", "pkey.json")
+JSON_KEY_INVALID = os.path.join(SCRIPT_PATH, "fixtures", "google", "pkey_invalid.json")
with open(JSON_KEY, "r") as f:
KEY_STR = json.loads(f.read())["private_key"]
@@ -81,28 +82,40 @@ GCS_S3_PARAMS_61 = (
# 40 base64 chars
"0102030405060708091011121314151617181920",
)
-PEM_KEY_FILE = os.path.join(SCRIPT_PATH, 'fixtures', 'google', 'pkey.pem')
-JSON_KEY_FILE = os.path.join(SCRIPT_PATH, 'fixtures', 'google', 'pkey.json')
-with open(JSON_KEY_FILE, 'r') as f:
- PEM_KEY_STR = json.loads(f.read())['private_key']
-with open(JSON_KEY_FILE, 'r') as f:
+PEM_KEY_FILE = os.path.join(SCRIPT_PATH, "fixtures", "google", "pkey.pem")
+PEM_KEY_FILE_INVALID = os.path.join(
+ SCRIPT_PATH, "fixtures", "google", "pkey_invalid.pem"
+)
+
+JSON_KEY_FILE = os.path.join(SCRIPT_PATH, "fixtures", "google", "pkey.json")
+
+with open(JSON_KEY_FILE, "r") as f:
+ PEM_KEY_STR = json.loads(f.read())["private_key"]
+
+with open(JSON_KEY_FILE, "r") as f:
JSON_KEY_STR = f.read()
JSON_KEY = json.loads(JSON_KEY_STR)
-GCE_USERID_EMAIL = 'email@developer.gserviceaccount.com'
-GCE_PARAMS = (GCE_USERID_EMAIL, 'key')
+GCE_USERID_EMAIL = "email@developer.gserviceaccount.com"
+GCE_PARAMS = (GCE_USERID_EMAIL, "key")
GCE_PARAMS_PEM_KEY_FILE = (GCE_USERID_EMAIL, PEM_KEY_FILE)
+GCE_PARAMS_PEM_KEY_FILE_INVALID = (GCE_USERID_EMAIL, PEM_KEY_FILE_INVALID)
GCE_PARAMS_PEM_KEY = (GCE_USERID_EMAIL, PEM_KEY_STR)
GCE_PARAMS_JSON_KEY_FILE = (GCE_USERID_EMAIL, JSON_KEY_FILE)
GCE_PARAMS_JSON_KEY = (GCE_USERID_EMAIL, JSON_KEY)
+GCE_PARAMS_JSON_KEY_INVALID = (GCE_USERID_EMAIL, JSON_KEY_INVALID)
GCE_PARAMS_JSON_KEY_STR = (GCE_USERID_EMAIL, JSON_KEY_STR)
-GCE_PARAMS_IA = ('client_id', 'client_secret')
-GCE_PARAMS_GCE = ('foo', 'bar')
-GCS_S3_PARAMS_20 = ('GOOG0123456789ABCXYZ', # GOOG + 16 alphanumeric chars
- '0102030405060708091011121314151617181920') # 40 base64 chars
-GCS_S3_PARAMS_24 = ('GOOGDF5OVRRGU4APFNSTVCXI', # GOOG + 20 alphanumeric chars
- '0102030405060708091011121314151617181920') # 40 base64 chars
+GCE_PARAMS_IA = ("client_id", "client_secret")
+GCE_PARAMS_GCE = ("foo", "bar")
+GCS_S3_PARAMS_20 = (
+ "GOOG0123456789ABCXYZ", # GOOG + 16 alphanumeric chars
+ "0102030405060708091011121314151617181920",
+) # 40 base64 chars
+GCS_S3_PARAMS_24 = (
+ "GOOGDF5OVRRGU4APFNSTVCXI", # GOOG + 20 alphanumeric chars
+ "0102030405060708091011121314151617181920",
+) # 40 base64 chars
STUB_UTCNOW = _utcnow()
@@ -357,12 +370,14 @@ class GoogleOAuth2CredentialTest(GoogleTestCase):
if SHA256:
kwargs["auth_type"] = GoogleAuthType.SA
cred1 = GoogleOAuth2Credential(*GCE_PARAMS_PEM_KEY_FILE, **kwargs)
- self.assertTrue(isinstance(cred1.oauth2_conn,
- GoogleServiceAcctAuthConnection))
+ self.assertTrue(
+ isinstance(cred1.oauth2_conn, GoogleServiceAcctAuthConnection)
+ )
cred1 = GoogleOAuth2Credential(*GCE_PARAMS_JSON_KEY_FILE, **kwargs)
- self.assertTrue(isinstance(cred1.oauth2_conn,
- GoogleServiceAcctAuthConnection))
+ self.assertTrue(
+ isinstance(cred1.oauth2_conn, GoogleServiceAcctAuthConnection)
+ )
cred1 = GoogleOAuth2Credential(*GCE_PARAMS_PEM_KEY, **kwargs)
self.assertTrue(
@@ -381,13 +396,36 @@ class GoogleOAuth2CredentialTest(GoogleTestCase):
kwargs["auth_type"] = GoogleAuthType.SA
cred1 = GoogleOAuth2Credential(*GCE_PARAMS_JSON_KEY_STR, **kwargs)
- self.assertTrue(isinstance(cred1.oauth2_conn,
- GoogleServiceAcctAuthConnection))
+ self.assertTrue(
+ isinstance(cred1.oauth2_conn, GoogleServiceAcctAuthConnection)
+ )
- self.assertRaises(GoogleAuthError, GoogleOAuth2Credential,
- *GCE_PARAMS, **kwargs)
+ self.assertRaises(
+ GoogleAuthError, GoogleOAuth2Credential, *GCE_PARAMS, **kwargs
+ )
+
+ # Invalid pem key
+ kwargs["auth_type"] = GoogleAuthType.SA
+ expected_msg = "Unable to decode provided PEM key:"
+ self.assertRaisesRegex(
+ GoogleAuthError,
+ expected_msg,
+ GoogleOAuth2Credential,
+ *GCE_PARAMS_PEM_KEY_FILE_INVALID,
+ **kwargs,
+ )
+
+ kwargs["auth_type"] = GoogleAuthType.SA
+ expected_msg = "Unable to decode provided PEM key:"
+ self.assertRaisesRegex(
+ GoogleAuthError,
+ expected_msg,
+ GoogleOAuth2Credential,
+ *GCE_PARAMS_JSON_KEY_INVALID,
+ **kwargs,
+ )
- kwargs['auth_type'] = GoogleAuthType.IA
+ kwargs["auth_type"] = GoogleAuthType.IA
cred2 = GoogleOAuth2Credential(*GCE_PARAMS_IA, **kwargs)
self.assertTrue(isinstance(cred2.oauth2_conn, GoogleInstalledAppAuthConnection))