AWS SDK for C++  0.12.9
AWS SDK for C++
AWSCredentialsProvider.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 
17 #pragma once
18 
19 #include <aws/core/Core_EXPORTS.h>
20 
24 
25 #include <memory>
26 #include <mutex>
27 
28 namespace Aws
29 {
30  namespace Internal
31  {
32  class EC2MetadataClient; //forward declaration;
33  } // namespace Internal
34  namespace Auth
35  {
36  static int REFRESH_THRESHOLD = 1000 * 60 * 15;
37 
42  {
43  public:
47  AWSCredentials(const Aws::String& accessKeyId, const Aws::String& secretKey, const Aws::String& sessionToken = "") :
48  m_accessKeyId(accessKeyId), m_secretKey(secretKey), m_sessionToken(sessionToken)
49  {
50  }
51 
55  inline const Aws::String& GetAWSAccessKeyId() const
56  {
57  return m_accessKeyId;
58  }
59 
63  inline const Aws::String& GetAWSSecretKey() const
64  {
65  return m_secretKey;
66  }
67 
71  inline const Aws::String& GetSessionToken() const
72  {
73  return m_sessionToken;
74  }
75 
79  inline void SetAWSAccessKeyId(const Aws::String& accessKeyId)
80  {
81  m_accessKeyId = accessKeyId;
82  }
83 
87  inline void SetAWSSecretKey(const Aws::String& secretKey)
88  {
89  m_secretKey = secretKey;
90  }
91 
95  inline void SetSessionToken(const Aws::String& sessionToken)
96  {
97  m_sessionToken = sessionToken;
98  }
99 
103  inline void SetAWSAccessKeyId(const char* accessKeyId)
104  {
105  m_accessKeyId = accessKeyId;
106  }
107 
111  inline void SetAWSSecretKey(const char* secretKey)
112  {
113  m_secretKey = secretKey;
114  }
115 
119  inline void SetSessionToken(const char* sessionToken)
120  {
121  m_sessionToken = sessionToken;
122  }
123 
124  private:
125  Aws::String m_accessKeyId;
126  Aws::String m_secretKey;
127  Aws::String m_sessionToken;
128  };
129 
135  {
136  public:
141  AWSCredentialsProvider() : m_lastLoadedMs(0)
142  {
143  }
144 
145  virtual ~AWSCredentialsProvider() = default;
146 
150  virtual AWSCredentials GetAWSCredentials() = 0;
151 
152  protected:
157  virtual bool IsTimeToRefresh(long reloadFrequency);
158 
159  private:
160  long long m_lastLoadedMs;
161  };
162 
168  {
169  public:
173  inline AWSCredentials GetAWSCredentials() override { return AWSCredentials("", ""); }
174  };
175 
182  {
183  public:
187  inline SimpleAWSCredentialsProvider(const Aws::String& awsAccessKeyId, const Aws::String& awsSecretAccessKey, const Aws::String& sessionToken = "")
188  : m_accessKeyId(awsAccessKeyId), m_secretAccessKey(awsSecretAccessKey), m_sessionToken(sessionToken)
189  { }
190 
194  inline SimpleAWSCredentialsProvider(const AWSCredentials& credentials)
195  : m_accessKeyId(credentials.GetAWSAccessKeyId()), m_secretAccessKey(credentials.GetAWSSecretKey()),
196  m_sessionToken(credentials.GetSessionToken())
197  { }
198 
203  {
204  return AWSCredentials(m_accessKeyId, m_secretAccessKey, m_sessionToken);
205  }
206 
207  private:
208  Aws::String m_accessKeyId;
209  Aws::String m_secretAccessKey;
210  Aws::String m_sessionToken;
211  };
212 
218  {
219  public:
224  AWSCredentials GetAWSCredentials() override;
225  };
226 
234  {
235  public:
236 
240  ProfileConfigFileAWSCredentialsProvider(long refreshRateMs = REFRESH_THRESHOLD);
241 
246  ProfileConfigFileAWSCredentialsProvider(const char* profile, long refreshRateMs = REFRESH_THRESHOLD);
247 
251  AWSCredentials GetAWSCredentials() override;
252 
256  static Aws::String GetProfileFilename();
257 
261  static Aws::String GetProfileFilenameNoPath();
262 
266  static Aws::String GetProfileDirectory();
267 
268  private:
269 
273  void RefreshIfExpired();
274  static Aws::Map<Aws::String, Aws::String> ParseProfileConfigFile(const Aws::String& filename);
275 
276  Aws::String m_fileName;
277  Aws::String m_profileToUse;
278  std::shared_ptr<AWSCredentials> m_credentials;
279  mutable std::mutex m_reloadMutex;
280  long m_loadFrequencyMs;
281  };
282 
288  {
289  public:
294  InstanceProfileCredentialsProvider(long refreshRateMs = REFRESH_THRESHOLD);
295 
300  InstanceProfileCredentialsProvider(const std::shared_ptr<Internal::EC2MetadataClient>&, long refreshRateMs = REFRESH_THRESHOLD);
301 
305  AWSCredentials GetAWSCredentials() override;
306 
307  private:
308  void RefreshIfExpired();
309 
310  std::shared_ptr<Internal::EC2MetadataClient> m_metadataClient;
311  std::shared_ptr<AWSCredentials> m_credentials;
312  long m_loadFrequencyMs;
313  mutable std::mutex m_reloadMutex;
314  };
315  } // namespace Auth
316 } // namespace Aws
317 
void SetAWSAccessKeyId(const Aws::String &accessKeyId)
const Aws::String & GetAWSSecretKey() const
static int REFRESH_THRESHOLD
void SetAWSSecretKey(const Aws::String &secretKey)
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition: AWSMap.h:28
const Aws::String & GetAWSAccessKeyId() const
void SetAWSAccessKeyId(const char *accessKeyId)
AWSCredentials(const Aws::String &accessKeyId, const Aws::String &secretKey, const Aws::String &sessionToken="")
SimpleAWSCredentialsProvider(const AWSCredentials &credentials)
void SetSessionToken(const char *sessionToken)
void SetAWSSecretKey(const char *secretKey)
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition: AWSString.h:97
SimpleAWSCredentialsProvider(const Aws::String &awsAccessKeyId, const Aws::String &awsSecretAccessKey, const Aws::String &sessionToken="")
void SetSessionToken(const Aws::String &sessionToken)
const Aws::String & GetSessionToken() const
#define AWS_CORE_API
Definition: Core_EXPORTS.h:30
JSON (JavaScript Object Notation).