Commit e93c9915 authored by Konrad Sztyber's avatar Konrad Sztyber
Browse files

keyring/file: expose {add,remove}_key() functions



We'll want to use them from places other than the RPC methods.

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


Reviewed-by: default avatarAleksey Marchuk <alexeymar@nvidia.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
Community-CI: Mellanox Build Bot
parent dd8f4270
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2024 Intel Corporation. All rights reserved.
 */

#ifndef SPDK_KEYRING_FILE_H
#define SPDK_KEYRING_FILE_H

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Add a file-based key to the keyring.
 *
 * \param name Name of a key.
 * \param path Path to a file containing the key.
 *
 * \return 0 on success, negative errno otherwise.
 */
int spdk_keyring_file_add_key(const char *name, const char *path);

/**
 * Remove a file-based key to the keyring.
 *
 * \param name Name of a key.
 *
 * \return 0 on success, negative errno otherwise.
 */
int spdk_keyring_file_remove_key(const char *name);

#ifdef __cplusplus
}
#endif

#endif /* SPDK_KEYRING_FILE_H */
+2 −2
Original line number Diff line number Diff line
@@ -4,12 +4,12 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/../../..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk

SO_VER := 1
SO_VER := 2
SO_MINOR := 0

C_SRCS = keyring.c keyring_rpc.c
LIBNAME = keyring_file

SPDK_MAP_FILE = $(SPDK_ROOT_DIR)/mk/spdk_blank.map
SPDK_MAP_FILE = $(abspath $(CURDIR)/spdk_keyring_file.map)

include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk
+26 −5
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@
 * Copyright (c) 2024 Intel Corporation. All rights reserved.
 */

#include "keyring_file.h"
#include "spdk/keyring_module.h"
#include "spdk/module/keyring/file.h"
#include "spdk/log.h"
#include "spdk/string.h"
#include "spdk/util.h"
@@ -12,6 +12,8 @@ struct keyring_file_key {
	char *path;
};

static struct spdk_keyring_module g_keyring_file;

