libzypp: zypp/ng/workflows/repoinfowf.cc Source File

libzypp 17.38.15

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>
11#include <zypp/repo/RepoMirrorList.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
31namespace 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 BLOCKTRACE("FetchGpgKeysLogic");
52 using namespace zyppng::operators;
53 using zyppng::operators::operator|;
54 using zyppng::expected;
55
56 zypp::RepoInfo::url_set gpgKeyUrls = _info.gpgKeyUrls();
57
58 if ( gpgKeyUrls.empty() ) {
59 if ( !_info.baseUrlsEmpty() ) {
60 MIL << "No gpgkey URL specified, Trying to generate the key file path." << std::endl;
61
62 zypp::Url bUrl = *_info.baseUrlsBegin();
63 zypp::repo::RepoType::Type rType = _info.type().toEnum ();
64 switch( rType ) {
65 case zypp::repo::RepoType::RPMMD_e:
66 bUrl.appendPathName( _info.path() / "/repodata/repomd.xml.key" );
67 gpgKeyUrls.push_back( bUrl );
68 break;
69 case zypp::repo::RepoType::YAST2_e:
70 bUrl.appendPathName( _info.path() / "/content.key" );
71 gpgKeyUrls.push_back( bUrl );
72 break;
73 case zypp::repo::RepoType::NONE_e:
74 case zypp::repo::RepoType::RPMPLAINDIR_e: {
75 MIL << "Repo type is not known, unable to generate the gpgkey Url on the fly." << std::endl;
76 break;
77 }
78 }
79 }
80
81 if ( gpgKeyUrls.empty () )
82 return makeReadyTask( expected<void>::success() );
83 }
84
85 _keysDownloaded.clear();
86
87 // no key in the cache is what we are looking for, lets download
88 // all keys specified in gpgkey= entries
89
90 // translator: %1% is a repositories name
91 _reports.info( zypp::str::Format(_("Looking for gpg keys in repository %1%.") ) % _info.asUserString() );
92
93 return std::move(gpgKeyUrls)
94 | transform( [this]( const zypp::Url &url ) {
95
96 _reports.info( " gpgkey=" + url.asString() );
97 return _reports.zyppContext()->provider ()->provide( url, zyppng::ProvideFileSpec().setMirrorsAllowed(false) )
98 | and_then( [this, url]( ProvideRes f ) -> expected<void> {
99 try {
100 zypp::PublicKey key(f.file());
101 if ( !key.isValid() )
102 return expected<void>::error(std::make_exception_ptr( zypp::Exception("Invalid public key.") ));
103
104 // import all keys into our keyring
105 _reports.zyppContext()->keyRing()->importKey(key, false);
106
107 } catch ( const std::exception & e ) {
108 //ignore and continue to next url
109 ZYPP_CAUGHT(e);
110 MIL << "Key import from url:'"<<url<<"' failed." << std::endl;
111 return expected<void>::error( ZYPP_FWD_CURRENT_EXCPT() );
112 }
113
114 return expected<void>::success();
115 });
116 } )
117 | []( std::list<expected<void>> && ) {
118 return expected<void>::success();
119 };
120 }
121
122 protected:
123 JobReportHelper _reports;
124 const zypp::RepoInfo _info;
125 std::set<std::string> _keysDownloaded;
126 };
127
128 }
129
130 MaybeAwaitable<expected<void>> RepoInfoWorkflow::fetchGpgKeys(ContextRef ctx, zypp::RepoInfo info )
131 {
132 FetchGpgKeysLogic impl( std::move(ctx), std::move(info) );
133 zypp_co_return zypp_co_await( impl.execute () );
134 }
135
136}
ZYPP_CAUGHT
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition Exception.h:475
ZYPP_FWD_CURRENT_EXCPT
#define ZYPP_FWD_CURRENT_EXCPT()
Drops a logline and returns the current Exception as a std::exception_ptr.
Definition Exception.h:471
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
KeyRing.h
MIL
#define MIL
Definition Logger.h:130
BLOCKTRACE
#define BLOCKTRACE(M)
Definition Logger.h:51
PublicKey.h
RepoMirrorList.h
StrMatcher.h
String.h
ZYppCallbacks.h
zypp::RepoInfo
What is known about a repository.
Definition RepoInfo.h:72
zypp::RepoInfo::url_set
std::list< Url > url_set
Definition RepoInfo.h:108
zypp::Url::asString
std::string asString() const
Returns a default string representation of the Url object.
Definition Url.cc:524
zypp::Url::appendPathName
void appendPathName(const Pathname &path_r, EEncoding eflag_r=zypp::url::E_DECODED)
Extend the path name.
Definition Url.cc:813
zyppng::Context::ProvideType
Provide ProvideType
Definition context.h:34
zyppng::ProvideRes::file
const zypp::Pathname file() const
Definition provide.cc:152
zyppng::expected::success
static expected success(ConsParams &&...params)
Definition expected.h:178
zyppng::expected::error
E & error()
Definition expected.h:288
zyppng::RepoInfoWorkflow::fetchGpgKeys
MaybeAwaitable< expected< void > > fetchGpgKeys(ContextRef ctx, zypp::RepoInfo info)
Definition repoinfowf.cc:130
zyppng
Definition MediaNetworkRequestExecutor.h:16
zyppng::and_then
ResultType and_then(const expected< T, E > &exp, Function &&f)
Definition expected.h:520
zyppng::transform
auto transform(Container< Msg, CArgs... > &&val, Transformation &&transformation)
Definition transform.h:64
provide.h
repoinfowf.h
reporthelper.h
zypp::repo::RepoType::Type
Type
Definition RepoType.h:36
zypp::repo::RepoType::RPMMD_e
@ RPMMD_e
Definition RepoType.h:38
zypp::repo::RepoType::NONE_e
@ NONE_e
Definition RepoType.h:37
zypp::repo::RepoType::YAST2_e
@ YAST2_e
Definition RepoType.h:39
zypp::repo::RepoType::RPMPLAINDIR_e
@ RPMPLAINDIR_e
Definition RepoType.h:40
ManagedFile.h
TmpPath.h