Skip to content

Commit 0ba815a

Browse files
committed
release 0.0.9-beta source code
1 parent 3f9261c commit 0ba815a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1847
-451
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
# 0.0.9-beta 2023-10-30
2+
3+
### G42Cloud SDK MPC
4+
5+
- _Features_
6+
- None
7+
- _Bug Fix_
8+
- None
9+
- _Change_
10+
- **CreateThumbnailsTask**
11+
- changes of request param
12+
- `- thumbnail_para.percent`
13+
- `- thumbnail_para.type: enum value [PERCENT]`
14+
- **CreateTranscodingTask**
15+
- changes of request param
16+
- `- thumbnail.params.percent`
17+
- `- thumbnail.params.type: enum value [PERCENT]`
18+
119
# 0.0.8-beta 2023-08-26
220

321
### G42Cloud SDK MPC

Core/Auth/AuthCache.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,8 @@
2323

2424
namespace G42Cloud.SDK.Core.Auth
2525
{
26-
public class AuthCache
26+
internal static class AuthCache
2727
{
28-
private static readonly ConcurrentDictionary<string, string> _authDict = new ConcurrentDictionary<string, string>();
29-
30-
public static string GetAuth(string akWithName)
31-
{
32-
return _authDict.TryGetValue(akWithName, out var value) ? value : null;
33-
}
34-
35-
public static void PutAuth(string akWithName, string id)
36-
{
37-
_authDict.AddOrUpdate(akWithName, id, (key, value) => id);
38-
}
28+
internal static readonly ConcurrentDictionary<string, string> Value = new ConcurrentDictionary<string, string>();
3929
}
4030
}

Core/Auth/BasicCredentials.cs

Lines changed: 21 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,76 +22,35 @@
2222
using System;
2323
using System.Collections.Generic;
2424
using System.Threading.Tasks;
25+
using Microsoft.Extensions.Logging;
2526

2627
namespace G42Cloud.SDK.Core.Auth
2728
{
28-
public class BasicCredentials : Credentials
29+
public class BasicCredentials : Credentials<BasicCredentials>
2930
{
30-
private string _derivedAuthServiceName;
31-
private string _regionId;
31+
32+
internal const string Type = "basic";
3233

3334
public BasicCredentials(string ak, string sk, string projectId = null)
3435
{
35-
if (string.IsNullOrEmpty(ak))
36-
{
37-
throw new ArgumentNullException(nameof(ak));
38-
}
39-
40-
if (string.IsNullOrEmpty(sk))
41-
{
42-
throw new ArgumentNullException(nameof(sk));
43-
}
44-
4536
Ak = ak;
4637
Sk = sk;
4738
ProjectId = projectId;
4839
}
4940

50-
private string Ak { set; get; }
51-
private string Sk { set; get; }
5241
private string ProjectId { set; get; }
53-
private string SecurityToken { set; get; }
54-
private string IamEndpoint { set; get; }
55-
private Func<HttpRequest, bool> DerivedPredicate { set; get; }
56-
57-
public BasicCredentials WithIamEndpoint(string endpoint)
58-
{
59-
IamEndpoint = endpoint;
60-
return this;
61-
}
62-
63-
public BasicCredentials WithSecurityToken(string token)
64-
{
65-
SecurityToken = token;
66-
return this;
67-
}
6842

69-
public BasicCredentials WithDerivedPredicate(Func<HttpRequest, bool> func)
70-
{
71-
DerivedPredicate = func;
72-
return this;
73-
}
74-
75-
protected bool IsDerivedAuth(HttpRequest httpRequest)
76-
{
77-
if (DerivedPredicate == null)
78-
{
79-
return false;
80-
}
81-
82-
return DerivedPredicate(httpRequest);
83-
}
8443

8544
public override void ProcessDerivedAuthParams(string derivedAuthServiceName, string regionId)
8645
{
87-
if (_derivedAuthServiceName == null)
46+
if (DerivedAuthServiceName == null)
8847
{
89-
_derivedAuthServiceName = derivedAuthServiceName;
48+
DerivedAuthServiceName = derivedAuthServiceName;
9049
}
9150

92-
if (_regionId == null)
51+
if (RegionId == null)
9352
{
94-
_regionId = regionId;
53+
RegionId = regionId;
9554
}
9655
}
9756

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

123-
if (!string.IsNullOrEmpty(request.ContentType) && !request.ContentType.Contains("application/json"))
124-
{
125-
request.Headers.Add("X-Sdk-Content-Sha256", "UNSIGNED-PAYLOAD");
126-
}
127-
12882
if (IsDerivedAuth(request))
12983
{
130-
var signer = new DerivedSigner
131-
{
132-
Key = Ak,
133-
Secret = Sk
134-
};
135-
signer.Sign(request, _regionId, _derivedAuthServiceName);
136-
}
137-
else
138-
{
139-
var signer = new Signer
140-
{
141-
Key = Ak,
142-
Secret = Sk
143-
};
144-
signer.Sign(request);
84+
DerivedSigner.GetInstance().Sign(request, this);
85+
return request;
14586
}
14687

88+
IAkSkSigner signer = AkSkSignerFactory.GetSigner(request.SigningAlgorithm);
89+
signer.Sign(request, this);
14790
return request;
14891
});
14992

