aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/getter.h
blob: b0327d69e99baa97da0bf9fac954d242379e02e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once

#include "checker.h"

namespace NTvmAuth {
    class TServiceTicketGetter {
    public:
        TServiceTicketGetter(TAsyncUpdaterPtr updater)
            : Updater_(std::move(updater))
        {
            Y_ENSURE(Updater_);
            GetCache();
        }

        /*!
         * Fetching must enabled in TClientSettings
         * Can throw exception if cache is invalid or wrong config
         * @param dst
         */
        TString GetTicket(const TClientSettings::TAlias& dst) const {
            TServiceTicketsPtr c = GetCache();
            return GetTicketImpl(dst, c->TicketsByAlias, c->ErrorsByAlias, c->UnfetchedAliases);
        }

        TString GetTicket(const TTvmId dst) const {
            TServiceTicketsPtr c = GetCache();
            return GetTicketImpl(dst, c->TicketsById, c->ErrorsById, c->UnfetchedIds);
        }

    private:
        template <class Key, class Cont, class UnfetchedCont>
        TString GetTicketImpl(const Key& dst, const Cont& tickets, const Cont& errors, const UnfetchedCont& unfetched) const {
            auto it = tickets.find(dst);
            if (it != tickets.end()) {
                return it->second;
            }

            it = errors.find(dst);
            if (it != errors.end()) {
                ythrow TMissingServiceTicket()
                    << "Failed to get ticket for '" << dst << "': "
                    << it->second;
            }

            if (unfetched.contains(dst)) {
                ythrow TMissingServiceTicket()
                    << "Failed to get ticket for '" << dst << "': this dst was not fetched yet.";
            }

            ythrow TBrokenTvmClientSettings()
                << "Destination '" << dst << "' was not specified in settings. "
                << "Check your settings (if you use Qloud/YP/tvmtool - check it's settings)";
        }

    private:
        TServiceTicketsPtr GetCache() const {
            TServiceTicketsPtr c = Updater_->GetCachedServiceTickets();
            Y_ENSURE_EX(c, TBrokenTvmClientSettings()
                               << "Need to use TClientSettings::EnableServiceTicketsFetchOptions()");
            return c;
        }

    private:
        TAsyncUpdaterPtr Updater_;
    };
}