Skip to content

Commit 2679d72

Browse files
committed
Move GcpSecretLookup to plugin_utils because may not be ansible module on the remote host
1 parent 2b74f48 commit 2679d72

4 files changed

Lines changed: 70 additions & 58 deletions

File tree

plugins/lookup/gcp_secret_access.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
from ansible.errors import AnsibleError
7171
from ansible.plugins.lookup import LookupBase
72-
from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import GcpSecretLookup
72+
from ansible_collections.google.cloud.plugins.plugin_utils.gcp_utils import GcpSecretLookup
7373

7474
try:
7575
from google.cloud import secretmanager

plugins/lookup/gcp_secret_resource_id.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
from ansible.errors import AnsibleError
5959
from ansible.plugins.lookup import LookupBase
60-
from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import GcpSecretLookup
60+
from ansible_collections.google.cloud.plugins.plugin_utils.gcp_utils import GcpSecretLookup
6161

6262
try:
6363
from google.cloud import secretmanager

plugins/module_utils/gcp_utils.py

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import ast
99
import os
1010
import json
11-
import re
1211

1312
try:
1413
import requests
@@ -25,7 +24,6 @@
2524
except ImportError:
2625
HAS_GOOGLE_LIBRARIES = False
2726

28-
from ansible.errors import AnsibleError
2927
from ansible.module_utils.basic import AnsibleModule, env_fallback
3028
from ansible.module_utils.six import string_types
3129
from ansible.module_utils._text import to_text, to_native
@@ -449,57 +447,3 @@ def _convert_value(self, value):
449447
new_dict[key] = self._convert_value(value[key])
450448
return new_dict
451449
return to_text(value)
452-
453-
454-
# Handles all authentication and options for GCP Secrets Manager API calls in Lookup plugins.
455-
class GcpSecretLookup():
456-
def __init__(self):
457-
if not HAS_GOOGLE_LIBRARIES:
458-
raise AnsibleError("Please install the google-auth library")
459-
460-
self.plugin_name = ''
461-
self.secret_id = None
462-
self.version_id = None
463-
self.project_id = None
464-
self.service_account_file = None
465-
self.scope = ["https://www.googleapis.com/auth/cloud-platform"]
466-
467-
def set_plugin_name(self, name):
468-
self.plugin_name = name
469-
470-
def client(self, secretmanager):
471-
if self.service_account_file is not None:
472-
path = os.path.realpath(os.path.expanduser(self.service_account_file))
473-
credentials = service_account.Credentials.from_service_account_file(path).with_scopes(self.scope)
474-
return secretmanager.SecretManagerServiceClient(credentials=credentials)
475-
476-
return secretmanager.SecretManagerServiceClient()
477-
478-
def process_options(self, terms, variables=None, **kwargs):
479-
self.secret_id = kwargs.get('secret')
480-
self.version_id = kwargs.get('version', 'latest')
481-
self.project_id = kwargs.get('project', os.getenv('GCP_PROJECT'))
482-
self.service_account_file = kwargs.get('service_account_file', os.getenv('GCP_SERVICE_ACCOUNT_FILE'))
483-
484-
if len(terms) > 1:
485-
raise AnsibleError("{0} lookup plugin can have only one secret name or resource id".format(self.plugin_name))
486-
487-
if self.secret_id is None and len(terms) == 1:
488-
self.secret_id = terms[0]
489-
490-
regex = r'^projects/([^/]+)/secrets/([^/]+)/versions/(.+)$'
491-
match = re.match(regex, self.secret_id)
492-
if match:
493-
self.name = self.secret_id
494-
self.project_id = match.group(1)
495-
self.secret_id = match.group(2)
496-
self.version_id = match.group(3)
497-
return
498-
499-
if self.project_id is None:
500-
raise AnsibleError("{0} lookup plugin required option: project or resource id".format(self.plugin_name))
501-
502-
if self.secret_id is None:
503-
raise AnsibleError("{0} lookup plugin required option: secret or resource id".format(self.plugin_name))
504-
505-
self.name = f"projects/{self.project_id}/secrets/{self.secret_id}/versions/{self.version_id}"

plugins/plugin_utils/gcp_utils.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from __future__ import (absolute_import, division, print_function)
2+
3+
__metaclass__ = type
4+
5+
import os
6+
import re
7+
8+
try:
9+
from google.oauth2 import service_account
10+
HAS_GOOGLE_LIBRARIES = True
11+
except ImportError:
12+
HAS_GOOGLE_LIBRARIES = False
13+
14+
from ansible.errors import AnsibleError
15+
16+
17+
# Handles all authentication and options for GCP Secrets Manager API calls in Lookup plugins.
18+
class GcpSecretLookup():
19+
def __init__(self):
20+
if not HAS_GOOGLE_LIBRARIES:
21+
raise AnsibleError("Please install the google-auth library")
22+
23+
self.plugin_name = ''
24+
self.secret_id = None
25+
self.version_id = None
26+
self.project_id = None
27+
self.service_account_file = None
28+
self.scope = ["https://www.googleapis.com/auth/cloud-platform"]
29+
30+
def set_plugin_name(self, name):
31+
self.plugin_name = name
32+
33+
def client(self, secretmanager):
34+
if self.service_account_file is not None:
35+
path = os.path.realpath(os.path.expanduser(self.service_account_file))
36+
credentials = service_account.Credentials.from_service_account_file(path).with_scopes(self.scope)
37+
return secretmanager.SecretManagerServiceClient(credentials=credentials)
38+
39+
return secretmanager.SecretManagerServiceClient()
40+
41+
def process_options(self, terms, variables=None, **kwargs):
42+
self.secret_id = kwargs.get('secret')
43+
self.version_id = kwargs.get('version', 'latest')
44+
self.project_id = kwargs.get('project', os.getenv('GCP_PROJECT'))
45+
self.service_account_file = kwargs.get('service_account_file', os.getenv('GOOGLE_APPLICATION_CREDENTIALS'))
46+
47+
if len(terms) > 1:
48+
raise AnsibleError("{0} lookup plugin can have only one secret name or resource id".format(self.plugin_name))
49+
50+
if self.secret_id is None and len(terms) == 1:
51+
self.secret_id = terms[0]
52+
53+
regex = r'^projects/([^/]+)/secrets/([^/]+)/versions/(.+)$'
54+
match = re.match(regex, self.secret_id)
55+
if match:
56+
self.name = self.secret_id
57+
self.project_id = match.group(1)
58+
self.secret_id = match.group(2)
59+
self.version_id = match.group(3)
60+
return
61+
62+
if self.project_id is None:
63+
raise AnsibleError("{0} lookup plugin required option: project or resource id".format(self.plugin_name))
64+
65+
if self.secret_id is None:
66+
raise AnsibleError("{0} lookup plugin required option: secret or resource id".format(self.plugin_name))
67+
68+
self.name = f"projects/{self.project_id}/secrets/{self.secret_id}/versions/{self.version_id}"

0 commit comments

Comments
 (0)