aboutsummaryrefslogtreecommitdiffstats
path: root/yt/yt/core/https/client.cpp
blob: 7d5fbc70e57d02f49a59c152ef6d53743c4df95a (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
#include "client.h"
#include "config.h"

#include <yt/yt/core/http/client.h>
#include <yt/yt/core/http/private.h>

#include <yt/yt/core/net/config.h>
#include <yt/yt/core/net/address.h>

#include <yt/yt/core/crypto/tls.h>

#include <yt/yt/core/concurrency/poller.h>

#include <library/cpp/openssl/io/stream.h>

namespace NYT::NHttps {

using namespace NNet;
using namespace NHttp;
using namespace NCrypto;
using namespace NConcurrency;

////////////////////////////////////////////////////////////////////////////////

class TClient
    : public IClient
{
public:
    explicit TClient(IClientPtr underlying)
        : Underlying_(std::move(underlying))
    { }

    TFuture<IResponsePtr> Get(
        const TString& url,
        const THeadersPtr& headers) override
    {
        return Underlying_->Get(url, headers);
    }

    TFuture<IResponsePtr> Post(
        const TString& url,
        const TSharedRef& body,
        const THeadersPtr& headers) override
    {
        return Underlying_->Post(url, body, headers);
    }

    TFuture<IResponsePtr> Patch(
        const TString& url,
        const TSharedRef& body,
        const THeadersPtr& headers) override
    {
        return Underlying_->Patch(url, body, headers);
    }

    TFuture<IResponsePtr> Put(
        const TString& url,
        const TSharedRef& body,
        const THeadersPtr& headers) override
    {
        return Underlying_->Put(url, body, headers);
    }

    TFuture<IResponsePtr> Delete(
        const TString& url,
        const THeadersPtr& headers) override
    {
        return Underlying_->Delete(url, headers);
    }

    TFuture<IActiveRequestPtr> StartPost(
        const TString& url,
        const THeadersPtr& headers) override
    {
        return Underlying_->StartPost(url, headers);
    }

    TFuture<IActiveRequestPtr> StartPatch(
        const TString& url,
        const THeadersPtr& headers) override
    {
        return Underlying_->StartPatch(url, headers);
    }

    TFuture<IActiveRequestPtr> StartPut(
        const TString& url,
        const THeadersPtr& headers) override
    {
        return Underlying_->StartPut(url, headers);
    }

    TFuture<IResponsePtr> Request(
        EMethod method,
        const TString& url,
        const std::optional<TSharedRef>& body,
        const THeadersPtr& headers) override
    {
        return Underlying_->Request(method, url, body, headers);
    }

private:
    const IClientPtr Underlying_;
};

IClientPtr CreateClient(
    const TClientConfigPtr& config,
    const IPollerPtr& poller)
{
    auto sslContext =  New<TSslContext>();
    if (config->Credentials) {
        if (config->Credentials->CertChain) {
            if (config->Credentials->CertChain->FileName) {
                sslContext->AddCertificateChainFromFile(*config->Credentials->CertChain->FileName);
            } else if (config->Credentials->CertChain->Value) {
                sslContext->AddCertificateChain(*config->Credentials->CertChain->Value);
            } else {
                THROW_ERROR_EXCEPTION("Neither \"file_name\" nor \"value\" is given for client certificate chain");
            }
        }
        if (config->Credentials->PrivateKey) {
            if (config->Credentials->PrivateKey->FileName) {
                sslContext->AddPrivateKeyFromFile(*config->Credentials->PrivateKey->FileName);
            } else if (config->Credentials->PrivateKey->Value) {
                sslContext->AddPrivateKey(*config->Credentials->PrivateKey->Value);
            } else {
                THROW_ERROR_EXCEPTION("Neither \"file_name\" nor \"value\" is given for client private key");
            }
        }
    } else {
        sslContext->UseBuiltinOpenSslX509Store();
    }
    sslContext->Commit();

    auto tlsDialer = sslContext->CreateDialer(
        New<TDialerConfig>(),
        poller,
        HttpLogger());

    auto httpClient = NHttp::CreateClient(
        config,
        tlsDialer,
        poller->GetInvoker());

    return New<TClient>(std::move(httpClient));
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NHttps