Coverage Report

Created: 2026-08-06 18:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/http_handler_with_auth.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "service/http/http_handler_with_auth.h"
19
20
#include <gen_cpp/HeartbeatService_types.h>
21
22
#include "runtime/cluster_info.h"
23
#include "service/http/http_channel.h"
24
#include "service/http/utils.h"
25
#include "util/client_cache.h"
26
#include "util/thrift_rpc_helper.h"
27
28
namespace doris {
29
30
class TPrivilegeType;
31
class TPrivilegeHier;
32
class ThriftRpcHelper;
33
34
HttpHandlerWithAuth::HttpHandlerWithAuth(ExecEnv* exec_env, TPrivilegeHier::type hier,
35
                                         TPrivilegeType::type type)
36
40
        : _exec_env(exec_env), _hier(hier), _type(type) {}
37
38
64
int HttpHandlerWithAuth::on_header(HttpRequest* req) {
39
    //if u return value isn't 0,u should `send_reply`,Avoid requesting links that never return.
40
64
    TCheckAuthRequest auth_request;
41
64
    TCheckAuthResult auth_result;
42
64
    AuthInfo auth_info;
43
44
64
    if (!config::enable_all_http_auth) {
45
30
        return 0;
46
30
    }
47
48
    // When enable_all_http_auth is true, even NONE type APIs require authentication
49
    // (enable_all_http_auth means "enable authentication for ALL HTTP APIs")
50
    // Note: This overrides the default behavior where NONE type APIs bypass auth
51
52
34
    if (!parse_basic_auth(*req, &auth_info)) {
53
15
        LOG(WARNING) << "parse basic authorization failed"
54
15
                     << ", request: " << req->debug_string();
55
15
        evhttp_add_header(evhttp_request_get_output_headers(req->get_evhttp_request()),
56
15
                          "WWW-Authenticate", "Basic realm=\"Restricted\"");
57
15
        HttpChannel::send_reply(req, HttpStatus::UNAUTHORIZED);
58
15
        return -1;
59
15
    }
60
61
    // check auth by token
62
19
    if (auth_info.token != "") {
63
2
#ifdef BE_TEST
64
2
        if (auth_info.token == "valid_token") {
65
1
            return 0;
66
#else
67
        if (_exec_env->check_auth_token(auth_info.token)) {
68
            return 0;
69
#endif
70
1
        } else {
71
1
            LOG(WARNING) << "invalid auth token, request: " << req->debug_string();
72
1
            HttpChannel::send_error(req, HttpStatus::BAD_REQUEST);
73
1
            return -1;
74
1
        }
75
2
    }
76
77
    // check auth by user/password
78
17
    auth_request.user = auth_info.user;
79
17
    auth_request.passwd = auth_info.passwd;
80
17
    auth_request.__set_cluster(auth_info.cluster);
81
17
    auth_request.__set_user_ip(auth_info.user_ip);
82
17
    auth_request.__set_thrift_rpc_timeout_ms(config::thrift_rpc_timeout_ms);
83
84
17
    if (!on_privilege(*req, auth_request)) {
85
1
        LOG(WARNING) << "invalid privilege, request: " << req->debug_string();
86
1
        HttpChannel::send_error(req, HttpStatus::BAD_REQUEST);
87
1
        return -1;
88
1
    }
89
90
#ifndef BE_TEST
91
    TNetworkAddress master_addr = _exec_env->cluster_info()->master_fe_addr;
92
    if (master_addr.hostname.empty() || master_addr.port == 0) {
93
        LOG(WARNING) << "Not found master fe, Can't auth API request: " << req->debug_string();
94
        HttpChannel::send_error(req, HttpStatus::SERVICE_UNAVAILABLE);
95
        return -1;
96
    }
97
    {
98
        auto status = ThriftRpcHelper::rpc<FrontendServiceClient>(
99
                master_addr.hostname, master_addr.port,
100
                [&auth_result, &auth_request](FrontendServiceConnection& client) {
101
                    client->checkAuth(auth_result, auth_request);
102
                });
103
        if (!status) {
104
            LOG(WARNING) << "CheckAuth Rpc Fail.Fe Ip:" << master_addr.hostname
105
                         << ", Fe port:" << master_addr.port << ".Status:" << status.to_string()
106
                         << ".Request: " << req->debug_string();
107
            HttpChannel::send_error(req, HttpStatus::SERVICE_UNAVAILABLE);
108
            return -1;
109
        }
110
    }
111
#else
112
16
    if (auth_request.user == "root" && auth_request.passwd.empty()) {
113
5
        auth_result.status.status_code = TStatusCode::type::OK;
114
5
        auth_result.status.error_msgs.clear();
115
11
    } else {
116
11
        HttpChannel::send_reply(req, HttpStatus::FORBIDDEN);
117
11
        return -1;
118
11
    }
119
5
#endif
120
5
    Status status(Status::create(auth_result.status));
121
5
    if (!status.ok()) {
122
0
        LOG(WARNING) << "permission verification failed, request: " << auth_request;
123
0
        HttpChannel::send_reply(req, HttpStatus::FORBIDDEN);
124
0
        return -1;
125
0
    }
126
5
    return 0;
127
5
}
128
129
} // namespace doris