Skip to content

Commit

Permalink
PAPP-34754 | ci-cd tools are now 1.22 | update dependencies in requir…
Browse files Browse the repository at this point in the history
…ements
  • Loading branch information
kczernik-splunk committed Sep 27, 2024
1 parent 088986f commit 1023ee9
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/phantomcyber/dev-cicd-tools
rev: v1.16
rev: v1.22
hooks:
- id: org-hook
- id: package-app-dependencies
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,4 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
9 changes: 2 additions & 7 deletions microsoftazuresql.json
Original file line number Diff line number Diff line change
Expand Up @@ -482,18 +482,13 @@
}
],
"pip_dependencies": {
"wheel": [
{
"module": "pymssql",
"input_file": "wheels/py36/pymssql-2.2.5-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl"
}
]
"wheel": []
},
"pip39_dependencies": {
"wheel": [
{
"module": "pymssql",
"input_file": "wheels/py39/pymssql-2.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl"
"input_file": "wheels/py39/pymssql-2.3.1-cp39-cp39-manylinux_2_28_x86_64.whl"
}
]
}
Expand Down
121 changes: 51 additions & 70 deletions microsoftazuresql_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _dump_error_log(self, error, message="Exception occurred."):
self.error_print(message, dump_object=error)

def _get_error_message_from_exception(self, e):
""" This method is used to get appropriate error message from the exception.
"""This method is used to get appropriate error message from the exception.
:param e: Exception object
:return: error message
"""
Expand All @@ -87,7 +87,7 @@ def _get_error_message_from_exception(self, e):
return error_text

def _bytes_to_date(self, binary_str):
unpacked = struct.unpack('QIhH', binary_str)
unpacked = struct.unpack("QIhH", binary_str)
date_fields = []
for tup in unpacked:
date_fields.append(tup)
Expand All @@ -96,7 +96,7 @@ def _bytes_to_date(self, binary_str):
microseconds = date_fields[0] / 10 if date_fields[0] else 0

timezone = date_fields[2]
time_zone = tzoffset('ANY', timezone * 60)
time_zone = tzoffset("ANY", timezone * 60)
date = datetime.datetime(*[1900, 1, 1, 0, 0, 0], tzinfo=time_zone)
time_date = datetime.timedelta(days=days, minutes=date_fields[2], microseconds=microseconds)
date += time_date
Expand All @@ -116,30 +116,26 @@ def _get_query_results(self, action_result):
date_from_byte = self._bytes_to_date(column)
column = str(date_from_byte)
except:
column = '0x{0}'.format(binascii.hexlify(column).decode().upper())
column = "0x{0}".format(binascii.hexlify(column).decode().upper())
column_dict[columns[index][0]] = column
results.append(column_dict)
else:
return RetVal(phantom.APP_SUCCESS)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return RetVal(action_result.set_status(
phantom.APP_ERROR,
"Unable to retrieve results from query, Error: {}".format(error_message),
None
))
return RetVal(
action_result.set_status(phantom.APP_ERROR, "Unable to retrieve results from query, Error: {}".format(error_message), None)
)
return RetVal(phantom.APP_SUCCESS, results)

def _check_for_valid_schema(self, action_result, schema):
format_vars = (schema, )
format_vars = (schema,)
query = "SELECT * FROM sys.schemas WHERE name = %s;"
try:
self._cursor.execute(query, format_vars)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(
phantom.APP_ERROR, "Error searching for schema, Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "Error searching for schema, Error: {}".format(error_message))

results = self._cursor.fetchall()
if len(results) == 0:
Expand All @@ -156,9 +152,7 @@ def _check_for_valid_table(self, action_result, table, check_single=False):
self._cursor.execute(query, format_vars)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(
phantom.APP_ERROR, "Error searching for table, Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "Error searching for table, Error: {}".format(error_message))

results = self._cursor.fetchall()
if len(results) == 0:
Expand All @@ -178,9 +172,7 @@ def _handle_test_connectivity(self, param):
except Exception as e:
error_message = self._get_error_message_from_exception(e)
self.save_progress("Test Connectivity Failed")
return action_result.set_status(
phantom.APP_ERROR, "Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "Error: {}".format(error_message))

for row in self._cursor:
self.save_progress("{}".format(row[0]))
Expand All @@ -190,7 +182,7 @@ def _handle_test_connectivity(self, param):

def _handle_list_tables(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
table_schema = param.get('table_schema')
table_schema = param.get("table_schema")
dbname = self.database

query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = %s AND TABLE_CATALOG = %s"
Expand All @@ -199,16 +191,14 @@ def _handle_list_tables(self, param):
if phantom.is_fail(self._check_for_valid_schema(action_result, table_schema)):
return phantom.APP_ERROR
query += " AND TABLE_SCHEMA = %s"
format_vars = ('BASE TABLE', dbname, table_schema)
format_vars = ("BASE TABLE", dbname, table_schema)
else:
format_vars = ('BASE TABLE', dbname)
format_vars = ("BASE TABLE", dbname)
try:
self._cursor.execute(query, format_vars)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(
phantom.APP_ERROR, "Error listing tables, Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "Error listing tables, Error: {}".format(error_message))

ret_val, results = self._get_query_results(action_result)
if phantom.is_fail(ret_val):
Expand All @@ -221,14 +211,14 @@ def _handle_list_tables(self, param):
action_result.add_data(row)

summary = action_result.update_summary({})
summary['num_tables'] = len(results)
summary["num_tables"] = len(results)

return action_result.set_status(phantom.APP_SUCCESS)

def _handle_list_columns(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
table_name = param.get('table_name')
table_schema = param.get('table_schema')
table_name = param.get("table_name")
table_schema = param.get("table_schema")
dbname = self.database

if phantom.is_fail(self._check_for_valid_table(action_result, table_name, not bool(table_schema))):
Expand All @@ -248,9 +238,7 @@ def _handle_list_columns(self, param):
self._cursor.execute(query, format_vars)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(
phantom.APP_ERROR, "Error listing columns, Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "Error listing columns, Error: {}".format(error_message))

ret_val, results = self._get_query_results(action_result)
if phantom.is_fail(ret_val):
Expand All @@ -263,27 +251,25 @@ def _handle_list_columns(self, param):
action_result.add_data(row)

summary = action_result.update_summary({})
summary['num_columns'] = len(results)
summary["num_columns"] = len(results)

return action_result.set_status(phantom.APP_SUCCESS)

def _get_format_vars(self, param):
format_vars = param.get('format_vars', [])
format_vars = param.get("format_vars", [])
if format_vars:
format_vars = tuple(next(csv.reader([format_vars], quotechar='"', skipinitialspace=True, escapechar='\\')))
format_vars = tuple(next(csv.reader([format_vars], quotechar='"', skipinitialspace=True, escapechar="\\")))
return format_vars

def _handle_run_query(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
query = param['query']
query = param["query"]
format_vars = self._get_format_vars(param)
try:
self._cursor.execute(query, format_vars)
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(
phantom.APP_ERROR, "Error running query, Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "Error running query, Error: {}".format(error_message))

ret_val, results = self._get_query_results(action_result)
if phantom.is_fail(ret_val):
Expand All @@ -296,19 +282,15 @@ def _handle_run_query(self, param):
results = [{"Status": MSAZURESQL_SUCCESS_MESSAGE}]
summary["num_rows"] = 0

if not param.get('no_commit', False):
if not param.get("no_commit", False):
try:
self._connection.commit()
except Exception as e:
error_message = self._get_error_message_from_exception(e)
return action_result.set_status(
phantom.APP_ERROR, "unable to commit changes, Error: {}".format(error_message)
)
return action_result.set_status(phantom.APP_ERROR, "unable to commit changes, Error: {}".format(error_message))

for row in results:
action_result.add_data(
{key: str(value) for key, value in row.items()}
)
action_result.add_data({key: str(value) for key, value in row.items()})

return action_result.set_status(phantom.APP_SUCCESS)

Expand All @@ -321,30 +303,28 @@ def handle_action(self, param):

self.debug_print("action_id", self.get_action_identifier())

if action_id == 'test_connectivity':
if action_id == "test_connectivity":
ret_val = self._handle_test_connectivity(param)

if action_id == 'list_tables':
if action_id == "list_tables":
ret_val = self._handle_list_tables(param)

if action_id == 'list_columns':
if action_id == "list_columns":
ret_val = self._handle_list_columns(param)

if action_id == 'run_query':
if action_id == "run_query":
ret_val = self._handle_run_query(param)

return ret_val

def _connect_sql(self):
config = self.get_config()
username = config['username']
password = config['password']
host = config['host']
database = config['database']
username = config["username"]
password = config["password"]
host = config["host"]
database = config["database"]
try:
self._connection = pymssql.connect(
server=host, user=username, password=password, database=database, port=MSAZURESQL_PORT
)
self._connection = pymssql.connect(server=host, user=username, password=password, database=database, port=MSAZURESQL_PORT)
self._cursor = self._connection.cursor()
except Exception as e:
return self._initialize_error("Error occurred while authenticating with database", e)
Expand All @@ -369,7 +349,7 @@ def finalize(self):
return phantom.APP_SUCCESS


if __name__ == '__main__':
if __name__ == "__main__":

import argparse

Expand All @@ -379,10 +359,10 @@ def finalize(self):

argparser = argparse.ArgumentParser()

argparser.add_argument('input_test_json', help='Input Test JSON file')
argparser.add_argument('-u', '--username', help='username', required=False)
argparser.add_argument('-p', '--password', help='password', required=False)
argparser.add_argument('-v', '--verify', action='store_true', help='verify', required=False, default=False)
argparser.add_argument("input_test_json", help="Input Test JSON file")
argparser.add_argument("-u", "--username", help="username", required=False)
argparser.add_argument("-p", "--password", help="password", required=False)
argparser.add_argument("-v", "--verify", action="store_true", help="verify", required=False, default=False)

args = argparser.parse_args()
session_id = None
Expand All @@ -395,27 +375,28 @@ def finalize(self):

# User specified a username but not a password, so ask
import getpass

password = getpass.getpass("Password: ")

if username and password:
login_url = BaseConnector._get_phantom_base_url() + "login"
try:
print("Accessing the Login page")
r = requests.get(login_url, verify=verify, timeout=DEFAULT_TIMEOUT)
csrftoken = r.cookies['csrftoken']
csrftoken = r.cookies["csrftoken"]

data = dict()
data['username'] = username
data['password'] = password
data['csrfmiddlewaretoken'] = csrftoken
data["username"] = username
data["password"] = password
data["csrfmiddlewaretoken"] = csrftoken

headers = dict()
headers['Cookie'] = 'csrftoken=' + csrftoken
headers['Referer'] = login_url
headers["Cookie"] = "csrftoken=" + csrftoken
headers["Referer"] = login_url

print("Logging into Platform to get the session id")
r2 = requests.post(login_url, verify=verify, data=data, headers=headers, timeout=DEFAULT_TIMEOUT)
session_id = r2.cookies['sessionid']
session_id = r2.cookies["sessionid"]
except Exception as e:
print("Unable to get session id from the platfrom. Error: {}".format(e))
sys.exit(1)
Expand All @@ -429,8 +410,8 @@ def finalize(self):
connector.print_progress_message = True

if session_id is not None:
in_json['user_session_token'] = session_id
connector._set_csrf_info(csrftoken, headers['Referer'])
in_json["user_session_token"] = session_id
connector._set_csrf_info(csrftoken, headers["Referer"])

ret_val = connector._handle_action(json.dumps(in_json), None)
print(json.dumps(json.loads(ret_val), indent=4))
Expand Down
10 changes: 5 additions & 5 deletions microsoftazuresql_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@
# and limitations under the License.
def display_query_results(provides, all_results, context):

context['results'] = results = []
context["results"] = results = []

adjusted_names = {}

for summary, action_results in all_results:
for result in action_results:
headers_set = set()
table = dict()
table['data'] = rows = []
table["data"] = rows = []
data = result.get_data()
if data:
headers_set.update(data[0].keys())
headers = sorted(headers_set)
table['headers'] = headers
table["headers"] = headers
for item in data:
row = []
for h in headers:
row.append({ 'value': item.get(adjusted_names.get(h, h)) })
row.append({"value": item.get(adjusted_names.get(h, h))})
rows.append(row)

results.append(table)

return 'microsoftazuresql_run_query.html'
return "microsoftazuresql_run_query.html"
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tool.black]
line-length = 145
target-version = ['py39']
verbose = true

[tool.isort]
line_length = 145
1 change: 1 addition & 0 deletions release_notes/unreleased.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
**Unreleased**
* PAPP-34725 update dependencies in requirements
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pymssql==2.2.5
pymssql==2.3.1
Loading

0 comments on commit 1023ee9

Please sign in to comment.