libzypp  17.38.7
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>
12 #include <zypp-core/ManagedFile.h>
13 #include <zypp-core/base/String.h>
14 #include <zypp-core/base/Gettext.h>
15 #include <zypp-core/fs/TmpPath.h>
16 #include <zypp/base/StrMatcher.h>
17 #include <zypp/KeyRing.h>
18 #include <zypp-common/PublicKey.h>
19 #include <zypp/ZYppCallbacks.h>
20 
21 #include <utility>
22 #include <zypp-core/ng/pipelines/Transform>
23 #include <zypp-core/ng/pipelines/Expected>
24 #include <zypp-core/ng/pipelines/MTry>
25 #include <zypp/ng/media/provide.h>
26 #include <zypp-media/ng/ProvideSpec>
27 #include <zypp/ng/Context>
28 #include <zypp/ng/UserRequest>
29 
30 
31 namespace zyppng {
32 
33  namespace {
34 
35  using namespace zyppng::operators;
36 
37  struct FetchGpgKeysLogic {
38 
39  using ZyppContextRefType =ContextRef;
40  using ZyppContextType = Context;
41  using ProvideType = typename ZyppContextType::ProvideType;
42  using MediaHandle = typename ProvideType::MediaHandle;
43  using ProvideRes = typename ProvideType::Res;
44 
45  FetchGpgKeysLogic( ZyppContextRefType &&zyppContext, zypp::RepoInfo &&info )
46  : _reports( std::move(zyppContext ))
47  , _info( std::move(info) )
48  { }
49 
50  MaybeAwaitable<expected<void>> execute () {
51  using namespace zyppng::operators;
52  using zyppng::operators::operator|;
53  using zyppng::expected;
54 
56 
57  if ( gpgKeyUrls.empty() ) {
58  if ( !_info.baseUrlsEmpty()
60 
61  MIL << "No gpgkey URL specified, but d.o.o server detected. Trying to generate the key file path." << std::endl;
62 
63  zypp::Url bUrl = *_info.baseUrlsBegin();
65  switch( rType ) {
67  bUrl.appendPathName( _info.path() / "/repodata/repomd.xml.key" );
68  gpgKeyUrls.push_back( bUrl );
69  break;
71  bUrl.appendPathName( _info.path() / "/content.key" );
72  gpgKeyUrls.push_back( bUrl );
73  break;
76  MIL << "Repo type is not known, unable to generate the gpgkey Url on the fly." << std::endl;
77  break;
78  }
79  }
80  }
81 
82  if ( gpgKeyUrls.empty () )
83  return makeReadyTask( expected<void>::success() );
84  }
85 
86  _keysDownloaded.clear();
87 
88  // no key in the cache is what we are looking for, lets download
89  // all keys specified in gpgkey= entries
90 
91  // translator: %1% is a repositories name
92  _reports.info( zypp::str::Format(_("Looking for gpg keys in repository %1%.") ) % _info.asUserString() );
93 
94  return std::move(gpgKeyUrls)
95  | transform( [this]( const zypp::Url &url ) {
96 
97  _reports.info( " gpgkey=" + url.asString() );
98  return _reports.zyppContext()->provider ()->provide( url, zyppng::ProvideFileSpec().setMirrorsAllowed(false) )
99  | and_then( [this, url]( ProvideRes f ) -> expected<void> {
100  try {
101  zypp::PublicKey key(f.file());
102  if ( !key.isValid() )
103  return expected<void>::error(std::make_exception_ptr( zypp::Exception("Invalid public key.") ));
104 
105  // import all keys into our keyring
106  _reports.zyppContext()->keyRing()->importKey(key, false);
107 
108  } catch ( const std::exception & e ) {
109  //ignore and continue to next url
110  ZYPP_CAUGHT(e);
111  MIL << "Key import from url:'"<<url<<"' failed." << std::endl;
113  }
114 
115  return expected<void>::success();
116  });
117  } )
118  | []( std::list<expected<void>> && ) {
119  return expected<void>::success();
120  };
121  }
122 
123  protected:
124  JobReportHelper _reports;
126  std::set<std::string> _keysDownloaded;
127  };
128 
129  }
130 
131  MaybeAwaitable<expected<void>> RepoInfoWorkflow::fetchGpgKeys(ContextRef ctx, zypp::RepoInfo info )
132  {
133  FetchGpgKeysLogic impl( std::move(ctx), std::move(info) );
134  zypp_co_return zypp_co_await( impl.execute () );
135  }
136 
137 }
Pathname path() const
Repository path.
Definition: RepoInfo.cc:824
#define MIL
Definition: Logger.h:103
url_set gpgKeyUrls() const
The list of gpgkey URLs defined for this repo.
Definition: RepoInfo.cc:806
auto transform(Transformation &&transformation)
Definition: transform.h:95
JobReportHelper _reports
Definition: repoinfowf.cc:124
Type toEnum() const
Definition: RepoType.h:49
MaybeAwaitable< expected< void > > fetchGpgKeys(ContextRef ctx, zypp::RepoInfo info)
Definition: repoinfowf.cc:131
void appendPathName(const Pathname &path_r, EEncoding eflag_r=zypp::url::E_DECODED)
Extend the path name.
Definition: Url.cc:813
std::set< std::string > _keysDownloaded
Definition: repoinfowf.cc:126
Definition: ansi.h:854
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:839
bool baseUrlsEmpty() const
whether repository urls are available
Definition: RepoInfo.cc:848
const zypp::RepoInfo _info
Definition: repoinfowf.cc:125
std::string asString() const
Returns a default string representation of the Url object.
Definition: Url.cc:524
#define _(MSG)
Definition: Gettext.h:39
std::list< Url > url_set
Definition: RepoInfo.h:108
static expected success(ConsParams &&...params)
Definition: expected.h:178
Provide::Res ProvideRes
Definition: provide.h:154
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
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:374
Base class for Exception.
Definition: Exception.h:152
auto and_then(Fun &&function)
Definition: expected.h:708
#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:791
Url manipulation class.
Definition: Url.h:92