1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #ifndef CEPH_RGW_AUTH_H
6 #define CEPH_RGW_AUTH_H
10 #include <type_traits>
11 #include <system_error>
14 #include "rgw_common.h"
15 #include "rgw_keystone.h"
16 #include "rgw_web_idp.h"
18 #define RGW_USER_ANON_ID "anonymous"
23 using Exception
= std::system_error
;
26 /* Load information about identity that will be used by RGWOp to authorize
27 * any operation that comes from an authenticated user. */
30 typedef std::map
<std::string
, int> aclspec_t
;
31 using idset_t
= boost::container::flat_set
<Principal
>;
33 virtual ~Identity() = default;
35 /* Translate the ACL provided in @aclspec into concrete permission set that
36 * can be used during the authorization phase (RGWOp::verify_permission).
37 * On error throws rgw::auth::Exception storing the reason.
39 * NOTE: an implementation is responsible for giving the real semantic to
40 * the items in @aclspec. That is, their meaning may depend on particular
41 * applier that is being used. */
42 virtual uint32_t get_perms_from_aclspec(const DoutPrefixProvider
* dpp
, const aclspec_t
& aclspec
) const = 0;
44 /* Verify whether a given identity *can be treated as* an admin of rgw_user
45 * (account in Swift's terminology) specified in @uid. On error throws
46 * rgw::auth::Exception storing the reason. */
47 virtual bool is_admin_of(const rgw_user
& uid
) const = 0;
49 /* Verify whether a given identity *is* the owner of the rgw_user (account
50 * in the Swift's terminology) specified in @uid. On internal error throws
51 * rgw::auth::Exception storing the reason. */
52 virtual bool is_owner_of(const rgw_user
& uid
) const = 0;
54 /* Return the permission mask that is used to narrow down the set of
55 * operations allowed for a given identity. This method reflects the idea
56 * of subuser tied to RGWUserInfo. On error throws rgw::auth::Exception
58 virtual uint32_t get_perm_mask() const = 0;
60 virtual bool is_anonymous() const final
{
61 /* If the identity owns the anonymous account (rgw_user), it's considered
62 * the anonymous identity. On error throws rgw::auth::Exception storing
64 return is_owner_of(rgw_user(RGW_USER_ANON_ID
));
67 virtual void to_str(std::ostream
& out
) const = 0;
69 /* Verify whether a given identity corresponds to an identity in the
71 virtual bool is_identity(const idset_t
& ids
) const = 0;
73 /* Identity Type: RGW/ LDAP/ Keystone */
74 virtual uint32_t get_identity_type() const = 0;
77 virtual string
get_acct_name() const = 0;
80 inline std::ostream
& operator<<(std::ostream
& out
,
81 const rgw::auth::Identity
& id
) {
87 std::unique_ptr
<Identity
> transform_old_authinfo(const req_state
* const s
);
90 /* Interface for classes applying changes to request state/RADOS store
91 * imposed by a particular rgw::auth::Engine.
93 * In contrast to rgw::auth::Engine, implementations of this interface
94 * are allowed to handle req_state or RGWRados in the read-write manner.
96 * It's expected that most (if not all) of implementations will also
97 * conform to rgw::auth::Identity interface to provide authorization
98 * policy (ACLs, account's ownership and entitlement). */
99 class IdentityApplier
: public Identity
{
101 typedef std::unique_ptr
<IdentityApplier
> aplptr_t
;
103 virtual ~IdentityApplier() {};
105 /* Fill provided RGWUserInfo with information about the account that
106 * RGWOp will operate on. Errors are handled solely through exceptions.
108 * XXX: be aware that the "account" term refers to rgw_user. The naming
110 virtual void load_acct_info(const DoutPrefixProvider
* dpp
, RGWUserInfo
& user_info
) const = 0; /* out */
112 /* Apply any changes to request state. This method will be most useful for
113 * TempURL of Swift API. */
114 virtual void modify_request_state(const DoutPrefixProvider
* dpp
, req_state
* s
) const {} /* in/out */
118 /* Interface class for completing the two-step authentication process.
119 * Completer provides the second step - the complete() method that should
120 * be called after Engine::authenticate() but before *committing* results
121 * of an RGWOp (or sending a response in the case of non-mutating ops).
123 * The motivation driving the interface is to address those authentication
124 * schemas that require message integrity verification *without* in-memory
125 * data buffering. Typical examples are AWS Auth v4 and the auth mechanism
126 * of browser uploads facilities both in S3 and Swift APIs (see RGWPostObj).
127 * The workflow of request from the authentication point-of-view does look
128 * like following one:
129 * A. authenticate (Engine::authenticate),
130 * B. authorize (see RGWOp::verify_permissions),
131 * C. execute-prepare (init potential data modifications),
132 * D. authenticate-complete - (Completer::complete),
133 * E. execute-commit - commit the modifications from point C. */
136 /* It's expected that Completers would tend to implement many interfaces
137 * and be used not only in req_state::auth::completer. Ref counting their
138 * instances would be helpful. */
139 typedef std::shared_ptr
<Completer
> cmplptr_t
;
141 virtual ~Completer() = default;
143 /* Complete the authentication process. Return boolean indicating whether
144 * the completion succeeded. On error throws rgw::auth::Exception storing
146 virtual bool complete() = 0;
148 /* Apply any changes to request state. The initial use case was injecting
149 * the AWSv4 filter over rgw::io::RestfulClient in req_state. */
150 virtual void modify_request_state(const DoutPrefixProvider
* dpp
, req_state
* s
) = 0; /* in/out */
154 /* Interface class for authentication backends (auth engines) in RadosGW.
156 * An engine is supposed only to authenticate (not authorize!) requests
157 * basing on their req_state and - if access has been granted - provide
158 * an upper layer with:
159 * - rgw::auth::IdentityApplier to commit all changes to the request state as
160 * well as to the RADOS store (creating an account, synchronizing
161 * user-related information with external databases and so on).
162 * - rgw::auth::Completer (optionally) to finish the authentication
163 * of the request. Typical use case is verifying message integrity
164 * in AWS Auth v4 and browser uploads (RGWPostObj).
166 * Both of them are supposed to be wrapped in Engine::AuthResult.
168 * The authentication process consists of two steps:
169 * - Engine::authenticate() which should be called before *initiating*
170 * any modifications to RADOS store that are related to an operation
171 * a client wants to perform (RGWOp::execute).
172 * - Completer::complete() supposed to be called, if completer has been
173 * returned, after the authenticate() step but before *committing*
174 * those modifications or sending a response (RGWOp::complete).
176 * An engine outlives both Applier and Completer. It's intended to live
177 * since RadosGW's initialization and handle multiple requests till
180 * Auth engine MUST NOT make any changes to req_state nor RADOS store.
181 * This is solely an Applier's responsibility!
183 * Separation between authentication and global state modification has
184 * been introduced because many auth engines are orthogonal to appliers
185 * and thus they can be decoupled. Additional motivation is to clearly
186 * distinguish all portions of code modifying data structures. */
189 virtual ~Engine() = default;
192 struct rejection_mark_t
{};
193 bool is_rejected
= false;
196 std::pair
<IdentityApplier::aplptr_t
, Completer::cmplptr_t
> result_pair
;
198 explicit AuthResult(const int reason
)
202 AuthResult(rejection_mark_t
&&, const int reason
)
207 /* Allow only the reasonable combintations - returning just Completer
208 * without accompanying IdentityApplier is strictly prohibited! */
209 explicit AuthResult(IdentityApplier::aplptr_t
&& applier
)
210 : result_pair(std::move(applier
), nullptr) {
213 AuthResult(IdentityApplier::aplptr_t
&& applier
,
214 Completer::cmplptr_t
&& completer
)
215 : result_pair(std::move(applier
), std::move(completer
)) {
220 /* Engine doesn't grant the access but also doesn't reject it. */
223 /* Engine successfully authenicated requester. */
226 /* Engine strictly indicates that a request should be rejected
227 * without trying any further engine. */
231 Status
get_status() const {
233 return Status::REJECTED
;
234 } else if (! result_pair
.first
) {
235 return Status::DENIED
;
237 return Status::GRANTED
;
241 int get_reason() const {
245 IdentityApplier::aplptr_t
get_applier() {
246 return std::move(result_pair
.first
);
249 Completer::cmplptr_t
&& get_completer() {
250 return std::move(result_pair
.second
);
253 static AuthResult
reject(const int reason
= -EACCES
) {
254 return AuthResult(rejection_mark_t(), reason
);
257 static AuthResult
deny(const int reason
= -EACCES
) {
258 return AuthResult(reason
);
261 static AuthResult
grant(IdentityApplier::aplptr_t
&& applier
) {
262 return AuthResult(std::move(applier
));
265 static AuthResult
grant(IdentityApplier::aplptr_t
&& applier
,
266 Completer::cmplptr_t
&& completer
) {
267 return AuthResult(std::move(applier
), std::move(completer
));
271 using result_t
= AuthResult
;
273 /* Get name of the auth engine. */
274 virtual const char* get_name() const noexcept
= 0;
276 /* Throwing method for identity verification. When the check is positive
277 * an implementation should return Engine::result_t containing:
278 * - a non-null pointer to an object conforming the Applier interface.
279 * Otherwise, the authentication is treated as failed.
280 * - a (potentially null) pointer to an object conforming the Completer
283 * On error throws rgw::auth::Exception containing the reason. */
284 virtual result_t
authenticate(const DoutPrefixProvider
* dpp
, const req_state
* s
) const = 0;
288 /* Interface for extracting a token basing from data carried by req_state. */
289 class TokenExtractor
{
291 virtual ~TokenExtractor() = default;
292 virtual std::string
get_token(const req_state
* s
) const = 0;
296 /* Abstract class for stacking sub-engines to expose them as a single
297 * Engine. It is responsible for ordering its sub-engines and managing
298 * fall-backs between them. Derivatee is supposed to encapsulate engine
299 * instances and add them using the add_engine() method in the order it
300 * wants to be tried during the call to authenticate().
302 * Each new Strategy should be exposed to StrategyRegistry for handling
303 * the dynamic reconfiguration. */
304 class Strategy
: public Engine
{
306 /* Specifiers controlling what happens when an associated engine fails.
307 * The names and semantic has been borrowed mostly from libpam. */
309 /* Failure of an engine injected with the REQUISITE specifier aborts
310 * the strategy's authentication process immediately. No other engine
314 /* Success of an engine injected with the SUFFICIENT specifier ends
315 * strategy's authentication process successfully. However, denying
316 * doesn't abort it -- there will be fall-back to following engine
317 * if the one that failed wasn't the last one. */
320 /* Like SUFFICIENT with the exception that on failure the reason code
321 * is not overridden. Instead, it's taken directly from the last tried
322 * non-FALLBACK engine. If there was no previous non-FALLBACK engine
323 * in a Strategy, then the result_t::deny(reason = -EACCES) is used. */
327 Engine::result_t
authenticate(const DoutPrefixProvider
* dpp
, const req_state
* s
) const override final
;
329 bool is_empty() const {
330 return auth_stack
.empty();
333 static int apply(const DoutPrefixProvider
* dpp
, const Strategy
& auth_strategy
, req_state
* s
) noexcept
;
336 /* Using the reference wrapper here to explicitly point out we are not
337 * interested in storing nulls while preserving the dynamic polymorphism. */
338 using stack_item_t
= std::pair
<std::reference_wrapper
<const Engine
>,
340 std::vector
<stack_item_t
> auth_stack
;
343 void add_engine(Control ctrl_flag
, const Engine
& engine
) noexcept
;
347 /* A class aggregating the knowledge about all Strategies in RadosGW. It is
348 * responsible for handling the dynamic reconfiguration on e.g. realm update.
349 * The definition is in rgw/rgw_auth_registry.h,
351 * Each new Strategy should be exposed to it. */
352 class StrategyRegistry
;
354 class WebIdentityApplier
: public IdentityApplier
{
356 CephContext
* const cct
;
357 RGWRados
* const store
;
358 rgw::web_idp::WebTokenClaims token_claims
;
360 string
get_idp_url() const;
363 WebIdentityApplier( CephContext
* const cct
,
364 RGWRados
* const store
,
365 const rgw::web_idp::WebTokenClaims
& token_claims
)
368 token_claims(token_claims
) {
371 void load_acct_info(const DoutPrefixProvider
* dpp
, RGWUserInfo
& user_info
) const override
{
372 user_info
.user_id
= rgw_user(token_claims
.sub
);
373 user_info
.display_name
= token_claims
.user_name
;
376 void modify_request_state(const DoutPrefixProvider
*dpp
, req_state
* s
) const override
;
378 uint32_t get_perms_from_aclspec(const DoutPrefixProvider
* dpp
, const aclspec_t
& aclspec
) const override
{
379 return RGW_PERM_NONE
;
382 bool is_admin_of(const rgw_user
& uid
) const override
{
386 bool is_owner_of(const rgw_user
& uid
) const override
{
390 uint32_t get_perm_mask() const override
{
391 return RGW_PERM_NONE
;
394 void to_str(std::ostream
& out
) const override
;
396 bool is_identity(const idset_t
& ids
) const override
;
398 uint32_t get_identity_type() const override
{
402 string
get_acct_name() const override
{
403 return token_claims
.user_name
;
407 virtual ~Factory() {}
409 virtual aplptr_t
create_apl_web_identity( CephContext
* cct
,
411 const rgw::web_idp::WebTokenClaims
& token
) const = 0;
415 /* rgw::auth::RemoteApplier targets those authentication engines which don't
416 * need to ask the RADOS store while performing the auth process. Instead,
417 * they obtain credentials from an external source like Keystone or LDAP.
419 * As the authenticated user may not have an account yet, RGWRemoteAuthApplier
420 * must be able to create it basing on data passed by an auth engine. Those
421 * data will be used to fill RGWUserInfo structure. */
422 class RemoteApplier
: public IdentityApplier
{
425 friend class RemoteApplier
;
427 const rgw_user acct_user
;
428 const std::string acct_name
;
429 const uint32_t perm_mask
;
431 const uint32_t acct_type
;
434 enum class acct_privilege_t
{
439 AuthInfo(const rgw_user
& acct_user
,
440 const std::string
& acct_name
,
441 const uint32_t perm_mask
,
442 const acct_privilege_t level
,
443 const uint32_t acct_type
=TYPE_NONE
)
444 : acct_user(acct_user
),
445 acct_name(acct_name
),
446 perm_mask(perm_mask
),
447 is_admin(acct_privilege_t::IS_ADMIN_ACCT
== level
),
448 acct_type(acct_type
) {
452 using aclspec_t
= rgw::auth::Identity::aclspec_t
;
453 typedef std::function
<uint32_t(const aclspec_t
&)> acl_strategy_t
;
456 CephContext
* const cct
;
458 /* Read-write is intensional here due to RGWUserInfo creation process. */
459 RGWRados
* const store
;
461 /* Supplemental strategy for extracting permissions from ACLs. Its results
462 * will be combined (ORed) with a default strategy that is responsible for
463 * handling backward compatibility. */
464 const acl_strategy_t extra_acl_strategy
;
467 const bool implicit_tenants
;
469 virtual void create_account(const DoutPrefixProvider
* dpp
,
470 const rgw_user
& acct_user
,
471 RGWUserInfo
& user_info
) const; /* out */
474 RemoteApplier(CephContext
* const cct
,
475 RGWRados
* const store
,
476 acl_strategy_t
&& extra_acl_strategy
,
477 const AuthInfo
& info
,
478 const bool implicit_tenants
)
481 extra_acl_strategy(std::move(extra_acl_strategy
)),
483 implicit_tenants(implicit_tenants
) {
486 uint32_t get_perms_from_aclspec(const DoutPrefixProvider
* dpp
, const aclspec_t
& aclspec
) const override
;
487 bool is_admin_of(const rgw_user
& uid
) const override
;
488 bool is_owner_of(const rgw_user
& uid
) const override
;
489 bool is_identity(const idset_t
& ids
) const override
;
491 uint32_t get_perm_mask() const override
{ return info
.perm_mask
; }
492 void to_str(std::ostream
& out
) const override
;
493 void load_acct_info(const DoutPrefixProvider
* dpp
, RGWUserInfo
& user_info
) const override
; /* out */
494 uint32_t get_identity_type() const override
{ return info
.acct_type
; }
495 string
get_acct_name() const override
{ return info
.acct_name
; }
498 virtual ~Factory() {}
499 /* Providing r-value reference here is required intensionally. Callee is
500 * thus disallowed to handle std::function in a way that could inhibit
501 * the move behaviour (like forgetting about std::moving a l-value). */
502 virtual aplptr_t
create_apl_remote(CephContext
* cct
,
504 acl_strategy_t
&& extra_acl_strategy
,
505 const AuthInfo
&info
) const = 0;
510 /* rgw::auth::LocalApplier targets those auth engines that base on the data
511 * enclosed in the RGWUserInfo control structure. As a side effect of doing
512 * the authentication process, they must have it loaded. Leveraging this is
513 * a way to avoid unnecessary calls to underlying RADOS store. */
514 class LocalApplier
: public IdentityApplier
{
515 using aclspec_t
= rgw::auth::Identity::aclspec_t
;
518 const RGWUserInfo user_info
;
519 const std::string subuser
;
522 uint32_t get_perm_mask(const std::string
& subuser_name
,
523 const RGWUserInfo
&uinfo
) const;
526 static const std::string NO_SUBUSER
;
528 LocalApplier(CephContext
* const cct
,
529 const RGWUserInfo
& user_info
,
531 const boost::optional
<uint32_t>& perm_mask
)
532 : user_info(user_info
),
533 subuser(std::move(subuser
)) {
535 this->perm_mask
= perm_mask
.get();
537 this->perm_mask
= RGW_PERM_INVALID
;
542 uint32_t get_perms_from_aclspec(const DoutPrefixProvider
* dpp
, const aclspec_t
& aclspec
) const override
;
543 bool is_admin_of(const rgw_user
& uid
) const override
;
544 bool is_owner_of(const rgw_user
& uid
) const override
;
545 bool is_identity(const idset_t
& ids
) const override
;
546 uint32_t get_perm_mask() const override
{
547 if (this->perm_mask
== RGW_PERM_INVALID
) {
548 return get_perm_mask(subuser
, user_info
);
550 return this->perm_mask
;
553 void to_str(std::ostream
& out
) const override
;
554 void load_acct_info(const DoutPrefixProvider
* dpp
, RGWUserInfo
& user_info
) const override
; /* out */
555 uint32_t get_identity_type() const override
{ return TYPE_RGW
; }
556 string
get_acct_name() const override
{ return {}; }
559 virtual ~Factory() {}
560 virtual aplptr_t
create_apl_local(CephContext
* cct
,
562 const RGWUserInfo
& user_info
,
563 const std::string
& subuser
,
564 const boost::optional
<uint32_t>& perm_mask
) const = 0;
568 class RoleApplier
: public IdentityApplier
{
570 const string role_name
;
571 const rgw_user user_id
;
572 vector
<std::string
> role_policies
;
576 RoleApplier(CephContext
* const cct
,
577 const string
& role_name
,
578 const rgw_user
& user_id
,
579 const vector
<std::string
>& role_policies
)
580 : role_name(role_name
),
582 role_policies(role_policies
) {}
584 uint32_t get_perms_from_aclspec(const DoutPrefixProvider
* dpp
, const aclspec_t
& aclspec
) const override
{
587 bool is_admin_of(const rgw_user
& uid
) const override
{
590 bool is_owner_of(const rgw_user
& uid
) const override
{
593 bool is_identity(const idset_t
& ids
) const override
;
594 uint32_t get_perm_mask() const override
{
595 return RGW_PERM_NONE
;
597 void to_str(std::ostream
& out
) const override
;
598 void load_acct_info(const DoutPrefixProvider
* dpp
, RGWUserInfo
& user_info
) const override
; /* out */
599 uint32_t get_identity_type() const override
{ return TYPE_ROLE
; }
600 string
get_acct_name() const override
{ return {}; }
601 void modify_request_state(const DoutPrefixProvider
* dpp
, req_state
* s
) const override
;
604 virtual ~Factory() {}
605 virtual aplptr_t
create_apl_role( CephContext
* cct
,
607 const string
& role_name
,
608 const rgw_user
& user_id
,
609 const vector
<std::string
>& role_policies
) const = 0;
613 /* The anonymous abstract engine. */
614 class AnonymousEngine
: public Engine
{
615 CephContext
* const cct
;
616 const rgw::auth::LocalApplier::Factory
* const apl_factory
;
619 AnonymousEngine(CephContext
* const cct
,
620 const rgw::auth::LocalApplier::Factory
* const apl_factory
)
622 apl_factory(apl_factory
) {
625 const char* get_name() const noexcept override
{
626 return "rgw::auth::AnonymousEngine";
629 Engine::result_t
authenticate(const DoutPrefixProvider
* dpp
, const req_state
* s
) const override final
;
632 virtual bool is_applicable(const req_state
*) const noexcept
{
637 } /* namespace auth */
638 } /* namespace rgw */
641 uint32_t rgw_perms_from_aclspec_default_strategy(
643 const rgw::auth::Identity::aclspec_t
& aclspec
);
645 #endif /* CEPH_RGW_AUTH_H */