0002-Add-kmgr-plugin-APIs.patch

text/x-patch

Filename: 0002-Add-kmgr-plugin-APIs.patch
Type: text/x-patch
Part: 1
Message: Re: [Proposal] Table-level Transparent Data Encryption (TDE) and Key Management Service (KMS)

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch 0002
Subject: Add kmgr plugin APIs.
File+
src/backend/storage/kmgr/plugin.c 105 0
src/backend/storage/Makefile 2 1
src/include/storage/kmgr_api.h 37 0
src/include/storage/kmgr_plugin.h 26 0
From 6bd40b495299a278e257bb537e575dd7cd983f08 Mon Sep 17 00:00:00 2001
From: Masahiko Sawada <sawada.mshk@gmail.com>
Date: Fri, 28 Jun 2019 17:43:28 +0900
Subject: [PATCH 2/9] Add kmgr plugin APIs.

---
 src/backend/storage/Makefile      |   3 +-
 src/backend/storage/kmgr/plugin.c | 105 ++++++++++++++++++++++++++++++++++++++
 src/include/storage/kmgr_api.h    |  37 ++++++++++++++
 src/include/storage/kmgr_plugin.h |  26 ++++++++++
 4 files changed, 170 insertions(+), 1 deletion(-)
 create mode 100644 src/backend/storage/kmgr/plugin.c
 create mode 100644 src/include/storage/kmgr_api.h
 create mode 100644 src/include/storage/kmgr_plugin.h

diff --git a/src/backend/storage/Makefile b/src/backend/storage/Makefile
index 8376cdf..8516041 100644
--- a/src/backend/storage/Makefile
+++ b/src/backend/storage/Makefile
@@ -8,6 +8,7 @@ subdir = src/backend/storage
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-SUBDIRS     = buffer file freespace ipc large_object lmgr page smgr sync
+SUBDIRS     = buffer file freespace ipc large_object lmgr page smgr sync \
+		kmgr
 
 include $(top_srcdir)/src/backend/common.mk
diff --git a/src/backend/storage/kmgr/plugin.c b/src/backend/storage/kmgr/plugin.c
new file mode 100644
index 0000000..2d2ee66
--- /dev/null
+++ b/src/backend/storage/kmgr/plugin.c
@@ -0,0 +1,105 @@
+/*-------------------------------------------------------------------------
+ *
+ * plugin.c
+ *	 This module manages the key managment plugin
+ *
+ * Copyright (c) 2019, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATIONf
+ *	  src/backend/storage/kmgr/plugin.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "miscadmin.h"
+#include "fmgr.h"
+
+#include "storage/kmgr_api.h"
+#include "storage/kmgr_plugin.h"
+#include "utils/memutils.h"
+
+static MemoryContext KmgrPluginCtx;
+static KmgrPluginCallbacks callbacks;
+
+static void load_kmgr_plugin(const char *libraryname);
+
+char *
+KmgrPluginGetKey(const char *id)
+{
+	return callbacks.getkey_cb(id);
+}
+
+void
+KmgrPluginGenerateKey(const char *id)
+{
+	callbacks.generatekey_cb(id);
+}
+
+void
+KmgrPluginRemoveKey(const char *id)
+{
+	callbacks.removekey_cb(id);
+}
+
+bool
+KmgrPluginIsExist(const char *id)
+{
+	return callbacks.isexistkey_cb(id);
+}
+
+void
+KmgrPluginStartup(void)
+{
+	if (callbacks.startup_cb)
+		callbacks.startup_cb();
+}
+
+/* Load keyring plugin */
+void
+startupKmgrPlugin(const char *libraryname)
+{
+	MemoryContext old_ctx;
+
+	if (libraryname == NULL || libraryname[0] == '\0')
+		return;
+
+	ereport(LOG,
+			(errmsg("loading keyring plugin \"%s\"", libraryname)));
+
+	if (!KmgrPluginCtx)
+		KmgrPluginCtx = AllocSetContextCreate(TopMemoryContext,
+											  "Key manager plugin",
+											  ALLOCSET_DEFAULT_SIZES);
+	old_ctx = MemoryContextSwitchTo(KmgrPluginCtx);
+
+	/* Load keyring plugins and call the startup callback */
+	load_kmgr_plugin(libraryname);
+
+	MemoryContextSwitchTo(old_ctx);
+}
+
+/* Load keyring plugin and check callbacks */
+static void
+load_kmgr_plugin(const char *libraryname)
+{
+	KmgrPluginInit plugin_init;
+
+	plugin_init = (KmgrPluginInit)
+		load_external_function(libraryname,
+							   "_PG_kmgr_init", false, NULL);
+
+	if (plugin_init == NULL)
+		elog(ERROR, "key management plugin have to declare the _PG_keyring_init symbol");
+
+	/* Call initialize function */
+	plugin_init(&callbacks);
+
+	if (callbacks.getkey_cb == NULL)
+		elog(ERROR, "key management plugin have to register a get key callback");
+	if (callbacks.generatekey_cb == NULL)
+		elog(ERROR, "key management plugin have to register a generate key callback");
+	if (callbacks.removekey_cb == NULL)
+		elog(ERROR, "key management plugin have to register a remove key callback");
+}
diff --git a/src/include/storage/kmgr_api.h b/src/include/storage/kmgr_api.h
new file mode 100644
index 0000000..d21153b
--- /dev/null
+++ b/src/include/storage/kmgr_api.h
@@ -0,0 +1,37 @@
+/*-------------------------------------------------------------------------
+ *
+ * kmgr_api.h
+ *    APIs for kmgr plugin
+ *
+ * Portions Copyright (c) 2019, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *    src/include/storage/kmgr_api.h
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef KMGR_API_H
+#define KMGR_API_H
+
+struct KmgrPluginCallbacks;
+
+typedef void (*KmgrPluginInit) (struct KmgrPluginCallbacks *cb);
+typedef void (*Startup_function) (void);
+typedef char *(*GetKey_function) (const char *keyid);
+typedef void (*GenerateKey_function) (const char *keyid);
+typedef bool (*IsExistKey_function) (const char *keyid);
+typedef void (*RemoveKey_function) (const char *keyid);
+
+/*
+ * Key management plugin callbacks
+ */
+typedef struct KmgrPluginCallbacks
+{
+	Startup_function startup_cb;
+	GetKey_function getkey_cb;
+	GenerateKey_function generatekey_cb;
+	IsExistKey_function isexistkey_cb;
+	RemoveKey_function removekey_cb;
+} KmgrPluginCallbacks;
+
+#endif /* KMGR_API_H */
diff --git a/src/include/storage/kmgr_plugin.h b/src/include/storage/kmgr_plugin.h
new file mode 100644
index 0000000..4c7c2c1
--- /dev/null
+++ b/src/include/storage/kmgr_plugin.h
@@ -0,0 +1,26 @@
+/*-------------------------------------------------------------------------
+ *
+ * kmgr_plugin.h
+ *    Key management plugin manager
+ *
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *    src/include/storage/kmgr_plugin.h
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef KMGR_PLUGIN_H
+#define KMGR_PLUGIN_H
+
+#include "storage/kmgr_api.h"
+
+extern void startupKmgrPlugin(const char *library_name);
+extern char *KmgrPluginGetKey(const char *id);
+extern void KmgrPluginGenerateKey(const char *id);
+extern void KmgrPluginRemoveKey(const char *id);
+extern bool KmgrPluginIsExist(const char *id);
+extern void KmgrPluginStartup(void);
+
+#endif /* KMGR_PLUGIN_H */
-- 
1.8.3.1