diff --git a/.github/workflows/test_publish_and_release.yml b/.github/workflows/test_publish_and_release.yml index 8867b33..fcd4866 100644 --- a/.github/workflows/test_publish_and_release.yml +++ b/.github/workflows/test_publish_and_release.yml @@ -1,233 +1,230 @@ -name: Run Tests - -on: - # PR merges count as pushes - push: - branches: - - main - pull_request: - workflow_dispatch: - -env: - REPO_NAME: sat-utils - SLACK_UPDATES: true - SLACK_CHANNEL_ID: C05Q5KLUW6Q - -jobs: - run_tests: - name: Run Tests - runs-on: runner-1 - steps: - - name: Get Code - uses: actions/checkout@v3 - with: - ref: ${{ github.head_ref }} - - - name: Get SAT Actions - uses: actions/checkout@v3 - with: - repository: SAT/sat-actions - path: sat-actions - - - name: Setup Python - uses: actions/setup-python@v4 - with: - python-version: "3.10" - - - name: Cache Packages - uses: actions/cache@v3 - with: - path: ~/.local - key: requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/requirements/base/base.txt') }}-${{ hashFiles('**/requirements/dev/dev.txt') }}-${{ hashFiles('.github/workflows/*.yml') }}-1 - restore-keys: | - requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}- - - - name: Setup Virtual Environment - run: | - python -m venv venv - source venv/bin/activate - make setup - - - name: Run Pre-commit - run: | - source venv/bin/activate - pre-commit install - pre-commit run --all - - - name: Run Tests - run: | - source venv/bin/activate - pytest - - - name: Send Failure Message on Slack if Tests Fail - if: ${{ failure() }} - uses: ./sat-actions/slack-updates - with: - channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} - message: "The tests for `${{ env.REPO_NAME }}` have failed." - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} - - - name: Send Success Message on Slack - uses: ./sat-actions/slack-updates - with: - channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} - message: "The tests for `${{ env.REPO_NAME }}` have passed." - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} - - build_and_publish: - name: Build and Publish - # only proceed if test job ran successfully - needs: run_tests - if: ${{ github.event_name == 'push' }} - runs-on: runner-1 - env: - FLIT_USERNAME: ${{ secrets.PYPI_USERNAME }} - FLIT_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - FLIT_INDEX_URL: https://pypi.ehps.ncsu.edu - steps: - - name: Get Code - uses: actions/checkout@v3 - with: - ref: ${{ github.head_ref }} - - - name: Retrieve current version - run: echo "CURRENT_VERSION=v$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)" >> $GITHUB_ENV - - - name: Retrieve previous version - run: echo "PREV_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV - - - name: Check if version has been updated - if: ${{ env.PREV_VERSION >= env.CURRENT_VERSION }} - run: | - echo "Version in pyproject.toml has not been updated" - exit 1 - - - name: Get SAT Actions - uses: actions/checkout@v3 - with: - repository: SAT/sat-actions - path: sat-actions - - - name: Setup Python - uses: actions/setup-python@v4 - with: - python-version: "3.10" - - - name: Cache Packages - uses: actions/cache@v3 - with: - path: ~/.local - key: requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/requirements/base/base.txt') }}-${{ hashFiles('**/requirements/dev/dev.txt') }}-${{ hashFiles('.github/workflows/*.yml') }}-1 - restore-keys: | - requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}- - - - name: Setup Virtual Environment - run: python -m venv venv - - - name: Configure pip - run: | - source venv/bin/activate - make setup - - - name: Build and Publish - run: | - source venv/bin/activate - flit publish - - - name: Send Failure Message on Slack if Build/Publish Fails - if: ${{ failure() }} - uses: ./sat-actions/slack-updates - with: - channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} - message: "The tests for `${{ env.REPO_NAME }}` have passed, but publishing to PyPI failed." - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} - - - name: Send Success Message on Slack - uses: ./sat-actions/slack-updates - with: - channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} - message: "A new version of `${{ env.REPO_NAME }}` has been uploaded to PyPI." - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} - - release: - name: Release - # only proceed if publish workflow ran successfully - needs: build_and_publish - if: ${{ github.event_name == 'push' }} - runs-on: runner-1 - env: - PR_BODY: ${{ github.event.pull_request.body }} - LATEST_COMMIT_MESSAGE: ${{ github.event.head_commit.message }} - steps: - - name: Get Code - uses: actions/checkout@v3 - with: - ref: ${{ github.head_ref }} - fetch-tags: true - - - name: Get SAT Actions - uses: actions/checkout@v3 - with: - repository: SAT/sat-actions - path: sat-actions - - - name: Retrieve current version - run: echo "CURRENT_VERSION=v$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)" >> $GITHUB_ENV - - - name: Retrieve previous version - run: echo "PREV_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV - - - name: Determine if event is a PR merge - if: contains(fromJSON('["pull_request"]'), github.event_name) - run: | - echo "BODY<> $GITHUB_ENV - echo "${PR_BODY}" >> ${GITHUB_ENV} - echo "EOF" >> $GITHUB_ENV - - # TODO still have to check whether non-PR push to main works correctly - - name: Determine if event is just a push - if: ${{ ! contains(fromJSON('["pull_request"]'), github.event_name) }} - run: | - echo "BODY<> $GITHUB_ENV - echo "${LATEST_COMMIT_MESSAGE}" >> ${GITHUB_ENV} - echo "EOF" >> $GITHUB_ENV - - - name: Create Release - uses: actions/create-release@v1 - # This token is provided by Actions, you do not need to create your own token - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: "${{ env.CURRENT_VERSION }}" - release_name: "${{ env.CURRENT_VERSION }}" - body: | - ## What's Changed - ${{ env.BODY }} - **Full Changelog**: https://github.ncsu.edu/SAT/sat-automations/compare/${{ env.PREV_VERSION }}...${{ env.CURRENT_VERSION }} - - - name: Send Failure Message on Slack if Release Fails - if: ${{ failure() }} - uses: ./sat-actions/slack-updates - with: - channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} - message: "The new version of `${{ env.REPO_NAME }}` has been uploaded to PyPI, but the GitHub release failed." - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} - - - name: Send Success Message on Slack - uses: ./sat-actions/slack-updates - with: - channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} - message: "A new Github release for `${{ env.REPO_NAME }}` with version `${{ env.CURRENT_VERSION }}` has been created." - env: - SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} - SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} +#name: Run Tests +# +#on: +# # PR merges count as pushes +# push: +# branches: +# - main +# pull_request: +# workflow_dispatch: +# +#env: +# REPO_NAME: sat-utils +# SLACK_UPDATES: true +# SLACK_CHANNEL_ID: C05Q5KLUW6Q +# +#jobs: +# run_tests: +# name: Run Tests +# runs-on: ubuntu-latest +# steps: +# - name: Get Code +# uses: actions/checkout@v3 +# with: +# ref: ${{ github.head_ref }} +# +# - name: Get SAT Actions +# uses: actions/checkout@v3 +# with: +# repository: SAT/sat-actions +# path: sat-actions +# +# - name: Setup Python +# uses: actions/setup-python@v4 +# with: +# python-version: "3.10" +# +# - name: Cache Packages +# uses: actions/cache@v3 +# with: +# path: ~/.local +# key: requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/requirements/base/base.txt') }}-${{ hashFiles('**/requirements/dev/dev.txt') }}-${{ hashFiles('.github/workflows/*.yml') }}-1 +# restore-keys: | +# requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}- +# +# - name: Setup Virtual Environment +# run: | +# python -m venv venv +# source venv/bin/activate +# make setup +# +# - name: Run Pre-commit +# run: | +# source venv/bin/activate +# pre-commit install +# pre-commit run --all +# +# - name: Run Tests +# run: | +# source venv/bin/activate +# pytest +# +# - name: Send Failure Message on Slack if Tests Fail +# if: ${{ failure() }} +# uses: ./sat-actions/slack-updates +# with: +# channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} +# message: "The tests for `${{ env.REPO_NAME }}` have failed." +# env: +# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +# SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} +# +# - name: Send Success Message on Slack +# uses: ./sat-actions/slack-updates +# with: +# channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} +# message: "The tests for `${{ env.REPO_NAME }}` have passed." +# env: +# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +# SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} +# +# build_and_publish: +# name: Build and Publish +# # only proceed if test job ran successfully +# needs: run_tests +# if: ${{ github.event_name == 'push' }} +# runs-on: ubuntu-latest +# environment: release +# steps: +# - name: Get Code +# uses: actions/checkout@v3 +# with: +# ref: ${{ github.head_ref }} +# +# - name: Retrieve current version +# run: echo "CURRENT_VERSION=v$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)" >> $GITHUB_ENV +# +# - name: Retrieve previous version +# run: echo "PREV_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV +# +# - name: Check if version has been updated +# if: ${{ env.PREV_VERSION >= env.CURRENT_VERSION }} +# run: | +# echo "Version in pyproject.toml has not been updated" +# exit 1 +# +# - name: Get SAT Actions +# uses: actions/checkout@v3 +# with: +# repository: SAT/sat-actions +# path: sat-actions +# +# - name: Setup Python +# uses: actions/setup-python@v4 +# with: +# python-version: "3.10" +# +# - name: Cache Packages +# uses: actions/cache@v3 +# with: +# path: ~/.local +# key: requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/requirements/base/base.txt') }}-${{ hashFiles('**/requirements/dev/dev.txt') }}-${{ hashFiles('.github/workflows/*.yml') }}-1 +# restore-keys: | +# requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}- +# +# - name: Setup Virtual Environment +# run: python -m venv venv +# +# - name: Configure pip +# run: | +# source venv/bin/activate +# make setup +# +# - name: Build and Publish +# run: | +# source venv/bin/activate +# flit publish +# +# - name: Send Failure Message on Slack if Build/Publish Fails +# if: ${{ failure() }} +# uses: ./sat-actions/slack-updates +# with: +# channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} +# message: "The tests for `${{ env.REPO_NAME }}` have passed, but publishing to PyPI failed." +# env: +# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +# SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} +# +# - name: Send Success Message on Slack +# uses: ./sat-actions/slack-updates +# with: +# channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} +# message: "A new version of `${{ env.REPO_NAME }}` has been uploaded to PyPI." +# env: +# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +# SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} +# +# release: +# name: Release +# # only proceed if publish workflow ran successfully +# needs: build_and_publish +# if: ${{ github.event_name == 'push' }} +# runs-on: ubuntu-latest +# env: +# PR_BODY: ${{ github.event.pull_request.body }} +# LATEST_COMMIT_MESSAGE: ${{ github.event.head_commit.message }} +# steps: +# - name: Get Code +# uses: actions/checkout@v3 +# with: +# ref: ${{ github.head_ref }} +# fetch-tags: true +# +# - name: Get SAT Actions +# uses: actions/checkout@v3 +# with: +# repository: SAT/sat-actions +# path: sat-actions +# +# - name: Retrieve current version +# run: echo "CURRENT_VERSION=v$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)" >> $GITHUB_ENV +# +# - name: Retrieve previous version +# run: echo "PREV_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV +# +# - name: Determine if event is a PR merge +# if: contains(fromJSON('["pull_request"]'), github.event_name) +# run: | +# echo "BODY<> $GITHUB_ENV +# echo "${PR_BODY}" >> ${GITHUB_ENV} +# echo "EOF" >> $GITHUB_ENV +# +# # TODO still have to check whether non-PR push to main works correctly +# - name: Determine if event is just a push +# if: ${{ ! contains(fromJSON('["pull_request"]'), github.event_name) }} +# run: | +# echo "BODY<> $GITHUB_ENV +# echo "${LATEST_COMMIT_MESSAGE}" >> ${GITHUB_ENV} +# echo "EOF" >> $GITHUB_ENV +# +# - name: Create Release +# uses: actions/create-release@v1 +# # This token is provided by Actions, you do not need to create your own token +# env: +# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# with: +# tag_name: "${{ env.CURRENT_VERSION }}" +# release_name: "${{ env.CURRENT_VERSION }}" +# body: | +# ## What's Changed +# ${{ env.BODY }} +# **Full Changelog**: https://github.ncsu.edu/SAT/sat-automations/compare/${{ env.PREV_VERSION }}...${{ env.CURRENT_VERSION }} +# +# - name: Send Failure Message on Slack if Release Fails +# if: ${{ failure() }} +# uses: ./sat-actions/slack-updates +# with: +# channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} +# message: "The new version of `${{ env.REPO_NAME }}` has been uploaded to PyPI, but the GitHub release failed." +# env: +# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +# SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} +# +# - name: Send Success Message on Slack +# uses: ./sat-actions/slack-updates +# with: +# channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }} +# message: "A new Github release for `${{ env.REPO_NAME }}` with version `${{ env.CURRENT_VERSION }}` has been created." +# env: +# SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} +# SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }} diff --git a/sat/ldap.py b/sat/ldap.py deleted file mode 100644 index ec5177e..0000000 --- a/sat/ldap.py +++ /dev/null @@ -1,133 +0,0 @@ -""" A simple LDAP connector - The library standardizes how we get connected to our LDAP environment. - - If no configuration data is available to the module - it will attempt to make a connection to localhost - on the default tcp port 389. -""" - -import ssl -from os import getenv -from pathlib import Path - -from ldap3 import Connection, Server, Tls - - -def get_connection(): - """ - Provide an insecure client that allows us to read - NCSU LDAP based people attributes - """ - - try: - tls = Tls(validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLSv1_2) - server = Server("ldap.ncsu.edu", use_ssl=True, tls=tls) - return Connection(server, auto_bind=True) - - except Exception as ex: - print("This went off like a SpaceX launch.", ex) - - -def get_secure_connection(**settings): - """ - Provide a secure client that allows us to read LDAP based - people attributes from any LDAP store. - - Parameters - ---------- - name : settings - A dictionary of values that define the ldap environment. - Should only contain the parameters used by the function. - - name: server - The ldap host to connect to. - - port: - The TCP port the ldap server is listening on. - - username: - The ldap user to specify in the bind operation. - - password: - The password associated to the bind user specified - in the username parameter. - - Usage: - ------ - get_connection(server='dilbert', port=1000) - or - settings = {"server': 'dilbert', 'port': 1000} - get_connection(**settings) - - """ - - _server = None - _tls = None - - try: - server_name = settings.get("server", getenv("server")) or "localhost" - tcp_port = settings.get("port", getenv("port")) or 389 - ldap_user = settings.get("username", getenv("username")) or None - ldap_password = settings.get("password", getenv("password")) or None - - if Path("intermediate.pem"): - _tls = Tls( - ciphers="ALL", - local_certificate_file="intermediate.pem", - validate=ssl.CERT_REQUIRED, - version=ssl.PROTOCOL_TLS, - ) - - _tls = Tls(ciphers="ALL", validate=ssl.CERT_NONE, version=ssl.PROTOCOL_TLS) - - _server = Server(host=server_name, port=tcp_port, use_ssl=True, tls=_tls) - - if not ldap_user: - return Connection(server=_server) - - return Connection(server=_server, user=ldap_user, password=ldap_password, auto_bind=True) - - except Exception as ex: - print("We encountered an error saving the universe.\n", ex) - - -def get_user_attributes(unity_id: str): - """ - Returns a dictionary that contains - the name, unity id, campus id and job title - for the person - - Parameters - ---------- - name : unity_id - The unity id for a campus person - """ - _attributes = ["cn", "uid", "uidNumber", "title"] - - try: - with get_connection() as conn: - conn.search("ou=people,dc=ncsu,dc=edu", f"(uid={unity_id})", attributes=_attributes) - - person_data = {} - - for e in conn.entries: - for attr in str(e).split("\n"): - if "DN" in attr: - continue - - if "cn" in attr: - person_data["name"] = attr.split(":")[1].strip() - - if "title" in attr: - person_data["title"] = attr.split(":")[1].strip() - - if "uid" in attr: - person_data["unity_id"] = attr.split(":")[1].strip() - - if "uidNumber" in attr: - person_data["campus_id"] = attr.split(":")[1].strip() - - return person_data - - except Exception as ex: - print("Hold on while we try that extension\n", ex)