aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/tool/threaded_updater.h
blob: e007553f812a50ecb1ddd6428fdcbc642dd872b5 (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
#pragma once

#include "meta_info.h"
#include "roles_fetcher.h"

#include <library/cpp/tvmauth/client/misc/async_updater.h>
#include <library/cpp/tvmauth/client/misc/threaded_updater.h>

#include <atomic>

namespace NTvmAuth::NTvmTool {
    class TThreadedUpdater: public TThreadedUpdaterBase {
    public:
        static TAsyncUpdaterPtr Create(const TClientSettings& settings, TLoggerPtr logger);
        ~TThreadedUpdater();

        TClientStatus GetStatus() const override;
        TString GetServiceTicketFor(const TClientSettings::TAlias& dst) const override;
        TString GetServiceTicketFor(const TTvmId dst) const override;
        TCheckedServiceTicket CheckServiceTicket(TStringBuf ticket, const TServiceContext::TCheckFlags& flags = TServiceContext::TCheckFlags{}) const override;
        TCheckedUserTicket CheckUserTicket(TStringBuf ticket, TMaybe<EBlackboxEnv> overrideEnv = {}) const override;
        NRoles::TRolesPtr GetRoles() const override;

    protected: // for tests
        TClientStatus::ECode GetState() const;

        TThreadedUpdater(const TClientSettings& settings, TLoggerPtr logger);

        void Init(const TClientSettings& settings);
        void UpdateState();

        TInstant UpdateServiceTickets(const TMetaInfo::TConfig& config);
        std::pair<TString, TInstant> FetchServiceTickets(const TMetaInfo::TConfig& config) const;
        TPairTicketsErrors ParseFetchTicketsResponse(const TString& resp,
                                                     const TMetaInfo::TDstAliases& dsts) const;

        TInstant UpdateKeys(const TMetaInfo::TConfig& config);
        std::pair<TString, TInstant> FetchPublicKeys() const;

        static TInstant GetBirthTimeFromResponse(const THttpHeaders& headers, TStringBuf errMsg);

        bool IsTimeToUpdateServiceTickets(const TMetaInfo::TConfig& config, TInstant lastUpdate) const;
        bool IsTimeToUpdatePublicKeys(TInstant lastUpdate) const;

        bool IsEverythingOk(const TMetaInfo::TConfig& config) const;
        bool AreServiceTicketsOk(const TMetaInfo::TConfig& config) const;
        bool AreServiceTicketsOk(size_t requiredCount) const;
        bool ArePublicKeysOk() const;
        bool IsConfigWarnTime() const;

    private:
        void Worker() override;

    protected:
        TMetaInfo MetaInfo_;
        TInstant LastVisitForServiceTickets_;
        TInstant LastVisitForPublicKeys_;
        TInstant LastVisitForConfig_;
        TDuration ConfigWarnDelay_;
        std::unique_ptr<TRolesFetcher> RolesFetcher_;

    private:
        const TClientSettings Settings_;
    };
}