This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
AWSCredentials.cs
1172 lines (971 loc) · 40.3 KB
/
AWSCredentials.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright 2014-2015 Amazon.com,
// Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Amazon Software License (the "License").
// You may not use this file except in compliance with the
// License. A copy of the License is located at
//
// http://aws.amazon.com/asl/
//
// or in the "license" file accompanying this file. This file is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, express or implied. See the License
// for the specific language governing permissions and
// limitations under the License.
//
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
#if BCL
using System.Configuration;
#endif
using ThirdParty.Json.LitJson;
using Amazon.Runtime.Internal.Util;
using System.Globalization;
using Amazon.Util;
namespace Amazon.Runtime
{
/// <summary>
/// Immutable representation of AWS credentials.
/// </summary>
public class ImmutableCredentials
{
#region Properties
/// <summary>
/// Gets the AccessKey property for the current credentials.
/// </summary>
public string AccessKey { get; private set; }
/// <summary>
/// Gets the SecretKey property for the current credentials.
/// </summary>
public string SecretKey { get; private set; }
/// <summary>
/// Gets the Token property for the current credentials.
/// </summary>
public string Token { get; private set; }
/// <summary>
/// Gets the UseToken property for the current credentials.
/// Specifies if Token property is non-emtpy.
/// </summary>
public bool UseToken { get { return !string.IsNullOrEmpty(Token); } }
#endregion
#region Constructors
/// <summary>
/// Constructs an ImmutableCredentials object with supplied accessKey, secretKey.
/// </summary>
/// <param name="awsAccessKeyId"></param>
/// <param name="awsSecretAccessKey"></param>
/// <param name="token">Optional. Can be set to null or empty for non-session credentials.</param>
public ImmutableCredentials(string awsAccessKeyId, string awsSecretAccessKey, string token)
{
if (string.IsNullOrEmpty(awsAccessKeyId)) throw new ArgumentNullException("awsAccessKeyId");
if (string.IsNullOrEmpty(awsSecretAccessKey)) throw new ArgumentNullException("awsSecretAccessKey");
AccessKey = awsAccessKeyId;
SecretKey = awsSecretAccessKey;
Token = token ?? string.Empty;
}
private ImmutableCredentials() { }
#endregion
#region Public methods
/// <summary>
/// Returns a copy of the current credentials.
/// </summary>
/// <returns></returns>
public ImmutableCredentials Copy()
{
ImmutableCredentials credentials = new ImmutableCredentials
{
AccessKey = this.AccessKey,
SecretKey = this.SecretKey,
Token = this.Token,
};
return credentials;
}
#endregion
#region Overrides
public override int GetHashCode()
{
return Hashing.Hash(AccessKey, SecretKey, Token);
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(this, obj))
return true;
ImmutableCredentials ic = obj as ImmutableCredentials;
if (ic == null)
return false;
return AWSSDKUtils.AreEqual(
new object[] { AccessKey, SecretKey, Token },
new object[] { ic.AccessKey, ic.SecretKey, ic.Token });
}
#endregion
}
/// <summary>
/// Abstract class that represents a credentials object for AWS services.
/// </summary>
public abstract class AWSCredentials
{
/// <summary>
/// Returns a copy of ImmutableCredentials
/// </summary>
/// <returns></returns>
public abstract ImmutableCredentials GetCredentials();
#if AWS_ASYNC_API
public virtual System.Threading.Tasks.Task<ImmutableCredentials> GetCredentialsAsync()
{
return System.Threading.Tasks.Task.Run<ImmutableCredentials>(() => this.GetCredentials());
}
#endif
}
/// <summary>
/// Basic set of credentials consisting of an AccessKey and SecretKey
/// </summary>
public class BasicAWSCredentials : AWSCredentials
{
#region Private members
private ImmutableCredentials _credentials;
#endregion
#region Constructors
/// <summary>
/// Constructs a BasicAWSCredentials object for the specified accessKey and secretKey.
/// </summary>
/// <param name="accessKey"></param>
/// <param name="secretKey"></param>
public BasicAWSCredentials(string accessKey, string secretKey)
{
if (!string.IsNullOrEmpty(accessKey))
{
_credentials = new ImmutableCredentials(accessKey, secretKey, null);
}
}
#endregion
#region Abstract class overrides
/// <summary>
/// Returns an instance of ImmutableCredentials for this instance
/// </summary>
/// <returns></returns>
public override ImmutableCredentials GetCredentials()
{
if (this._credentials == null)
return null;
return _credentials.Copy();
}
#endregion
}
/// <summary>
/// Session credentials consisting of AccessKey, SecretKey and Token
/// </summary>
public class SessionAWSCredentials : AWSCredentials
{
#region Public constructors
/// <summary>
/// Constructs a SessionAWSCredentials object for the specified accessKey, secretKey.
/// </summary>
/// <param name="awsAccessKeyId"></param>
/// <param name="awsSecretAccessKey"></param>
/// <param name="token"></param>
public SessionAWSCredentials(string awsAccessKeyId, string awsSecretAccessKey, string token)
{
if (string.IsNullOrEmpty(awsAccessKeyId)) throw new ArgumentNullException("awsAccessKeyId");
if (string.IsNullOrEmpty(awsSecretAccessKey)) throw new ArgumentNullException("awsSecretAccessKey");
if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token");
_lastCredentials = new ImmutableCredentials(awsAccessKeyId, awsSecretAccessKey, token);
}
#endregion
#region Credentials data
private ImmutableCredentials _lastCredentials;
#endregion
#region Abstract class overrides
/// <summary>
/// Returns an instance of ImmutableCredentials for this instance
/// </summary>
/// <returns></returns>
public override ImmutableCredentials GetCredentials()
{
return _lastCredentials.Copy();
}
#endregion
}
#if BCL
/// <summary>
/// Credentials that are retrieved using the stored profile. The SDK Store is searched which is the credentials store shared with the SDK, PowerShell CLI and Toolkit.
/// To manage the SDK Store with the SDK use Amazon.Util.ProfileManager. If the profile is not found in the SDK Store then credentials file shared with other AWS SDKs
/// is searched. The credentials file is stored in the .aws directory in the current user's home directory.
/// <para>
/// The profile name can be specified in the App.config using the AWSProfileName setting.
/// </para>
/// <para>
/// The location to search for credentials can be overridden in the App.config using the AWSProfilesLocation setting.
/// </para>
/// </summary>
public class StoredProfileAWSCredentials : AWSCredentials
{
#region Private members
private const string DEFAULT_PROFILE_NAME = "default";
private ImmutableCredentials _wrappedCredentials;
#endregion
#region Public constructors
/// <summary>
/// Constructs an instance of StoredProfileAWSCredentials. This constructor searches for credentials using the
/// account name specified in the App.config. If no account is specified then the default credentials are used.
/// </summary>
public StoredProfileAWSCredentials()
: this(AWSConfigs.AWSProfileName)
{
}
/// <summary>
/// Constructs an instance of StoredProfileAWSCredentials. Credentials will be searched for using the profileName parameter.
/// </summary>
/// <param name="profileName">The profile name to search for credentials for</param>
public StoredProfileAWSCredentials(string profileName)
: this(profileName, AWSConfigs.AWSProfilesLocation)
{
}
/// <summary>
/// Constructs an instance of StoredProfileAWSCredentials. Credentials will be searched for using the profileName parameter.
/// </summary>
/// <param name="profileName">The profile name to search for credentials for</param>
/// <param name="profilesLocation">Overrides the location to search for credentials</param>
public StoredProfileAWSCredentials(string profileName, string profilesLocation)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
var lookupName = string.IsNullOrEmpty(profileName) ? DEFAULT_PROFILE_NAME : profileName;
ProfileName = lookupName;
ProfilesLocation = null;
// If not overriding the credentials lookup location check the SDK Store for credentials. If an override is being used then
// assume the intent is to use the credentials file.
if (string.IsNullOrEmpty(profilesLocation))
{
AWSCredentials credentials;
if (Amazon.Util.ProfileManager.TryGetAWSCredentials(lookupName, out credentials))
{
this._wrappedCredentials = credentials.GetCredentials();
var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials));
logger.InfoFormat("Credentials found using account name {0} and looking in SDK account store.", lookupName);
}
}
// If credentials weren't found in the SDK store then search the shared credentials file.
if (this._wrappedCredentials == null)
{
var credentialsFilePath = DetermineCredentialsFilePath(profilesLocation);
if (File.Exists(credentialsFilePath))
{
var parser = new CredentialsFileParser(credentialsFilePath);
var section = parser.FindSection(lookupName);
if (section != null && section.HasValidCredentials)
{
this._wrappedCredentials = section.Credentials;
var logger = Logger.GetLogger(typeof(StoredProfileAWSCredentials));
logger.InfoFormat("Credentials found using account name {0} and looking in {1}.", lookupName, credentialsFilePath);
}
ProfilesLocation = credentialsFilePath;
}
}
// No credentials found so error out.
if (this._wrappedCredentials == null)
{
throw new ArgumentException("App.config does not contain credentials information. Either add the AWSAccessKey and AWSSecretKey or AWSProfileName.");
}
}
#endregion
#region Public properties
/// <summary>
/// Name of the profile being used.
/// </summary>
public string ProfileName { get; private set; }
/// <summary>
/// Location of the profiles, if used.
/// </summary>
public string ProfilesLocation { get; private set; }
#endregion
/// <summary>
/// Determine the location of the shared credentials file.
/// </summary>
/// <param name="profilesLocation">If accountsLocation is null then the shared credentials file stored .aws directory under the home directory.</param>
/// <returns>The file path to the credentials file to be used.</returns>
private static string DetermineCredentialsFilePath(string profilesLocation)
{
if (!string.IsNullOrEmpty(profilesLocation))
{
if (Directory.Exists(profilesLocation))
return Path.Combine(profilesLocation, "credentials");
else
return profilesLocation;
}
else
{
if (!string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("HOME")))
{
var envPath = Path.Combine(
System.Environment.GetEnvironmentVariable("HOME"),
".aws/credentials");
if (File.Exists(envPath))
return envPath;
}
#if !BCL35
var path = Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile),
".aws/credentials");
#else
var path = Path.Combine(
Directory.GetParent(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)).FullName,
".aws/credentials");
#endif
return path;
}
}
private class CredentialsFileParser
{
private const string profileNamePrefix = "[";
private const string profileNameSuffix = "]";
private const string dataPrefix = "aws_";
private const string keyValueSeparator = "=";
private const string accessKeyName = "aws_access_key_id";
private const string secretKeyName = "aws_secret_access_key";
private const string tokenName = "aws_session_token";
private List<CredentialsSection> Sections { get; set; }
public CredentialsFileParser(string filePath)
{
var lines = File.ReadAllLines(filePath);
Parse(lines);
}
private void Parse(string[] lines)
{
Sections = new List<CredentialsSection>();
CredentialsSection currentSection = null;
foreach (var l in lines)
{
var line = l ?? string.Empty;
line = line.Trim();
if (string.IsNullOrEmpty(line))
continue;
if (line.StartsWith(profileNamePrefix, StringComparison.OrdinalIgnoreCase) &&
line.EndsWith(profileNameSuffix, StringComparison.OrdinalIgnoreCase))
{
if (currentSection != null)
Sections.Add(currentSection);
var profileName = GetProfileName(line);
currentSection = new CredentialsSection(profileName);
}
else if (line.StartsWith(dataPrefix, StringComparison.OrdinalIgnoreCase) && currentSection != null)
{
// split data into key-value pairs, store appropriately
var split = SplitData(line);
if (split.Count > 0)
{
var name = split[0];
var value = split.Count > 1 ? split[1] : null;
SetSectionValue(currentSection, name, value);
}
}
}
if (currentSection != null)
Sections.Add(currentSection);
}
private static List<string> SplitData(string line)
{
var split = line
.Split(new string[] { keyValueSeparator }, StringSplitOptions.None)
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s))
.ToList();
return split;
}
private static string GetProfileName(string line)
{
// get profile name by trimming off the [ and ] characters
var profileName = line;
profileName = profileName.Substring(profileNamePrefix.Length);
profileName = profileName.Substring(0, profileName.Length - profileNameSuffix.Length);
return profileName;
}
private static void SetSectionValue(CredentialsSection section, string name, string value)
{
if (string.Equals(accessKeyName, name, StringComparison.OrdinalIgnoreCase))
section.AccessKey = value;
else if (string.Equals(secretKeyName, name, StringComparison.OrdinalIgnoreCase))
section.SecretKey = value;
else if (string.Equals(tokenName, name, StringComparison.OrdinalIgnoreCase))
section.Token = value;
}
public CredentialsSection FindSection(string profileName)
{
foreach (var section in Sections)
if (string.Equals(section.ProfileName, profileName, StringComparison.Ordinal))
return section;
return null;
}
public class CredentialsSection
{
public CredentialsSection(string profileName)
{
ProfileName = profileName;
}
public string ProfileName { get; set; }
public string AccessKey { get; set; }
public string SecretKey { get; set; }
public string Token { get; set; }
public bool HasValidCredentials
{
get
{
return
!string.IsNullOrEmpty(AccessKey) &&
!string.IsNullOrEmpty(SecretKey);
}
}
public ImmutableCredentials Credentials
{
get
{
return new ImmutableCredentials(AccessKey, SecretKey, Token);
}
}
}
}
#region Abstract class overrides
/// <summary>
/// Returns an instance of ImmutableCredentials for this instance
/// </summary>
/// <returns></returns>
public override ImmutableCredentials GetCredentials()
{
return this._wrappedCredentials.Copy();
}
#endregion
}
/// <summary>
/// Uses aws credentials stored in environment variables to construct the credentials object.
/// AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are used for the access key id and secret key. If the variable AWS_SESSION_TOKEN exists
/// then it will be used to create temporary session credentials.
/// </summary>
public class EnvironmentVariablesAWSCredentials : AWSCredentials
{
private const string ENVIRONMENT_VARIABLE_ACCESSKEY = "AWS_ACCESS_KEY_ID";
private const string ENVIRONMENT_VARIABLE_SECRETKEY = "AWS_SECRET_ACCESS_KEY";
private const string ENVIRONMENT_VARIABLE_SESSION_TOKEN = "AWS_SESSION_TOKEN";
private ImmutableCredentials _wrappedCredentials;
#region Public constructors
/// <summary>
/// Constructs an instance of EnvironmentVariablesAWSCredentials. If no credentials are found in the environment variables
/// then an InvalidOperationException.
/// </summary>
public EnvironmentVariablesAWSCredentials()
{
string accessKeyId = Environment.GetEnvironmentVariable(ENVIRONMENT_VARIABLE_ACCESSKEY);
string secretKey = Environment.GetEnvironmentVariable(ENVIRONMENT_VARIABLE_SECRETKEY);
if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secretKey))
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"The environment variables {0} and {1} were not set with AWS credentials.", ENVIRONMENT_VARIABLE_ACCESSKEY, ENVIRONMENT_VARIABLE_SECRETKEY));
}
string sessionToken = Environment.GetEnvironmentVariable(ENVIRONMENT_VARIABLE_SESSION_TOKEN);
this._wrappedCredentials = new ImmutableCredentials(accessKeyId, secretKey, sessionToken);
var logger = Logger.GetLogger(typeof(EnvironmentVariablesAWSCredentials));
logger.InfoFormat("Credentials found using environment variables.");
}
#endregion
/// <summary>
/// Returns an instance of ImmutableCredentials for this instance
/// </summary>
/// <returns></returns>
public override ImmutableCredentials GetCredentials()
{
return this._wrappedCredentials.Copy();
}
}
/// <summary>
/// Credentials that are retrieved from ConfigurationManager.AppSettings
/// </summary>
public class EnvironmentAWSCredentials : AWSCredentials
{
private const string ACCESSKEY = "AWSAccessKey";
private const string SECRETKEY = "AWSSecretKey";
private ImmutableCredentials _wrappedCredentials;
#region Public constructors
/// <summary>
/// Constructs an instance of EnvironmentAWSCredentials and attempts
/// to load AccessKey and SecretKey from ConfigurationManager.AppSettings
/// </summary>
public EnvironmentAWSCredentials()
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
// Use hardcoded credentials
if (!string.IsNullOrEmpty(appConfig[ACCESSKEY]) && !string.IsNullOrEmpty(appConfig[SECRETKEY]))
{
var accessKey = appConfig[ACCESSKEY];
var secretKey = appConfig[SECRETKEY];
this._wrappedCredentials = new ImmutableCredentials(accessKey, secretKey, null);
var logger = Logger.GetLogger(typeof(EnvironmentAWSCredentials));
logger.InfoFormat("Credentials found with {0} and {1} app settings", ACCESSKEY, SECRETKEY);
}
// Fallback to the StoredProfileAWSCredentials provider
else
{
this._wrappedCredentials = new StoredProfileAWSCredentials().GetCredentials();
}
}
#endregion
#region Abstract class overrides
/// <summary>
/// Returns an instance of ImmutableCredentials for this instance
/// </summary>
/// <returns></returns>
public override ImmutableCredentials GetCredentials()
{
return this._wrappedCredentials.Copy();
}
#endregion
}
#endif
/// <summary>
/// Abstract class for automatically refreshing AWS credentials
/// </summary>
public abstract class RefreshingAWSCredentials : AWSCredentials
{
#region Refresh data
/// <summary>
/// Refresh state container consisting of credentials
/// and the date of the their expiration
/// </summary>
public class CredentialsRefreshState
{
public ImmutableCredentials Credentials { get; set; }
public DateTime Expiration { get; set; }
public CredentialsRefreshState()
{ }
public CredentialsRefreshState(ImmutableCredentials credentials, DateTime expiration)
{
Credentials = credentials;
Expiration = expiration;
}
}
protected CredentialsRefreshState _currentState = null;
private object _refreshLock = new object();
#endregion
#region Private members
private TimeSpan _preemptExpiryTime = TimeSpan.FromMinutes(0);
#endregion
#region Properties
/// <summary>
/// The time before actual expiration to expire the credentials.
/// Property cannot be set to a negative TimeSpan.
/// </summary>
public TimeSpan PreemptExpiryTime
{
get { return _preemptExpiryTime; }
set
{
if (value < TimeSpan.Zero) throw new ArgumentOutOfRangeException("value", "PreemptExpiryTime cannot be negative");
_preemptExpiryTime = value;
}
}
#endregion
#region Override methods
/// <summary>
/// Returns an instance of ImmutableCredentials for this instance
/// </summary>
/// <returns></returns>
public override ImmutableCredentials GetCredentials()
{
lock (this._refreshLock)
{
// If credentials are expired, update
if (ShouldUpdate)
{
_currentState = GenerateNewCredentials();
UpdateToGeneratedCredentials(_currentState);
}
return _currentState.Credentials.Copy();
}
}
#if AWS_ASYNC_API
public async override System.Threading.Tasks.Task<ImmutableCredentials> GetCredentialsAsync()
{
// If credentials are expired, update
if (ShouldUpdate)
{
var state = await GenerateNewCredentialsAsync().ConfigureAwait(false);
lock (this._refreshLock)
{
_currentState = state;
UpdateToGeneratedCredentials(_currentState);
}
}
return _currentState.Credentials.Copy();
}
#endif
#endregion
#region Private/protected credential update methods
private void UpdateToGeneratedCredentials(CredentialsRefreshState state)
{
// Check if the new credentials are already expired
if (ShouldUpdate)
{
var errorMessage = string.Format(CultureInfo.InvariantCulture,
"The retrieved credentials have already expired: Now = {0}, Credentials expiration = {1}",
DateTime.Now, state.Expiration);
throw new AmazonClientException(errorMessage);
}
// Offset the Expiration by PreemptExpiryTime
state.Expiration -= PreemptExpiryTime;
if (ShouldUpdate)
{
// This could happen if the default value of PreemptExpiryTime is
// overriden and set too high such that ShouldUpdate returns true.
var logger = Logger.GetLogger(typeof(RefreshingAWSCredentials));
logger.InfoFormat(
"The preempt expiry time is set too high: Current time = {0}, Credentials expiry time = {1}, Preempt expiry time = {2}.",
DateTime.Now,
_currentState.Expiration,
PreemptExpiryTime);
}
}
// Test credentials existence and expiration time
private bool ShouldUpdate
{
get
{
// should update if:
// credentials have not been loaded yet
if (_currentState == null)
return true;
// it's past the expiration time
var now = DateTime.UtcNow;
var exp = _currentState.Expiration.ToUniversalTime();
return (now > exp);
}
}
/// <summary>
/// When overridden in a derived class, generates new credentials and new expiration date.
///
/// Called on first credentials request and when expiration date is in the past.
/// </summary>
/// <returns></returns>
protected virtual CredentialsRefreshState GenerateNewCredentials()
{
throw new NotImplementedException();
}
#if AWS_ASYNC_API
/// <summary>
/// When overridden in a derived class, generates new credentials and new expiration date.
///
/// Called on first credentials request and when expiration date is in the past.
/// </summary>
/// <returns></returns>
protected virtual System.Threading.Tasks.Task<CredentialsRefreshState> GenerateNewCredentialsAsync()
{
return System.Threading.Tasks.Task.Run(() => this.GenerateNewCredentials());
}
#endif
/// <summary>
/// Clears currently-stored credentials, forcing the next GetCredentials call to generate new credentials.
/// </summary>
public virtual void ClearCredentials()
{
_currentState = null;
}
#endregion
}
/// <summary>
/// Credentials that are retrieved from the Instance Profile service on an EC2 instance
/// </summary>
public class InstanceProfileAWSCredentials : RefreshingAWSCredentials
{
#region Private members
// Set preempt expiry to 15 minutes. New access keys are available at least 15 minutes before expiry time.
// http://docs.aws.amazon.com/IAM/latest/UserGuide/role-usecase-ec2app.html
private static TimeSpan _preemptExpiryTime = TimeSpan.FromMinutes(15);
private CredentialsRefreshState _currentRefreshState = null;
private static TimeSpan _refreshAttemptPeriod = TimeSpan.FromHours(1);
#endregion
#region Properties
/// <summary>
/// Role for which the credentials are retrieved
/// </summary>
public string Role { get; set; }
#endregion
#region Overrides
protected override CredentialsRefreshState GenerateNewCredentials()
{
CredentialsRefreshState newState = null;
try
{
// Attempt to get early credentials. OK to fail at this point.
newState = GetRefreshState();
}
catch (Exception e)
{
var logger = Logger.GetLogger(typeof(InstanceProfileAWSCredentials));
logger.InfoFormat("Error getting credentials from Instance Profile service: {0}", e);
}
// If successful, save new credentials
if (newState != null)
_currentRefreshState = newState;
// If still not successful (no credentials available at start), attempt once more to
// get credentials, but now without swallowing exception
if (_currentRefreshState == null)
_currentRefreshState = GetRefreshState();
// Return credentials that will expire in at most one hour
CredentialsRefreshState state = GetEarlyRefreshState(_currentRefreshState);
return state;
}
#endregion
#region Constructors
/// <summary>
/// Constructs a InstanceProfileAWSCredentials object for specific role
/// </summary>
/// <param name="role">Role to use</param>
public InstanceProfileAWSCredentials(string role)
{
Role = role;
this.PreemptExpiryTime = _preemptExpiryTime;
}
/// <summary>
/// Constructs a InstanceProfileAWSCredentials object for the first found role
/// </summary>
public InstanceProfileAWSCredentials()
: this(GetFirstRole()) { }
#endregion
#region Public static methods
/// <summary>
/// Retrieves a list of all roles available through current InstanceProfile service
/// </summary>
/// <returns></returns>
public static IEnumerable<string> GetAvailableRoles()
{
string allAliases = GetContents(RolesUri);
if (string.IsNullOrEmpty(allAliases))
yield break;
string[] parts = allAliases.Split(AliasSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
{
var trim = part.Trim();
if (!string.IsNullOrEmpty(trim))
yield return trim;
}
}
#endregion
#region Private members
private static string[] AliasSeparators = new string[] { "<br/>" };
private static string Server = "http://169.254.169.254";
private static string RolesPath = "/latest/meta-data/iam/security-credentials/";
private static string InfoPath = "/latest/meta-data/iam/info";
private static string SuccessCode = "Success";
private static Uri RolesUri
{
get
{
return new Uri(Server + RolesPath);
}
}
private Uri CurrentRoleUri
{
get
{
return new Uri(Server + RolesPath + Role);
}
}
private static Uri InfoUri
{
get
{
return new Uri(Server + InfoPath);
}
}
private CredentialsRefreshState GetEarlyRefreshState(CredentialsRefreshState state)
{
// New expiry time = Now + _refreshAttemptPeriod + PreemptExpiryTime
var newExpiryTime = DateTime.Now + _refreshAttemptPeriod + PreemptExpiryTime;
// Use this only if the time is earlier than the default expiration time
if (newExpiryTime.ToUniversalTime() > state.Expiration.ToUniversalTime())
newExpiryTime = state.Expiration;
return new CredentialsRefreshState
{
Credentials = state.Credentials.Copy(),
Expiration = newExpiryTime
};
}
private CredentialsRefreshState GetRefreshState()
{
SecurityInfo info = GetServiceInfo();
if (!string.IsNullOrEmpty(info.Message))
{
throw new AmazonServiceException(string.Format(CultureInfo.InvariantCulture,
"Unable to retrieve credentials. Message = \"{0}\".",
info.Message));
}
SecurityCredentials credentials = GetRoleCredentials();
CredentialsRefreshState refreshState = new CredentialsRefreshState
{
Credentials = new ImmutableCredentials(credentials.AccessKeyId, credentials.SecretAccessKey, credentials.Token),
Expiration = credentials.Expiration
};
return refreshState;
}
private static SecurityInfo GetServiceInfo()
{
string json = GetContents(InfoUri);
SecurityInfo info = JsonMapper.ToObject<SecurityInfo>(json);
ValidateResponse(info);
return info;
}
private SecurityCredentials GetRoleCredentials()
{
string json = GetContents(CurrentRoleUri);
SecurityCredentials credentials = JsonMapper.ToObject<SecurityCredentials>(json);
ValidateResponse(credentials);
return credentials;