From: Yehuda Sadeh Date: Thu, 16 Nov 2017 01:04:22 +0000 (-0800) Subject: cls/otp: add client interfaces X-Git-Tag: v13.1.0~343^2~31 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=9fe808b683ffaa2161a29cd279b9ea716385e9bb;p=ceph.git cls/otp: add client interfaces Signed-off-by: Yehuda Sadeh --- diff --git a/src/cls/CMakeLists.txt b/src/cls/CMakeLists.txt index c9cf3862a0d63..d6a401b405a25 100644 --- a/src/cls/CMakeLists.txt +++ b/src/cls/CMakeLists.txt @@ -78,13 +78,13 @@ set_target_properties(cls_otp PROPERTIES INSTALL_RPATH "") install(TARGETS cls_otp DESTINATION ${cls_dir}) -# set(cls_otp_client_srcs -# otp/cls_otp_client.cc -# otp/cls_otp_types.cc -# otp/cls_otp_ops.cc) -# add_library(cls_otp_client STATIC ${cls_otp_client_srcs}) -# -# list(APPEND cls_embedded_srcs ${cls_otp_srcs} ${cls_otp_client_srcs}) +set(cls_otp_client_srcs + otp/cls_otp_client.cc +# otp/cls_otp_types.cc +# otp/cls_otp_ops.cc + ) +add_library(cls_otp_client STATIC ${cls_otp_client_srcs}) +list(APPEND cls_embedded_srcs ${cls_otp_srcs} ${cls_otp_client_srcs}) # cls_refcount set(cls_refcount_srcs diff --git a/src/cls/otp/cls_otp_client.cc b/src/cls/otp/cls_otp_client.cc new file mode 100644 index 0000000000000..484b10310f0df --- /dev/null +++ b/src/cls/otp/cls_otp_client.cc @@ -0,0 +1,143 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "include/types.h" +#include "msg/msg_types.h" +#include "include/rados/librados.hpp" +#include "include/utime.h" + +using namespace librados; + +#include "cls/otp/cls_otp_ops.h" +#include "cls/otp/cls_otp_client.h" + +#include "rgw/rgw_common.h" /* for gen_random_bytes() */ + +namespace rados { + namespace cls { + namespace otp { + + void OTP::create(librados::ObjectWriteOperation *rados_op, + const otp_info_t& config) { + cls_otp_set_otp_op op; + op.entries.push_back(config); + bufferlist in; + ::encode(op, in); + rados_op->exec("otp", "otp_set", in); + } + + void OTP::remove(librados::ObjectWriteOperation *rados_op, + const string& id) { + cls_otp_remove_otp_op op; + op.ids.push_back(id); + bufferlist in; + ::encode(op, in); + rados_op->exec("otp", "otp_remove", in); + } + + int OTP::check(CephContext *cct, librados::IoCtx& ioctx, const string& oid, + const string& id, const string& val, otp_check_t *result) { + cls_otp_check_otp_op op; + op.id = id; + op.val = val; +#define TOKEN_LEN 16 + char buf[TOKEN_LEN + 1]; + gen_rand_alphanumeric(cct, buf, sizeof(buf));; + op.token = buf; + + bufferlist in; + bufferlist out; + ::encode(op, in); + int r = ioctx.exec(oid, "otp", "otp_check", in, out); + if (r < 0) { + return r; + } + + cls_otp_get_result_op op2; + op2.token = buf; + bufferlist in2; + bufferlist out2; + ::encode(op2, in2); + r = ioctx.exec(oid, "otp", "otp_get_result", in, out); + if (r < 0) { + return r; + } + + auto iter = out.begin(); + cls_otp_get_result_reply ret; + try { + ::decode(ret, iter); + } catch (buffer::error& err) { + return -EBADMSG; + } + + *result = ret.result; + + return 0; + } + + int OTP::get(librados::IoCtx& ioctx, const string& oid, + const list *ids, bool get_all, list *result) { + cls_otp_get_otp_op op; + if (ids) { + op.ids = *ids; + } + op.get_all = get_all; + bufferlist in; + bufferlist out; + ::encode(op, in); + int r = ioctx.exec(oid, "otp", "otp_get", in, out); + if (r < 0) { + return r; + } + + cls_otp_get_otp_reply ret; + auto iter = out.begin(); + try { + ::decode(ret, iter); + } catch (buffer::error& err) { + return -EBADMSG; + } + + *result = ret.found_entries;; + + return 0; + } + + int OTP::get(librados::IoCtx& ioctx, const string& oid, + const string& id, otp_info_t *result) { + list ids{ id }; + list ret; + + int r = get(ioctx, oid, &ids, false, &ret); + if (r < 0) { + return r; + } + if (ret.empty()) { + return -ENOENT; + } + *result = ret.front(); + + return 0; + } + + int OTP::get_all(librados::IoCtx& ioctx, const string& oid, + list *result) { + return get(ioctx, oid, nullptr, true, result); + } + + } // namespace otp + } // namespace cls +} // namespace rados + diff --git a/src/cls/otp/cls_otp_client.h b/src/cls/otp/cls_otp_client.h new file mode 100644 index 0000000000000..6c34684f8189d --- /dev/null +++ b/src/cls/otp/cls_otp_client.h @@ -0,0 +1,54 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#ifndef CEPH_CLS_OTP_CLIENT_H +#define CEPH_CLS_OTP_CLIENT_H + +#include "cls/otp/cls_otp_types.h" + +namespace librados { + class ObjectWriteOperation; + class IoCtx; +} + +namespace rados { + namespace cls { + namespace otp { + + class OTP { + public: + static void create(librados::ObjectWriteOperation *op, const otp_info_t& config); + static void remove(librados::ObjectWriteOperation *op, const string& id); + static int get(librados::IoCtx& ioctx, const string& oid, + const list *ids, bool get_all, list *result); + static int get(librados::IoCtx& ioctx, const string& oid, + const string& id, otp_info_t *result); + static int get_all(librados::IoCtx& ioctx, const string& oid, + list *result); + static int check(CephContext *cct, librados::IoCtx& ioctx, const string& oid, + const string& id, const string& val, otp_check_t *result); + }; + + class TOTPConfig { + otp_info_t config; + public: + TOTPConfig(const string& id, const string& seed) { + config.type = OTP_TOTP; + config.id = id; + config.seed = seed; + } + void set_step_size(int step_size) { + config.step_size = step_size; + } + void set_window(int window) { + config.window = window; + } + void get_config(otp_info_t *conf) { + *conf = config; + } + }; + } // namespace otp + } // namespace cls +} // namespace rados + +#endif