Skip to content

Commit

Permalink
An updated version of utils/admin script with more options
Browse files Browse the repository at this point in the history
  • Loading branch information
alanvitor committed Oct 3, 2023
1 parent 420d295 commit 2e58d3e
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 63 deletions.
1 change: 1 addition & 0 deletions utils/admin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VinylDNSProto_pb2.py
8 changes: 4 additions & 4 deletions utils/admin/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ RUN mkdir -p /vinyldns/python && \
FROM vinyldns/build:base-build
ARG DOCKERFILE_PATH
WORKDIR /app
RUN pip install mysql-connector-python==8.0.27
RUN pip install mysql-connector-python==8.0.27 protobuf==3.20.3

ENV DB_USER="root" DB_PASS="pass" DB_HOST="vinyldns-integration" DB_NAME="vinyldns" DB_PORT="19002"

COPY --from=pbcompile /vinyldns/python .
COPY ${DOCKERFILE_PATH}/update-support-user.py .
RUN chmod 755 update-support-user.py
COPY ${DOCKERFILE_PATH}/update-user.py .
RUN chmod 755 update-user.py

ENTRYPOINT ["./update-support-user.py"]
ENTRYPOINT ["./update-user.py"]
8 changes: 7 additions & 1 deletion utils/admin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ endif

.ONESHELL:

.PHONY: all build run
.PHONY: all build run proto

all: build run

Expand All @@ -41,3 +41,9 @@ run:
docker network create --driver bridge vinyldns_net &> /dev/null || true
USE_TTY="" && test -t 1 && USE_TTY="-t"
docker run -i $${USE_TTY} --network vinyldns_net --rm $(DOCKER_PARAMS) $(IMAGE_NAME) $(WITH_ARGS)

proto:
@set -euo pipefail
cp ../../modules/core/src/main/protobuf/VinylDNSProto.proto .
protoc VinylDNSProto.proto --python_out=.
rm VinylDNSProto.proto
36 changes: 36 additions & 0 deletions utils/admin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## Running update-user.py locally

#### You will need protoc installed:

# On Ubuntu:
sudo apt install protobuf-compiler

# On macOS:
brew install protobuf

for any other OS, see [github protobuf releases page](https://github.com/protocolbuffers/protobuf/releases).

#### Compile the proto file:

make proto

#### Set env vars:

export DB_USER=
export DB_PASS=
export DB_HOST=
export DB_NAME=
export DB_PORT=

#### Create a virtualenv:

python3 -m venv .venv
source .venv/bin/activate

#### Install dependencies:

pip install -r requirements.txt

#### Run:

./update-user.py --help
57 changes: 0 additions & 57 deletions utils/admin/update-support-user.py

This file was deleted.

101 changes: 101 additions & 0 deletions utils/admin/update-user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/usr/bin/env python3
import os
import sys
import base64
import argparse

import VinylDNSProto_pb2
import mysql.connector

db_user = os.getenv('DB_USER')
db_pass = os.getenv('DB_PASS')
db_host = os.getenv('DB_HOST')
db_name = os.getenv('DB_NAME')
db_port = os.getenv('DB_PORT')

conn = mysql.connector.connect(
user=db_user, password=db_pass, host=db_host, database=db_name,port=db_port)
cursor = conn.cursor(dictionary=True)


def check_user(username):
try:
cursor.execute("SELECT data FROM user where user_name = %(u)s", {"u": username})
except Exception as err:
print(f"Error checking user {username}: {err}")
return None

user = VinylDNSProto_pb2.User()

for row in cursor:
user_raw = row['data']

if isinstance(user_raw, str):
print(f"Type of user data is string {user_raw}")
user.ParseFromString(base64.encodebytes(user_raw.encode()))
else:
print("Type of user data is bytes")
user.ParseFromString(user_raw)

return user


def run(args):
username = args.username
user = check_user(username)

if user is None:
return False, f"Check user error."

if user.userName is None or len(user.userName) == 0:
return False, f"User {username} not found."

updated = (args.superuser or args.support or args.test or args.locked)

if not updated and not args.info:
return True, f"User {username} found."

if args.superuser:
user.isSuper = True if args.superuser == "yes" else False

if args.support:
user.isSupport = True if args.support == "yes" else False

if args.test:
user.isTest = True if args.test == "yes" else False

if args.locked:
user.lockStatus = "Locked" if args.locked == "yes" else "Unlocked"

if args.info:
return True, f"{user}\r"

try:
cursor.execute("UPDATE user SET data = %(d)s WHERE user_name = %(u)s",
{"d": user.SerializeToString(), "u": username})
conn.commit()
except Exception as err:
return False, f"Error updating user {username}: {err}"
finally:
cursor.close()
conn.close()

if updated:
return True, f"User {username} updated!"

return True, None


if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Update a VinylDNS User.')
parser.add_argument("username", help="The VinylDNS username")
parser.add_argument("--superuser", help="Changes user isSuper flag", choices=["yes", "no"])
parser.add_argument("--support", help="Changes user isSupport flag", choices=["yes", "no"])
parser.add_argument("--test", help="Changes user isTest flag", choices=["yes", "no"])
parser.add_argument("--locked", help="Changes user lockStatus flag", choices=["yes", "no"])
parser.add_argument("--info", help="Show user info", action="store_true")

args = parser.parse_args()
ok, msg = run(args)
print(msg)
sys.exit(0 if ok else 1)
8 changes: 7 additions & 1 deletion utils/update-support-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ while [ "$1" != "" ]; do
done

VINYL_USER="$1"
MAKE_SUPPORT="$2"
MAKE_SUPPORT=""

if [ $2 = "True" ]; then
MAKE_SUPPORT="--support=yes"
elif [ $2 = "False" ]; then
MAKE_SUPPORT="--support=no"
fi

ERROR=
if [ -z "$DB_USER" ]; then
Expand Down

0 comments on commit 2e58d3e

Please sign in to comment.