15093
return httpRequestTask;
15194
}
15295

153-
public override Credentials ProcessAuthParams(SdkHttpClient client, string regionId)
96+
public override ICredential ProcessAuthParams(SdkHttpClient client, string regionId)
15497
{
15598
if (ProjectId != null)
15699
{
157100
return this;
158101
}
159102

160103
var akWithName = Ak + regionId;
161-
var projectId = AuthCache.GetAuth(akWithName);
162-
if (!string.IsNullOrEmpty(projectId))
104+
if (AuthCache.Value.ContainsKey(akWithName))
163105
{
164-
ProjectId = projectId;
106+
ProjectId = AuthCache.Value[akWithName];
165107
return this;
166108
}
167109

168110
var derivedFunc = DerivedPredicate;
169111
DerivedPredicate = null;
170112

171113
IamEndpoint = string.IsNullOrEmpty(IamEndpoint) ? IamService.DefaultIamEndpoint : IamEndpoint;
172-
var request = IamService.GetKeystoneListProjectsRequest(IamEndpoint, regionId);
114+
var logger = client.GetLogger();
115+
logger.LogInformation("Project id of region '{}' not found in BasicCredentials, trying to obtain project id from IAM service: {}",
116+
regionId, IamEndpoint);
117+
var request = IamService.GetKeystoneListProjectsRequest(IamEndpoint, regionId, client.GetHttpConfig());
173118
request = SignAuthRequest(request).Result;
174119
try
175120
{
176121
ProjectId = IamService.KeystoneListProjects(client, request);
177-
AuthCache.PutAuth(akWithName, ProjectId);
122+
logger.LogInformation("Success to obtain project id of region '{}': {}", regionId, ProjectId);
123+
AuthCache.Value[akWithName] = ProjectId;
178124
DerivedPredicate = derivedFunc;
179125
return this;
180126
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2023 G42 Technologies Co.,Ltd.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
using System;
23+
using System.Collections.Generic;
24+
25+
namespace G42Cloud.SDK.Core.Auth
26+
{
27+
public class CredentialProviderChain : ICredentialProvider
28+
{
29+
private readonly ICredentialProvider[] _providers;
30+
31+
public CredentialProviderChain(ICredentialProvider[] providers)
32+
{
33+
_providers = providers;
34+
}
35+
36+
public static CredentialProviderChain GetDefault(string credentialType)
37+
{
38+
var credentialProviders = new ICredentialProvider[]
39+
{
40+
new EnvCredentialProvider(credentialType),
41+
new ProfileCredentialProvider(credentialType)
42+
};
43+
44+
return new CredentialProviderChain(credentialProviders);
45+
}
46+
47+
public static CredentialProviderChain GetBasic()
48+
{
49+
return GetDefault(BasicCredentials.Type);
50+
}
51+
52+
public static CredentialProviderChain GetGlobal()
53+
{
54+
return GetDefault(GlobalCredentials.Type);
55+
}
56+
57+
public ICredential GetCredentials()
58+
{
59+
var exceptions = new List<SdkException>();
60+
foreach (var provider in _providers)
61+
{
62+
try
63+
{
64+
var credentials = provider.GetCredentials();
65+
if (credentials != null)
66+
{
67+
return credentials;
68+
}
69+
}
70+
catch (SdkException e)
71+
{
72+
exceptions.Add(e);
73+
}
74+
}
75+
76+
throw new SdkException("failed to get credentials in providers", new AggregateException(exceptions));
77+
}
78+
}
79+
}

Core/Auth/Credentials.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,52 @@
2626

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

3434
public static Func<HttpRequest, bool> DefaultDerivedPredicate = httpRequest =>
3535
!Regex.IsMatch(httpRequest.Url.Host, DEFAULT_ENDPOINT_REG);
36+
37+
internal string DerivedAuthServiceName;
38+
internal string RegionId;
39+
40+
public string Ak { set; get; }
41+
public string Sk { set; get; }
42+
public string SecurityToken { set; get; }
43+
public string IamEndpoint { set; get; }
44+
public Func<HttpRequest, bool> DerivedPredicate { set; get; }
3645

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

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

41-
public abstract Credentials ProcessAuthParams(SdkHttpClient client, string regionId);
50+
public abstract ICredential ProcessAuthParams(SdkHttpClient client, string regionId);
4251

4352
public abstract void ProcessDerivedAuthParams(string derivedAuthServiceName, string regionId);
53+
54+
public T WithIamEndpoint(string endpoint)
55+
{
56+
IamEndpoint = endpoint;
57+
return (T)this;
58+
}
59+
60+
public T WithSecurityToken(string token)
61+
{
62+
SecurityToken = token;
63+
return (T)this;
64+
}
65+
66+
public T WithDerivedPredicate(Func<HttpRequest, bool> func)
67+
{
68+
DerivedPredicate = func;
69+
return (T)this;
70+
}
71+
72+
protected bool IsDerivedAuth(HttpRequest httpRequest)
73+
{
74+
return DerivedPredicate == null ? false : DerivedPredicate(httpRequest);
75+
}
4476
}
4577
}

0 commit comments

Comments
 (0)