-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Aliyun.Acs.Core.Utils; | ||
using Aliyun.Acs.Core.Exceptions; | ||
using Aliyun.Acs.Core.Reader; | ||
|
||
using Aliyun.Acs.Core.Http; | ||
|
||
namespace Aliyun.Acs.Core.Auth | ||
{ | ||
public class OIDCCredentialsProvider : AlibabaCloudCredentialsProvider | ||
{ | ||
private string RoleArn { get; set; } | ||
private string OIDCProviderArn { get; set; } | ||
private string OIDCTokenFilePath { get; set; } | ||
private string RoleSessionName { get; set; } | ||
private string Policy { get; set; } | ||
|
||
private readonly string stsEndpoint; | ||
|
||
private readonly long durationSeconds; | ||
|
||
private BasicSessionCredentials credentials; | ||
Check warning on line 23 in aliyun-net-sdk-core/Auth/Provider/OIDCCredentialsProvider.cs GitHub Actions / build
Check warning on line 23 in aliyun-net-sdk-core/Auth/Provider/OIDCCredentialsProvider.cs GitHub Actions / build
Check warning on line 23 in aliyun-net-sdk-core/Auth/Provider/OIDCCredentialsProvider.cs GitHub Actions / build
|
||
private IAcsClient stsClient; | ||
Check warning on line 24 in aliyun-net-sdk-core/Auth/Provider/OIDCCredentialsProvider.cs GitHub Actions / build
Check warning on line 24 in aliyun-net-sdk-core/Auth/Provider/OIDCCredentialsProvider.cs GitHub Actions / build
Check warning on line 24 in aliyun-net-sdk-core/Auth/Provider/OIDCCredentialsProvider.cs GitHub Actions / build
|
||
|
||
public OIDCCredentialsProvider(string roleArn, string oidcProviderArn, string oidcTokenFilePath, string roleSessionName, string regionId) | ||
{ | ||
RoleArn = ParameterHelper.ValidateEnvNotNull(roleArn, "ALIBABA_CLOUD_ROLE_ARN", "roleArn", "roleArn does not exist and env ALIBABA_CLOUD_ROLE_ARN is null."); | ||
OIDCProviderArn = ParameterHelper.ValidateEnvNotNull(oidcProviderArn, "ALIBABA_CLOUD_OIDC_PROVIDER_ARN", "oidcProviderArn", "OIDCProviderArn must not be null."); | ||
OIDCTokenFilePath = ParameterHelper.ValidateEnvNotNull(oidcTokenFilePath, "ALIBABA_CLOUD_OIDC_TOKEN_FILE", "oidcTokenFilePath", "OIDCTokenFilePath must not be null."); | ||
|
||
if (!string.IsNullOrEmpty(roleSessionName)) | ||
{ | ||
RoleSessionName = roleSessionName; | ||
} | ||
else | ||
{ | ||
RoleSessionName = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_SESSION_NAME"); | ||
} | ||
|
||
if (string.IsNullOrEmpty(RoleSessionName)) | ||
{ | ||
RoleSessionName = "DEFAULT_ROLE_SESSION_NAME_FOR_C#_SDK_V1"; | ||
} | ||
|
||
if (string.IsNullOrEmpty(regionId)) | ||
{ | ||
stsEndpoint = "https://sts.aliyuncs.com/"; | ||
} | ||
else | ||
{ | ||
stsEndpoint = string.Format("https://sts.{0}.aliyuncs.com/", regionId); | ||
} | ||
|
||
durationSeconds = 3600; | ||
} | ||
|
||
public string InvokeAssumeRoleWithOIDC() | ||
{ | ||
var queries = new Dictionary<string, string> | ||
{ | ||
{ "Action", "AssumeRoleWithOIDC" }, | ||
{ "Format", "JSON" }, | ||
{ "Version", "2015-04-01" }, | ||
{ "Timestamp", ParameterHelper.FormatIso8601Date(DateTime.UtcNow) } | ||
}; | ||
|
||
string url; | ||
try | ||
{ | ||
url = stsEndpoint + "?" + ParameterHelper.GetFormData(queries); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new ClientException("AssumeRoleWithOIDC failed: " + ex.Message); | ||
} | ||
|
||
var httpRequest = new HttpRequest(url) | ||
{ | ||
Method = MethodType.POST, | ||
ContentType = FormatType.FORM, | ||
}; | ||
|
||
httpRequest.SetConnectTimeoutInMilliSeconds(1000); | ||
httpRequest.SetReadTimeoutInMilliSeconds(3000); | ||
|
||
var oidcToken = AuthUtils.GetOIDCToken(OIDCTokenFilePath); | ||
if (oidcToken == null) | ||
{ | ||
throw new ClientException("Read OIDC token failed"); | ||
} | ||
|
||
var body = new Dictionary<string, string> | ||
{ | ||
{ "DurationSeconds", durationSeconds.ToString() }, | ||
{ "RoleArn", RoleArn }, | ||
{ "OIDCProviderArn", OIDCProviderArn }, | ||
{ "OIDCToken", oidcToken }, | ||
{ "RoleSessionName", RoleSessionName }, | ||
{ "Policy", Policy } | ||
}; | ||
|
||
var content = ParameterHelper.GetFormDataWithoutNullOrEmpty(body); | ||
httpRequest.SetContent(content, "UTF-8", FormatType.FORM); | ||
|
||
HttpResponse httpResponse; | ||
try | ||
{ | ||
httpResponse = HttpResponse.GetResponse(httpRequest); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new ClientException("AssumeRoleWithOIDC failed " + ex.Message); | ||
} | ||
|
||
if (!httpResponse.isSuccess()) | ||
{ | ||
var responseBody = httpResponse.GetHttpContentString(); | ||
var jsonReader = new JsonReader(); | ||
var requestID = jsonReader.Read(responseBody, "RequestId"); | ||
var msg = jsonReader.Read(responseBody, "Message"); | ||
var code = jsonReader.Read(responseBody, "Code"); | ||
var message = string.Format("{0}(RequestID: {1}, Code: {2})", msg, requestID, code); | ||
throw new ClientException("AssumeRoleWithOIDC failed: " + message); | ||
} | ||
|
||
return httpResponse.GetHttpContentString(); | ||
} | ||
|
||
public AlibabaCloudCredentials GetCredentials() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
|
||
using System; | ||
using System.Text; | ||
using System.IO; | ||
using System.Security; | ||
using System.Threading.Tasks; | ||
using Aliyun.Acs.Core.Exceptions; | ||
|
||
namespace Aliyun.Acs.Core.Utils | ||
{ | ||
public class AuthUtils | ||
{ | ||
private static volatile string oidcToken; | ||
|
||
static AuthUtils authUtils = new AuthUtils(); | ||
|
||
AuthUtils() | ||
{ | ||
} | ||
|
||
|
||
public static string GetOIDCToken(string OIDCTokenFilePath) | ||
{ | ||
byte[] buffer; | ||
if (!File.Exists(OIDCTokenFilePath)) | ||
{ | ||
throw new ClientException("OIDCTokenFilePath " + OIDCTokenFilePath + " does not exist."); | ||
} | ||
try | ||
{ | ||
using (var inStream = new FileStream(OIDCTokenFilePath, FileMode.Open, FileAccess.Read)) | ||
{ | ||
buffer = new byte[inStream.Length]; | ||
inStream.Read(buffer, 0, buffer.Length); | ||
} | ||
oidcToken = Encoding.UTF8.GetString(buffer); | ||
} | ||
catch (UnauthorizedAccessException) | ||
{ | ||
throw new ClientException("OIDCTokenFilePath " + OIDCTokenFilePath + " cannot be read."); | ||
} | ||
catch (SecurityException) | ||
{ | ||
throw new ClientException("Security Exception: Do not have the required permission. " + "OIDCTokenFilePath " + OIDCTokenFilePath); | ||
} | ||
catch (IOException e) | ||
{ | ||
Console.WriteLine(e.StackTrace); | ||
} | ||
return oidcToken; | ||
} | ||
|
||
|
||
} | ||
} |