From ab75f2ab76edf9c9b10349e33d949dde3e63d3e2 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 15 Jun 2011 11:34:55 -0700 Subject: [PATCH] auth: KeyRing, RotatingKeyRing: deglobalize Signed-off-by: Colin McCabe --- src/auth/KeyRing.cc | 38 +++++++++++++++++++------------------ src/auth/KeyRing.h | 9 +++++---- src/auth/RotatingKeyRing.cc | 8 ++++---- src/auth/RotatingKeyRing.h | 4 +++- src/cauthtool.cc | 2 +- src/mon/AuthMonitor.cc | 2 +- src/mon/MonClient.cc | 4 ++-- 7 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/auth/KeyRing.cc b/src/auth/KeyRing.cc index 5f21a1d3e3a60..71caef42743a4 100644 --- a/src/auth/KeyRing.cc +++ b/src/auth/KeyRing.cc @@ -33,23 +33,24 @@ using std::auto_ptr; using namespace std; KeyRing *KeyRing:: -from_ceph_conf(const md_config_t *conf) +from_ceph_context(CephContext *cct) { + const md_config_t *conf = cct->_conf; bool found_key = false; auto_ptr < KeyRing > keyring(new KeyRing()); if (!is_supported_auth(CEPH_AUTH_CEPHX)) { - dout(2) << "KeyRing::from_ceph_conf: CephX auth is not supported." << dendl; + ldout(cct, 2) << "KeyRing::from_ceph_context: CephX auth is not supported." << dendl; return keyring.release(); } int ret = 0; string filename; if (ceph_resolve_file_search(conf->keyring, filename)) { - ret = keyring->load(filename); + ret = keyring->load(cct, filename); if (ret) { - derr << "KeyRing::from_ceph_conf: failed to load " << filename - << ": error " << ret << dendl; + lderr(cct) << "KeyRing::from_ceph_context: failed to load " << filename + << ": error " << ret << dendl; } else { found_key = true; @@ -70,8 +71,8 @@ from_ceph_conf(const md_config_t *conf) int res = fread(buf, 1, sizeof(buf) - 1, fp); if (res < 0) { res = ferror(fp); - derr << "KeyRing::from_ceph_conf: failed to read '" << conf->keyfile - << "'" << dendl; + lderr(cct) << "KeyRing::from_ceph_conf: failed to read '" << conf->keyfile + << "'" << dendl; } else { string k = buf; @@ -143,8 +144,7 @@ void KeyRing::decode_plaintext(bufferlist::iterator& bli) ConfFile cf; std::deque parse_errors; if (cf.parse_bufferlist(&bl, &parse_errors) != 0) { - derr << "cannot parse buffer" << dendl; - throw buffer::error(); + throw buffer::malformed_input("cannot parse buffer"); } for (ConfFile::const_section_iter_t s = cf.sections_begin(); @@ -156,8 +156,9 @@ void KeyRing::decode_plaintext(bufferlist::iterator& bli) EntityName ename; map caps; if (!ename.from_str(name)) { - derr << "bad entity name: " << name << dendl; - throw buffer::error(); + ostringstream oss; + oss << "bad entity name: " << name; + throw buffer::malformed_input(oss.str().c_str()); } for (ConfSection::const_line_iter_t l = s->second.lines.begin(); @@ -168,9 +169,10 @@ void KeyRing::decode_plaintext(bufferlist::iterator& bli) std::replace(k.begin(), k.end(), '_', ' '); ret = set_modifier(k.c_str(), l->val.c_str(), ename, caps); if (ret < 0) { - derr << "error setting modifier for [" << name << "] type=" << k - << " val=" << l->val << dendl; - throw buffer::error(); + ostringstream oss; + oss << "error setting modifier for [" << name << "] type=" << k + << " val=" << l->val; + throw buffer::malformed_input(oss.str().c_str()); } } } @@ -188,7 +190,7 @@ void KeyRing::decode(bufferlist::iterator& bl) { } } -int KeyRing::load(const std::string &filename) +int KeyRing::load(CephContext *cct, const std::string &filename) { if (filename.empty()) return -EINVAL; @@ -209,7 +211,7 @@ int KeyRing::load(const std::string &filename) derr << "error parsing file " << filename << dendl; } - dout(2) << "KeyRing::load: loaded key file " << filename << dendl; + ldout(cct, 2) << "KeyRing::load: loaded key file " << filename << dendl; return 0; } @@ -233,12 +235,12 @@ void KeyRing::print(ostream& out) } } -void KeyRing::import(KeyRing& other) +void KeyRing::import(CephContext *cct, KeyRing& other) { for (map::iterator p = other.keys.begin(); p != other.keys.end(); ++p) { - dout(10) << " importing " << p->first << " " << p->second << dendl; + ldout(cct, 10) << " importing " << p->first << " " << p->second << dendl; keys[p->first] = p->second; } } diff --git a/src/auth/KeyRing.h b/src/auth/KeyRing.h index b02a9c664d591..08b6d8bb03829 100644 --- a/src/auth/KeyRing.h +++ b/src/auth/KeyRing.h @@ -28,14 +28,15 @@ class KeyRing { int set_modifier(const char *type, const char *val, EntityName& name, map& caps); void decode_plaintext(bufferlist::iterator& bl); public: - /* Create a KeyRing from a Ceph configuration */ - static KeyRing *from_ceph_conf(const md_config_t *conf); + /* Create a KeyRing from a Ceph context. + * We will use the configuration stored inside the context. */ + static KeyRing *from_ceph_context(CephContext *cct); /* Create an empty KeyRing */ static KeyRing *create_empty(); map& get_keys() { return keys; } // yuck - int load(const std::string &filename); + int load(CephContext *cct, const std::string &filename); void print(ostream& out); // accessors @@ -70,7 +71,7 @@ public: void set_key(EntityName& ename, CryptoKey& key) { keys[ename].key = key; } - void import(KeyRing& other); + void import(CephContext *cct, KeyRing& other); // encoders void encode(bufferlist& bl) const { diff --git a/src/auth/RotatingKeyRing.cc b/src/auth/RotatingKeyRing.cc index d00a7d601daa8..9ded2901c9d50 100644 --- a/src/auth/RotatingKeyRing.cc +++ b/src/auth/RotatingKeyRing.cc @@ -34,11 +34,11 @@ void RotatingKeyRing::set_secrets(RotatingSecrets& s) void RotatingKeyRing::dump_rotating() const { - dout(10) << "dump_rotating:" << dendl; + ldout(cct, 10) << "dump_rotating:" << dendl; for (map::const_iterator iter = secrets.secrets.begin(); iter != secrets.secrets.end(); ++iter) - dout(10) << " id " << iter->first << " " << iter->second << dendl; + ldout(cct, 10) << " id " << iter->first << " " << iter->second << dendl; } bool RotatingKeyRing::get_secret(const EntityName& name, CryptoKey& secret) const @@ -53,7 +53,7 @@ bool RotatingKeyRing::get_service_secret(uint32_t service_id_, uint64_t secret_i Mutex::Locker l(lock); if (service_id_ != this->service_id) { - dout(0) << "do not have service " << ceph_entity_type_name(service_id_) + ldout(cct, 0) << "do not have service " << ceph_entity_type_name(service_id_) << ", i am " << ceph_entity_type_name(this->service_id) << dendl; return false; } @@ -61,7 +61,7 @@ bool RotatingKeyRing::get_service_secret(uint32_t service_id_, uint64_t secret_i map::const_iterator iter = secrets.secrets.find(secret_id); if (iter == secrets.secrets.end()) { - dout(0) << "could not find secret_id=" << secret_id << dendl; + ldout(cct, 0) << "could not find secret_id=" << secret_id << dendl; dump_rotating(); return false; } diff --git a/src/auth/RotatingKeyRing.h b/src/auth/RotatingKeyRing.h index a088d995d0a9d..efcb6382c7fbc 100644 --- a/src/auth/RotatingKeyRing.h +++ b/src/auth/RotatingKeyRing.h @@ -28,13 +28,15 @@ class KeyRing; class RotatingKeyRing : public KeyStore { + CephContext *cct; uint32_t service_id; RotatingSecrets secrets; KeyRing *keyring; mutable Mutex lock; public: - RotatingKeyRing(uint32_t s, KeyRing *kr) : + RotatingKeyRing(CephContext *cct_, uint32_t s, KeyRing *kr) : + cct(cct_), service_id(s), keyring(kr), lock("RotatingKeyRing::lock") {} diff --git a/src/cauthtool.cc b/src/cauthtool.cc index 0ea02cfec2add..333f8812dec46 100644 --- a/src/cauthtool.cc +++ b/src/cauthtool.cc @@ -178,7 +178,7 @@ int main(int argc, const char **argv) cout << "importing contents of " << import_keyring << " into " << fn << std::endl; //other.print(cout); - keyring.import(other); + keyring.import(&g_ceph_context, other); modified = true; } else { cerr << "can't open " << import_keyring << ": " << err << std::endl; diff --git a/src/mon/AuthMonitor.cc b/src/mon/AuthMonitor.cc index 5a0b88ebcb908..0a64440e4be8c 100644 --- a/src/mon/AuthMonitor.cc +++ b/src/mon/AuthMonitor.cc @@ -94,7 +94,7 @@ void AuthMonitor::create_initial(bufferlist& bl) dout(10) << "create_initial -- creating initial map" << dendl; KeyRing keyring; - if (keyring.load(g_conf->keyring) == 0) { + if (keyring.load(&g_ceph_context, g_conf->keyring) == 0) { import_keyring(keyring); } diff --git a/src/mon/MonClient.cc b/src/mon/MonClient.cc index 792d9a8b7b4bd..3d44b876bbc79 100644 --- a/src/mon/MonClient.cc +++ b/src/mon/MonClient.cc @@ -345,12 +345,12 @@ int MonClient::init() messenger->add_dispatcher_head(this); - keyring = KeyRing::from_ceph_conf(cct->_conf); + keyring = KeyRing::from_ceph_context(cct); if (!keyring) { derr << "MonClient::init(): Failed to create keyring" << dendl; return -EDOM; } - rotating_secrets = new RotatingKeyRing(cct->get_module_type(), keyring); + rotating_secrets = new RotatingKeyRing(cct, cct->get_module_type(), keyring); entity_name = g_conf->name; -- 2.39.5