]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cls/otp: add client interfaces
authorYehuda Sadeh <yehuda@redhat.com>
Thu, 16 Nov 2017 01:04:22 +0000 (17:04 -0800)
committerYehuda Sadeh <yehuda@redhat.com>
Mon, 9 Apr 2018 14:01:02 +0000 (07:01 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@redhat.com>
src/cls/CMakeLists.txt
src/cls/otp/cls_otp_client.cc [new file with mode: 0644]
src/cls/otp/cls_otp_client.h [new file with mode: 0644]

index c9cf3862a0d63e547afba4ac48c0f0e0c8d341a1..d6a401b405a250aff72eb96b7c6d0d3d57d5bf4b 100644 (file)
@@ -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 (file)
index 0000000..484b103
--- /dev/null
@@ -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 <sage@newdream.net>
+ *
+ * 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<string> *ids, bool get_all, list<otp_info_t> *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<string> ids{ id };
+        list<otp_info_t> 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<otp_info_t> *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 (file)
index 0000000..6c34684
--- /dev/null
@@ -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<string> *ids, bool get_all, list<otp_info_t> *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<otp_info_t> *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