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

#include "meta_info.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;

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

        TThreadedUpdater(const TString& host, ui16 port, TDuration socketTimeout, TDuration connectTimeout, 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_;
    };
}