Skip to content

Commit

Permalink
release 0.0.9-beta source code
Browse files Browse the repository at this point in the history
  • Loading branch information
unionsdk committed Oct 30, 2023
1 parent 3f9261c commit 0ba815a
Show file tree
Hide file tree
Showing 66 changed files with 1,847 additions and 451 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# 0.0.9-beta 2023-10-30

### G42Cloud SDK MPC

- _Features_
- None
- _Bug Fix_
- None
- _Change_
- **CreateThumbnailsTask**
- changes of request param
- `- thumbnail_para.percent`
- `- thumbnail_para.type: enum value [PERCENT]`
- **CreateTranscodingTask**
- changes of request param
- `- thumbnail.params.percent`
- `- thumbnail.params.type: enum value [PERCENT]`

# 0.0.8-beta 2023-08-26

### G42Cloud SDK MPC
Expand Down
14 changes: 2 additions & 12 deletions Core/Auth/AuthCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,8 @@

namespace G42Cloud.SDK.Core.Auth
{
public class AuthCache
internal static class AuthCache
{
private static readonly ConcurrentDictionary<string, string> _authDict = new ConcurrentDictionary<string, string>();

public static string GetAuth(string akWithName)
{
return _authDict.TryGetValue(akWithName, out var value) ? value : null;
}

public static void PutAuth(string akWithName, string id)
{
_authDict.AddOrUpdate(akWithName, id, (key, value) => id);
}
internal static readonly ConcurrentDictionary<string, string> Value = new ConcurrentDictionary<string, string>();
}
}
96 changes: 21 additions & 75 deletions Core/Auth/BasicCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,76 +22,35 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace G42Cloud.SDK.Core.Auth
{
public class BasicCredentials : Credentials
public class BasicCredentials : Credentials<BasicCredentials>
{
private string _derivedAuthServiceName;
private string _regionId;

internal const string Type = "basic";

public BasicCredentials(string ak, string sk, string projectId = null)
{
if (string.IsNullOrEmpty(ak))
{
throw new ArgumentNullException(nameof(ak));
}

if (string.IsNullOrEmpty(sk))
{
throw new ArgumentNullException(nameof(sk));
}

Ak = ak;
Sk = sk;
ProjectId = projectId;
}

private string Ak { set; get; }
private string Sk { set; get; }
private string ProjectId { set; get; }
private string SecurityToken { set; get; }
private string IamEndpoint { set; get; }
private Func<HttpRequest, bool> DerivedPredicate { set; get; }

public BasicCredentials WithIamEndpoint(string endpoint)
{
IamEndpoint = endpoint;
return this;
}

public BasicCredentials WithSecurityToken(string token)
{
SecurityToken = token;
return this;
}

public BasicCredentials WithDerivedPredicate(Func<HttpRequest, bool> func)
{
DerivedPredicate = func;
return this;
}

protected bool IsDerivedAuth(HttpRequest httpRequest)
{
if (DerivedPredicate == null)
{
return false;
}

return DerivedPredicate(httpRequest);
}

public override void ProcessDerivedAuthParams(string derivedAuthServiceName, string regionId)
{
if (_derivedAuthServiceName == null)
if (DerivedAuthServiceName == null)
{
_derivedAuthServiceName = derivedAuthServiceName;
DerivedAuthServiceName = derivedAuthServiceName;
}

if (_regionId == null)
if (RegionId == null)
{
_regionId = regionId;
RegionId = regionId;
}
}

Expand Down Expand Up @@ -120,61 +79,48 @@ public override Task<HttpRequest> SignAuthRequest(HttpRequest request)
request.Headers.Add("X-Security-Token", SecurityToken);
}

if (!string.IsNullOrEmpty(request.ContentType) && !request.ContentType.Contains("application/json"))
{
request.Headers.Add("X-Sdk-Content-Sha256", "UNSIGNED-PAYLOAD");
}

if (IsDerivedAuth(request))
{
var signer = new DerivedSigner
{
Key = Ak,
Secret = Sk
};
signer.Sign(request, _regionId, _derivedAuthServiceName);
}
else
{
var signer = new Signer
{
Key = Ak,
Secret = Sk
};
signer.Sign(request);
DerivedSigner.GetInstance().Sign(request, this);
return request;
}

IAkSkSigner signer = AkSkSignerFactory.GetSigner(request.SigningAlgorithm);
signer.Sign(request, this);
return request;
});

return httpRequestTask;
}

