Seismic Drive Client API
SDManager.h
Go to the documentation of this file.
1 // ********************************************************************************
2 // Copyright (c) 2018-2024 Schlumberger. All Rights Reserved. Schlumberger Private.
3 // ********************************************************************************
4 
5 #pragma once
6 
7 #include "DLL_EXPORT.h"
8 #include "HttpContext.h"
9 #include "Types.h"
10 
11 #include <cstdint>
12 #include <map>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 namespace seismicdrive
18 {
19  /*******************************************************************************
20  * @class SDManager SDManager.h
21  *
22  * @brief Seismic Drive Manager class.
23  *
24  * This class will manage url and apikey for the seismic-drive service, authentication providers and gcsaccessors cache.
25  *
26  ******************************************************************************/
27  class DLL_PUBLIC SDManager
28  {
29  public:
30  /*******************************************************************************
31  * @brief Constructor.
32  *
33  * If executed without parameters it will try to load the seismic-drive service url and apikey from the environment (SDURL, SDAPIKEY).
34  *
35  * @param sdurl is the url of the seismic-drive service.
36  * @param sdapikey is the api key of the seismic-drive service.
37  * @param logLevel is the desired sdapi logging level
38  ******************************************************************************/
39  // pragma: allowlist nextline secret
40  SDManager(const std::string &sdurl = "", const std::string &sdapikey = "", const int16_t logLevel = 0);
41 
42  /*******************************************************************************
43  * @brief Destructor.
44  *
45  * If the file is opened for writing (not READ_ONLY), it wll be closed.
46  ******************************************************************************/
47  ~SDManager();
48 
49  /*******************************************************************************
50  * @brief Move Constructor.
51  *
52  * This constructor moves the SDManager object from
53  * the source to the new object and destroy the source object.
54  *
55  ******************************************************************************/
56  SDManager(SDManager &&sdsm) noexcept;
57 
58  /*******************************************************************************
59  * @brief Move Assignment Operator.
60  *
61  * Same functionality as the move constructor.
62  *
63  ******************************************************************************/
64  SDManager &operator=(SDManager &&sdsm) noexcept;
65 
66  /*******************************************************************************
67  * @brief Copy Constructor.
68  *
69  * This constructor makes a copy of the SDManager object from
70  * the source to the new object without modifying the source object.
71  *
72  ******************************************************************************/
73  SDManager(const SDManager &op);
74 
75  /*******************************************************************************
76  * @brief Copy Assignment Operator.
77  *
78  * Same functionality as the copy constructor.
79  *
80  ******************************************************************************/
81  SDManager &operator=(const SDManager &op);
82 
83  /*******************************************************************************
84  * @brief Authentication provider from impersonation token.
85  *
86  * If the impersonation_token param is not specified, it will try to load it from the environment (SD_IMP_AUTH_TOKEN)
87  *
88  * @param impersonation_token is the impersonation token
89  ******************************************************************************/
90  void setAuthProviderFromImpToken(const std::string &impersonation_token = "");
91 
92  /*******************************************************************************
93  * @brief Authentication provider from string.
94  *
95  * This method will always provide the authentication token provided using the idtoken parameter.
96  *
97  * @param idtoken is the credential token
98  ******************************************************************************/
99  void setAuthProviderFromString(const std::string &idtoken);
100 
101  // Used with setAuthProviderCallback()
102  //
103  typedef std::string (*AuthProviderCallback)(const void *data);
104 
105  /*******************************************************************************
106  * @brief Authentication provider will obtain token from callback (when expired).
107  *
108  * The authentication provider will always use the authentication token returned by the callback.
109  *
110  * @param callback is the callback function to be used
111  *
112  * @param data is passed to the callback, it can be anything.
113  ******************************************************************************/
114  void setAuthProviderCallback(AuthProviderCallback callback, const void *data = nullptr);
115 
116  /*******************************************************************************
117  * @brief gets an ID token.
118  *
119  * This method gets an authentication ID token.
120  *
121  * @return an ID token.
122  ******************************************************************************/
123  std::string getIDToken() const;
124 
125  /*******************************************************************************
126  * @brief gets the url of seismic store svc.
127  *
128  * This method gets the latest url of the seismic store service endpoint.
129  *
130  * @return the url of the seismic-drive service.
131  ******************************************************************************/
132  std::string getSDUrl() const;
133 
134  /*******************************************************************************
135  * @brief gets the API Key of seismic store svc.
136  *
137  * This method gets the API Key of the seismic store service endpoint.
138  *
139  * @return the api key of the seismic-drive service.
140  ******************************************************************************/
141  std::string getSDApiKey() const;
142 
143  /*******************************************************************************
144  * @brief Serialize the SDManager object
145  *
146  * This method serialize the SDManager object and returns the serialized string version.
147  *
148  * @return a serialized version of the SDManager object
149  ******************************************************************************/
150  std::string serialize() const;
151 
152  /*******************************************************************************
153  * @brief Creates an SDManager object.
154  *
155  * This method creates an SDManager instance from a serialized string SDManager object.
156  *
157  * @param sd_manager_serialized is the serialized SDManager object
158  *
159  * @return a deserialized SDManager
160  ******************************************************************************/
161  static SDManager *create(const std::string &sd_manager_serialized);
162 
163  /*******************************************************************************
164  * @brief Deprecated
165  *
166  * Use copyHttpHeaders() instead.
167  ******************************************************************************/
168  static const std::map<std::string, std::string> &getHttpHeaders();
169 
170  /*******************************************************************************
171  * @brief Get HTTP headers
172  *
173  * Returns the HTTP headers set by previous calls to setHttpHeaders()
174  ******************************************************************************/
175  static std::map<std::string, std::string> copyHttpHeaders();
176 
177  /*******************************************************************************
178  * @brief Set HTTP headers
179  *
180  * Sets the HTTP headers to use for subsequent calls to the seismic store service.
181  *
182  * @param httpHeaders contains the HTTP headers to use
183  *
184  * @param clearExistingHeaders if true, removes the HTTP headers set with previous calls to setHttpHeaders()
185  *
186  ******************************************************************************/
187  static void setHttpHeaders(const std::map<std::string, std::string> &httpHeaders, bool clearExistingHeaders = true);
188 
189  /*******************************************************************************
190  * @brief Clear HTTP headers
191  *
192  * Clears the HTTP headers set by previous calls to setHttpHeaders()
193  ******************************************************************************/
194  static void clearHttpHeaders();
195 
196  /*******************************************************************************
197  * @brief Deprecated
198  *
199  * Use setLogLevel() instead.
200  ******************************************************************************/
201  void setLogStatus(const bool status);
202 
203  /*******************************************************************************
204  * @brief Deprecated
205  *
206  * Use getLogLevel() instead.
207  ******************************************************************************/
208  bool getLogStatus() const;
209 
210  /*******************************************************************************
211  * @brief Get value for the translate-user-info parameter in dataset_get()
212  ******************************************************************************/
213  bool isTranslateUserInfo() const;
214 
215  /*******************************************************************************
216  * @brief Sets the translate-user-info parameter in dataset_get()
217  *
218  * @param status value of true or false
219  ******************************************************************************/
220  void translateUserInfo(bool status);
221 
222  /*******************************************************************************
223  * @brief Sets the current logging level
224  *
225  * 0 --> only critical messages will be logged (default)
226  * 1 --> some logging
227  * 2 --> more detailed logging
228  * 3 --> even more details
229  * ...
230  *
231  ******************************************************************************/
232  void setLogLevel(int level);
233 
234  /*******************************************************************************
235  * @brief gets the current logging level
236  *
237  * @return the current logging level
238  ******************************************************************************/
239  int getLogLevel() const;
240 
244  DEPRECATED("Use versionMajor() instead")
245  uint_least16_t getVersionMajor() const;
246 
250  DEPRECATED("Use versionMinor() instead")
251  uint_least16_t getVersionMinor() const;
252 
256  DEPRECATED("Use versionPatch() instead")
257  uint_least16_t getVersionPatch() const;
258 
262  DEPRECATED("Use version() instead")
263  std::string getVersion() const;
264 
265  /*******************************************************************************
266  * @brief gets major version number
267  *
268  * This method returns the sdapi major version number.
269  *
270  * @return the sdapi major version number
271  ******************************************************************************/
272  static uint_least16_t versionMajor();
273 
274  /*******************************************************************************
275  * @brief gets minor version number
276  *
277  * This method returns the sdapi minor version number.
278  *
279  * @return the sdapi minor version number
280  ******************************************************************************/
281  static uint_least16_t versionMinor();
282 
283  /*******************************************************************************
284  * @brief gets version patch number
285  *
286  * This method returns the sdapi version patch number.
287  *
288  * @return the sdapi version patch number
289  ******************************************************************************/
290  static uint_least16_t versionPatch();
291 
292  /*******************************************************************************
293  * @brief gets version
294  *
295  * This method returns the sdapi version as a string.
296  *
297  * The format is major.minor.patch, e.g. "2.7.1"
298  *
299  * @return the sdapi version
300  ******************************************************************************/
301  static std::string version();
302 
303  /*******************************************************************************
304  * @note Method should only be used before the manager is passed to create the
305  * dataset or utils objects
306  *
307  * @brief Sets the ExponentialRetryBackoffPolicy at SDManager level
308  *
309  * This method sets the ExponentialRetryBackoffPolicy at SDManager level for Both
310  * the seismicstore and storage
311  *
312  * @param policy the retry policy that needs to be applied to the service
313  * @param link the HttpConnectionLink that determines if the policy should be
314  * applied to SDMS and/or CLOUD_STORAGE
315  *
316  * usage example:
317  *
318  * ExponentialRetryBackoffPolicy policy;
319  * policy.maxRetry = 3;
320  *
321  * SDManager manager;
322  * manager.setExponentialRetryBackoffPolicy(&policy, HttpConnectionLink::ANY);
323  * SDutils utils(&manager); OR SDGenericDataset dataset(&manager, path);
324  ******************************************************************************/
325  void setExponentialRetryBackoffPolicy(const ExponentialRetryBackoffPolicy *policy, HttpConnectionLink link);
326 
327  /*******************************************************************************
328  * @note ExponentialRetryBackoffPolicy should have been set before the dataset
329  * or utils objects were created
330  *
331  * @brief Returns the retry policy for the HttpConnectionLink provided
332  *
333  * @return a shared_ptr that points to the policy. HttpConnectionLink::ANY will
334  * default to the HttpConnectionLink::SEISMIC_DMS option
335  ******************************************************************************/
336  std::shared_ptr<const ExponentialRetryBackoffPolicy> getExponentialRetryBackoffPolicy(HttpConnectionLink link) const;
337 
338  /*******************************************************************************
339  * @brief Authentication Provider Setter
340  *
341  * @brief This function sets the authentication provider required by the library to generate credentials.
342  *
343  * @param authProvider The authentication provider to use.
344  * @param args The list of arguments to be used with the authentication provider.
345  *
346  * @details This generic method is responsible for configuring the authentication provider for the library. The user needs
347  * to specify the desired authentication provider and provide any necessary arguments. This information is crucial for
348  * the library to generate the appropriate credentials.
349  *
350  * @note Ensure that the authentication provider is compatible with the library, and provide the required arguments accordingly.
351  *
352  * @example
353  * // Example usage of setAuthProvider function
354  * setCredentialsProvider("ExampleProvider", {"arg1", "arg2"});
355  *
356  ******************************************************************************/
357  void setCredentialsProvider(const std::string &authProvider, const std::vector<std::string> &args);
358 
359  // Slb proprietary code start ----------------------------------------------------------
360 
361  /*******************************************************************************
362  * @brief Creates an SDManager object.
363  *
364  * This method creates an SDManager instance from a serialized string SDManager object.
365  *
366  * @param sd_manager_serialized is the serialized SDManager object
367  *
368  * @return a deserialized SDManager
369  ******************************************************************************/
370  static SDManager *create(const std::string &sd_manager_serialized, const std::string &clientId, const std::string &clientSecret);
371  /*******************************************************************************
372  * @brief Removed method. Use setAuthProviderSauthV2() instead.
373  ******************************************************************************/
374  void setAuthProviderSauth(const std::string &clientID = "", const std::string &filepath = "");
375 
376  /*******************************************************************************
377  * @brief Authentication provider from SAuth.
378  *
379  * This generic method will try to automatically generate a token from a PTS env or from a credential file
380  * In a single sign on mode (PTS env) the clientID and filepath parameter can be omitted as are not required (they will be ignored in any case)
381  * If using credentials file the ClientID must be specified and if the filepath is omitted the default one will be used (~/.sdcfg/slbauth.tk)
382  *
383  * @param clientID is the id of the client used to generate the tokens saved in the file.
384  * @param clientCredentialsFile is the path of the file that contain the access and refresh token
385  ******************************************************************************/
386  void setAuthProviderSauthV2(const std::string &clientID, const std::string &clientSecret, const std::string &clientCredentialsFile = "");
387 
388  /*******************************************************************************
389  * @brief Removed method. Use setAuthProviderServiceSauthV2() instead.
390  ******************************************************************************/
391  void setAuthProviderServiceSauth(const std::string &projectID = "", const std::string &serviceID = "", const std::string &privateKey = "");
392 
393  /*******************************************************************************
394  * @brief Authentication provider from SAuth Service Account.
395  *
396  * This generic method will try to automatically generate a token for an sauth service account.
397  *
398  * @param projectID is the id of the service account project.
399  * @param serviceID is the id of the service account.
400  * @param privateKey is the secret private key of the sauth service account.
401  ******************************************************************************/
402  void setAuthProviderServiceSauthV2(const std::string &clientID, const std::string &clientSecret);
403 
404  /*******************************************************************************
405  * @brief Removed method. Use setAuthProviderFromFileV2() instead.
406  ******************************************************************************/
407  void setAuthProviderFromFile(const std::string &clientID, const std::string &filepath = "");
408 
409  /*******************************************************************************
410  * @brief Authentication provider from file. If the filepath param is not specified, it will use the default path ~/.sdcfg/slbauth.tk.
411  *
412  * The file must contain an access token and a refresh token comma separated [access_token],[refresh_token].
413  *
414  * @param filepath is the path of the file that contain the access and refresh token
415  * @param clientID is the id of the client used to generate the tokens saved in the file.
416  ******************************************************************************/
417  void setAuthProviderFromFileV2(const std::string &clientID, const std::string &clientSecret, const std::string &filepath = "");
418 
419  /*******************************************************************************
420  * @brief Authentication provider for impersonation credentials signed with sauth.
421  *
422  * This provider manage impersonation credentials, externally created and signed with sauth.
423  * The impersonation credential is automatically refreshed when expired and dismissed at the end of usage.
424  * If the impersonationToken argument is not specified, the library will try to load it from the environment (SD_IMP_AUTH_TOKEN).
425  * If the impersonationTokenContext argument is not specified, the library will try to load it from the environemnt (SD_IMP_AUTH_TOKEN_CONTEXT)
426  *
427  * @param clientId the client id of the trusted application that generates the impersonation token credential.
428  * @param clientSecret the client secret of the trusted application that generates the impersonation token credential.
429  * @param impersonationToken the impersonation token.
430  * @param impersonationTokenContext the impersonation token context.
431  ******************************************************************************/
432  void setAuthProviderSauthImpersonationToken(
433  const std::string &clientId,
434  const std::string &clientSecret,
435  const std::string &impersonationToken = "",
436  const std::string &impersonationTokenContext = "");
437  /*******************************************************************************
438  * @brief Single page application credentials provider.
439  *
440  * This provider will automatically refresh client credentials.
441  * It requires a single page application public client (enabled to handle refresh credentials).
442  *
443  * optional env settings:
444  * 1. SDAPI_SAUTH_CREDENTIALS_FILENAME: string = if set the method will flush
445  * refreshed credentials, comma separated, in the specified filename as "access-token,refresh-token"
446  * 2. SDAPI_SAUTH_USE_PROD: switch "ON" or "OFF" = enforce the use of sauth prod endpoints,
447  * by default it is disabled and sauth p4d is used.
448  * 3. SDAPI_SAUTH_TOKEN_SERVICE_URL: string = specify the sauth token service endpoints url to use
449  *
450  * @param clientID is the single page application public client id
451  * @param accessToken is the user access token
452  * @param refreshToken is the user refresh token
453  ******************************************************************************/
454  void setAuthProviderSauthSinglePageApp(
455  const std::string &clientID, const std::string &accessToken, const std::string &refreshToken);
456 
457  /*******************************************************************************
458  * @brief Single page application credentials provider.
459  *
460  * This provider will automatically refresh client credentials.
461  * It requires a single page application public client (enabled to handle refresh credentials).
462  *
463  * optional env settings:
464  * 1. SDAPI_SAUTH_USE_PROD: switch "ON" or "OFF" = enforce the use of sauth prod endpoints,
465  * by default it is disabled and sauth p4d is used.
466  * 2. SDAPI_SAUTH_TOKEN_SERVICE_URL: string = specify the sauth token service endpoints url to use
467  *
468  * @param clientID is the single page application public client id
469  * @param accessToken is the user access token
470  * @param refreshToken is the user refresh token
471  * @param credentialsFileName file where to load and save refreshed credentials.
472  * If set as empty, the access-token and refresh-token argument will be used
473  ******************************************************************************/
474  void setAuthProviderSauthSinglePageApp(
475  const std::string &clientID, const std::string &accessToken, const std::string &refreshToken, const std::string &credentialsFileName);
476 
477  /*******************************************************************************
478  * @brief Single page application credentials provider.
479  *
480  * This provider will automatically refresh client credentials.
481  * It requires a single page application public client (enabled to handle refresh credentials).
482  *
483  * optional env settings:
484  * 1. SDAPI_SAUTH_USE_PROD: switch "ON" or "OFF" = enforce the use of sauth prod endpoints,
485  * by default it is disabled and sauth p4d is used.
486  * 2. SDAPI_SAUTH_TOKEN_SERVICE_URL: string = specify the sauth token service endpoints url to use
487  *
488  * @param clientID is the single page application public client id
489  * @param credentialsFileName file where to load and save refreshed credentials
490  ******************************************************************************/
491  void setAuthProviderSauthSinglePageApp(
492  const std::string &clientID, const std::string &credentialsFileName);
493 
494  private:
495  SDManager(const std::string &managerSerialized, const std::string &clientId, const std::string &clientSecret);
496  // Slb proprietary code end ----------------------------------------------------------
497 
498  private:
499 #ifdef _MSC_VER
500  #pragma warning(push)
501  #pragma warning(disable : 4251)
502 #endif
503 
504  class Impl;
505 
506  std::unique_ptr<Impl> _impl;
507 
508 #ifdef _MSC_VER
509  #pragma warning(pop)
510 #endif
511  friend class SDDataset;
512  friend class SDManagerImplTest;
514  friend class SeismicStore;
515  };
516 }
517 
518 // Slb proprietary code start ----------------------------------------------------------
519 
520 #include "SDException.h"
521 
522 namespace seismicdrive
523 {
524  namespace error
525  {
526  namespace manager
527  {
531  class DLL_PUBLIC ClientSecretNotSet : public Error
532  {
533  public:
534  class Impl;
535 
536  ClientSecretNotSet(const Impl &impl);
537 
539 
540  protected:
541  ClientSecretNotSet(Impl *impl);
542 
543  Impl &impl;
544  };
545  }
546  }
547 }
548 
549 // Slb proprietary code end ----------------------------------------------------------
Impl & impl
Definition: SDManager.h:543
Defines the exponential retry backoff policy parameters.
Definition: HttpContext.h:67
This class represents a generic cloud storage dataset in read only mode.
Definition: SDReadOnlyGenericDatasetAccessor.h:36
HttpConnectionLink
Defines the sdapi connection links.
Definition: HttpContext.h:31
Definition: SDManager.h:27
Definition: Constants.h:27
Definition: SDException.h:740