libzypp  17.37.5
repoinfowf.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 #include "repoinfowf.h"
10 #include <zypp/ng/reporthelper.h>
13 #include <zypp-core/ManagedFile.h>
14 #include <zypp-core/base/String.h>
15 #include <zypp-core/base/Gettext.h>
16 #include <zypp-core/fs/TmpPath.h>
17 #include <zypp/base/StrMatcher.h>
18 #include <zypp/KeyRing.h>
19 #include <zypp-common/PublicKey.h>
20 #include <zypp/ZYppCallbacks.h>
21 
22 #include <utility>
23 #include <zypp-core/zyppng/pipelines/Transform>
24 #include <zypp-core/zyppng/pipelines/Expected>
25 #include <zypp-core/zyppng/pipelines/MTry>
26 #include <zypp-media/ng/Provide>
27 #include <zypp-media/ng/ProvideSpec>
28 #include <zypp/MediaSetAccess.h>
29 #include <zypp/ng/Context>
30 #include <zypp/ng/UserRequest>
32 
33 namespace zyppng {
34 
35  namespace {
36 
37  using namespace zyppng::operators;
38 
39  template<class Executor, class OpType>
40  struct FetchGpgKeysLogic : public LogicBase<Executor, OpType> {
41 
42  using ZyppContextRefType = MaybeAsyncContextRef<OpType>;
43  using ZyppContextType = remove_smart_ptr_t<ZyppContextRefType>;
44  using ProvideType = typename ZyppContextType::ProvideType;
45  using MediaHandle = typename ProvideType::MediaHandle;
46  using ProvideRes = typename ProvideType::Res;
47 
48  FetchGpgKeysLogic( ZyppContextRefType &&zyppContext, zypp::RepoInfo &&info )
49  : _reports( std::move(zyppContext ))
50  , _info( std::move(info) )
51  { }
52 
53  ZYPP_ENABLE_LOGIC_BASE( Executor, OpType );
54 
55  MaybeAsyncRef<expected<void>> execute () {
56  using namespace zyppng::operators;
57  using zyppng::operators::operator|;
58  using zyppng::expected;
59 
61 
62  if ( gpgKeyUrls.empty() ) {
63  if ( !_info.baseUrlsEmpty()
65 
66  MIL << "No gpgkey URL specified, but d.o.o server detected. Trying to generate the key file path." << std::endl;
67 
68  zypp::Url bUrl = *_info.baseUrlsBegin();
70  switch( rType ) {
72  bUrl.appendPathName( _info.path() / "/repodata/repomd.xml.key" );
73  gpgKeyUrls.push_back( bUrl );
74  break;
76  bUrl.appendPathName( _info.path() / "/content.key" );
77  gpgKeyUrls.push_back( bUrl );
78  break;
81  MIL << "Repo type is not known, unable to generate the gpgkey Url on the fly." << std::endl;
82  break;
83  }
84  }
85  }
86 
87  if ( gpgKeyUrls.empty () )
89  }
90 
91  _keysDownloaded.clear();
92 
93  // no key in the cache is what we are looking for, lets download
94  // all keys specified in gpgkey= entries
95 
96  // translator: %1% is a repositories name
97  _reports.info( zypp::str::Format(_("Looking for gpg keys in repository %1%.") ) % _info.asUserString() );
98 
99  return std::move(gpgKeyUrls)
100  | transform( [this]( const zypp::Url &url ) {
101 
102  _reports.info( " gpgkey=" + url.asString() );
103  return _reports.zyppContext()->provider ()->provide( url, zyppng::ProvideFileSpec() )
104  | and_then( [this, url]( ProvideRes f ) -> expected<void> {
105  try {
106  zypp::PublicKey key(f.file());
107  if ( !key.isValid() )
108  return expected<void>::error(std::make_exception_ptr( zypp::Exception("Invalid public key.") ));
109 
110  // import all keys into our keyring
111  _reports.zyppContext()->keyRing()->multiKeyImport(f.file(), false);
112 
113  } catch ( const std::exception & e ) {
114  //ignore and continue to next url
115  ZYPP_CAUGHT(e);
116  MIL << "Key import from url:'"<<url<<"' failed." << std::endl;
118  }
119 
120  return expected<void>::success();
121  });
122  } )
123  | [this]( std::list<expected<void>> && ) {
124  return expected<void>::success();
125  };
126  }
127 
128  protected:
129  JobReportHelper<ZyppContextRefType> _reports;
131  std::set<std::string> _keysDownloaded;
132  };
133 
134  }
135 
137  {
138  return SimpleExecutor<FetchGpgKeysLogic, SyncOp<expected<void>>>::run( std::move(ctx), std::move(info) );
139  }
140 
142  {
143  return SimpleExecutor<FetchGpgKeysLogic, AsyncOp<expected<void>>>::run( std::move(ctx), std::move(info) );
144  }
145 
146 }
Pathname path() const
Repository path.
Definition: RepoInfo.cc:792
#define MIL
Definition: Logger.h:100
url_set gpgKeyUrls() const
The list of gpgkey URLs defined for this repo.
Definition: RepoInfo.cc:774
auto transform(Transformation &&transformation)
Definition: transform.h:70
#define _(MSG)
Definition: Gettext.h:39
Type toEnum() const
Definition: RepoType.h:49
void appendPathName(const Pathname &path_r, EEncoding eflag_r=zypp::url::E_DECODED)
Extend the path name.
Definition: Url.cc:804
std::set< std::string > _keysDownloaded
Definition: repoinfowf.cc:131
JobReportHelper< ZyppContextRefType > _reports
Definition: repoinfowf.cc:129
Definition: Arch.h:363
What is known about a repository.
Definition: RepoInfo.h:71
std::string asUserString() const
User string: label (alias or name)
Definition: RepoInfoBase.h:87
Convenient building of std::string with boost::format.
Definition: String.h:253
urls_const_iterator baseUrlsBegin() const
iterator that points at begin of repository urls
Definition: RepoInfo.cc:810
bool baseUrlsEmpty() const
whether repository urls are available
Definition: RepoInfo.cc:819
expected< void > fetchGpgKeys(SyncContextRef ctx, zypp::RepoInfo info)
Definition: repoinfowf.cc:136
const zypp::RepoInfo _info
Definition: repoinfowf.cc:130
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:515
#define ZYPP_ENABLE_LOGIC_BASE(Executor, OpType)
Definition: logichelpers.h:223
std::list< Url > url_set
Definition: RepoInfo.h:108
std::conditional_t< isAsync, AsyncOpRef< T >, T > makeReadyResult(T &&result)
Definition: asyncop.h:297
static expected success(ConsParams &&...params)
Definition: expected.h:115
static bool urlSupportsMirrorLink(const zypp::Url &url)
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:475
std::shared_ptr< AsyncOp< T > > AsyncOpRef
Definition: asyncop.h:255
Base class for Exception.
Definition: Exception.h:152
auto and_then(Fun &&function)
Definition: expected.h:623
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition: Exception.h:471
repo::RepoType type() const
Type of repository,.
Definition: RepoInfo.cc:759
Url manipulation class.
Definition: Url.h:92