Author: jkupec
Date: Wed Sep 17 17:11:47 2008
New Revision: 11083
URL: http://svn.opensuse.org/viewcvs/zypp?rev=11083&view=rev
Log:
- new CredentialManager Impl
- INI file format of credential files
- uses /etc/zypp/credentials.cat, ~/.zypp/credentials.cat for
credentials with URLs, and /etc/zypp/credentials.d/* for credentials
without URL (specified via ?credentials=file URL parameter)
(todo: make the locations configurable via ZConfig)
- todo: some more convenient methods
Added:
trunk/libzypp/tests/media/data/credentials.cat
trunk/libzypp/tests/media/data/credentials.d/
trunk/libzypp/tests/media/data/credentials.d/cred1 (contents, props changed)
- copied, changed from r11077, trunk/libzypp/tests/media/data/credentials
Removed:
trunk/libzypp/tests/media/data/credentials
Modified:
trunk/libzypp/tests/media/CredentialFileReader_test.cc
trunk/libzypp/tests/media/CredentialManager_test.cc
trunk/libzypp/zypp/media/CredentialFileReader.cc
trunk/libzypp/zypp/media/CredentialManager.cc
trunk/libzypp/zypp/media/CredentialManager.h
trunk/libzypp/zypp/media/MediaUserAuth.cc
trunk/libzypp/zypp/media/MediaUserAuth.h
Modified: trunk/libzypp/tests/media/CredentialFileReader_test.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/media/CredentialFileReader_test.cc?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/tests/media/CredentialFileReader_test.cc (original)
+++ trunk/libzypp/tests/media/CredentialFileReader_test.cc Wed Sep 17 17:11:47 2008
@@ -3,12 +3,13 @@
#include <set>
#include "zypp/Url.h"
+#include "zypp/PathInfo.h"
+#include "zypp/base/Easy.h"
#include "zypp/media/MediaUserAuth.h"
#include "zypp/media/CredentialFileReader.h"
-using std::cout;
-using std::endl;
+using namespace std;
using namespace zypp;
using namespace zypp::media;
@@ -29,8 +30,9 @@
BOOST_AUTO_TEST_CASE(read_cred)
{
CredCollector collector;
- CredentialFileReader reader(TESTS_SRC_DIR "/media/data/credentials",
- bind( &CredCollector::collect, &collector, _1 ));
+ Pathname credfile = TESTS_SRC_DIR "/media/data/credentials.cat";
+ CredentialFileReader reader(credfile,
+ bind( &CredCollector::collect, &collector, _1 ));
BOOST_CHECK(collector.creds.size() == 2);
}
Modified: trunk/libzypp/tests/media/CredentialManager_test.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/media/CredentialManager_test.cc?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/tests/media/CredentialManager_test.cc (original)
+++ trunk/libzypp/tests/media/CredentialManager_test.cc Wed Sep 17 17:11:47 2008
@@ -2,9 +2,12 @@
#include
#include "zypp/Url.h"
-
+#include "zypp/TmpPath.h"
+#include "zypp/media/CredentialFileReader.cc"
#include "zypp/media/CredentialManager.h"
+#include "zypp/PathInfo.h"
+
using std::cout;
using std::endl;
using namespace zypp;
@@ -14,7 +17,7 @@
BOOST_AUTO_TEST_CASE(read_cred_for_url)
{
CredManagerOptions opts;
- opts.globalCredFilePath = TESTS_SRC_DIR "/media/data/credentials";
+ opts.globalCredFilePath = TESTS_SRC_DIR "/media/data/credentials.cat";
opts.userCredFilePath = Pathname();
CredentialManager cm(opts);
@@ -22,21 +25,65 @@
AuthData_Ptr credentials = cm.getCred(url);
-/*
- cout << "credentials:";
- if (credentials)
- cout << *credentials;
- else
- cout << "(null)";
- cout << endl;
-*/
-
BOOST_CHECK(credentials->username() == "ginger");
BOOST_CHECK(credentials->password() == "ale");
-
- Url url2("ftp://maria@weprovidesoft.fr/download/opensuse/110");
+
+ Url url2("ftp://magda@weprovidesoft.fr/download/opensuse/110");
+
credentials = cm.getCred(url2);
+ BOOST_CHECK(credentials.get() != NULL);
+ if (!credentials)
+ return;
- BOOST_CHECK(credentials->username() == "maria");
- BOOST_CHECK(credentials->password() == "antoin");
+ BOOST_CHECK(credentials->username() == "magda");
+ BOOST_CHECK(credentials->password() == "richard");
}
+
+struct CredCollector
+{
+ bool collect(AuthData_Ptr & cred)
+ {
+ cout << "got: " << endl << *cred << endl;
+ creds.insert(cred);
+ return true;
+ }
+
+ CredentialManager::CredentialSet creds;
+};
+
+BOOST_AUTO_TEST_CASE(save_creds)
+{
+ filesystem::TmpDir tmp;
+
+ CredManagerOptions opts;
+ opts.globalCredFilePath = tmp / "fooha";
+
+
+ CredentialManager cm1(opts);
+ AuthData cr1("benson","absolute");
+ cr1.setUrl(Url("http://joooha.com"));
+ AuthData cr2("pat","vymetheny");
+ cr2.setUrl(Url("ftp://filesuck.org"));
+
+ cm1.saveInGlobal(cr1);
+
+ CredCollector collector;
+ CredentialFileReader reader(opts.globalCredFilePath,
+ bind( &CredCollector::collect, &collector, _1 ));
+
+ BOOST_CHECK(collector.creds.size() == 1);
+ collector.creds.clear();
+
+
+ cm1.saveInGlobal(cr2);
+
+ filesystem::copy(opts.globalCredFilePath, "/home/jkupec/tmp/foo");
+
+ CredentialFileReader reader1(opts.globalCredFilePath,
+ bind( &CredCollector::collect, &collector, _1 ));
+
+ BOOST_CHECK(collector.creds.size() == 2);
+ collector.creds.clear();
+ // todo save the same creds.
+ // todo check created file permissions
+}
\ No newline at end of file
Added: trunk/libzypp/tests/media/data/credentials.cat
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/media/data/credentials.cat?rev=11083&view=auto
==============================================================================
--- trunk/libzypp/tests/media/data/credentials.cat (added)
+++ trunk/libzypp/tests/media/data/credentials.cat Wed Sep 17 17:11:47 2008
@@ -0,0 +1,15 @@
+[https://drink.it/repo/roots]
+username=ginger
+password=ale
+
+[ftp://weprovidesoft.fr/download/opensuse/110]
+username=magda
+password=richard
+
+[http://url.ok/but/not/creds]
+username=
+password=any
+
+[badurl]
+username=foo
+password=bar
Copied: trunk/libzypp/tests/media/data/credentials.d/cred1 (from r11077, trunk/libzypp/tests/media/data/credentials)
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/media/data/credentials.d/cred1?p2=trunk/libzypp/tests/media/data/credentials.d/cred1&p1=trunk/libzypp/tests/media/data/credentials&r1=11077&r2=11083&rev=11083&view=diff
==============================================================================
--- trunk/libzypp/tests/media/data/credentials (original)
+++ trunk/libzypp/tests/media/data/credentials.d/cred1 Wed Sep 17 17:11:47 2008
@@ -1,4 +1,2 @@
-https://ginger:ale@drink.it/repo/roots
-
-weirdo@invalid.record
-ftp://maria:antoin@weprovidesoft.fr/download/opensuse/110
\ No newline at end of file
+username=helene
+password=elena
Modified: trunk/libzypp/zypp/media/CredentialFileReader.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/media/CredentialFileReader.cc?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/zypp/media/CredentialFileReader.cc (original)
+++ trunk/libzypp/zypp/media/CredentialFileReader.cc Wed Sep 17 17:11:47 2008
@@ -13,7 +13,7 @@
#include "zypp/base/Logger.h"
#include "zypp/base/InputStream.h"
-#include "zypp/base/IOStream.h"
+#include "zypp/parser/IniDict.h"
#include "zypp/media/CredentialFileReader.h"
@@ -38,29 +38,51 @@
const ProcessCredentials & callback)
{
InputStream is(crfile);
-
- for(iostr::EachLine in(is); in; in.next())
+ parser::IniDict dict(is);
+ for (parser::IniDict::section_const_iterator its = dict.sectionsBegin();
+ its != dict.sectionsEnd();
+ ++its)
{
- try
+ Url storedUrl;
+ if (!its->empty())
{
- Url storedUrl(*in);
-
- AuthData_Ptr credentials;
- credentials.reset(
- new AuthData(storedUrl));
+ try { storedUrl = Url(*its); }
+ catch (const url::UrlException &)
+ {
+ ERR << "invalid URL '" << *its << "' in credentials in file: "
+ << crfile << endl;
+ continue;
+ }
+ }
- if (credentials->valid())
- callback(credentials);
+ AuthData_Ptr credentials;
+ credentials.reset(new AuthData());
+
+ // set url
+ if (storedUrl.isValid())
+ credentials->setUrl(storedUrl);
+
+ for (parser::IniDict::entry_const_iterator it = dict.entriesBegin(*its);
+ it != dict.entriesEnd(*its);
+ ++it)
+ {
+ if (it->first == "username")
+ credentials->setUserName(it->second);
+ else if (it->first == "password")
+ credentials->setPassword(it->second);
else
- // report invalid record
- DBG << "invalid record: " << *in << endl;
+ ERR << "Unknown attribute in [" << crfile << "]: "
+ << it->second << " ignored" << endl;
}
- catch (const url::UrlException &)
- {} // not a URL
- //! \todo this will need to be a bit more sophisticated to be able to pinpoint bad records
- }
+
+ if (credentials->valid())
+ callback(credentials);
+ else
+ ERR << "invalid credentials in file: " << crfile << endl;
+ } // sections
}
+
CredentialFileReader::~CredentialFileReader()
{}
Modified: trunk/libzypp/zypp/media/CredentialManager.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/media/CredentialManager.cc?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/zypp/media/CredentialManager.cc (original)
+++ trunk/libzypp/zypp/media/CredentialManager.cc Wed Sep 17 17:11:47 2008
@@ -10,14 +10,21 @@
*
*/
#include <iostream>
+#include <fstream>
#include "zypp/base/Function.h"
#include "zypp/base/Logger.h"
+#include "zypp/base/Easy.h"
+#include "zypp/PathInfo.h"
#include "zypp/media/CredentialFileReader.h"
#include "zypp/media/CredentialManager.h"
+#define CUSTOM_CREDENTIALS_FILE_DIR "/etc/zypp/credentials.d"
+#define GLOBAL_CREDENTIALS_FILE "/etc/zypp/credentials.cat"
+#define USER_CREDENTIALS_FILE ".zypp/credentials.cat"
+
using namespace std;
//////////////////////////////////////////////////////////////////////
@@ -35,11 +42,12 @@
//////////////////////////////////////////////////////////////////////
CredManagerOptions::CredManagerOptions(const Pathname & rootdir)
- : globalCredFilePath(rootdir / "/etc/zypp/credentials")
+ : globalCredFilePath(rootdir / GLOBAL_CREDENTIALS_FILE)
+ , customCredFileDir(rootdir / CUSTOM_CREDENTIALS_FILE_DIR)
{
char * homedir = getenv("HOME");
if (homedir)
- userCredFilePath = rootdir / homedir / ".zypp/credentials";
+ userCredFilePath = rootdir / homedir / USER_CREDENTIALS_FILE;
}
@@ -53,11 +61,17 @@
~Impl()
{}
-
+ void init_globalCredentials();
+ void init_userCredentials();
+
bool processCredentials(AuthData_Ptr & cred);
- AuthData_Ptr getCred(const Url & url);
+ AuthData_Ptr getCred(const Url & url) const;
+ AuthData_Ptr getCredFromFile(const Pathname & file);
+ void saveGlobalCredentials();
+ void saveUserCredentials();
+
CredManagerOptions _options;
@@ -77,20 +91,53 @@
CredentialManager::Impl::Impl(const CredManagerOptions & options)
: _options(options)
{
- CredentialFileReader(
- _options.globalCredFilePath,
- bind(&Impl::processCredentials, this, _1));
+ init_globalCredentials();
+ init_userCredentials();
+ }
+
+
+ void CredentialManager::Impl::init_globalCredentials()
+ {
+ if (_options.globalCredFilePath.empty())
+ DBG << "global cred file not known";
+ else if (PathInfo(_options.globalCredFilePath).isExist())
+ {
+ /* list<Pathname> entries;
+ if (filesystem::readdir(entries, _options.globalCredFilePath, false) != 0)
+ ZYPP_THROW(Exception("failed to read directory"));
+
+ for_(it, entries.begin(), entries.end())*/
+
+ CredentialFileReader(_options.globalCredFilePath,
+ bind(&Impl::processCredentials, this, _1));
+ }
+ else
+ DBG << "global cred file does not exist";
+
_credsGlobal = _credsTmp; _credsTmp.clear();
DBG << "Got " << _credsGlobal.size() << " global records." << endl;
+ }
- if (!_options.userCredFilePath.empty())
+
+ void CredentialManager::Impl::init_userCredentials()
+ {
+ if (_options.userCredFilePath.empty())
+ DBG << "user cred file not known";
+ else if (PathInfo(_options.userCredFilePath).isExist())
{
- CredentialFileReader(
- _options.userCredFilePath,
+ /* list<Pathname> entries;
+ if (filesystem::readdir(entries, _options.userCredFilePath, false ) != 0)
+ ZYPP_THROW(Exception("failed to read directory"));
+
+ for_(it, entries.begin(), entries.end())*/
+ CredentialFileReader(_options.userCredFilePath,
bind(&Impl::processCredentials, this, _1));
- _credsUser = _credsTmp; _credsTmp.clear();
- DBG << "Got " << _credsUser.size() << " user records." << endl;
}
+ else
+ DBG << "user cred file does not exist";
+
+ _credsUser = _credsTmp; _credsTmp.clear();
+ DBG << "Got " << _credsUser.size() << " user records." << endl;
}
@@ -100,6 +147,7 @@
return true;
}
+
static AuthData_Ptr findIn(const CredentialManager::CredentialSet & set,
const Url & url,
url::ViewOption vopt)
@@ -114,7 +162,7 @@
}
- AuthData_Ptr CredentialManager::Impl::getCred(const Url & url)
+ AuthData_Ptr CredentialManager::Impl::getCred(const Url & url) const
{
AuthData_Ptr result;
@@ -124,7 +172,7 @@
// if the wanted URL does not contain username, ignore that, too
url::ViewOption vopt;
- if (url.getUsername().empty())
+// if (url.getUsername().empty())
vopt = vopt - url::ViewOption::WITH_USERNAME;
// search in global credentials
@@ -143,6 +191,58 @@
}
+ AuthData_Ptr CredentialManager::Impl::getCredFromFile(const Pathname & file)
+ {
+ AuthData_Ptr result;
+
+ Pathname credfile;
+ if (file.absolute())
+ // get from that file
+ credfile = file;
+ else
+ // get from /etc/zypp/credentials.d
+ credfile = _options.customCredFileDir / file;
+
+ CredentialFileReader(credfile, bind(&Impl::processCredentials, this, _1));
+ if (_credsTmp.empty())
+ WAR << file << " does not contain valid credentials or is not readable." << endl;
+ else
+ {
+ result = *_credsTmp.begin();
+ _credsTmp.clear();
+ }
+
+ return result;
+ }
+
+ static void save_creds_in_file(
+ const CredentialManager::CredentialSet creds,
+ const Pathname & file)
+ {
+ filesystem::assert_dir(file.dirname());
+
+ std::ofstream fs(file.c_str());
+ if (!fs)
+ ZYPP_THROW(Exception("Can't open " + file.asString()));
+
+ for_(it, creds.begin(), creds.end())
+ {
+ (*it)->dumpAsIniOn(fs);
+ fs << endl;
+ }
+ }
+
+ void CredentialManager::Impl::saveGlobalCredentials()
+ {
+ save_creds_in_file(_credsGlobal, _options.globalCredFilePath);
+ }
+
+ void CredentialManager::Impl::saveUserCredentials()
+ {
+ save_creds_in_file(_credsUser, _options.userCredFilePath);
+ }
+
+
//////////////////////////////////////////////////////////////////////
//
// CLASS NAME : CredentialManager
@@ -156,18 +256,26 @@
AuthData_Ptr CredentialManager::getCred(const Url & url)
{ return _pimpl->getCred(url); }
+ AuthData_Ptr CredentialManager::getCredFromFile(const Pathname & file)
+ { return _pimpl->getCredFromFile(file); }
void CredentialManager::save(const AuthData & cred, bool global)
{ global ? saveInGlobal(cred) : saveInUser(cred); }
void CredentialManager::saveInGlobal(const AuthData & cred)
{
- //! \todo
+ AuthData_Ptr c_ptr;
+ c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
+ _pimpl->_credsGlobal.insert(c_ptr); //! \todo avoid adding duplicates
+ _pimpl->saveGlobalCredentials();
}
void CredentialManager::saveInUser(const AuthData & cred)
{
- //! \todo
+ AuthData_Ptr c_ptr;
+ c_ptr.reset(new AuthData(cred)); // FIX for child classes if needed
+ _pimpl->_credsUser.insert(c_ptr); //! \todo avoid adding duplicates
+ _pimpl->saveUserCredentials();
}
void saveIn(const AuthData &, const Pathname & credFile)
@@ -175,7 +283,6 @@
//! \todo
}
-
CredentialManager::CredentialIterator CredentialManager::credsGlobalBegin() const
{ return _pimpl->_credsGlobal.begin(); }
Modified: trunk/libzypp/zypp/media/CredentialManager.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/media/CredentialManager.h?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/zypp/media/CredentialManager.h (original)
+++ trunk/libzypp/zypp/media/CredentialManager.h Wed Sep 17 17:11:47 2008
@@ -36,6 +36,7 @@
Pathname globalCredFilePath;
Pathname userCredFilePath;
+ Pathname customCredFileDir;
};
//////////////////////////////////////////////////////////////////////
@@ -71,6 +72,11 @@
* AuthData_Ptr otherwise.
*/
AuthData_Ptr getCred(const Url & url);
+
+ /**
+ *
+ */
+ AuthData_Ptr getCredFromFile(const Pathname & file);
void save(const AuthData &, bool global = false);
@@ -89,6 +95,11 @@
*
*/
void saveIn(const AuthData &, const Pathname & credFile);
+
+ /**
+ *
+ */
+ void clear(bool global = false) {}
CredentialIterator credsGlobalBegin() const;
CredentialIterator credsGlobalEnd() const;
Modified: trunk/libzypp/zypp/media/MediaUserAuth.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/media/MediaUserAuth.cc?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/zypp/media/MediaUserAuth.cc (original)
+++ trunk/libzypp/zypp/media/MediaUserAuth.cc Wed Sep 17 17:11:47 2008
@@ -20,6 +20,8 @@
#include "zypp/media/MediaUserAuth.h"
+using namespace std;
+
namespace zypp {
namespace media {
@@ -45,6 +47,22 @@
return str;
}
+std::ostream & AuthData::dumpAsIniOn( std::ostream & str ) const
+{
+ if (_url.isValid())
+ str
+ << "[" << _url.asString(
+ url::ViewOptions()
+ - url::ViewOptions::WITH_USERNAME
+ - url::ViewOptions::WITH_PASSWORD)
+ << "]" << endl;
+
+ str
+ << "username = " << _username << endl
+ << "password = " << _password << endl;
+
+ return str;
+}
bool CurlAuthData::valid() const
{
Modified: trunk/libzypp/zypp/media/MediaUserAuth.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/media/MediaUserAuth.h?rev=11083&r1=11082&r2=11083&view=diff
==============================================================================
--- trunk/libzypp/zypp/media/MediaUserAuth.h (original)
+++ trunk/libzypp/zypp/media/MediaUserAuth.h Wed Sep 17 17:11:47 2008
@@ -14,6 +14,7 @@
#include
+#include "zypp/Url.h"
#include "zypp/base/PtrTypes.h"
namespace zypp {
@@ -34,7 +35,7 @@
AuthData(const Url & url);
- AuthData(std::string & username, std::string & password)
+ AuthData(const std::string & username, const std::string & password)
: _username(username), _password(password)
{}
@@ -57,6 +58,7 @@
virtual std::ostream & dumpOn( std::ostream & str ) const;
+ virtual std::ostream & dumpAsIniOn( std::ostream & str ) const;
private:
Url _url;
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org