SDManagerImpl.h 6.9 KB
Newer Older
1
// ============================================================================
Diego Molteni's avatar
Diego Molteni committed
2
// Copyright 2017-2021, Schlumberger
Sid Stenersen's avatar
Sid Stenersen committed
3
//
4
5
6
// Licensed 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
Sid Stenersen's avatar
Sid Stenersen committed
7
//
8
//      http://www.apache.org/licenses/LICENSE-2.0
Sid Stenersen's avatar
Sid Stenersen committed
9
//
10
11
12
13
14
15
16
// 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.
// ============================================================================

17
#pragma once
18
19

#include "DatasetPath.h"
Sid Stenersen's avatar
Sid Stenersen committed
20
#include "SDExceptionImpl.h"
Sid Stenersen's avatar
Sid Stenersen committed
21
#include "SDManager.h"
22
23
#include "SDVersion.h"

Sid Stenersen's avatar
Sid Stenersen committed
24
25
#include "auth/callback_auth_provider.h"
#include "auth/direct_auth_provider.h"
26
#include "auth/gcs_accessor_auth_provider.h"
Sid Stenersen's avatar
Sid Stenersen committed
27
#include "auth/imp_auth_provider.h"
28
29
#include "shared/config.h"
#include "shared/logger.h"
Sid Stenersen's avatar
Sid Stenersen committed
30
#include "shared/mex.h"
31
32
33
34
#include "shared/utils.h"

#include <cstdint>
#include <map>
Sid Stenersen's avatar
Sid Stenersen committed
35
#include <mutex>
36
37
#include <sstream>

Sid Stenersen's avatar
Sid Stenersen committed
38
39
namespace
{
40
41
42
    std::mutex _sdmmu;
}