static int
keyring_file_check_path(const char *path, int *size)
{
@@ -130,16 +132,16 @@ keyring_file_remove_key(struct spdk_key *key)
static int
keyring_file_add_key(struct spdk_key *key, void *ctx)
{
	struct keyring_file_key_opts *opts = ctx;
	struct keyring_file_key *kkey = spdk_key_get_ctx(key);
	const char *path = ctx;
	int rc;

	rc = keyring_file_check_path(opts->path, NULL);
	rc = keyring_file_check_path(path, NULL);
	if (rc != 0) {
		return rc;
	}

	kkey->path = strdup(opts->path);
	kkey->path = strdup(path);
	if (kkey->path == NULL) {
		return -ENOMEM;
	}
@@ -147,7 +149,26 @@ keyring_file_add_key(struct spdk_key *key, void *ctx)
	return 0;
}

struct spdk_keyring_module g_keyring_file = {
int
spdk_keyring_file_add_key(const char *name, const char *path)
{
	struct spdk_key_opts opts = {};

	opts.size = SPDK_SIZEOF(&opts, ctx);
	opts.name = name;
	opts.module = &g_keyring_file;
	opts.ctx = (void *)path;

	return spdk_keyring_add_key(&opts);
}

int
spdk_keyring_file_remove_key(const char *name)
{
	return spdk_keyring_remove_key(name, &g_keyring_file);
}

static struct spdk_keyring_module g_keyring_file = {
	.name = "keyring_file",
	.add_key = keyring_file_add_key,
	.remove_key = keyring_file_remove_key,
+0 −17
Original line number Diff line number Diff line
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2024 Intel Corporation. All rights reserved.
 */

#ifndef SPDK_KEYRING_FILE_H
#define SPDK_KEYRING_FILE_H

#include "spdk/keyring_module.h"

struct keyring_file_key_opts {
	char *name;
	char *path;
};

extern struct spdk_keyring_module g_keyring_file;

#endif
+24 −18
Original line number Diff line number Diff line
@@ -2,20 +2,24 @@
 * Copyright (c) 2024 Intel Corporation. All rights reserved.
 */

#include "keyring_file.h"
#include "spdk/json.h"
#include "spdk/keyring_module.h"
#include "spdk/module/keyring/file.h"
#include "spdk/rpc.h"
#include "spdk/string.h"
#include "spdk/util.h"

static const struct spdk_json_object_decoder keyring_file_key_opts_decoders[] = {
	{"name", offsetof(struct keyring_file_key_opts, name), spdk_json_decode_string},
	{"path", offsetof(struct keyring_file_key_opts, path), spdk_json_decode_string},
struct rpc_keyring_file_add_key {
	char *name;
	char *path;
};

static const struct spdk_json_object_decoder rpc_keyring_file_add_key_decoders[] = {
	{"name", offsetof(struct rpc_keyring_file_add_key, name), spdk_json_decode_string},
	{"path", offsetof(struct rpc_keyring_file_add_key, path), spdk_json_decode_string},
};

static void
free_keyring_file_key_opts(struct keyring_file_key_opts *opts)
free_rpc_keyring_file_add_key(struct rpc_keyring_file_add_key *opts)
{
	free(opts->name);
	free(opts->path);
@@ -25,23 +29,18 @@ static void
rpc_keyring_file_add_key(struct spdk_jsonrpc_request *request,
			 const struct spdk_json_val *params)
{
	struct spdk_key_opts opts = {};
	struct keyring_file_key_opts kopts = {};
	struct rpc_keyring_file_add_key opts = {};
	int rc;

	if (spdk_json_decode_object_relaxed(params, keyring_file_key_opts_decoders,
					    SPDK_COUNTOF(keyring_file_key_opts_decoders),
					    &kopts)) {
	if (spdk_json_decode_object_relaxed(params, rpc_keyring_file_add_key_decoders,
					    SPDK_COUNTOF(rpc_keyring_file_add_key_decoders),
					    &opts)) {
		spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
						 spdk_strerror(EINVAL));
		return;
	}

	opts.size = SPDK_SIZEOF(&opts, ctx);
	opts.name = kopts.name;
	opts.module = &g_keyring_file;
	opts.ctx = &kopts;
	rc = spdk_keyring_add_key(&opts);
	rc = spdk_keyring_file_add_key(opts.name, opts.path);
	if (rc != 0) {
		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
		goto out;
@@ -49,7 +48,7 @@ rpc_keyring_file_add_key(struct spdk_jsonrpc_request *request,

	spdk_jsonrpc_send_bool_response(request, true);
out:
	free_keyring_file_key_opts(&kopts);
	free_rpc_keyring_file_add_key(&opts);
}
SPDK_RPC_REGISTER("keyring_file_add_key", rpc_keyring_file_add_key, SPDK_RPC_RUNTIME)

@@ -72,6 +71,7 @@ rpc_keyring_file_remove_key(struct spdk_jsonrpc_request *request,
			    const struct spdk_json_val *params)
{
	struct rpc_keyring_file_remove_key req = {};
	int rc;

	if (spdk_json_decode_object(params, rpc_keyring_file_remove_key_decoders,
				    SPDK_COUNTOF(rpc_keyring_file_remove_key_decoders),
@@ -81,8 +81,14 @@ rpc_keyring_file_remove_key(struct spdk_jsonrpc_request *request,
		return;
	}

	spdk_keyring_remove_key(req.name, &g_keyring_file);
	rc = spdk_keyring_file_remove_key(req.name);
	if (rc != 0) {
		spdk_jsonrpc_send_error_response(request, rc, spdk_strerror(-rc));
		goto out;
	}

	spdk_jsonrpc_send_bool_response(request, true);
out:
	free_rpc_keyring_file_remove_key(&req);
}
SPDK_RPC_REGISTER("keyring_file_remove_key", rpc_keyring_file_remove_key, SPDK_RPC_RUNTIME)
Loading