aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/api/roles_fetcher.h
blob: 63691223b5010406ee2dea966f438759123d1b73 (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
#pragma once

#include "retry_settings.h"

#include <library/cpp/tvmauth/client/misc/fetch_result.h>
#include <library/cpp/tvmauth/client/misc/proc_info.h>
#include <library/cpp/tvmauth/client/misc/utils.h>
#include <library/cpp/tvmauth/client/misc/roles/roles.h>

#include <library/cpp/tvmauth/client/logger.h>

#include <library/cpp/http/simple/http_client.h>

namespace NTvmAuth::NTvmApi {
    struct TRolesFetcherSettings {
        TString TiroleHost;
        ui16 TirolePort = 0;
        TString CacheDir;
        NUtils::TProcInfo ProcInfo;
        TTvmId SelfTvmId = 0;
        TString IdmSystemSlug;
        TDuration Timeout = TDuration::Seconds(30);
    };

    class TRolesFetcher {
    public:
        TRolesFetcher(const TRolesFetcherSettings& settings, TLoggerPtr logger);

        TInstant ReadFromDisk();

        bool AreRolesOk() const;
        static bool IsTimeToUpdate(const TRetrySettings& settings, TDuration sinceUpdate);
        static bool ShouldWarn(const TRetrySettings& settings, TDuration sinceUpdate);

        NUtils::TFetchResult FetchActualRoles(const TString& serviceTicket);
        void Update(NUtils::TFetchResult&& fetchResult, TInstant now = TInstant::Now());

        NTvmAuth::NRoles::TRolesPtr GetCurrentRoles() const;

        void ResetConnection();

    public:
        static std::pair<TString, TString> ParseDiskFormat(TStringBuf filebody);
        static TString PrepareDiskFormat(TStringBuf roles, TStringBuf slug);

        struct TRequest {
            TString Url;
            TKeepAliveHttpClient::THeaders Headers;
        };
        TRequest CreateTiroleRequest(const TString& serviceTicket) const;

    private:
        const TRolesFetcherSettings Settings_;
        const TLoggerPtr Logger_;
        const TString CacheFilePath_;
        const TString XYaServiceTicket_ = "X-Ya-Service-Ticket";
        const TString IfNoneMatch_ = "If-None-Match";

        NUtils::TProtectedValue<NTvmAuth::NRoles::TRolesPtr> CurrentRoles_;

        std::unique_ptr<TKeepAliveHttpClient> Client_;
    };
}