Sid Stenersen's avatar
Sid Stenersen committed
43
44
45
46
47
namespace seismicdrive
{
    class SDManager::Impl
    {
      public:
Sid Stenersen's avatar
Sid Stenersen committed
48
        Impl(const std::string &sdurl, const std::string &sdapikey, int logLevel)
49
50
51
            : _sdurl(sdurl),
              _sdapikey(sdapikey),
              _log_level(logLevel)
Sid Stenersen's avatar
Sid Stenersen committed
52
53
54
        {
            if (_sdurl.empty())
            {
Sid Stenersen's avatar
Sid Stenersen committed
55
56
57
58
59
                _sdurl = sdutils::getStringEnv(sdconfig::SDURL);
                if (_sdurl.empty())
                {
                    throw error::manager::ServiceUrlNotSet(1);
                }
60
            }
Sid Stenersen's avatar
Sid Stenersen committed
61
62
            if (_sdapikey.empty())
            {
Sid Stenersen's avatar
Sid Stenersen committed
63
64
65
66
67
                _sdapikey = sdutils::getStringEnv(sdconfig::SDAPIKEY);
                if (_sdapikey.empty())
                {
                    throw error::manager::ApiKeyNotSet(1);
                }
68
            }
69
            if (!_log_level)
Sid Stenersen's avatar
Sid Stenersen committed
70
            {
71
                _log_level = sdutils::getNumericEnv(sdconfig::LOG_LEVEL);
Sid Stenersen's avatar
Sid Stenersen committed
72
            }
73
74
        }

75
        void setAuthProviderFromString(const std::string &idtoken)
Sid Stenersen's avatar
Sid Stenersen committed
76
        {
77
            std::unique_lock<std::mutex> lock(_sdmmu);
78
            if (_sd_auth_provider)
Sid Stenersen's avatar
Sid Stenersen committed
79
            {
80
                _sd_auth_provider->refreshServiceAuthToken(idtoken);
81
            }
Sid Stenersen's avatar
Sid Stenersen committed
82
83
            else
            {
84
85
86
                _sd_auth_provider.reset(new DirectAuthProvider(_sdurl, _sdapikey, idtoken));
                _authProviderType = AuthProviderType::Direct;
                _sd_auth_provider->setLogLevel(_log_level);
87
88
89
            }
        };

90
        void setAuthProviderFromImpToken(const std::string &imp_token)
Sid Stenersen's avatar
Sid Stenersen committed
91
        {
92
            std::unique_lock<std::mutex> lock(_sdmmu);
93
            if (_sd_auth_provider)
Sid Stenersen's avatar
Sid Stenersen committed
94
            {
95
                _sd_auth_provider->refreshServiceAuthToken(imp_token);
96
            }
Sid Stenersen's avatar
Sid Stenersen committed
97
98
            else
            {
99
100
101
                _sd_auth_provider.reset(new ImpAuthProvider(_sdurl, _sdapikey, imp_token));
                _authProviderType = AuthProviderType::Imp;
                _sd_auth_provider->setLogLevel(_log_level);
102
103
104
            }
        };

Sid Stenersen's avatar
Sid Stenersen committed
105
106
        void setAuthProviderCallback(AuthProviderCallback callback, const void *data)
        {
Diego Molteni's avatar
Diego Molteni committed
107
            std::unique_lock<std::mutex> lock(_sdmmu);
108
            if (_sd_auth_provider)
Sid Stenersen's avatar
Sid Stenersen committed
109
            {
Diego Molteni's avatar
Diego Molteni committed
110
111
112
                auto ap = _sd_auth_provider.get();
                static_cast<CallbackAuthProvider *>(ap)->setCallback(callback, data);
            }
Sid Stenersen's avatar
Sid Stenersen committed
113
114
            else
            {
Diego Molteni's avatar
Diego Molteni committed
115
                _sd_auth_provider.reset(new CallbackAuthProvider(callback, data, _sdurl, _sdapikey));
116
117
                _authProviderType = AuthProviderType::Callback;
                _sd_auth_provider->setLogLevel(_log_level);
Diego Molteni's avatar
Diego Molteni committed
118
119
120
            }
        };

Sid Stenersen's avatar
Sid Stenersen committed
121
122
        std::string getIDToken() const
        {
123
            if (!_sd_auth_provider)
Sid Stenersen's avatar
Sid Stenersen committed
124
            {
Sid Stenersen's avatar
Sid Stenersen committed
125
                throw error::manager::AuthProviderNotSet(1);
126
            }
127
            return _sd_auth_provider->getServiceAuthToken();
128
129
        }

Sid Stenersen's avatar
Sid Stenersen committed
130
131
        std::string getSDUrl() const
        {
132
133
134
            return _sdurl;
        };

Sid Stenersen's avatar
Sid Stenersen committed
135
136
        std::string getSDApiKey() const
        {
137
138
139
            return _sdapikey;
        }

Sid Stenersen's avatar
Sid Stenersen committed
140
141
        AuthProvider *getAuthProvider() const
        {
142
143
144
            return _sd_auth_provider.get();
        }

Sid Stenersen's avatar
Sid Stenersen committed
145
146
        std::string serialize() const
        {
147
148
            std::string ss{sdconfig::SDMANAGER_SERIALV + ';' + _sdurl + ';' + _sdapikey + ';' + std::to_string(_log_level) + ';' + std::to_string(_authProviderType)};
            ss += (';' + getIDToken());
149
150
151
            return ss;
        }

Sid Stenersen's avatar
Sid Stenersen committed
152
153
        void deserialize(const std::string &sdms)
        {
Sid Stenersen's avatar
Sid Stenersen committed
154
            auto tokens = sdutils::split(sdms, ';', true);
Sid Stenersen's avatar
Sid Stenersen committed
155
156
            if (tokens[0] != sdconfig::SDMANAGER_SERIALV)
            {
Sid Stenersen's avatar
Sid Stenersen committed
157
                throw error::manager::context::UnknownFormat(1);
158
159
160
            }
            _sdurl = tokens[1];
            _sdapikey = tokens[2];
161
            _log_level = std::stoi(tokens[3]);
162
163

            int atc = atoi(tokens[4].c_str());
164
165
            _sd_auth_provider.reset(); // Force creation of the right type of AuthProvider.
            if (atc == AuthProviderType::Direct)
Sid Stenersen's avatar
Sid Stenersen committed
166
            {
167
                setAuthProviderFromString(tokens[5]);
Diego Molteni's avatar
Diego Molteni committed
168
            }
169
            else if (atc == AuthProviderType::Imp)
Sid Stenersen's avatar
Sid Stenersen committed
170
            {
171
172
                setAuthProviderFromImpToken(tokens[5]);
            }
173
            else if (atc == AuthProviderType::Callback)
Sid Stenersen's avatar
Sid Stenersen committed
174
            {
Diego Molteni's avatar
Diego Molteni committed
175
176
                // User must call setAuthProviderCallback()
            }
177
178
        }

179
        int getLogLevel() const
Sid Stenersen's avatar
Sid Stenersen committed
180
        {
181
            return _log_level;
Sid Stenersen's avatar
Sid Stenersen committed
182
183
        }

184
        void setLogLevel(int level)
Sid Stenersen's avatar
Sid Stenersen committed
185
        {
186
187
188
189
190
            _log_level = level;
            if (_sd_auth_provider)
            {
                _sd_auth_provider->setLogLevel(level);
            }
191
192
        }

Sid Stenersen's avatar
Sid Stenersen committed
193
        static uint_least16_t versionMajor()
Sid Stenersen's avatar
Sid Stenersen committed
194
        {
195
196
            return static_cast<uint_least16_t>(SDVersion::Major);
        }
Sid Stenersen's avatar
Sid Stenersen committed
197

Sid Stenersen's avatar
Sid Stenersen committed
198
        static uint_least16_t versionMinor()
Sid Stenersen's avatar
Sid Stenersen committed
199
        {
200
201
202
            return static_cast<uint_least16_t>(SDVersion::Minor);
        }

Sid Stenersen's avatar
Sid Stenersen committed
203
        static uint_least16_t versionPatch()
Sid Stenersen's avatar
Sid Stenersen committed
204
        {
205
206
207
            return static_cast<uint_least16_t>(SDVersion::Patch);
        }

Sid Stenersen's avatar
Sid Stenersen committed
208
        static std::string version()
Sid Stenersen's avatar
Sid Stenersen committed
209
        {
Sid Stenersen's avatar
Sid Stenersen committed
210
211
            static std::string v;
            if (v.empty())
Sid Stenersen's avatar
Sid Stenersen committed
212
            {
Sid Stenersen's avatar
Sid Stenersen committed
213
214
215
                std::stringstream ss;
                ss << versionMajor() << '.' << versionMinor() << '.' << versionPatch();
                v = ss.str();
216
            }
Sid Stenersen's avatar
Sid Stenersen committed
217
            return v;
218
        }
Sid Stenersen's avatar
Sid Stenersen committed
219

Sid Stenersen's avatar
Sid Stenersen committed
220
      private:
Sid Stenersen's avatar
Sid Stenersen committed
221
        std::shared_ptr<AuthProvider> _sd_auth_provider;
222

223
224
        std::string _sdurl, _sdapikey;

225
        int _log_level;
226
227

        // For serialization/deserialization
228
229
230
231
232
        enum AuthProviderType
        {
            None,
            Direct,
            Callback,
Diego Molteni's avatar
Diego Molteni committed
233

234
235
236
237
            Imp = 100, // deprecated
        };

        int _authProviderType = AuthProviderType::None;
238
239
    };
}