Skip to content

Commit ece9757

Browse files
authored
Merge pull request #16 from raccoon-mh/master
feat: add proxy support in GoogleCloudConnector and update pip requir…
2 parents 9f512b1 + 1546db1 commit ece9757

3 files changed

Lines changed: 67 additions & 10 deletions

File tree

pkg/pip_requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
spaceone-api
22
google-api-python-client
33
requests
4-
beautifulsoup4
4+
beautifulsoup4
5+
PySocks # for proxy support

src/cloudforet/plugin/connector/base.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +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
5-
7+
import httplib2
8+
import socks
9+
from google_auth_httplib2 import AuthorizedHttp
610
from spaceone.core.connector import BaseConnector
711

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

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

src/cloudforet/plugin/main.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import time
21
import logging
2+
import os
3+
import time
34

45
from spaceone.inventory.plugin.collector.lib.server import CollectorPluginServer
56

@@ -32,7 +33,13 @@ def collector_collect(params: dict) -> dict:
3233
options = params["options"]
3334
secret_data = params["secret_data"]
3435
schema = params.get("schema")
35-
36+
37+
proxy_env = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy")
38+
if proxy_env:
39+
_LOGGER.debug(
40+
f"** Using proxy in environment variable HTTPS_PROXY/https_proxy: {proxy_env}"
41+
) # src/cloudforet/plugin/connector/base.py _create_http_client
42+
3643
resource_mgrs = ResourceManager.list_managers()
3744
for manager in resource_mgrs:
3845
start_time = time.time()

0 commit comments

Comments
 (0)