Skip to content

Instantly share code, notes, and snippets.

View deangrant's full-sized avatar

Dean Grant deangrant

View GitHub Profile
@deangrant
deangrant / Dockerfile
Last active June 7, 2023 09:33
Due to the Keycloak image being modified to enhance security and curl being removed. The below bash script provides an alternative method for providing a healthcheck. The Dockerfile and docker-compose.yml files contain the snippets to enable the healthchck
# Enable health endpoint.
ENV KC_HEALTH_ENABLED=true
# Copies the docker-healthcheck.sh file into the /opt/keycloak directory.
COPY --chown=keycloak:keycloak \
./docker-healthcheck.sh \
/opt/keycloak/bin/docker-healthcheck.sh
# Add healthcheck to execute a shell command as the health check
HEALTHCHECK --interval=30s --timeout=10s --retries=3 \
@deangrant
deangrant / is_valid_polygon.py
Created April 27, 2023 15:07
Check if the polygons in the data are valid and closed.
def is_valid_polygon(
data: list
) -> list:
""""
Check if the polygons in the data are valid and closed.
Args:
data: A list containing polygon data in the format:
{'polygons': [{'coordinates': [{'latitude': float,
@deangrant
deangrant / update_on_case.sql
Created April 25, 2023 15:33
Provides example of conditional update in SQL if column value is specified
UPDATE {{ table_name }}
SET {{ column_name }} CASE WHEN {{ value}} IS NOT NULL THEN {{ value }} ELSE {{column_name }} END
WHERE {{ column_name }} = {{ value }}
@deangrant
deangrant / returning_example.sql
Created April 25, 2023 15:26
Obtain data from modified rows while they are being manipulated
-- https://www.postgresql.org/docs/current/dml-returning.html
-- Use of RETURNING avoids performing an extra database query to collect the
-- data, and is especially valuable when it would otherwise be difficult to
-- identify the modified rows reliably. The allowed contents of a RETURNING
-- clause are the same as a SELECT command's output list (see Section 7.3).
-- It can contain column names of the command's target table, or value
-- expressions using those columns. A common shorthand is RETURNING *, which
-- selects all columns of the target table in order.
@deangrant
deangrant / is_valid_email.py
Created February 13, 2023 16:14
Checks if a string is a valid email address in Python. Uses a regular expression to check that the email address conforms to the syntax rules specified in RFC 5322.
def is_valid_email(
email_str: str
) -> bool:
"""
Check if a string is a valid email address. The regular expression checks
that the email address conforms to the syntax rules specified in RFC 5322.
Parameters:
email_str (str): The string to check.
@deangrant
deangrant / block_validate_request_content.py
Created February 13, 2023 15:43
A code block that parses a JSON object from an HTTP request validates it contents, and raises an error if it is invalid.
from flask import (
request
)
required_fields: [
# Add list of required field names.
]
# Parse request data as JSON.
data = request.get_json()
@deangrant
deangrant / secretary_problem.go
Last active February 13, 2023 20:57
The code used the optimal stopping problem using secretary problem algorithm.
package main
import (
"fmt"
"math/rand"
)
func secretaryProblem(n, cutoff int) int {
// Generate a slice of candidates from 1 to n.
candidates := make([]int, n)
@deangrant
deangrant / mask_string.py
Last active January 17, 2023 06:05
Mask a string by replacing a specific range of characters with asterisks (*)
def mask_string(
string: str,
end: int = 4
) -> str:
"""
Mask a string by replacing all but the last number of characters with asterisks(*).
Parameters:
string (str): The string to be masked.
@deangrant
deangrant / check_letsencrypt_cert_authenticity.sh
Last active January 8, 2023 07:35
Check the Lets Encrypt certificate against the trusted CAs and any untrusted certificates specified to verify its authenticity
# Set the DOMAIN variable to the desired domain name
DOMAIN={{ domain }}
# Use the openssl utility to verify the SSL/TLS certificate
# located at /etc/letsencrypt/live/${DOMAIN}/cert.pem
# The -CAfile flag specifies a file containing trusted CA certificates
# The -untrusted flag specifies a file containing additional, untrusted certificates
# that will also be used in the verification process
@deangrant
deangrant / generate_string.py
Created January 4, 2023 09:14
Generate a string of the specified length with randomly chosen characters from the characters string. It will also ensure that the string contains at least one uppercase letter and one number from the required_characters string.
import random
# Set the length of the string
length = 8
# Set the characters that the string can contain
characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
# Set the required characters
required_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"