Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pymysql lambda summariser for private rds instances #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dblambda/PyMySQL-1.1.1.dist-info/INSTALLER
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pip
19 changes: 19 additions & 0 deletions dblambda/PyMySQL-1.1.1.dist-info/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2010, 2013 PyMySQL contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
135 changes: 135 additions & 0 deletions dblambda/PyMySQL-1.1.1.dist-info/METADATA
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
Metadata-Version: 2.1
Name: PyMySQL
Version: 1.1.1
Summary: Pure Python MySQL Driver
Author-email: Inada Naoki <[email protected]>, Yutaka Matsubara <[email protected]>
License: MIT License
Project-URL: Project, https://github.com/PyMySQL/PyMySQL
Project-URL: Documentation, https://pymysql.readthedocs.io/
Keywords: MySQL
Classifier: Development Status :: 5 - Production/Stable
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Topic :: Database
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: ed25519
Requires-Dist: PyNaCl >=1.4.0 ; extra == 'ed25519'
Provides-Extra: rsa
Requires-Dist: cryptography ; extra == 'rsa'

[![Documentation Status](https://readthedocs.org/projects/pymysql/badge/?version=latest)](https://pymysql.readthedocs.io/)
[![codecov](https://codecov.io/gh/PyMySQL/PyMySQL/branch/main/graph/badge.svg?token=ppEuaNXBW4)](https://codecov.io/gh/PyMySQL/PyMySQL)

# PyMySQL

This package contains a pure-Python MySQL client library, based on [PEP
249](https://www.python.org/dev/peps/pep-0249/).

## Requirements

- Python -- one of the following:
- [CPython](https://www.python.org/) : 3.7 and newer
- [PyPy](https://pypy.org/) : Latest 3.x version
- MySQL Server -- one of the following:
- [MySQL](https://www.mysql.com/) \>= 5.7
- [MariaDB](https://mariadb.org/) \>= 10.4

## Installation

Package is uploaded on [PyPI](https://pypi.org/project/PyMySQL).

You can install it with pip:

$ python3 -m pip install PyMySQL

To use "sha256_password" or "caching_sha2_password" for authenticate,
you need to install additional dependency:

$ python3 -m pip install PyMySQL[rsa]

To use MariaDB's "ed25519" authentication method, you need to install
additional dependency:

$ python3 -m pip install PyMySQL[ed25519]

## Documentation

Documentation is available online: <https://pymysql.readthedocs.io/>

For support, please refer to the
[StackOverflow](https://stackoverflow.com/questions/tagged/pymysql).

## Example

The following examples make use of a simple table

``` sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
AUTO_INCREMENT=1 ;
```

``` python
import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
database='db',
cursorclass=pymysql.cursors.DictCursor)

with connection:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('[email protected]', 'very-secret'))

# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()

with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('[email protected]',))
result = cursor.fetchone()
print(result)
```

This example will print:

``` python
{'password': 'very-secret', 'id': 1}
```

## Resources

- DB-API 2.0: <https://www.python.org/dev/peps/pep-0249/>
- MySQL Reference Manuals: <https://dev.mysql.com/doc/>
- MySQL client/server protocol:
<https://dev.mysql.com/doc/internals/en/client-server-protocol.html>
- "Connector" channel in MySQL Community Slack:
<https://lefred.be/mysql-community-on-slack/>
- PyMySQL mailing list:
<https://groups.google.com/forum/#!forum/pymysql-users>

## License

PyMySQL is released under the MIT License. See LICENSE for more
information.
43 changes: 43 additions & 0 deletions dblambda/PyMySQL-1.1.1.dist-info/RECORD
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
PyMySQL-1.1.1.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
PyMySQL-1.1.1.dist-info/LICENSE,sha256=MUEg3GXwgA9ziksxQAx27hTezR--d86cNUCkIbhup7Y,1070
PyMySQL-1.1.1.dist-info/METADATA,sha256=9rEWPHhKScrQDgtyF9-myblwCpZVxwoGCXLJMDtxWGQ,4404
PyMySQL-1.1.1.dist-info/RECORD,,
PyMySQL-1.1.1.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
PyMySQL-1.1.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
PyMySQL-1.1.1.dist-info/top_level.txt,sha256=IKlV-f4o90sOdnMd6HBvo0l2nqfJOGUzkwZeaEEGuRg,8
pymysql/__init__.py,sha256=tz3GIFRN1ug3ycSCxhFkPZ_rsVE5IHjuVTpIR8rTJRQ,4265
pymysql/__pycache__/__init__.cpython-311.pyc,,
pymysql/__pycache__/_auth.cpython-311.pyc,,
pymysql/__pycache__/charset.cpython-311.pyc,,
pymysql/__pycache__/connections.cpython-311.pyc,,
pymysql/__pycache__/converters.cpython-311.pyc,,
pymysql/__pycache__/cursors.cpython-311.pyc,,
pymysql/__pycache__/err.cpython-311.pyc,,
pymysql/__pycache__/optionfile.cpython-311.pyc,,
pymysql/__pycache__/protocol.cpython-311.pyc,,
pymysql/__pycache__/times.cpython-311.pyc,,
pymysql/_auth.py,sha256=ytTe6T4_dRKkT4x1gwXJYBXeUKI50sR7_IZj_oWtYY0,7417
pymysql/charset.py,sha256=_f1uIga7AaWoeKLXzA-9Xra9jYPqqgDiT78ikqtn5yE,10238
pymysql/connections.py,sha256=Yvd97VqhGr6QciyZy9lsQeoZFjXdpQ1xaGcWiXMel7c,53684
pymysql/constants/CLIENT.py,sha256=SSvMFPZCTVMU1UWa4zOrfhYMDdR2wG2mS0E5GzJhDsg,878
pymysql/constants/COMMAND.py,sha256=TGITAUcNWlq2Gwg2wv5UK2ykdTd4LYTk_EcJJOCpGIc,679
pymysql/constants/CR.py,sha256=Qk35FWRMxRHd6Sa9CCIATMh7jegR3xnLdrdaBCT0dTQ,2320
pymysql/constants/ER.py,sha256=nwqX_r0o4mmN4Cxm7NVRyJOTVov_5Gbl5peGe6oz5fk,12357
pymysql/constants/FIELD_TYPE.py,sha256=ytFzgAnGmb9hvdsBlnK68qdZv_a6jYFIXT6VSAb60z8,370
pymysql/constants/FLAG.py,sha256=Fy-PrCLnUI7fx_o5WypYnUAzWAM0E9d5yL8fFRVKffY,214
pymysql/constants/SERVER_STATUS.py,sha256=m28Iq5JGCFCWLhafE73-iOvw_9gDGqnytW3NkHpbugA,333
pymysql/constants/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
pymysql/constants/__pycache__/CLIENT.cpython-311.pyc,,
pymysql/constants/__pycache__/COMMAND.cpython-311.pyc,,
pymysql/constants/__pycache__/CR.cpython-311.pyc,,
pymysql/constants/__pycache__/ER.cpython-311.pyc,,
pymysql/constants/__pycache__/FIELD_TYPE.cpython-311.pyc,,
pymysql/constants/__pycache__/FLAG.cpython-311.pyc,,
pymysql/constants/__pycache__/SERVER_STATUS.cpython-311.pyc,,
pymysql/constants/__pycache__/__init__.cpython-311.pyc,,
pymysql/converters.py,sha256=8Jl-1K1Nt-ZKAiahBJV4MoSvO1O-PZtu8CfQG9EDftk,9523
pymysql/cursors.py,sha256=a4-JHYP148kx-9qVNRz8vTtlilGlKDbk_QtFlWph5L4,16535
pymysql/err.py,sha256=wLe0af6AmK6z7fq_MnYfgYsc6LnUuMj7EliHPZKquBA,4178
pymysql/optionfile.py,sha256=eQoz6c43yvmHtp5MI9TB2GPRdoggOLemcUWABksfutk,651
pymysql/protocol.py,sha256=aD-PGPRYcwkSI6ZJoJWZVRKn9H_A0f70KfPDu65tq0o,11812
pymysql/times.py,sha256=_qXgDaYwsHntvpIKSKXp1rrYIgtq6Z9pLyLnO2XNoL0,360
Empty file.
5 changes: 5 additions & 0 deletions dblambda/PyMySQL-1.1.1.dist-info/WHEEL
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Wheel-Version: 1.0
Generator: bdist_wheel (0.43.0)
Root-Is-Purelib: true
Tag: py3-none-any

1 change: 1 addition & 0 deletions dblambda/PyMySQL-1.1.1.dist-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymysql
111 changes: 111 additions & 0 deletions dblambda/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# DB probe lambda, rough as ...
# call needs event json containing { "DB_SECRET_ARN": "arn:....", "DB_HOST": "rds.host", "DB_NAME": "db" }

import json
import os
import logging
import boto3
import pymysql

from botocore.exceptions import ClientError

logger = logging.getLogger()

def lambda_handler(event, context):
secret_name=event["DB_SECRET_ARN"]
host_name=event["DB_HOST"]
db_name=event["DB_NAME"]

try:
#Extract secret from DB_SECRET_ARN
session = boto3.session.Session()
client = session.client(
service_name = 'secretsmanager'
)
secret_value_response = client.get_secret_value(
SecretId = secret_name
)

except ClientError as e:
# For a list of exceptions thrown, see
# https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
logger.error(f"Boto3Client Error: {e}")
raise e

if 'SecretString' in secret_value_response:
secret_dict = json.loads(secret_value_response['SecretString'])
username = secret_dict.get("username")
password = secret_dict.get("password")
print (f" connect to db {db_name} on {host_name} as {username}")

try:
# Connect to MySQL database
connection = pymysql.connect(
host = host_name,
database = db_name,
user = username,
password = password,
connect_timeout = 2
)

except Exception as e:
logger.error(f"DB Connect Error: {e}")
raise e

try:
with connection:
print("DB Connect okay ")
with connection.cursor() as cursor:
cursor.execute("SELECT VERSION()")
db_info = cursor.fetchone()
print(f"DB Version: {db_info}")
cursor.execute("SELECT * FROM performance_schema.processlist WHERE HOST != 'localhost' and DB is not NULL ")
processes = cursor.fetchall()
print(f"Processes: {processes}")
cursor.execute("SELECT * FROM performance_schema.global_status WHERE VARIABLE_NAME regexp 'Aborted|bytes|error|que|perf|slow|uptime|threads|temp|table'")
status = cursor.fetchall()
print(f"Status: {status}")
cursor.execute("SHOW ENGINES")
engines = cursor.fetchall()
print(f"Engines: {engines}")
cursor.execute("SHOW SLAVE STATUS")
slave_status = cursor.fetchall()
print(f"Slave Status: {slave_status}")
cursor.execute("SHOW MASTER STATUS")
master_status = cursor.fetchall()
print(f"Master Status: {master_status}")
cursor.execute("SHOW GRANTS")
grants = cursor.fetchall()
print(f"Grants: {grants}")
cursor.execute("SHOW PRIVILEGES")
privileges = cursor.fetchall()
print(f"Privileges: {privileges}")
cursor.execute("SELECT * FROM performance_schema.global_status WHERE VARIABLE_NAME regexp 'Aborted|bytes|error|que|perf|slow|uptime|threads|temp|table'")
functions = cursor.fetchall()
print(f"Functions: {functions}")
cursor.execute("SELECT * from performance_schema.hosts")
hosts = cursor.fetchall()
print(f"Hosts: {hosts}")
cursor.execute("SELECT * from performance_schema.users")
users = cursor.fetchall()
print(f"Users: {users}")
cursor.execute("Select schema_name as 'Database' from information_schema.SCHEMATA where schema_name not in ('mysql','information_schema','performance_schema','sys')")
databases = cursor.fetchall()
db_list = [db[0] for db in databases]
print(f"DB List: {db_list}")
for db in db_list:
print(f"DB: {db}")
cursor.execute(f"SELECT * from information_schema.tables where table_schema = '{db}'")
tables = cursor.fetchall()
table_list = [table[2] for table in tables]
print(f"Table Info: {tables}")
cursor.close()

except Exception as e:
logger.error(f"DB Error: {e}")

return {
'statusCode': 200,
'body': json.dumps("That's all Folks!")
}

Loading