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

Mpalmi cicd #1

Open
wants to merge 6 commits into
base: master
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
12 changes: 12 additions & 0 deletions .github/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# set_user Docker image
# This image is used for testing the set_user build process
ARG PGVER
FROM postgres:${PGVER}
ARG PGVER
ARG DEVPKG
COPY . /src/set_user
WORKDIR /src/set_user
RUN apt-get update && \
apt-get -y upgrade && \
apt-get -y install postgresql-server-dev-${DEVPKG} make gcc
RUN make USE_PGXS=1 install
64 changes: 64 additions & 0 deletions .github/resources/scripts/healthcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env bash
# Modified from https://github.com/jordyv/wait-for-healthy-container
container_name=$1
shift
timeout=$1

default_timeout=120

if [ -z ${timeout} ]; then
timeout=${default_timeout}
fi

RETURN_HEALTHY=0
RETURN_STARTING=1
RETURN_UNHEALTHY=2
RETURN_UNKNOWN=3
RETURN_ERROR=99

function usage() {
echo "
Usage: healthcheck.sh <container name> [timeout]
"
return
}

function get_health_state {
state=$(docker inspect -f '{{ .State.Health.Status }}' ${container_name})
return_code=$?
if [ ! ${return_code} -eq 0 ]; then
exit ${RETURN_ERROR}
fi
if [[ "${state}" == "healthy" ]]; then
return ${RETURN_HEALTHY}
elif [[ "${state}" == "unhealthy" ]]; then
return ${RETURN_UNHEALTHY}
elif [[ "${state}" == "starting" ]]; then
return ${RETURN_STARTING}
else
return ${RETURN_UNKNOWN}
fi
}

function wait_for() {
echo "Wait for container '$container_name' to be healthy for max $timeout seconds..."
for i in `seq ${timeout}`; do
get_health_state
state=$?
if [ ${state} -eq 0 ]; then
echo "Container is healthy after ${i} seconds."
exit 0
fi
sleep 1
done

echo "Timeout exceeded. Health status returned: $(docker inspect -f '{{ .State.Health.Status }}' ${container_name})"
exit 1
}

if [ -z ${container_name} ]; then
usage
exit 1
else
wait_for
fi
12 changes: 12 additions & 0 deletions .github/resources/set_user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Custom service that contains the postgres with the installed set_user extension
version: '3'
services:
set_user:
container_name: set_user
image: set_user:latest
environment:
POSTGRES_HOST_AUTH_METHOD: "trust"
healthcheck:
test: ["CMD", "pg_isready"]
interval: 10s
timeout: 5s
54 changes: 54 additions & 0 deletions .github/workflows/regression-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# CI for set_user Pull Requests and pushes to the cicd branch.
# Runs regression tests against all supported versions of postgres.
on:
push:
branches:
- cicd
pull_request:
branches:
- master

jobs:
regression-tests:
runs-on: ubuntu-latest
env:
DOCKER_DIR: ${{ github.workspace }}/.github/docker
RESOURCE_DIR: ${{ github.workspace }}/.github/resources
SCRIPT_DIR: ${{ github.workspace }}/.github/resources/scripts
strategy:
matrix:
include:
- {pgver: 9.4, devpkg: 9.4}
- {pgver: 9.5, devpkg: 9.5}
- {pgver: 9.6, devpkg: 9.6}
- {pgver: 10, devpkg: 10}
- {pgver: 11, devpkg: 11}
- {pgver: 12, devpkg: 12}
- {pgver: 13, devpkg: 13}
- {pgver: 14beta3, devpkg: 14}
steps:
- name: Checkout set_user repo
uses: actions/[email protected]

- name: Build set_user
run: |
docker build -t set_user:latest \
--build-arg PGVER=${{ matrix.pgver }} \
--build-arg DEVPKG=${{ matrix.devpkg }} \
-f ${{ env.DOCKER_DIR }}/Dockerfile .

- name: Run PG set_user
run: |
docker-compose -f ${{ env.RESOURCE_DIR }}/set_user.yml up -d
/bin/bash ${{ env.SCRIPT_DIR }}/healthcheck.sh set_user 60

- name: Run tests
run: |
docker exec set_user make -C /src/set_user USE_PGXS=1 REGRESS_OPTS='--user=postgres' installcheck

- name: Show any regression diffs
if: always()
continue-on-error: true
run: |
docker cp set_user:/src/set_user/regression.diffs ./regression.diffs
cat ./regression.diffs
84 changes: 79 additions & 5 deletions compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
standard_ProcessUtility(pstmt, queryString, context, params, queryEnv, dest, qc)
#endif

#define TABLEOPEN

#endif /* 13+ */

/*
Expand All @@ -68,12 +70,34 @@
*/
#if PG_VERSION_NUM >= 120000
#define HEAP_TUPLE_GET_OID

/*
* _heap_tuple_get_oid
*
* Return the oid of the tuple based on the provided catalogID.
*/
static inline Oid
_heap_tuple_get_oid(HeapTuple roleTup)
_heap_tuple_get_oid(HeapTuple tuple, Oid catalogID)
{
return ((Form_pg_authid) GETSTRUCT(roleTup))->oid;
switch (catalogID)
{
case ProcedureRelationId:
return ((Form_pg_proc) GETSTRUCT(tuple))->oid;
break;
case AuthIdRelationId:
return((Form_pg_authid) GETSTRUCT(tuple))->oid;
break;
default:
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("set_user: invalid relation ID provided")));
return 0;
}
}


#include "access/table.h"
#define OBJECTADDRESS
#endif /* 12+ */

