Commit 48a04a3f authored by Konrad Sztyber's avatar Konrad Sztyber Committed by Tomasz Zawadzki
Browse files

keyring: add RPC for listing available keys



Signed-off-by: default avatarKonrad Sztyber <konrad.sztyber@intel.com>
Change-Id: I4f9f6753fa616bc1c61abb43656f95c91abf3120
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/21739


Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
parent 8db51b4e
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -12809,3 +12809,43 @@ Example response:
  "result": true
}
~~~

### keyring_get_keys {#rpc_keyring_get_keys}

Get a list of available keys.

#### Example

Example request:
~~~json
{
  "jsonrpc": "2.0",
  "method": "keyring_get_keys",
  "id": 1
}
~~~

Example response:

~~~json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": [
    {
      "name": "key0",
      "module": "keyring_file",
      "removed": false,
      "refcnt": 1,
      "path": "/path/to/key0"
    },
    {
      "name": "key1",
      "module": "keyring_file",
      "removed": false,
      "refcnt": 1,
      "path": "/path/to/key1"
    }
  ]
}
~~~
+18 −0
Original line number Diff line number Diff line
@@ -64,4 +64,22 @@ int spdk_keyring_init(void);
 */
void spdk_keyring_cleanup(void);

struct spdk_keyring;

/** Iterate over all keys including those that were removed, but still have active references */
#define SPDK_KEYRING_FOR_EACH_ALL 0x1

/**
 * Execute a function on each registered key attached to a given keyring.  For now, this function
 * only supports iterating over keys from all keyrings and the `keyring` parameter must be set to
 * NULL.
 *
 * \param keyring Keyring over which to iterate.  If NULL, iterate over keys from all keyrings.
 * \param ctx Context to pass to the function.
 * \param fn Function to call.
 * \param flags Flags controlling the keys to iterate over.
 */
void spdk_keyring_for_each_key(struct spdk_keyring *keyring, void *ctx,
			       void (*fn)(void *ctx, struct spdk_key *key), uint32_t flags);

#endif /* SPDK_KEYRING_H */
+6 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#define SPDK_KEYRING_MODULE_H

#include "spdk/stdinc.h"
#include "spdk/json.h"
#include "spdk/keyring.h"
#include "spdk/queue.h"

@@ -54,6 +55,11 @@ struct spdk_keyring_module {
	int (*get_key)(struct spdk_key *key, void *buf, int len);
	/** Get the size of the context associated with a key */
	size_t (*get_ctx_size)(void);
	/**
	 * Dump information about a key to JSON.  This callback should never dump keying material
	 * itself, only non-sensitive properties of a key must be dumped.
	 */
	void (*dump_info)(struct spdk_key *key, struct spdk_json_write_ctx *w);

	TAILQ_ENTRY(spdk_keyring_module) tailq;
};
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
SO_VER := 1
SO_MINOR := 0

C_SRCS = keyring.c
C_SRCS = keyring.c keyring_rpc.c
LIBNAME = keyring

SPDK_MAP_FILE = $(abspath $(CURDIR)/spdk_keyring.map)
+36 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
 * Copyright (c) 2024 Intel Corporation. All rights reserved.
 */

#include "keyring_internal.h"
#include "spdk/keyring.h"
#include "spdk/keyring_module.h"
#include "spdk/log.h"
@@ -216,12 +217,47 @@ spdk_key_get_ctx(struct spdk_key *key)
	return key + 1;
}

void
spdk_keyring_for_each_key(struct spdk_keyring *keyring,
			  void *ctx, void (*fn)(void *ctx, struct spdk_key *key), uint32_t flags)
{
	struct spdk_key *key, *tmp;

	assert(keyring == NULL);
	pthread_mutex_lock(&g_keyring.mutex);
	TAILQ_FOREACH_SAFE(key, &g_keyring.keys, tailq, tmp) {
		fn(ctx, key);
	}

	if (flags & SPDK_KEYRING_FOR_EACH_ALL) {
		TAILQ_FOREACH_SAFE(key, &g_keyring.removed_keys, tailq, tmp) {
			fn(ctx, key);
		}
	}
	pthread_mutex_unlock(&g_keyring.mutex);
}

void
spdk_keyring_register_module(struct spdk_keyring_module *module)
{
	TAILQ_INSERT_TAIL(&g_keyring.modules, module, tailq);
}

void
keyring_dump_key_info(struct spdk_key *key, struct spdk_json_write_ctx *w)
{
	struct spdk_keyring_module *module = key->module;

	spdk_json_write_named_string(w, "name", key->name);
	spdk_json_write_named_string(w, "module", module->name);
	spdk_json_write_named_bool(w, "removed", key->removed);
	spdk_json_write_named_int32(w, "refcnt", key->refcnt);

	if (!key->removed && module->dump_info != NULL) {
		module->dump_info(key, w);
	}
}

int
spdk_keyring_init(void)
{
Loading