public override Credentials ProcessAuthParams(SdkHttpClient client, string regionId)
public override ICredential ProcessAuthParams(SdkHttpClient client, string regionId)
{
if (ProjectId != null)
{
return this;
}

var akWithName = Ak + regionId;
var projectId = AuthCache.GetAuth(akWithName);
if (!string.IsNullOrEmpty(projectId))
if (AuthCache.Value.ContainsKey(akWithName))
{
ProjectId = projectId;
ProjectId = AuthCache.Value[akWithName];
return this;
}

var derivedFunc = DerivedPredicate;
DerivedPredicate = null;

IamEndpoint = string.IsNullOrEmpty(IamEndpoint) ? IamService.DefaultIamEndpoint : IamEndpoint;
var request = IamService.GetKeystoneListProjectsRequest(IamEndpoint, regionId);
var logger = client.GetLogger();
logger.LogInformation("Project id of region '{}' not found in BasicCredentials, trying to obtain project id from IAM service: {}",
regionId, IamEndpoint);
var request = IamService.GetKeystoneListProjectsRequest(IamEndpoint, regionId, client.GetHttpConfig());
request = SignAuthRequest(request).Result;
try
{
ProjectId = IamService.KeystoneListProjects(client, request);
AuthCache.PutAuth(akWithName, ProjectId);
logger.LogInformation("Success to obtain project id of region '{}': {}", regionId, ProjectId);
AuthCache.Value[akWithName] = ProjectId;
DerivedPredicate = derivedFunc;
return this;
}
Expand Down
79 changes: 79 additions & 0 deletions Core/Auth/CredentialProviderChain.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2023 G42 Technologies Co.,Ltd.
*
* 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.Collections.Generic;

namespace G42Cloud.SDK.Core.Auth
{
public class CredentialProviderChain : ICredentialProvider
{
private readonly ICredentialProvider[] _providers;

public CredentialProviderChain(ICredentialProvider[] providers)
{
_providers = providers;
}

public static CredentialProviderChain GetDefault(string credentialType)
{
var credentialProviders = new ICredentialProvider[]
{
new EnvCredentialProvider(credentialType),
new ProfileCredentialProvider(credentialType)
};

return new CredentialProviderChain(credentialProviders);
}

public static CredentialProviderChain GetBasic()
{
return GetDefault(BasicCredentials.Type);
}

public static CredentialProviderChain GetGlobal()
{
return GetDefault(GlobalCredentials.Type);
}

public ICredential GetCredentials()
{
var exceptions = new List<SdkException>();
foreach (var provider in _providers)
{
try
{
var credentials = provider.GetCredentials();
if (credentials != null)
{
return credentials;
}
}
catch (SdkException e)
{
exceptions.Add(e);
}
}

throw new SdkException("failed to get credentials in providers", new AggregateException(exceptions));
}
}
}
38 changes: 35 additions & 3 deletions Core/Auth/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,52 @@

namespace G42Cloud.SDK.Core.Auth
{
public abstract class Credentials
public abstract class Credentials<T> : ICredential where T : Credentials<T>
{
public static readonly string DEFAULT_ENDPOINT_REG =
private const string DEFAULT_ENDPOINT_REG =
"^[a-z][a-z0-9-]+(\\.[a-z]{2,}-[a-z]+-\\d{1,2})?\\.(my)?(g42cloud|myhwclouds).(com|cn)";

public static Func<HttpRequest, bool> DefaultDerivedPredicate = httpRequest =>
!Regex.IsMatch(httpRequest.Url.Host, DEFAULT_ENDPOINT_REG);

internal string DerivedAuthServiceName;
internal string RegionId;

public string Ak { set; get; }
public string Sk { set; get; }
public string SecurityToken { set; get; }
public string IamEndpoint { set; get; }
public Func<HttpRequest, bool> DerivedPredicate { set; get; }

public abstract Dictionary<string, string> GetPathParamDictionary();

public abstract Task<HttpRequest> SignAuthRequest(HttpRequest request);

public abstract Credentials ProcessAuthParams(SdkHttpClient client, string regionId);
public abstract ICredential ProcessAuthParams(SdkHttpClient client, string regionId);

public abstract void ProcessDerivedAuthParams(string derivedAuthServiceName, string regionId);

public T WithIamEndpoint(string endpoint)
{
IamEndpoint = endpoint;
return (T)this;
}

public T WithSecurityToken(string token)
{
SecurityToken = token;
return (T)this;
}

public T WithDerivedPredicate(Func<HttpRequest, bool> func)
{
DerivedPredicate = func;
return (T)this;
}

protected bool IsDerivedAuth(HttpRequest httpRequest)
{
return DerivedPredicate == null ? false : DerivedPredicate(httpRequest);
}
}
}
Loading

0 comments on commit 0ba815a

Please sign in to comment.