libzypp 17.35.11
repoinfowf.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "repoinfowf.h"
17#include <zypp/KeyRing.h>
18#include <zypp/PublicKey.h>
19#include <zypp/ZYppCallbacks.h>
20
21#include <utility>
22#include <zypp-core/zyppng/pipelines/Transform>
23#include <zypp-core/zyppng/pipelines/Expected>
24#include <zypp-core/zyppng/pipelines/MTry>
25#include <zypp-media/ng/Provide>
26#include <zypp-media/ng/ProvideSpec>
27#include <zypp/MediaSetAccess.h>
28#include <zypp/ng/Context>
29#include <zypp/ng/UserRequest>
31
32#include <fstream>
33
34
35namespace zyppng {
36
37 namespace {
38
39 using namespace zyppng::operators;
40
41 template<class Executor, class OpType>
42 struct RepoInfoProvideKeyLogic : public LogicBase<Executor, OpType> {
43
44 using ZyppContextRefType = MaybeAsyncContextRef<OpType>;
45 using ZyppContextType = remove_smart_ptr_t<ZyppContextRefType>;
46 using ProvideType = typename ZyppContextType::ProvideType;
47 using MediaHandle = typename ProvideType::MediaHandle;
48 using ProvideRes = typename ProvideType::Res;
49
50 RepoInfoProvideKeyLogic( ZyppContextRefType &&zyppContext, zypp::RepoInfo &&info, std::string &&keyID_r, zypp::Pathname &&targetDirectory_r )
51 : _reports( std::move(zyppContext ))
52 , _info( std::move(info) )
53 , _keyID_r(std::move( keyID_r ))
54 , _targetDirectory_r(std::move( targetDirectory_r ))
55 , _keyIDStr( _keyID_r.size() > 8 ? _keyID_r.substr( _keyID_r.size()-8 ) : _keyID_r ) // print short ID in Jobreports
57 { }
58
59 ZYPP_ENABLE_LOGIC_BASE( Executor, OpType );
60
61 MaybeAsyncRef<zypp::filesystem::Pathname> execute () {
62 using namespace zyppng::operators;
63 using zyppng::operators::operator|;
64 using zyppng::expected;
65
66
67 if ( _keyID_r.empty() )
69
70 importKeysInTargetDir();
71
72 if ( _tempKeyRing.isKeyTrusted( _keyID_r) ) {
73 return makeReadyResult(writeKeysToTargetDir());
74 }
75
76 if ( _info.gpgKeyUrlsEmpty() ) {
77 // translator: %1% is a repositories name
78 _reports.info( zypp::str::Format(_("Repository %1% does not define additional 'gpgkey=' URLs.") ) % _info.asUserString() );
79 return makeReadyResult(writeKeysToTargetDir());
80 }
81
82 // no key in the cache is what we are looking for, lets download
83 // all keys specified in gpgkey= entries
84
85 // translator: %1% is a gpg key ID like 3DBDC284
86 // %2% is a repositories name
87 _reports.info( zypp::str::Format(_("Looking for gpg key ID %1% in repository %2%.") ) % _keyIDStr % _info.asUserString() );
88
89 return _info.gpgKeyUrls()
90 | transform( [this]( const zypp::Url &url ) {
91
92 _reports.info( " gpgkey=" + url.asString() );
93 return fetchKey( url )
94 | and_then( [this, url]( zypp::ManagedFile f ) -> expected<void> {
95 try {
96 if ( f->empty() )
97 return expected<void>::error(std::make_exception_ptr( zypp::Exception("Empty ManagedFile returned.") ));
98
99 zypp::PublicKey key(f);
100 if ( !key.isValid() )
101 return expected<void>::error(std::make_exception_ptr( zypp::Exception("Invalid public key.") ));
102
103 // import all keys into our temporary keyring
105
106 } catch ( const std::exception & e ) {
107 //ignore and continue to next url
108 ZYPP_CAUGHT(e);
109 MIL << "Key import from url:'"<<url<<"' failed." << std::endl;
111 }
112
114 });
115
116 })
117 | [this]( std::list<expected<void>> && ) ->zypp::Pathname {
118 return writeKeysToTargetDir();
119 };
120 }
121
122 protected:
123
124 MaybeAsyncRef<zyppng::expected<zypp::ManagedFile>> fetchKey ( const zypp::Url &url ) {
125 return _reports.zyppContext()->provider ()->provide( url, zyppng::ProvideFileSpec() )
126 | and_then( ProvideType::copyResultToDest( _reports.zyppContext()->provider(), _targetDirectory_r / zypp::Pathname( url.getPathName() ).basename() ) );
127 }
128
129 void importKeysInTargetDir () {
130 MIL << "Check for " << _keyID_r << " at " << _targetDirectory_r << std::endl;
131
132 // translator: %1% is a gpg key ID like 3DBDC284
133 // %2% is a cache directories path
134 _reports.info( zypp::str::Format(_("Looking for gpg key ID %1% in cache %2%.") ) % _keyIDStr % _targetDirectory_r );
135 zypp::filesystem::dirForEach( _targetDirectory_r,
137 [this]( const zypp::Pathname & dir_r, const std::string & str_r ){
138 try {
139
140 // deprecate a month old keys
141 zypp::PathInfo fileInfo ( dir_r/str_r );
142 if ( zypp::Date::now() - fileInfo.mtime() > zypp::Date::month ) {
143 //if unlink fails, the file will be overriden in the next step, no need
144 //to show a error
145 zypp::filesystem::unlink( dir_r/str_r );
146 } else {
147 _tempKeyRing.multiKeyImport(dir_r/str_r, true);
148 }
149 } catch (const zypp::KeyRingException& e) {
150 ZYPP_CAUGHT(e);
151 ERR << "Error importing cached key from file '"<<dir_r/str_r<<"'."<<std::endl;
152 }
153 return true;
154 });
155 }
156
157 zypp::Pathname writeKeysToTargetDir() {
158
159 zypp::filesystem::assert_dir( _targetDirectory_r );
160
161 //now write all keys into their own files in cache, override existing ones to always have
162 //up to date key data
163 for ( const auto & key: _tempKeyRing.trustedPublicKeyData()) {
164 MIL << "KEY ID in KEYRING: " << key.id() << std::endl;
165
166 zypp::Pathname keyFile = _targetDirectory_r/(zypp::str::Format("%1%.key") % key.rpmName()).asString();
167
168 std::ofstream fout( keyFile.c_str(), std::ios_base::out | std::ios_base::trunc );
169
170 if (!fout)
171 ZYPP_THROW(zypp::Exception(zypp::str::form("Cannot open file %s",keyFile.c_str())));
172
173 _tempKeyRing.dumpTrustedPublicKey( key.id(), fout );
174 }
175
176 // key is STILL not known, we give up
177 if ( !_tempKeyRing.isKeyTrusted( _keyID_r) ) {
178 return zypp::Pathname();
179 }
180
182 if ( !keyData ) {
183 ERR << "Error when exporting key from temporary keychain." << std::endl;
184 return zypp::Pathname();
185 }
186
187 return _targetDirectory_r/(zypp::str::Format("%1%.key") % keyData.rpmName()).asString();
188 }
189
192 const std::string _keyID_r;
194 const std::string _keyIDStr;
195
198 };
199
200
201 struct AsyncRepoInfoProvideKey : public RepoInfoProvideKeyLogic<AsyncRepoInfoProvideKey, zyppng::AsyncOp<zypp::Pathname>>
202 {
203 using RepoInfoProvideKeyLogic::RepoInfoProvideKeyLogic;
204 };
205
206 struct SyncRepoInfoProvideKey : public RepoInfoProvideKeyLogic<SyncRepoInfoProvideKey, zyppng::SyncOp<zypp::Pathname>>
207 {
208 using RepoInfoProvideKeyLogic::RepoInfoProvideKeyLogic;
209 };
210 }
211
212 zypp::filesystem::Pathname RepoInfoWorkflow::provideKey( SyncContextRef ctx, zypp::RepoInfo info, std::string keyID_r, zypp::filesystem::Pathname targetDirectory_r )
213 {
214 return SimpleExecutor<RepoInfoProvideKeyLogic, SyncOp<zypp::filesystem::Pathname>>::run( std::move(ctx), std::move(info), std::move(keyID_r), std::move(targetDirectory_r) );
215 }
216
218 {
219 return SimpleExecutor<RepoInfoProvideKeyLogic, AsyncOp<zypp::filesystem::Pathname>>::run( std::move(ctx), std::move(info), std::move(keyID_r), std::move(targetDirectory_r) );
220 }
221
222}
static const ValueType month
Definition Date.h:49
static Date now()
Return the current time.
Definition Date.h:78
Base class for Exception.
Definition Exception.h:147
Gpg key handling.
Definition KeyRing.h:187
void dumpTrustedPublicKey(const std::string &id, std::ostream &stream)
Definition KeyRing.h:237
void multiKeyImport(const Pathname &keyfile_r, bool trusted_r=false)
Initial import from RpmDb.
Definition KeyRing.cc:465
std::list< PublicKeyData > trustedPublicKeyData()
Get a list of trusted public key data in the keyring (key data only)
Definition KeyRing.cc:483
bool isKeyTrusted(const std::string &id)
true if the key id is trusted
Definition KeyRing.cc:506
@ STRINGEND
Match at string end.
Definition StrMatcher.h:45
Class representing one GPG Public Keys data.
Definition PublicKey.h:208
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition PublicKey.h:365
What is known about a repository.
Definition RepoInfo.h:72
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition StrMatcher.h:298
Url manipulation class.
Definition Url.h:92
std::string asString() const
Returns a default string representation of the Url object.
Definition Url.cc:501
std::string getPathName(EEncoding eflag=zypp::url::E_DECODED) const
Returns the path name from the URL.
Definition Url.cc:608
Wrapper class for stat/lstat.
Definition PathInfo.h:222
const char * c_str() const
String representation.
Definition Pathname.h:112
std::string basename() const
Return the last component of this path.
Definition Pathname.h:130
bool empty() const
Test for an empty path.
Definition Pathname.h:116
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:182
A ProvideRes object is a reference counted ownership of a resource in the cache provided by a Provide...
Definition provideres.h:36
static expected success(ConsParams &&...params)
Definition expected.h:115
#define ZYPP_ENABLE_LOGIC_BASE(Executor, OpType)
Definition Arch.h:364
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:705
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
Definition PathInfo.cc:32
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:324
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:37
std::string asString(const Patch::Category &obj)
Definition Patch.cc:122
zypp::Pathname provideKey(SyncContextRef ctx, zypp::RepoInfo info, std::string keyID_r, zypp::Pathname targetDirectory_r)
std::conditional_t< isAsync, AsyncOpRef< T >, T > makeReadyResult(T &&result)
Definition asyncop.h:297
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition asyncop.h:255
typename remove_smart_ptr< T >::type remove_smart_ptr_t
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition expected.h:423
std::conditional_t< detail::is_async_op_v< OpType >, ContextRef, SyncContextRef > MaybeAsyncContextRef
Container< Ret > transform(Container< Msg, CArgs... > &&val, Transformation &&transformation)
Definition transform.h:31
zypp::filesystem::TmpDir _tmpKeyRingDir
zypp::KeyRing _tempKeyRing
const zypp::Pathname _targetDirectory_r
const std::string _keyIDStr
JobReportHelper< ZyppContextRefType > _reports
const std::string _keyID_r
RepoInfo _info
Convenient building of std::string with boost::format.
Definition String.h:253
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:440
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition Exception.h:436
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:424
#define _(MSG)
Definition Gettext.h:39
#define MIL
Definition Logger.h:98
#define ERR
Definition Logger.h:100