-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwsCloudIdProvider.cs
161 lines (129 loc) · 6.6 KB
/
AwsCloudIdProvider.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
using System.Text;
using Newtonsoft.Json;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using Amazon.Runtime.Internal.Auth;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
namespace akeyless.Cloudid
{
public class AwsCloudIdProvider : ICloudIdProvider
{
private static string ToHexString(IReadOnlyCollection<byte> array)
{
var hex = new StringBuilder(array.Count * 2);
foreach (var b in array)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
public string SignRequest(string awsAccessId, string awsSecretAccessKey, string awsSecurityToken)
{
var algorithm = "AWS4-HMAC-SHA256";
var service = "sts";
var region = "us-east-1";
var method = "POST";
var host = "sts.amazonaws.com";
var contentType = "application/x-www-form-urlencoded; charset=utf-8";
var body = "Action=GetCallerIdentity&Version=2011-06-15";
var t = DateTime.UtcNow;
var amzDate = t.ToString("yyyyMMddTHHmmssZ");
var datestamp = t.ToString("yyyyMMdd");
var credentialScope = datestamp + '/' + region + '/' + service + '/' + "aws4_request";
var canonicalUri = "/";
var canonicalQuerystring = "";
var signedHeaders = "content-length;content-type;host;x-amz-date";
if (!String.IsNullOrEmpty(awsSecurityToken)) {
signedHeaders += ";x-amz-security-token";
}
var hasher = SHA256.Create();
var bodyDigest = ToHexString(hasher.ComputeHash(Encoding.UTF8.GetBytes(body)));
var canonicalHeaders = string.Format("content-length:{0}\ncontent-type:{1}\nhost:{2}\nx-amz-date:{3}\n", body.Length, contentType, host, amzDate);
if (!String.IsNullOrEmpty(awsSecurityToken)) {
canonicalHeaders += string.Format("x-amz-security-token:{0}\n", awsSecurityToken);
}
var canonicalRequest = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}", method, canonicalUri, canonicalQuerystring, canonicalHeaders, signedHeaders, bodyDigest);
var canonicalHeaderHash = ToHexString(hasher.ComputeHash(Encoding.UTF8.GetBytes(canonicalRequest)));
var stringToSign = string.Format("{0}\n{1}\n{2}\n{3}", algorithm, amzDate, credentialScope, canonicalHeaderHash);
hasher.Clear();
hasher = SHA256.Create();
// var signingKey = GetSignatureKey(awsSecretAccessKey, datestamp, region, service);
var signingKey = AWS4Signer.ComposeSigningKey(awsSecretAccessKey, region, datestamp, "sts");
var signature = ToHexString(AWS4Signer.ComputeKeyedHash(SigningAlgorithm.HmacSHA256, signingKey, stringToSign));
var auth = string.Format("{0} Credential={1}/{2}, SignedHeaders={3}, Signature={4}",
algorithm, awsAccessId, credentialScope, signedHeaders, signature);
var headers = new Dictionary<string, string[]>();
headers["Authorization"] = new string[] { auth };
headers["Content-Length"] = new string[] { body.Length.ToString() };
headers["Content-Type"] = new string[] { contentType };
headers["Host"] = new string[] { host };
headers["X-Amz-Date"] = new string[] { amzDate };
if (!String.IsNullOrEmpty(awsSecurityToken)) {
headers["X-Amz-Security-Token"] = new string[] { awsSecurityToken };
}
var headersJson = JsonConvert.SerializeObject(headers);
var awsData = new Dictionary<string, string>();
awsData["sts_request_method"] = method;
awsData["sts_request_url"] = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("https://sts.amazonaws.com/"));
awsData["sts_request_body"] = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(body));
awsData["sts_request_headers"] = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(headersJson));
var awsDump = JsonConvert.SerializeObject(awsData);
var cloudId = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(awsDump));
return cloudId;
}
public async Task<string> GetCloudIdAsync()
{
var chain = new CredentialProfileStoreChain();
AWSCredentials awsCredentials;
// First try to take credentials from local file
var success = chain.TryGetAWSCredentials("default", out awsCredentials);
if (success) {
var creds = await awsCredentials.GetCredentialsAsync();
var accessKey = creds.AccessKey;
var secretKey = creds.SecretKey;
var securityToken = creds.Token;
return SignRequest(accessKey, secretKey, securityToken);
}
// if failed, try to take from environment
// Apperantly lambda functions use env vars and not profiles
try {
var profileCreds = new Amazon.Runtime.EnvironmentVariablesAWSCredentials();
var creds = await profileCreds.GetCredentialsAsync();
var accessKey = creds.AccessKey;
var secretKey = creds.SecretKey;
var securityToken = creds.Token;
if (accessKey != "" && secretKey != "") {
return SignRequest(accessKey, secretKey, securityToken);
}
} catch (Exception)
{
// Do nothing on failure, since we want to try the Instance Profile
}
// if failed, take from current session
try {
var profileCreds = new Amazon.Runtime.InstanceProfileAWSCredentials();
var creds = await profileCreds.GetCredentialsAsync();
var accessKey = creds.AccessKey;
var secretKey = creds.SecretKey;
var securityToken = creds.Token;
return SignRequest(accessKey, secretKey, securityToken);
} catch (Exception)
{
throw new Exception("Failed to get AWS credentials");
}
}
public string GetCloudId()
{
string token = "";
var cont = GetCloudIdAsync().ContinueWith(cloudIdTaskRes =>
{
token = cloudIdTaskRes.Result;
});
cont.Wait();
return token;
}
}
}