Skip to content

Commit

Permalink
release 0.0.3-beta source code for net
Browse files Browse the repository at this point in the history
  • Loading branch information
unionsdk committed Jan 6, 2023
1 parent 15a4156 commit d77115a
Show file tree
Hide file tree
Showing 2,038 changed files with 49,735 additions and 11,867 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# 0.0.3-beta 2023-01-06

### G42Cloud SDK IMS

- _Features_
- New Support IMS
- _Bug Fix_
- None
- _Change_
- None

### G42Cloud SDK SMN

- _Features_
- New Support SMN
- _Bug Fix_
- None
- _Change_
- None

# 0.0.2-beta 2022-11-29

### G42Cloud SDK CBR
Expand Down
42 changes: 42 additions & 0 deletions Core/Auth/AuthCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020 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.Collections.Concurrent;

namespace G42Cloud.SDK.Core.Auth
{
public class AuthCache
{
private static 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);
}


}
}
187 changes: 187 additions & 0 deletions Core/Auth/BasicCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* Copyright 2020 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;
using System.Threading.Tasks;
using static System.String;

namespace G42Cloud.SDK.Core.Auth
{
public class BasicCredentials : Credentials
{
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; }
private string _derivedAuthServiceName;
private string _regionId;

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

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

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

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

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

public BasicCredentials WithDerivedPredicate(Func<HttpRequest, bool> func)
{
this.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 (this._derivedAuthServiceName == null)
{
this._derivedAuthServiceName = derivedAuthServiceName;
}

if (this._regionId == null)
{
this._regionId = regionId;
}
}

public override Dictionary<string, string> GetPathParamDictionary()
{
var pathParamDictionary = new Dictionary<string, string>();
if (ProjectId != null)
{
pathParamDictionary.Add("project_id", ProjectId);
}

return pathParamDictionary;
}

public override Task<HttpRequest> SignAuthRequest(HttpRequest request)
{
var httpRequestTask = Task<HttpRequest>.Factory.StartNew(() =>
{
if (ProjectId != null)
{
request.Headers.Add("X-Project-Id", ProjectId);
}

if (SecurityToken != null)
{
request.Headers.Add("X-Security-Token", SecurityToken);
}

if (!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);
}

return request;
});

return httpRequestTask;
}

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

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

Func<HttpRequest, bool> derivedFunc = DerivedPredicate;
DerivedPredicate = null;

IamEndpoint = IsNullOrEmpty(IamEndpoint) ? IamService.DefaultIamEndpoint : IamEndpoint;
var request = IamService.GetKeystoneListProjectsRequest(IamEndpoint, regionId);
request = SignAuthRequest(request).Result;
try
{
ProjectId = IamService.KeystoneListProjects(client, request);
AuthCache.PutAuth(akWithName, ProjectId);
DerivedPredicate = derivedFunc;
return this;
}
catch (ServiceResponseException e)
{
throw new ArgumentException("Failed to get project id, " + e.ErrorMsg);
}
}
}
}
44 changes: 44 additions & 0 deletions Core/Auth/Credentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020 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.Collections.Generic;
using System.Threading.Tasks;
using System;
using System.Text.RegularExpressions;

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

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

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

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

public static Func<HttpRequest, bool> DefaultDerivedPredicate = httpRequest =>
!Regex.IsMatch(httpRequest.Url.Host, DEFAULT_ENDPOINT_REG);
}
}
56 changes: 56 additions & 0 deletions Core/Auth/EnvCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 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;

namespace G42Cloud.SDK.Core.Auth
{
public class EnvCredentials
{
private const string AkEnvName = "G42CLOUD_SDK_AK";
private const string SkEnvName = "G42CLOUD_SDK_SK";
private const string ProjectIdEnvName = "G42CLOUD_SDK_PROJECT_ID";
private const string DomainIdEnvName = "G42CLOUD_SDK_DOMAIN_ID";

private const string BasicCredentialsType = "BasicCredentials";
private const string GlobalCredentialsType = "GlobalCredentials";

public static Credentials LoadCredentialsFromEnv(string defaultType)
{
var ak = Environment.GetEnvironmentVariable(AkEnvName);
var sk = Environment.GetEnvironmentVariable(SkEnvName);

if (Equals(BasicCredentialsType, defaultType))
{
var projectId = Environment.GetEnvironmentVariable(ProjectIdEnvName);
return new BasicCredentials(ak, sk, projectId);
}

if (Equals(GlobalCredentialsType, defaultType))
{
var domainId = Environment.GetEnvironmentVariable(DomainIdEnvName);
return new GlobalCredentials(ak, sk, domainId);
}

return null;
}
}
}
Loading

0 comments on commit d77115a

Please sign in to comment.