Authentication.cc 6.0 KB
Newer Older
S
saandrews 已提交
1
/**
2 3 4 5 6 7 8
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
S
saandrews 已提交
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
S
saandrews 已提交
11
 *
12 13 14 15 16 17
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
S
saandrews 已提交
18 19 20
 */
#include <stdio.h>

21
#include <pulsar/Authentication.h>
S
saandrews 已提交
22 23 24 25 26 27 28 29 30
#include <lib/LogUtils.h>

#include <string>
#include <vector>
#include <iostream>
#include <dlfcn.h>
#include <cstdlib>
#include <boost/make_shared.hpp>
#include <boost/thread.hpp>
31
#include <boost/algorithm/string.hpp>
S
saandrews 已提交
32 33 34 35

DECLARE_LOG_OBJECT()

namespace pulsar {
36

37
    AuthenticationDataProvider::AuthenticationDataProvider(){
38

S
saandrews 已提交
39
    }
40

41
    AuthenticationDataProvider::~AuthenticationDataProvider() {
42

S
saandrews 已提交
43
    }
44

45 46 47
    bool AuthenticationDataProvider::hasDataForTls() {
        return false;
    }
48

49
    std::string AuthenticationDataProvider::getTlsCertificates() {
S
saandrews 已提交
50 51
        return "none";
    }
52

53 54
    std::string AuthenticationDataProvider::getTlsPrivateKey() {
        return "none";
S
saandrews 已提交
55
    }
56

57 58
    bool AuthenticationDataProvider::hasDataForHttp() {
        return false;
S
saandrews 已提交
59
    }
60

61 62 63
    std::string AuthenticationDataProvider::getHttpAuthType() {
        return "none";
    }
64

65 66 67
    std::string AuthenticationDataProvider::getHttpHeaders() {
        return "none";
    }
68

69 70 71
    bool AuthenticationDataProvider::hasDataFromCommand(){
        return false;
    }
72

73 74 75
    std::string AuthenticationDataProvider::getCommandData() {
        return "none";
    }
76 77


78 79
    Authentication::Authentication() {
    }
80

81 82
    Authentication::~Authentication() {
    }
83

84 85 86 87 88
    class AuthDisabledData : public AuthenticationDataProvider {
    public:
        AuthDisabledData(ParamMap& params){
        }
    };
89

90 91 92 93 94
    class AuthDisabled : public Authentication {
    public:
        AuthDisabled(AuthenticationDataPtr& authData) {
            authData_ = authData;
        }
95

96 97 98 99
        static AuthenticationPtr create(ParamMap& params) {
            AuthenticationDataPtr authData = AuthenticationDataPtr(new AuthDisabledData(params));
            return AuthenticationPtr(new AuthDisabled(authData));
        }
100

101 102 103 104
        const std::string getAuthMethodName() const {
            return "none";
        }
    };
105 106


107
    AuthenticationPtr AuthFactory::Disabled() {
108 109 110
        ParamMap params;
        return AuthDisabled::create(params);
    }
111

112
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath) {
113
        ParamMap params;
114
        return AuthFactory::create(dynamicLibPath, params);
115
    }
116

117
    boost::mutex mutex;
118 119
    std::vector<void*> AuthFactory::loadedLibrariesHandles_;
    bool AuthFactory::isShutdownHookRegistered_ = false;
120

121
    void AuthFactory::release_handles() {
S
saandrews 已提交
122
        boost::lock_guard<boost::mutex> lock(mutex);
123
        for (std::vector<void*>::iterator ite = AuthFactory::loadedLibrariesHandles_.begin(); ite != AuthFactory::loadedLibrariesHandles_.end();
124 125 126 127 128
             ite++) {
            dlclose(*ite);
        }
        loadedLibrariesHandles_.clear();
    }
129

130
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath, const std::string& authParamsString) {
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
        {
            boost::lock_guard<boost::mutex> lock(mutex);
            if (!AuthFactory::isShutdownHookRegistered_) {
                atexit(release_handles);
                AuthFactory::isShutdownHookRegistered_ = true;
            }
        }
        Authentication *auth = NULL;
        void *handle = dlopen(dynamicLibPath.c_str(), RTLD_LAZY);
        if (handle != NULL) {
            {
                boost::lock_guard<boost::mutex> lock(mutex);
                loadedLibrariesHandles_.push_back(handle);
            }
            Authentication *(*createAuthentication)(const std::string&);
            *(void **) (&createAuthentication) = dlsym(handle, "create");
            if (createAuthentication != NULL) {
                auth = createAuthentication(authParamsString);
            } else {
                ParamMap paramMap;
                if(!authParamsString.empty()) {
                    std::vector<std::string> params;
                    boost::algorithm::split(params, authParamsString, boost::is_any_of(","));
                    for(int i = 0; i<params.size(); i++) {
                        std::vector<std::string> kv;
                        boost::algorithm::split(kv, params[i], boost::is_any_of(":"));
                        if (kv.size() == 2) {
                            paramMap[kv[0]] = kv[1];
                        }
                    }
161
                }
162
                return AuthFactory::create(dynamicLibPath, paramMap);
163
            }
S
saandrews 已提交
164
        }
165
        return AuthenticationPtr(auth);
S
saandrews 已提交
166
    }
167

168
    AuthenticationPtr AuthFactory::create(const std::string& dynamicLibPath, ParamMap& params) {
S
saandrews 已提交
169 170
        {
            boost::lock_guard<boost::mutex> lock(mutex);
171
            if (!AuthFactory::isShutdownHookRegistered_) {
172
                atexit(release_handles);
173
                AuthFactory::isShutdownHookRegistered_ = true;
174
            }
S
saandrews 已提交
175
        }
176 177 178 179 180 181
        Authentication *auth = NULL;
        void *handle = dlopen(dynamicLibPath.c_str(), RTLD_LAZY);
        if (handle != NULL) {
            boost::lock_guard<boost::mutex> lock(mutex);
            loadedLibrariesHandles_.push_back(handle);
            Authentication *(*createAuthentication)(ParamMap&);
182
            *(void **) (&createAuthentication) = dlsym(handle, "createFromMap");
183 184 185
            if (createAuthentication != NULL) {
                auth = createAuthentication(params);
            }
S
saandrews 已提交
186
        }
187
        return AuthenticationPtr(auth);
S
saandrews 已提交
188
    }
189

S
saandrews 已提交
190
}