aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/fq/libs/common/util.cpp
blob: 45f1fdb391fd6f57f7d4fd4d94fbc2c1f2199807 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "util.h"

#include <util/generic/string.h>
#include <util/string/builder.h>
#include <util/string/subst.h>
#include <regex>

namespace NFq {

namespace {

TString GetServiceAccountId(const FederatedQuery::IamAuth& auth) {
    return auth.has_service_account()
            ? auth.service_account().id()
            : TString{};
}

EYdbComputeAuth GetIamAuthMethod(const FederatedQuery::IamAuth& auth) {
    switch (auth.identity_case()) {
        case FederatedQuery::IamAuth::kNone:
            return EYdbComputeAuth::NONE;
        case FederatedQuery::IamAuth::kServiceAccount:
            return EYdbComputeAuth::SERVICE_ACCOUNT;
        case FederatedQuery::IamAuth::kCurrentIam:
        // Do not replace with default. Adding a new auth item should cause a compilation error
        case FederatedQuery::IamAuth::IDENTITY_NOT_SET:
            return EYdbComputeAuth::UNKNOWN;
    }
}

EYdbComputeAuth GetBasicAuthMethod(const FederatedQuery::IamAuth& auth) {
    switch (auth.identity_case()) {
        case FederatedQuery::IamAuth::kNone:
            return EYdbComputeAuth::BASIC;
        case FederatedQuery::IamAuth::kServiceAccount:
            return EYdbComputeAuth::MDB_BASIC;
        case FederatedQuery::IamAuth::kCurrentIam:
        // Do not replace with default. Adding a new auth item should cause a compilation error
        case FederatedQuery::IamAuth::IDENTITY_NOT_SET:
            return EYdbComputeAuth::UNKNOWN;
    }
}

}

TString EscapeString(const TString& value,
                     const TString& enclosingSeq,
                     const TString& replaceWith) {
    auto escapedValue = value;
    SubstGlobal(escapedValue, enclosingSeq, replaceWith);
    return escapedValue;
}

TString EscapeString(const TString& value, char enclosingChar) {
    auto escapedValue = value;
    SubstGlobal(escapedValue,
                TString{enclosingChar},
                TStringBuilder{} << '\\' << enclosingChar);
    return escapedValue;
}

TString EncloseAndEscapeString(const TString& value, char enclosingChar) {
    return TStringBuilder{} << enclosingChar << EscapeString(value, enclosingChar)
                            << enclosingChar;
}

TString EncloseAndEscapeString(const TString& value,
                               const TString& enclosingSeq,
                               const TString& replaceWith) {
    return TStringBuilder{} << enclosingSeq
                            << EscapeString(value, enclosingSeq, replaceWith)
                            << enclosingSeq;
}

static TString SECRET_BEGIN = "/* 51a91b5d91a99eb7 */";
static TString SECRET_END   = "/* e87c9b191b202354 */";
static auto SECRET_REGEXP   = std::regex{
    "\\/\\* 51a91b5d91a99eb7 \\*\\/.*\\/\\* e87c9b191b202354 \\*\\/"};

TString EncloseSecret(const TString& value) {
    return TStringBuilder{} << SECRET_BEGIN << value << SECRET_END;
}

TString HideSecrets(const TString& value) {
    return std::regex_replace(std::string{value}, SECRET_REGEXP, "/*SECRET*/");
}

TString ExtractServiceAccountId(const FederatedQuery::ConnectionSetting& setting) {
    switch (setting.connection_case()) {
    case FederatedQuery::ConnectionSetting::kYdbDatabase: {
        return GetServiceAccountId(setting.ydb_database().auth());
    }
    case FederatedQuery::ConnectionSetting::kDataStreams: {
        return GetServiceAccountId(setting.data_streams().auth());
    }
    case FederatedQuery::ConnectionSetting::kObjectStorage: {
        return GetServiceAccountId(setting.object_storage().auth());
    }
    case FederatedQuery::ConnectionSetting::kMonitoring: {
        return GetServiceAccountId(setting.monitoring().auth());
    }
    case FederatedQuery::ConnectionSetting::kClickhouseCluster: {
        return GetServiceAccountId(setting.clickhouse_cluster().auth());
    }
    case FederatedQuery::ConnectionSetting::kPostgresqlCluster: {
        return GetServiceAccountId(setting.postgresql_cluster().auth());
    }
    // Do not replace with default. Adding a new connection should cause a compilation error
    case FederatedQuery::ConnectionSetting::CONNECTION_NOT_SET:
    break;
    }
    return {};
}

TString ExtractServiceAccountId(const FederatedQuery::ConnectionContent& content) {
    return ExtractServiceAccountId(content.setting());
}

TString ExtractServiceAccountId(const FederatedQuery::Connection& connection) {
    return ExtractServiceAccountId(connection.content());
}

TMaybe<TString> GetLogin(const FederatedQuery::ConnectionSetting& setting) {
    switch (setting.connection_case()) {
        case FederatedQuery::ConnectionSetting::CONNECTION_NOT_SET:
            return {};
        case FederatedQuery::ConnectionSetting::kYdbDatabase:
            return {};
        case FederatedQuery::ConnectionSetting::kClickhouseCluster:
            return setting.clickhouse_cluster().login();
        case FederatedQuery::ConnectionSetting::kDataStreams:
            return {};
        case FederatedQuery::ConnectionSetting::kObjectStorage:
            return {};
        case FederatedQuery::ConnectionSetting::kMonitoring:
            return {};
        case FederatedQuery::ConnectionSetting::kPostgresqlCluster:
            return setting.postgresql_cluster().login();
    }
}

TMaybe<TString> GetPassword(const FederatedQuery::ConnectionSetting& setting) {
    switch (setting.connection_case()) {
        case FederatedQuery::ConnectionSetting::CONNECTION_NOT_SET:
            return {};
        case FederatedQuery::ConnectionSetting::kYdbDatabase:
            return {};
        case FederatedQuery::ConnectionSetting::kClickhouseCluster:
            return setting.clickhouse_cluster().password();
        case FederatedQuery::ConnectionSetting::kDataStreams:
            return {};
        case FederatedQuery::ConnectionSetting::kObjectStorage:
            return {};
        case FederatedQuery::ConnectionSetting::kMonitoring:
            return {};
        case FederatedQuery::ConnectionSetting::kPostgresqlCluster:
            return setting.postgresql_cluster().password();
    }
}

EYdbComputeAuth GetYdbComputeAuthMethod(const FederatedQuery::ConnectionSetting& setting) {
    switch (setting.connection_case()) {
        case FederatedQuery::ConnectionSetting::CONNECTION_NOT_SET:
            return EYdbComputeAuth::UNKNOWN;
        case FederatedQuery::ConnectionSetting::kYdbDatabase:
            return GetIamAuthMethod(setting.ydb_database().auth());
        case FederatedQuery::ConnectionSetting::kClickhouseCluster:
            return GetBasicAuthMethod(setting.clickhouse_cluster().auth());
        case FederatedQuery::ConnectionSetting::kDataStreams:
            return GetIamAuthMethod(setting.data_streams().auth());
        case FederatedQuery::ConnectionSetting::kObjectStorage:
            return GetIamAuthMethod(setting.object_storage().auth());
        case FederatedQuery::ConnectionSetting::kMonitoring:
            return GetIamAuthMethod(setting.monitoring().auth());
        case FederatedQuery::ConnectionSetting::kPostgresqlCluster:
            return GetBasicAuthMethod(setting.postgresql_cluster().auth());
    }
}

FederatedQuery::IamAuth GetAuth(const FederatedQuery::Connection& connection) {
    switch (connection.content().setting().connection_case()) {
    case FederatedQuery::ConnectionSetting::kObjectStorage:
        return connection.content().setting().object_storage().auth();
    case FederatedQuery::ConnectionSetting::kYdbDatabase:
        return connection.content().setting().ydb_database().auth();
    case FederatedQuery::ConnectionSetting::kClickhouseCluster:
        return connection.content().setting().clickhouse_cluster().auth();
    case FederatedQuery::ConnectionSetting::kDataStreams:
        return connection.content().setting().data_streams().auth();
    case FederatedQuery::ConnectionSetting::kMonitoring:
        return connection.content().setting().monitoring().auth();
    case FederatedQuery::ConnectionSetting::kPostgresqlCluster:
        return connection.content().setting().postgresql_cluster().auth();
    case FederatedQuery::ConnectionSetting::CONNECTION_NOT_SET:
        return FederatedQuery::IamAuth{};
    }
}

} // namespace NFq