/*
Expand Down Expand Up @@ -110,7 +134,11 @@ _heap_tuple_get_oid(HeapTuple roleTup)
*/
#if PG_VERSION_NUM >= 90500
#define GETUSERNAMEFROMID(ouserid) GetUserNameFromId(ouserid, false)
#endif

#define INITSESSIONUSER
#define _InitializeSessionUserId(name,ouserid) InitializeSessionUserId(name,ouserid)

#endif /* 9.5+ */

/*
* PostgreSQL version 9.4+
Expand All @@ -137,13 +165,59 @@ _heap_tuple_get_oid(HeapTuple roleTup)

# ifndef HEAP_TUPLE_GET_OID
static inline Oid
_heap_tuple_get_oid(HeapTuple roleTup)
_heap_tuple_get_oid(HeapTuple tup, Oid catalogId)
{
return HeapTupleGetOid(roleTup);
return HeapTupleGetOid(tup);
}
# endif

#ifndef TABLEOPEN
#define table_open(r, l) heap_open(r, l)
#define table_close(r, l) heap_close(r, l)
#endif

#include "access/heapam.h"

#ifndef OBJECTADDRESS
#include "utils/tqual.h"
#endif

#ifndef Anum_pg_proc_oid
#include "access/sysattr.h"
#define Anum_pg_proc_oid ObjectIdAttributeNumber
#define Anum_pg_authid_oid ObjectIdAttributeNumber
#endif

/*
* _scan_key_init
*
* Initialize entry based on the catalogID provided.
*/
static inline void
_scan_key_init(ScanKey entry,
Oid catalogID,
StrategyNumber strategy,
RegProcedure procedure,
Datum argument)
{
switch (catalogID)
{
case ProcedureRelationId:
ScanKeyInit(entry, Anum_pg_proc_oid, strategy, procedure, argument);
break;
default:
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("set_user: invalid relation ID provided")));
}
}

#ifndef INITSESSIONUSER
#define _InitializeSessionUserId(name,ouserid) InitializeSessionUserId(name)
#endif

#endif /* 9.4 */

#if !defined(PG_VERSION_NUM) || PG_VERSION_NUM < 90400
#error "This extension only builds with PostgreSQL 9.4 or later"
#endif
Expand Down
1 change: 0 additions & 1 deletion deprecated_gucs.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
#include "miscadmin.h"
#include "utils/guc.h"
#include "utils/varlena.h"

static bool
check_set_user_list(char **newval, void **extra, GucSource source,
Expand Down
28 changes: 14 additions & 14 deletions set_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/objectaddress.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_proc.h"
#include "miscadmin.h"
Expand Down Expand Up @@ -284,7 +284,7 @@ set_user(PG_FUNCTION_ARGS)
if (!HeapTupleIsValid(roleTup))
elog(ERROR, "role \"%s\" does not exist", newuser);

NewUserId = _heap_tuple_get_oid(roleTup);
NewUserId = _heap_tuple_get_oid(roleTup, AuthIdRelationId);
NewUser_is_superuser = ((Form_pg_authid) GETSTRUCT(roleTup))->rolsuper;
ReleaseSysCache(roleTup);

Expand Down Expand Up @@ -596,7 +596,7 @@ set_session_auth(PG_FUNCTION_ARGS)
errmsg("switching to superuser not allowed"),
errhint("Use \'set_user_u\' to escalate.")));

InitializeSessionUserId(newuser, InvalidOid);
_InitializeSessionUserId(newuser, InvalidOid);
#else
ExitOnAnyError = exit_on_error;
elog(ERROR, "Assert build disables set_session_auth()");
Expand Down Expand Up @@ -698,18 +698,18 @@ set_user_check_proc(HeapTuple procTup, Relation rel)
MemoryContext ctx;
Datum prosrcdatum;
bool isnull;
Form_pg_proc procform;
Oid procoid;

/* For function metadata (Oid) */
procform = (Form_pg_proc) GETSTRUCT(procTup);
procoid = _heap_tuple_get_oid(procTup, ProcedureRelationId);

/* Figure out the underlying function */
prosrcdatum = heap_getattr(procTup, Anum_pg_proc_prosrc, RelationGetDescr(rel), &isnull);
if (isnull)
{
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("set_user: null prosrc for function %u", procform->oid)));
errmsg("set_user: null prosrc for function %u", procoid)));
}

/*
Expand All @@ -721,11 +721,11 @@ set_user_check_proc(HeapTuple procTup, Relation rel)
/* Make sure the Oid cache is up-to-date */
if (strcmp(TextDatumGetCString(prosrcdatum), set_config_proc_name) == 0)
{
set_config_oid_cache = list_append_unique_oid(set_config_oid_cache, procform->oid);
set_config_oid_cache = list_append_unique_oid(set_config_oid_cache, procoid);
}
else if (list_member_oid(set_config_oid_cache, procform->oid))
else if (list_member_oid(set_config_oid_cache, procoid))
{
set_config_oid_cache = list_delete_oid(set_config_oid_cache, procform->oid);
set_config_oid_cache = list_delete_oid(set_config_oid_cache, procoid);
}

MemoryContextSwitchTo(ctx);
Expand Down Expand Up @@ -763,11 +763,11 @@ set_user_cache_proc(Oid functionId)
*/
if (functionId != InvalidOid)
{
indexId = ProcedureOidIndexId;
indexOk = true;
snapshot = SnapshotSelf;
nkeys = 1;
ScanKeyInit(&skey, Anum_pg_proc_oid, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(functionId));
indexId = ProcedureOidIndexId;
indexOk = true;
snapshot = SnapshotSelf;
nkeys = 1;
_scan_key_init(&skey, ProcedureRelationId, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(functionId));
}
else if (set_config_oid_cache != NIL)
{
Expand Down