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

initial stab at an user update role and test #57

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
49 changes: 24 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
include docker.mk
include test/test.mk

PG_DUMP ?= pg_dump
PSQL ?= psql
SCHEMA_FILES := \
schema/sql/schema.sql \
schema/sql/access.sql \
schema/sql/api.sql \
schema/sql/metrics.sql \
schema/sql/nss.sql \
schema/sql/reserved.sql \
schema/sql/stats.sql \
schema/ext/json-schema/postgres-json-schema--0.1.0.sql \
out/json-schemas.sql

.PHONY: help
help:
Expand All @@ -15,44 +25,20 @@ help:
@echo ""
@$(MAKE) -s docker-help

out/json-schemas.sql: schema/sql/json-schemas.sql
mkdir -p $(@D)
./scripts/build schema < "$<" > "$@"

.PHONY: build
build: out/json-schemas.sql

schema/ext/json-schema/%:
git submodule update --init --recursive $(@D)

test/sql/plpgunit/%:
git submodule update --init --recursive $(@D)

.PHONY: fetch
fetch: schema/ext/json-schema/ test/sql/plpgunit/

.PHONY: fetch-latest
fetch-latest:
git submodule foreach 'git checkout master && git pull'

SCHEMA_FILES := \
schema/sql/schema.sql \
schema/sql/access.sql \
schema/sql/api.sql \
schema/sql/metrics.sql \
schema/sql/nss.sql \
schema/sql/reserved.sql \
schema/sql/stats.sql \
schema/ext/json-schema/postgres-json-schema--0.1.0.sql \
out/json-schemas.sql

.PHONY: install
install: $(SCHEMA_FILES)
$(PSQL) -v ON_ERROR_STOP=1 $(foreach file,$(SCHEMA_FILES),-f $(file));

schema-dump.psql:
$(PG_DUMP) -s > $@

.PHONY: test
test: \
docker-test-build \
Expand All @@ -70,3 +56,16 @@ test-shell: \
.PHONY: clean
clean:
rm -rf out

out/json-schemas.sql: schema/sql/json-schemas.sql
mkdir -p $(@D)
./scripts/build schema < "$<" > "$@"

out/schema-dump.psql:
$(PG_DUMP) -s > $@

schema/ext/json-schema/%:
git submodule update --init --recursive $(@D)

test/sql/plpgunit/%:
git submodule update --init --recursive $(@D)
17 changes: 17 additions & 0 deletions schema/sql/api.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,23 @@ grant usage on sequence "user_id" to "api-user-create";
grant "api-user-create" to "api";
grant "api-anon" to "api-user-create";

create role "api-user-manage";
comment on role "api-user-manage" is
$$Intended for use with user management systems$$;
grant usage on sequence "user_id" to "api-user-manage";
grant select,insert,update on table
public."passwd",
lrvick marked this conversation as resolved.
Show resolved Hide resolved
public."ssh_public_key",
public."openpgp_public_key"
lrvick marked this conversation as resolved.
Show resolved Hide resolved
to "api-user-manage";
lrvick marked this conversation as resolved.
Show resolved Hide resolved
grant "api-user-manage" to "api";
grant "api-anon" to "api-user-manage";
grant "api-user-create" to "api-user-manage";
lrvick marked this conversation as resolved.
Show resolved Hide resolved

create schema v1;
grant create,usage on schema v1 to api;
grant usage on schema v1 to "api-anon";
grant create,usage on schema v1 to "api-user-manage";
lrvick marked this conversation as resolved.
Show resolved Hide resolved

create view v1.hosts as
select
Expand Down Expand Up @@ -73,6 +87,7 @@ comment on column v1.passwd.data is
alter view v1."passwd" owner to api;
grant select on table v1."passwd" to "api-anon";
grant insert("name","host","data") on table v1."passwd" to "api-user-create";
grant update("host","data") on table v1."passwd" to "api-user-manage";

create view v1."group" as
select
Expand Down Expand Up @@ -127,6 +142,7 @@ comment on column v1.ssh_public_key.uid is
$$User ID the key is currently linked to$$;
alter view v1."ssh_public_key" owner to api;
grant select on table v1."ssh_public_key" to "api-anon";
grant update,insert on table v1."ssh_public_key" to "api-user-manage";
lrvick marked this conversation as resolved.
Show resolved Hide resolved

-- PGP Key
create view v1.openpgp_public_key as
Expand All @@ -144,6 +160,7 @@ comment on column v1.openpgp_public_key.ascii_armoured_public_key is
comment on column v1.openpgp_public_key.uid is
$$User ID the key is currently linked to$$;
grant insert("uid", "ascii_armoured_public_key") on table v1."openpgp_public_key" to "api-user-create";
grant update("uid", "ascii_armoured_public_key") on table v1."openpgp_public_key" to "api-user-manage";

