Skip to content

Instantly share code, notes, and snippets.

View beargiles's full-sized avatar

Bear Giles beargiles

  • Boulder, CO
  • 15:12 (UTC -06:00)
View GitHub Profile
@beargiles
beargiles / create-get-host-enum-function.sql
Last active December 7, 2023 01:41
Figures for 'syslog-ng with triggers and custom enum types'
create or replace function get_host_enum(hostname text)
returns host_enum as
$$
declare
_value host_enum;
begin
select hosts1.key from hosts1 where hosts1.hostname = $1 into _value;
if not found then
insert into hosts1(hostname) values ($1) on conflict do nothing;
@beargiles
beargiles / PermissiveSecurityManager.java
Last active October 11, 2023 19:29
Example of using AccessControlContext to restrict access to sensitive resources
/**
* Permissive SecurityManager
*
* This SecurityManager can still be highly restricive but it allows
* access to a sensitive service.
*/
public class PermissiveSecurityManager extends SecurityManager {
private final List<String> hosts = new ArrayList<>();
public PermissiveSecurityManager(String... hosts) {
@beargiles
beargiles / key_manager.sql
Created October 27, 2021 03:06
Key store with triggers
-- ---------------------------------------
-- Preparation
-- ---------------------------------------
CREATE SCHEMA pgcrypto;
CREATE EXTENSION pgcrypto WITH SCHEMA pgcrypto;
CREATE USER key_manager;
CREATE SCHEMA key_manager;
ALTER SCHEMA key_manager OWNER TO key_manager;
ALTER ROLE key_manager SET search_path TO key_manager;
@beargiles
beargiles / skey_type.sql
Created October 26, 2021 01:04
PostgreSQL user-defined RECORD type
--
-- Create user-defined record type for symmetric keys
--
CREATE TYPE skey AS (
key_id int4,
key bytea,
type text
);
--
@beargiles
beargiles / color_check.sql
Created October 25, 2021 23:32
PostgreSQL function checking whether a parameter is in a valid set of values
--
-- Function that needs to explicitly check whether the 'color' value is valid
--
CREATE OR REPLACE FUNCTION get_gluon(color text) RETURNS TEXT AS $$
#print_strict_params on
DECLARE
duck TEXT;
BEGIN
IF color not in ('red', 'green', 'blue') THEN
RAISE EXCEPTION 'unknown color %', color;
@beargiles
beargiles / color.sql
Created October 25, 2021 23:27
PostgreSQL table with check constraint
CREATE TABLE color (
color TEXT CHECK (color IN ('RED', 'GREEN', 'BLUE'))
);
@beargiles
beargiles / color.sql
Last active October 25, 2021 23:38
Defining a PostgreSQL enum type
--
-- Create user-defined enum type.
-- (List details with '\dT+ color' in psql)
--
CREATE TYPE color AS ENUM ('RED', 'GREEN', 'BLUE');
--
-- Create a table using the user-defined enum type.
--
CREATE TABLE quark (
@beargiles
beargiles / get_gluon.sql
Last active October 25, 2021 23:35
PostgreSQL function using user-defined enum type in case statement
CREATE OR REPLACE FUNCTION get_gluon(color color) RETURNS TEXT AS $$
#print_strict_params on
DECLARE
duck TEXT;
BEGIN
CASE color
WHEN 'RED'::color THEN
duck := 'Huey';
WHEN 'GREEN'::color THEN
duck := 'Dewey';
@beargiles
beargiles / typesafe_encode.sql
Last active October 25, 2021 23:26
Type-safe PostgreSQL encode/decode
--
-- Define custom enum type containing supported encoding algorithms
--
CREATE TYPE encoding AS ENUM ('BASE64', 'ESCAPE', 'HEX');
--
-- Type-safe alternative to encode(bytea, text)
--
CREATE OR REPLACE FUNCTION encode (value bytea, alg encoding) RETURNS text AS $$
#print_strict_params on
@beargiles
beargiles / BasicKdcTest.java
Last active May 14, 2018 17:48
JAAS with Kerberos; Unit Test using Apache Hadoop Mini-KDC.
import java.io.File;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;