aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/tvmauth/client/misc/async_updater.cpp
blob: 9cb0332ed40d39e49c754b4a90126c223f17c34b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "async_updater.h"

#include "utils.h"

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

#include <util/string/builder.h>
#include <util/system/spin_wait.h>

namespace NTvmAuth {
    TAsyncUpdaterBase::TAsyncUpdaterBase() {
        ServiceTicketsDurations_.RefreshPeriod = TDuration::Hours(1);
        ServiceTicketsDurations_.Expiring = TDuration::Hours(2);
        ServiceTicketsDurations_.Invalid = TDuration::Hours(11);

        PublicKeysDurations_.RefreshPeriod = TDuration::Days(1);
        PublicKeysDurations_.Expiring = TDuration::Days(2);
        PublicKeysDurations_.Invalid = TDuration::Days(6);
    }

    NRoles::TRolesPtr TAsyncUpdaterBase::GetRoles() const {
        ythrow TIllegalUsage() << "not implemented";
    }

    TInstant TAsyncUpdaterBase::GetUpdateTimeOfPublicKeys() const {
        return PublicKeysTime_.Get();
    }

    TInstant TAsyncUpdaterBase::GetUpdateTimeOfServiceTickets() const {
        return ServiceTicketsTime_.Get();
    }

    TInstant TAsyncUpdaterBase::GetUpdateTimeOfRoles() const {
        return RolesTime_.Get();
    }

    TInstant TAsyncUpdaterBase::GetInvalidationTimeOfPublicKeys() const {
        TInstant ins = GetUpdateTimeOfPublicKeys();
        return ins == TInstant() ? TInstant() : ins + PublicKeysDurations_.Invalid;
    }

    TInstant TAsyncUpdaterBase::GetInvalidationTimeOfServiceTickets() const {
        TServiceTicketsPtr c = GetCachedServiceTickets();
        return c ? c->InvalidationTime : TInstant();
    }

    bool TAsyncUpdaterBase::ArePublicKeysInvalid(TInstant now) const {
        return IsInvalid(GetInvalidationTimeOfPublicKeys(), now);
    }

    bool TAsyncUpdaterBase::AreServiceTicketsInvalid(TInstant now) const {
        TServiceTicketsPtr c = GetCachedServiceTickets();
        // Empty set of tickets is allways valid.
        return c && !c->TicketsById.empty() && IsInvalid(GetInvalidationTimeOfServiceTickets(), now);
    }

    bool TAsyncUpdaterBase::IsInvalid(TInstant invTime, TInstant now) {
        return invTime -
                   TDuration::Minutes(1) // lag for closing from balancer
               < now;
    }

    void TAsyncUpdaterBase::SetBbEnv(EBlackboxEnv original, TMaybe<EBlackboxEnv> overrided) {
        if (overrided) {
            Y_ENSURE_EX(NUtils::CheckBbEnvOverriding(original, *overrided),
                        TBrokenTvmClientSettings() << "Overriding of BlackboxEnv is illegal: "
                                                   << original << " -> " << *overrided);
        }

        Envs_.store({original, overrided}, std::memory_order_relaxed);
    }

    TServiceTicketsPtr TAsyncUpdaterBase::GetCachedServiceTickets() const {
        return ServiceTickets_.Get();
    }

    TServiceContextPtr TAsyncUpdaterBase::GetCachedServiceContext() const {
        return ServiceContext_.Get();
    }

    TUserContextPtr TAsyncUpdaterBase::GetCachedUserContext(TMaybe<EBlackboxEnv> overridenEnv) const {
        TAllUserContextsPtr ctx = AllUserContexts_.Get();
        if (!ctx) {
            return nullptr;
        }

        const TEnvs envs = Envs_.load(std::memory_order_relaxed);
        if (!envs.Original) {
            return nullptr;
        }

        EBlackboxEnv env = *envs.Original;

        if (overridenEnv) {
            Y_ENSURE_EX(NUtils::CheckBbEnvOverriding(*envs.Original, *overridenEnv),
                        TBrokenTvmClientSettings() << "Overriding of BlackboxEnv is illegal: "
                                                   << *envs.Original << " -> " << *overridenEnv);
            env = *overridenEnv;
        } else if (envs.Overrided) {
            env = *envs.Overrided;
        }

        return ctx->Get(env);
    }

    void TAsyncUpdaterBase::SetServiceTickets(TServiceTicketsPtr c) {
        ServiceTickets_.Set(std::move(c));
    }

    void TAsyncUpdaterBase::SetServiceContext(TServiceContextPtr c) {
        ServiceContext_.Set(std::move(c));
    }

    void TAsyncUpdaterBase::SetUserContext(TStringBuf publicKeys) {
        AllUserContexts_.Set(MakeIntrusiveConst<TAllUserContexts>(publicKeys));
    }

    void TAsyncUpdaterBase::SetUpdateTimeOfPublicKeys(TInstant ins) {
        PublicKeysTime_.Set(ins);
    }

    void TAsyncUpdaterBase::SetUpdateTimeOfServiceTickets(TInstant ins) {
        ServiceTicketsTime_.Set(ins);
    }

    void TAsyncUpdaterBase::SetUpdateTimeOfRoles(TInstant ins) {
        RolesTime_.Set(ins);
    }

    bool TAsyncUpdaterBase::IsServiceTicketMapOk(TServiceTicketsPtr c, size_t expectedTicketCount, bool strict) {
        return c &&
               (strict
                    ? c->TicketsById.size() == expectedTicketCount
                    : !c->TicketsById.empty());
    }

    TAllUserContexts::TAllUserContexts(TStringBuf publicKeys) {
        auto add = [&, this](EBlackboxEnv env) {
            Ctx_[(size_t)env] = MakeIntrusiveConst<TUserContext>(env, publicKeys);
        };

        add(EBlackboxEnv::Prod);
        add(EBlackboxEnv::Test);
        add(EBlackboxEnv::ProdYateam);
        add(EBlackboxEnv::TestYateam);
        add(EBlackboxEnv::Stress);
    }

    TUserContextPtr TAllUserContexts::Get(EBlackboxEnv env) const {
        return Ctx_[(size_t)env];
    }
}