create function insert_pgp_key() returns trigger as $$
begin
Expand Down
104 changes: 103 additions & 1 deletion test/bats/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load test_helper

@test "Can connect to userdb PostgreSQL" {
sleep 1
run pg_isready -U postgres -h userdb;
run pg_isready -U postgres -h userdb-postgres;
lrvick marked this conversation as resolved.
Show resolved Hide resolved
[ "$status" -eq 0 ]
echo "$output" | grep "accepting connections"
}
Expand Down Expand Up @@ -93,3 +93,105 @@ load test_helper
run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "testuser43"
}

@test "Can update user with a valid update permissioned token" {

run curl http://userdb-postgrest:3000/signup \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(jwt_token 'api-user-create')" \
-X POST \
--data-binary @- <<-EOF
{
"name": "testuser43",
"host": "test.hashbang.sh",
"shell": "/bin/zsh",
"keys": ["$(cat bats/keys/id_ed25519.pub)"]
}
EOF
[ "$status" -eq 0 ]

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "test.hashbang.sh"

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(jwt_token 'api-user-manage')" \
-X PATCH \
--data-binary @- <<-EOF
{
"host": "test2.hashbang.sh"
}
EOF
[ "$status" -eq 0 ]

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "test2.hashbang.sh"
}

@test "Can not update user with a JWT with an invalid role" {

run curl http://userdb-postgrest:3000/signup \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(jwt_token 'api-user-create')" \
-X POST \
--data-binary @- <<-EOF
{
"name": "testuser43",
"host": "test.hashbang.sh",
"shell": "/bin/zsh",
"keys": ["$(cat bats/keys/id_ed25519.pub)"]
}
EOF
[ "$status" -eq 0 ]

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "test.hashbang.sh"

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(jwt_token 'api-derp')" \
-X PATCH \
--data-binary @- <<-EOF
{
"host": "test2.hashbang.sh"
}
EOF
echo "$output" | grep "does not exist"

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "test.hashbang.sh"
}

@test "Can not update user with a JWT with the wrong role" {

run curl http://userdb-postgrest:3000/signup \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(jwt_token 'api-user-create')" \
-X POST \
--data-binary @- <<-EOF
{
"name": "testuser43",
"host": "test.hashbang.sh",
"shell": "/bin/zsh",
"keys": ["$(cat bats/keys/id_ed25519.pub)"]
}
EOF
[ "$status" -eq 0 ]

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "test.hashbang.sh"

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(jwt_token 'api-anon')" \
-X PATCH \
--data-binary @- <<-EOF
{
"host": "test2.hashbang.sh"
}
EOF
echo "$output" | grep "permission denied"

run curl http://userdb-postgrest:3000/passwd?name=eq.testuser43
echo "$output" | grep "test.hashbang.sh"
}
1 change: 1 addition & 0 deletions test/bats/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

setup(){
psql -c "insert into hosts (name,maxusers) values ('test.hashbang.sh','500');";
psql -c "insert into hosts (name,maxusers) values ('test2.hashbang.sh','500');";
}

teardown(){
Expand Down
8 changes: 5 additions & 3 deletions docker.mk → test/test.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NAMESPACE ?= userdb
POSTGRES_USER ?= postgres
POSTGRES_DB ?= postgres
IMAGE_POSTGRES ?= postgres:latest
IMAGE_POSTGRES ?= postgres@sha256:3657548977d593c9ab6d70d1ffc43ceb3b5164ae07ac0f542d2ea139664eb6b3
lrvick marked this conversation as resolved.
Show resolved Hide resolved
IMAGE_POSTGREST ?= postgrest/postgrest:v7.0.1@sha256:2a10713acc388f9a64320443e949eb87a0424ab280e68c4ed4a6d0653c001586
lrvick marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: docker-help
Expand All @@ -21,7 +21,8 @@ docker-start:
docker network inspect $(NAMESPACE) \
|| docker network create $(NAMESPACE)
# Start database
docker run \
docker inspect -f '{{.State.Running}}' $(NAMESPACE)-postgres 2>/dev/null \
|| docker run \
--detach=true \
--name=$(NAMESPACE)-postgres \
--network=$(NAMESPACE) \
Expand Down Expand Up @@ -53,7 +54,8 @@ docker-start:
$(IMAGE_POSTGRES) psql" \
install
# Start web API
docker run \
docker inspect -f '{{.State.Running}}' $(NAMESPACE)-postgrest 2>/dev/null \
|| docker run \
--rm \
--detach=true \
--name $(NAMESPACE)-postgrest \
Expand Down