Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / auth / KeyRing.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2004-2009 Sage Weil <sage@newdream.net>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software 
11  * Foundation.  See file COPYING.
12  * 
13  */
14
15 #ifndef CEPH_KEYRING_H
16 #define CEPH_KEYRING_H
17
18 #include "auth/Auth.h"
19
20 class CephContext;
21
22 class KeyRing : public KeyStore {
23   map<EntityName, EntityAuth> keys;
24
25   int set_modifier(const char *type, const char *val, EntityName& name, map<string, bufferlist>& caps);
26 public:
27   void decode_plaintext(bufferlist::iterator& bl);
28   /* Create a KeyRing from a Ceph context.
29    * We will use the configuration stored inside the context. */
30   int from_ceph_context(CephContext *cct);
31
32   /* Create an empty KeyRing */
33   static KeyRing *create_empty();
34
35   map<EntityName, EntityAuth>& get_keys() { return keys; }  // yuck
36
37   int load(CephContext *cct, const std::string &filename);
38   void print(ostream& out);
39
40   // accessors
41   bool get_auth(const EntityName& name, EntityAuth &a) const {
42     map<EntityName, EntityAuth>::const_iterator k = keys.find(name);
43     if (k == keys.end())
44       return false;
45     a = k->second;
46     return true;
47   }
48   bool get_secret(const EntityName& name, CryptoKey& secret) const override {
49     map<EntityName, EntityAuth>::const_iterator k = keys.find(name);
50     if (k == keys.end())
51       return false;
52     secret = k->second.key;
53     return true;
54   }
55   bool get_service_secret(uint32_t service_id, uint64_t secret_id,
56                           CryptoKey& secret) const override {
57     return false;
58   }
59   bool get_caps(const EntityName& name,
60                 const std::string& type, AuthCapsInfo& caps) const {
61     map<EntityName, EntityAuth>::const_iterator k = keys.find(name);
62     if (k == keys.end())
63       return false;
64     map<string,bufferlist>::const_iterator i = k->second.caps.find(type);
65     if (i != k->second.caps.end()) {
66       caps.caps = i->second;
67     }
68     return true;
69   }
70
71   // modifiers
72   void add(const EntityName& name, EntityAuth &a) {
73     keys[name] = a;
74   }
75   void add(const EntityName& name, CryptoKey &k) {
76     EntityAuth a;
77     a.key = k;
78     keys[name] = a;
79   }
80   void remove(const EntityName& name) {
81     keys.erase(name);
82   }
83   void set_caps(EntityName& name, map<string, bufferlist>& caps) {
84     keys[name].caps = caps;
85   }
86   void set_uid(EntityName& ename, uint64_t auid) {
87     keys[ename].auid = auid;
88   }
89   void set_key(EntityName& ename, CryptoKey& key) {
90     keys[ename].key = key;
91   }
92   void import(CephContext *cct, KeyRing& other);
93
94   // encoders
95   void decode(bufferlist::iterator& bl);
96
97   void encode_plaintext(bufferlist& bl);
98   void encode_formatted(string label, Formatter *f, bufferlist& bl);
99 };
100
101 // don't use WRITE_CLASS_ENCODER macro because we don't have an encode
102 // macro.  don't juse encode_plaintext in that case because it is not
103 // wrappable; it assumes it gets the entire bufferlist.
104 static inline void decode(KeyRing& kr, bufferlist::iterator& p) {
105   kr.decode(p);
106 }
107
108 #endif