Skip to content

Commit 9d8b165

Browse files
authored
Merge pull request #143 from raccoon-mh/master
Add GCP Proxy Feature
2 parents 70e5254 + e7a4b53 commit 9d8b165

5 files changed

Lines changed: 75 additions & 27 deletions

File tree

pkg/pip_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ MarkupSafe>=2.0.0rc2
44
google-cloud-storage
55
requests
66
beautifulsoup4
7-
grpcio
7+
grpcio
8+
PySocks # for proxy support

src/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616

17-
from setuptools import setup, find_packages
17+
from setuptools import find_packages, setup
1818

1919
with open("VERSION", "r") as f:
2020
VERSION = f.read().strip()
@@ -39,6 +39,7 @@
3939
"google-cloud-storage",
4040
"requests",
4141
"beautifulsoup4",
42+
"PySocks",
4243
],
4344
package_data={
4445
"spaceone": [

src/spaceone/inventory/connector/compute_engine/vm_instance.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import logging
22
import os
3-
import google.oauth2.service_account
4-
import googleapiclient
5-
import googleapiclient.discovery
63

74
from spaceone.inventory.libs.connector import GoogleCloudConnector
85

@@ -34,14 +31,6 @@ def get_connect(self, secret_data):
3431
- ...
3532
"""
3633
self.project_id = secret_data.get("project_id")
37-
credentials = (
38-
google.oauth2.service_account.Credentials.from_service_account_info(
39-
secret_data
40-
)
41-
)
42-
self.client = googleapiclient.discovery.build(
43-
"compute", "v1", credentials=credentials
44-
)
4534

4635
def list_regions(self):
4736
result = self.client.regions().list(project=self.project_id).execute()
@@ -342,7 +331,7 @@ def get_instance_in_group(self, key, value, instance_group, **query):
342331
)
343332
# NoneType error occurs sometimes. To prevent them insert default value.
344333
if response is None:
345-
_LOGGER.debug(f"[get_instance_in_group] response is None")
334+
_LOGGER.debug("[get_instance_in_group] response is None")
346335
response = {"items": []}
347336
else:
348337
_LOGGER.debug(f"[get_instance_in_group] response => {response}")

src/spaceone/inventory/libs/connector.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import logging
2+
import os
3+
14
import google.oauth2.service_account
25
import googleapiclient
36
import googleapiclient.discovery
4-
import logging
7+
import httplib2
8+
import socks
9+
from google_auth_httplib2 import AuthorizedHttp
510

611
from spaceone.core.connector import BaseConnector
712

@@ -35,9 +40,26 @@ def __init__(self, *args, **kwargs):
3540
secret_data
3641
)
3742
)
38-
self.client = googleapiclient.discovery.build(
39-
self.google_client_service, self.version, credentials=self.credentials
40-
)
43+
proxy_http = self._create_http_client()
44+
if proxy_http:
45+
self.client = googleapiclient.discovery.build(
46+
self.google_client_service,
47+
self.version,
48+
http=AuthorizedHttp(
49+
self.credentials.with_scopes(
50+
[
51+
"https://www.googleapis.com/auth/cloud-platform"
52+
] # FOR PROXY SCOPE SUPPORT
53+
),
54+
http=proxy_http,
55+
),
56+
)
57+
else:
58+
self.client = googleapiclient.discovery.build(
59+
self.google_client_service,
60+
self.version,
61+
credentials=self.credentials,
62+
)
4163

4264
def verify(self, **kwargs):
4365
if self.client is None:
@@ -55,3 +77,33 @@ def list_zones(self, **query):
5577
query = self.generate_query(**query)
5678
result = self.client.zones().list(**query).execute()
5779
return result.get("items", [])
80+
81+
def _create_http_client(self):
82+
https_proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
83+
84+
if https_proxy:
85+
# _LOGGER.info(
86+
# f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {https_proxy}"
87+
# ) # TOO MANY LOGGING
88+
try:
89+
proxy_url = https_proxy.replace("http://", "").replace("https://", "")
90+
if ":" in proxy_url:
91+
proxy_host, proxy_port = proxy_url.split(":", 1)
92+
proxy_port = int(proxy_port)
93+
94+
proxy_info = httplib2.ProxyInfo(
95+
proxy_host=proxy_host,
96+
proxy_port=proxy_port,
97+
proxy_type=socks.PROXY_TYPE_HTTP,
98+
)
99+
100+
return httplib2.Http(
101+
proxy_info=proxy_info, disable_ssl_certificate_validation=True
102+
)
103+
except Exception as e:
104+
_LOGGER.warning(
105+
f"Failed to configure proxy. Using direct connection.: {e}. "
106+
)
107+
return None
108+
else:
109+
return None

src/spaceone/inventory/service/collector_service.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import concurrent.futures
2+
import json
3+
import logging
14
import os
25
import time
3-
import logging
4-
import json
5-
import concurrent.futures
6-
from spaceone.inventory.connector.resource_manager.project import ProjectConnector
7-
from spaceone.inventory.libs.manager import GoogleCloudManager
6+
87
from spaceone.core import utils
98
from spaceone.core.service import *
9+
from spaceone.inventory.conf.cloud_service_conf import *
10+
from spaceone.inventory.connector.resource_manager.project import ProjectConnector
11+
from spaceone.inventory.libs.manager import GoogleCloudManager
1012
from spaceone.inventory.libs.schema.cloud_service import (
11-
ErrorResourceResponse,
1213
CloudServiceResponse,
14+
ErrorResourceResponse,
1315
)
14-
from spaceone.inventory.conf.cloud_service_conf import *
1516

1617
_LOGGER = logging.getLogger(__name__)
1718

@@ -97,7 +98,12 @@ def collect(self, params):
9798

9899
start_time = time.time()
99100

100-
_LOGGER.debug(f"EXECUTOR START: Google Cloud Service")
101+
_LOGGER.debug("EXECUTOR START: Google Cloud Service")
102+
proxy_env = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
103+
if proxy_env:
104+
_LOGGER.debug(
105+
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_env}"
106+
) # src/spaceone/inventory/libs/connector.py _create_http_client
101107
# Get target manager to collect
102108
try:
103109
self.execute_managers = self._get_target_execute_manager(
@@ -214,7 +220,6 @@ def make_namespace_or_metric_response(
214220
namespace=None,
215221
resource_type: str = "inventory.Metric",
216222
) -> dict:
217-
218223
response = {
219224
"state": "SUCCESS",
220225
"resource_type": resource_type,

0 commit comments

Comments
 (0)