From 85c113f12a8e5b612f37d12ad1c732f477f1f8d6 Mon Sep 17 00:00:00 2001 From: yndu13 Date: Mon, 23 Sep 2024 21:39:03 +0800 Subject: [PATCH] improve credentials null check --- csharp/core/Client.cs | 64 +++++++++ csharp/core/Properties/AssemblyInfo.cs | 4 +- golang/client/client.go | 32 +++++ golang/client/client_test.go | 70 +++++++++ .../java/com/aliyun/teaopenapi/Client.java | 28 ++++ .../com/aliyun/teaopenapi/ClientTest.java | 73 ++++++++++ main.tea | 24 ++++ php/src/OpenApiClient.php | 24 ++++ python/alibabacloud_tea_openapi/client.py | 40 ++++++ python/setup.py | 2 +- python/tests/test_client.py | 136 ++++++++++++++++++ .../Sources/AlibabacloudOpenApi/Client.swift | 24 ++++ ts/src/client.ts | 28 ++++ ts/test/client.spec.ts | 69 +++++++++ 14 files changed, 615 insertions(+), 3 deletions(-) diff --git a/csharp/core/Client.cs b/csharp/core/Client.cs index bddb278..9c168d2 100644 --- a/csharp/core/Client.cs +++ b/csharp/core/Client.cs @@ -264,6 +264,14 @@ public Dictionary DoRPCRequest(string action, string version, st } if (!AlibabaCloud.TeaUtil.Common.EqualString(authType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = this._credential.GetCredential(); string credentialType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(credentialType, "bearer")) @@ -536,6 +544,14 @@ public async Task> DoRPCRequestAsync(string action, s } if (!AlibabaCloud.TeaUtil.Common.EqualString(authType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = await this._credential.GetCredentialAsync(); string credentialType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(credentialType, "bearer")) @@ -791,6 +807,14 @@ public Dictionary DoROARequest(string action, string version, st } if (!AlibabaCloud.TeaUtil.Common.EqualString(authType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = this._credential.GetCredential(); string credentialType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(credentialType, "bearer")) @@ -1043,6 +1067,14 @@ public async Task> DoROARequestAsync(string action, s } if (!AlibabaCloud.TeaUtil.Common.EqualString(authType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = await this._credential.GetCredentialAsync(); string credentialType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(credentialType, "bearer")) @@ -1296,6 +1328,14 @@ public Dictionary DoROARequestWithForm(string action, string ver } if (!AlibabaCloud.TeaUtil.Common.EqualString(authType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = this._credential.GetCredential(); string credentialType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(credentialType, "bearer")) @@ -1547,6 +1587,14 @@ public async Task> DoROARequestWithFormAsync(string a } if (!AlibabaCloud.TeaUtil.Common.EqualString(authType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = await this._credential.GetCredentialAsync(); string credentialType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(credentialType, "bearer")) @@ -1833,6 +1881,14 @@ public Dictionary DoRequest(Params params_, OpenApiRequest reque request_.Headers["x-acs-content-sha256"] = hashedRequestPayload; if (!AlibabaCloud.TeaUtil.Common.EqualString(params_.AuthType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = this._credential.GetCredential(); string authType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(authType, "bearer")) @@ -2130,6 +2186,14 @@ public async Task> DoRequestAsync(Params params_, Ope request_.Headers["x-acs-content-sha256"] = hashedRequestPayload; if (!AlibabaCloud.TeaUtil.Common.EqualString(params_.AuthType, "Anonymous")) { + if (AlibabaCloud.TeaUtil.Common.IsUnset(_credential)) + { + throw new TeaException(new Dictionary + { + {"code", "InvalidCredentials"}, + {"message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."}, + }); + } Aliyun.Credentials.Models.CredentialModel credentialModel = await this._credential.GetCredentialAsync(); string authType = credentialModel.Type; if (AlibabaCloud.TeaUtil.Common.EqualString(authType, "bearer")) diff --git a/csharp/core/Properties/AssemblyInfo.cs b/csharp/core/Properties/AssemblyInfo.cs index 119de3a..f2b04aa 100644 --- a/csharp/core/Properties/AssemblyInfo.cs +++ b/csharp/core/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly : AssemblyVersion("0.1.11.0")] -[assembly : AssemblyFileVersion("0.1.11.0")] +[assembly : AssemblyVersion("0.1.12.0")] +[assembly : AssemblyFileVersion("0.1.12.0")] diff --git a/golang/client/client.go b/golang/client/client.go index 5726c5a..79d9ea2 100644 --- a/golang/client/client.go +++ b/golang/client/client.go @@ -733,6 +733,14 @@ func (client *Client) DoRPCRequest(action *string, version *string, protocol *st } if !tea.BoolValue(util.EqualString(authType, tea.String("Anonymous"))) { + if tea.BoolValue(util.IsUnset(client.Credential)) { + _err = tea.NewSDKError(map[string]interface{}{ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", + }) + return _result, _err + } + credentialModel, _err := client.Credential.GetCredential() if _err != nil { return _result, _err @@ -1004,6 +1012,14 @@ func (client *Client) DoROARequest(action *string, version *string, protocol *st } if !tea.BoolValue(util.EqualString(authType, tea.String("Anonymous"))) { + if tea.BoolValue(util.IsUnset(client.Credential)) { + _err = tea.NewSDKError(map[string]interface{}{ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", + }) + return _result, _err + } + credentialModel, _err := client.Credential.GetCredential() if _err != nil { return _result, _err @@ -1277,6 +1293,14 @@ func (client *Client) DoROARequestWithForm(action *string, version *string, prot } if !tea.BoolValue(util.EqualString(authType, tea.String("Anonymous"))) { + if tea.BoolValue(util.IsUnset(client.Credential)) { + _err = tea.NewSDKError(map[string]interface{}{ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", + }) + return _result, _err + } + credentialModel, _err := client.Credential.GetCredential() if _err != nil { return _result, _err @@ -1591,6 +1615,14 @@ func (client *Client) DoRequest(params *Params, request *OpenApiRequest, runtime request_.Headers["x-acs-content-sha256"] = hashedRequestPayload if !tea.BoolValue(util.EqualString(params.AuthType, tea.String("Anonymous"))) { + if tea.BoolValue(util.IsUnset(client.Credential)) { + _err = tea.NewSDKError(map[string]interface{}{ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", + }) + return _result, _err + } + credentialModel, _err := client.Credential.GetCredential() if _err != nil { return _result, _err diff --git a/golang/client/client_test.go b/golang/client/client_test.go index c8795d8..9f5cd56 100644 --- a/golang/client/client_test.go +++ b/golang/client/client_test.go @@ -369,6 +369,10 @@ func CreateBearerTokenConfig() (_result *Config) { return _result } +func CreateAnonymousConfig() (_result *Config) { + return &Config{} +} + func CreateRuntimeOptions() (_result *util.RuntimeOptions) { extendsParameters := &util.ExtendsParameters{ Headers: map[string]*string{ @@ -491,6 +495,17 @@ func TestCallApiForRPCWithV2Sign_AK_Form(t *testing.T) { tea_util.AssertNotNil(t, find) // tea_util.AssertEqual(t, "bearer token", headers["authorization"]) + // Anonymous error + config = CreateAnonymousConfig() + config.Protocol = tea.String("HTTP") + config.SignatureAlgorithm = tea.String("v2") + config.Endpoint = tea.String("127.0.0.1:9001") + client, _err = NewClient(config) + tea_util.AssertNil(t, _err) + _, _err = client.CallApi(params, request, runtime) + err := _err.(*tea.SDKError) + tea_util.AssertEqual(t, "InvalidCredentials", tea.StringValue(err.Code)) + tea_util.AssertEqual(t, "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", tea.StringValue(err.Message)) } func TestCallApiForRPCWithV2Sign_Anonymous_JSON(t *testing.T) { @@ -649,6 +664,18 @@ func TestCallApiForROAWithV2Sign_HTTPS_AK_Form(t *testing.T) { tea_util.AssertEqual(t, "token", headers["x-acs-bearer-token"]) tea_util.AssertEqual(t, "BEARERTOKEN", headers["x-acs-signature-type"]) + // Anonymous error + config = CreateAnonymousConfig() + config.Protocol = tea.String("HTTP") + config.SignatureAlgorithm = tea.String("v2") + config.Endpoint = tea.String("127.0.0.1:9003") + client, _err = NewClient(config) + tea_util.AssertNil(t, _err) + _, _err = client.CallApi(params, request, runtime) + err := _err.(*tea.SDKError) + tea_util.AssertEqual(t, "InvalidCredentials", tea.StringValue(err.Code)) + tea_util.AssertEqual(t, "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", tea.StringValue(err.Message)) + params = &Params{ Action: tea.String("TestAPI"), Version: tea.String("2022-06-01"), @@ -660,6 +687,13 @@ func TestCallApiForROAWithV2Sign_HTTPS_AK_Form(t *testing.T) { ReqBodyType: tea.String("json"), BodyType: tea.String("json"), } + // bearer token + config = CreateBearerTokenConfig() + config.Protocol = tea.String("HTTP") + config.SignatureAlgorithm = tea.String("v2") + config.Endpoint = tea.String("127.0.0.1:9003") + client, _err = NewClient(config) + tea_util.AssertNil(t, _err) result, _err = client.CallApi(params, request, runtime) tea_util.AssertNil(t, _err) @@ -668,6 +702,19 @@ func TestCallApiForROAWithV2Sign_HTTPS_AK_Form(t *testing.T) { // tea_util.AssertEqual(t, "bearer token", headers["authorization"]) tea_util.AssertEqual(t, "token", headers["x-acs-bearer-token"]) tea_util.AssertEqual(t, "BEARERTOKEN", headers["x-acs-signature-type"]) + + // Anonymous error + config = CreateAnonymousConfig() + config.Protocol = tea.String("HTTP") + config.SignatureAlgorithm = tea.String("v2") + config.Endpoint = tea.String("127.0.0.1:9003") + client, _err = NewClient(config) + tea_util.AssertNil(t, _err) + _, _err = client.CallApi(params, request, runtime) + err = _err.(*tea.SDKError) + tea_util.AssertEqual(t, "InvalidCredentials", tea.StringValue(err.Code)) + tea_util.AssertEqual(t, "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", tea.StringValue(err.Message)) + } func TestCallApiForROAWithV2Sign_Anonymous_JSON(t *testing.T) { @@ -826,6 +873,17 @@ func TestCallApiForRPCWithV3Sign_AK_Form(t *testing.T) { // tea_util.AssertEqual(t, "bearer token", headers["authorization"]) tea_util.AssertEqual(t, "token", headers["x-acs-bearer-token"]) tea_util.AssertEqual(t, "SignatureType=BEARERTOKEN&extends-key=extends-value&key1=value&key2=1&key3=true", headers["raw-query"]) + + // Anonymous error + config = CreateAnonymousConfig() + config.Protocol = tea.String("HTTP") + config.Endpoint = tea.String("127.0.0.1:9005") + client, _err = NewClient(config) + tea_util.AssertNil(t, _err) + _, _err = client.CallApi(params, request, runtime) + err := _err.(*tea.SDKError) + tea_util.AssertEqual(t, "InvalidCredentials", tea.StringValue(err.Code)) + tea_util.AssertEqual(t, "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", tea.StringValue(err.Message)) } func TestCallApiForRPCWithV3Sign_Anonymous_JSON(t *testing.T) { @@ -982,6 +1040,18 @@ func TestCallApiForROAWithV3Sign_AK_Form(t *testing.T) { // tea_util.AssertEqual(t, "bearer token", headers["authorization"]) tea_util.AssertEqual(t, "token", headers["x-acs-bearer-token"]) tea_util.AssertEqual(t, "BEARERTOKEN", headers["x-acs-signature-type"]) + + // Anonymous error + config = CreateAnonymousConfig() + config.Protocol = tea.String("HTTP") + config.SignatureAlgorithm = tea.String("v2") + config.Endpoint = tea.String("127.0.0.1:9003") + client, _err = NewClient(config) + tea_util.AssertNil(t, _err) + _, _err = client.CallApi(params, request, runtime) + err := _err.(*tea.SDKError) + tea_util.AssertEqual(t, "InvalidCredentials", tea.StringValue(err.Code)) + tea_util.AssertEqual(t, "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", tea.StringValue(err.Message)) } func TestCallApiForROAWithV3Sign_Anonymous_JSON(t *testing.T) { diff --git a/java/src/main/java/com/aliyun/teaopenapi/Client.java b/java/src/main/java/com/aliyun/teaopenapi/Client.java index c913779..08624be 100644 --- a/java/src/main/java/com/aliyun/teaopenapi/Client.java +++ b/java/src/main/java/com/aliyun/teaopenapi/Client.java @@ -233,6 +233,13 @@ public Client(com.aliyun.teaopenapi.models.Config config) throws Exception { } if (!com.aliyun.teautil.Common.equalString(authType, "Anonymous")) { + if (com.aliyun.teautil.Common.isUnset(_credential)) { + throw new TeaException(TeaConverter.buildMap( + new TeaPair("code", "InvalidCredentials"), + new TeaPair("message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.") + )); + } + com.aliyun.credentials.models.CredentialModel credentialModel = _credential.getCredential(); String credentialType = credentialModel.type; if (com.aliyun.teautil.Common.equalString(credentialType, "bearer")) { @@ -452,6 +459,13 @@ public Client(com.aliyun.teaopenapi.models.Config config) throws Exception { } if (!com.aliyun.teautil.Common.equalString(authType, "Anonymous")) { + if (com.aliyun.teautil.Common.isUnset(_credential)) { + throw new TeaException(TeaConverter.buildMap( + new TeaPair("code", "InvalidCredentials"), + new TeaPair("message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.") + )); + } + com.aliyun.credentials.models.CredentialModel credentialModel = _credential.getCredential(); String credentialType = credentialModel.type; if (com.aliyun.teautil.Common.equalString(credentialType, "bearer")) { @@ -669,6 +683,13 @@ public Client(com.aliyun.teaopenapi.models.Config config) throws Exception { } if (!com.aliyun.teautil.Common.equalString(authType, "Anonymous")) { + if (com.aliyun.teautil.Common.isUnset(_credential)) { + throw new TeaException(TeaConverter.buildMap( + new TeaPair("code", "InvalidCredentials"), + new TeaPair("message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.") + )); + } + com.aliyun.credentials.models.CredentialModel credentialModel = _credential.getCredential(); String credentialType = credentialModel.type; if (com.aliyun.teautil.Common.equalString(credentialType, "bearer")) { @@ -913,6 +934,13 @@ public Client(com.aliyun.teaopenapi.models.Config config) throws Exception { request_.headers.put("x-acs-content-sha256", hashedRequestPayload); if (!com.aliyun.teautil.Common.equalString(params.authType, "Anonymous")) { + if (com.aliyun.teautil.Common.isUnset(_credential)) { + throw new TeaException(TeaConverter.buildMap( + new TeaPair("code", "InvalidCredentials"), + new TeaPair("message", "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.") + )); + } + com.aliyun.credentials.models.CredentialModel credentialModel = _credential.getCredential(); String authType = credentialModel.type; if (com.aliyun.teautil.Common.equalString(authType, "bearer")) { diff --git a/java/src/test/java/com/aliyun/teaopenapi/ClientTest.java b/java/src/test/java/com/aliyun/teaopenapi/ClientTest.java index 58bb33c..3ed89d2 100644 --- a/java/src/test/java/com/aliyun/teaopenapi/ClientTest.java +++ b/java/src/test/java/com/aliyun/teaopenapi/ClientTest.java @@ -211,6 +211,10 @@ public static Config createBearerTokenConfig() throws Exception { return config; } + public static Config createAnonymousConfig() throws Exception { + return new Config(); + } + public static RuntimeOptions createRuntimeOptions() throws Exception { ExtendsParameters extendsParameters = ExtendsParameters.build(TeaConverter.buildMap( new TeaPair("headers", TeaConverter.buildMap( @@ -329,6 +333,19 @@ public void testCallApiForRPCWithV2Sign_AK_Form() throws Exception { .withHeader("x-acs-request-id", "A45EE076-334D-5012-9746-A8F828D20FD4"))); result = client.callApi(params, request, runtime); Assert.assertEquals(200, result.get("statusCode")); + + // Anonymous error + config = ClientTest.createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = "localhost:" + wireMock.port(); + client = new Client(config); + try { + client.callApi(params, request, runtime); + } catch (TeaException e) { + Assert.assertEquals("Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", e.getMessage()); + Assert.assertEquals("InvalidCredentials", e.getCode()); + } } @Test @@ -460,6 +477,19 @@ public void testCallApiForROAWithV2Sign_AK_Form() throws Exception { result = client.callApi(params, request, runtime); Assert.assertEquals(200, result.get("statusCode")); + // Anonymous error + config = ClientTest.createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = "localhost:" + wireMock.port(); + client = new Client(config); + try { + client.callApi(params, request, runtime); + } catch (TeaException e) { + Assert.assertEquals("Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", e.getMessage()); + Assert.assertEquals("InvalidCredentials", e.getCode()); + } + params = Params.build(TeaConverter.buildMap( new TeaPair("action", "TestAPI"), new TeaPair("version", "2022-06-01"), @@ -471,6 +501,12 @@ public void testCallApiForROAWithV2Sign_AK_Form() throws Exception { new TeaPair("reqBodyType", "json"), new TeaPair("bodyType", "json") )); + // bearer token + config = ClientTest.createBearerTokenConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = "localhost:" + wireMock.port(); + client = new Client(config); stubFor(post(anyUrl()) .withHeader("x-acs-version", equalTo("2022-06-01")) .withHeader("x-acs-action", equalTo("TestAPI")) @@ -482,6 +518,19 @@ public void testCallApiForROAWithV2Sign_AK_Form() throws Exception { .withHeader("x-acs-request-id", "A45EE076-334D-5012-9746-A8F828D20FD4"))); result = client.callApi(params, request, runtime); Assert.assertEquals(200, result.get("statusCode")); + + // Anonymous error + config = ClientTest.createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = "localhost:" + wireMock.port(); + client = new Client(config); + try { + client.callApi(params, request, runtime); + } catch (TeaException e) { + Assert.assertEquals("Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", e.getMessage()); + Assert.assertEquals("InvalidCredentials", e.getCode()); + } } @Test @@ -608,6 +657,18 @@ public void testCallApiForRPCWithV3Sign_AK_Form() throws Exception { .withHeader("x-acs-request-id", "A45EE076-334D-5012-9746-A8F828D20FD4"))); result = client.callApi(params, request, runtime); Assert.assertEquals(200, result.get("statusCode")); + + // Anonymous error + config = ClientTest.createAnonymousConfig(); + config.protocol = "HTTP"; + config.endpoint = "localhost:" + wireMock.port(); + client = new Client(config); + try { + client.callApi(params, request, runtime); + } catch (TeaException e) { + Assert.assertEquals("Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", e.getMessage()); + Assert.assertEquals("InvalidCredentials", e.getCode()); + } } @Test @@ -732,6 +793,18 @@ public void testCallApiForROAWithV3Sign_AK_Form() throws Exception { .withHeader("x-acs-request-id", "A45EE076-334D-5012-9746-A8F828D20FD4"))); result = client.callApi(params, request, runtime); Assert.assertEquals(200, result.get("statusCode")); + + // Anonymous error + config = ClientTest.createAnonymousConfig(); + config.protocol = "HTTP"; + config.endpoint = "localhost:" + wireMock.port(); + client = new Client(config); + try { + client.callApi(params, request, runtime); + } catch (TeaException e) { + Assert.assertEquals("Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.", e.getMessage()); + Assert.assertEquals("InvalidCredentials", e.getCode()); + } } @Test diff --git a/main.tea b/main.tea index 986275c..b6243c4 100644 --- a/main.tea +++ b/main.tea @@ -227,6 +227,12 @@ api doRPCRequest(action: string, version: string, protocol: string, method: stri } if (!Util.equalString(authType, 'Anonymous')) { + if (Util.isUnset(@credential)) { + throw { + code = `InvalidCredentials`, + message = `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.` + } + } var credentialModel = @credential.getCredential(); var credentialType = credentialModel.type; if (Util.equalString(credentialType, 'bearer')) { @@ -405,6 +411,12 @@ api doROARequest(action: string, version: string, protocol: string, method: stri } if (!Util.equalString(authType, 'Anonymous')) { + if (Util.isUnset(@credential)) { + throw { + code = `InvalidCredentials`, + message = `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.` + } + } var credentialModel = @credential.getCredential(); var credentialType = credentialModel.type; if (Util.equalString(credentialType, 'bearer')) { @@ -584,6 +596,12 @@ api doROARequestWithForm(action: string, version: string, protocol: string, meth } if (!Util.equalString(authType, 'Anonymous')) { + if (Util.isUnset(@credential)) { + throw { + code = `InvalidCredentials`, + message = `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.` + } + } var credentialModel = @credential.getCredential(); var credentialType = credentialModel.type; if (Util.equalString(credentialType, 'bearer')) { @@ -816,6 +834,12 @@ api doRequest(params: Params, request: OpenApiRequest, runtime: Util.RuntimeOpti __request.headers.x-acs-content-sha256 = hashedRequestPayload; if (!Util.equalString(params.authType, 'Anonymous')) { + if (Util.isUnset(@credential)) { + throw { + code = `InvalidCredentials`, + message = `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.` + } + } var credentialModel = @credential.getCredential(); var authType = credentialModel.type; if (Util.equalString(authType, 'bearer')) { diff --git a/php/src/OpenApiClient.php b/php/src/OpenApiClient.php index f428e55..915e080 100644 --- a/php/src/OpenApiClient.php +++ b/php/src/OpenApiClient.php @@ -259,6 +259,12 @@ public function doRPCRequest($action, $version, $protocol, $method, $authType, $ $_request->headers["content-type"] = "application/x-www-form-urlencoded"; } if (!Utils::equalString($authType, "Anonymous")) { + if (Utils::isUnset($this->_credential)) { + throw new TeaError([ + "code" => "InvalidCredentials", + "message" => "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]); + } $credentialType = $this->getType(); if (Utils::equalString($credentialType, "bearer")) { $bearerToken = $this->getBearerToken(); @@ -455,6 +461,12 @@ public function doROARequest($action, $version, $protocol, $method, $authType, $ $_request->query = Tea::merge($_request->query, $request->query); } if (!Utils::equalString($authType, "Anonymous")) { + if (Utils::isUnset($this->_credential)) { + throw new TeaError([ + "code" => "InvalidCredentials", + "message" => "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]); + } $credentialType = $this->getType(); if (Utils::equalString($credentialType, "bearer")) { $bearerToken = $this->getBearerToken(); @@ -652,6 +664,12 @@ public function doROARequestWithForm($action, $version, $protocol, $method, $aut $_request->query = Tea::merge($_request->query, $request->query); } if (!Utils::equalString($authType, "Anonymous")) { + if (Utils::isUnset($this->_credential)) { + throw new TeaError([ + "code" => "InvalidCredentials", + "message" => "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]); + } $credentialType = $this->getType(); if (Utils::equalString($credentialType, "bearer")) { $bearerToken = $this->getBearerToken(); @@ -867,6 +885,12 @@ public function doRequest($params, $request, $runtime) } $_request->headers["x-acs-content-sha256"] = $hashedRequestPayload; if (!Utils::equalString($params->authType, "Anonymous")) { + if (Utils::isUnset($this->_credential)) { + throw new TeaError([ + "code" => "InvalidCredentials", + "message" => "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]); + } $authType = $this->getType(); if (Utils::equalString($authType, "bearer")) { $bearerToken = $this->getBearerToken(); diff --git a/python/alibabacloud_tea_openapi/client.py b/python/alibabacloud_tea_openapi/client.py index 94301da..2d7f4a0 100644 --- a/python/alibabacloud_tea_openapi/client.py +++ b/python/alibabacloud_tea_openapi/client.py @@ -223,6 +223,11 @@ def do_rpcrequest( _request.body = UtilClient.to_form_string(tmp) _request.headers['content-type'] = 'application/x-www-form-urlencoded' if not UtilClient.equal_string(auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = self._credential.get_credential() credential_type = credential_model.type if UtilClient.equal_string(credential_type, 'bearer'): @@ -419,6 +424,11 @@ async def do_rpcrequest_async( _request.body = UtilClient.to_form_string(tmp) _request.headers['content-type'] = 'application/x-www-form-urlencoded' if not UtilClient.equal_string(auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = await self._credential.get_credential_async() credential_type = credential_model.type if UtilClient.equal_string(credential_type, 'bearer'): @@ -605,6 +615,11 @@ def do_roarequest( _request.query = TeaCore.merge(_request.query, request.query) if not UtilClient.equal_string(auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = self._credential.get_credential() credential_type = credential_model.type if UtilClient.equal_string(credential_type, 'bearer'): @@ -790,6 +805,11 @@ async def do_roarequest_async( _request.query = TeaCore.merge(_request.query, request.query) if not UtilClient.equal_string(auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = await self._credential.get_credential_async() credential_type = credential_model.type if UtilClient.equal_string(credential_type, 'bearer'): @@ -976,6 +996,11 @@ def do_roarequest_with_form( _request.query = TeaCore.merge(_request.query, request.query) if not UtilClient.equal_string(auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = self._credential.get_credential() credential_type = credential_model.type if UtilClient.equal_string(credential_type, 'bearer'): @@ -1160,6 +1185,11 @@ async def do_roarequest_with_form_async( _request.query = TeaCore.merge(_request.query, request.query) if not UtilClient.equal_string(auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = await self._credential.get_credential_async() credential_type = credential_model.type if UtilClient.equal_string(credential_type, 'bearer'): @@ -1361,6 +1391,11 @@ def do_request( _request.headers['content-type'] = 'application/x-www-form-urlencoded' _request.headers['x-acs-content-sha256'] = hashed_request_payload if not UtilClient.equal_string(params.auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = self._credential.get_credential() auth_type = credential_model.type if UtilClient.equal_string(auth_type, 'bearer'): @@ -1568,6 +1603,11 @@ async def do_request_async( _request.headers['content-type'] = 'application/x-www-form-urlencoded' _request.headers['x-acs-content-sha256'] = hashed_request_payload if not UtilClient.equal_string(params.auth_type, 'Anonymous'): + if UtilClient.is_unset(self._credential): + raise TeaException({ + 'code': f'InvalidCredentials', + 'message': f'Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.' + }) credential_model = await self._credential.get_credential_async() auth_type = credential_model.type if UtilClient.equal_string(auth_type, 'bearer'): diff --git a/python/setup.py b/python/setup.py index 38aa5c9..8ee9e56 100644 --- a/python/setup.py +++ b/python/setup.py @@ -24,7 +24,7 @@ """ setup module for alibabacloud_tea_openapi. -Created on 19/09/2024 +Created on 23/09/2024 @author: Alibaba Cloud SDK """ diff --git a/python/tests/test_client.py b/python/tests/test_client.py index 4dbcf3e..3206417 100644 --- a/python/tests/test_client.py +++ b/python/tests/test_client.py @@ -179,6 +179,9 @@ def create_bearer_token_config(self) -> open_api_models.Config: credential=credential ) return config + + def create_anonymous_config(self) -> open_api_models.Config: + return open_api_models.Config() def create_runtime_options(self) -> util_models.RuntimeOptions: extends_parameters = util_models.ExtendsParameters( @@ -314,6 +317,18 @@ def bearer_request_callback(request: HTTPrettyRequest, uri: str, headers: dict): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + client.call_api(params, request, runtime) + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @aioresponses() def test_call_api_for_rpcwith_v2sign_ak_form_async(self, m): requestBody = 'key1=value&key2=1&key3=True' @@ -405,6 +420,19 @@ def bearer_request_callback(url, **request): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + loop.run_until_complete(client.call_api_async(params, request, runtime)) + assert False + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @httpretty.activate(allow_net_connect=False) def test_call_api_for_rpcwith_v2sign_anonymous_json(self): requestBody = 'key1=value&key2=1&key3=True' @@ -611,7 +639,23 @@ def bearer_request_callback(request: HTTPrettyRequest, uri: str, headers: dict): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + client.call_api(params, request, runtime) + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + requestBody = '{"key1":"value","key2":1,"key3":true}' + # bearer token + config = self.create_bearer_token_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' config.endpoint = 'test2.aliyuncs.com' client = OpenApiClient(config) params = open_api_models.Params( @@ -647,6 +691,18 @@ def bearer_json_request_callback(request: HTTPrettyRequest, uri: str, headers: d self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + client.call_api(params, request, runtime) + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @aioresponses() def test_call_api_for_roawith_v2sign_ak_form_async(self, m): requestBody = 'key1=value&key2=1&key3=True' @@ -747,7 +803,24 @@ def bearer_request_callback(url, **request): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + loop.run_until_complete(client.call_api_async(params, request, runtime)) + assert False + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + requestBody = '{"key1":"value","key2":1,"key3":true}' + # bearer token + config = self.create_bearer_token_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' config.endpoint = 'test2.aliyuncs.com' client = OpenApiClient(config) params = open_api_models.Params( @@ -790,6 +863,19 @@ def bearer_json_request_callback(url, **request): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + loop.run_until_complete(client.call_api_async(params, request, runtime)) + assert False + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @httpretty.activate(allow_net_connect=False) def test_call_api_for_roawith_v2sign_anonymous_json(self): requestBody = '{"key1":"value","key2":1,"key3":true}' @@ -999,6 +1085,18 @@ def bearer_request_callback(request: HTTPrettyRequest, uri: str, headers: dict): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + client.call_api(params, request, runtime) + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @aioresponses() def test_call_api_for_rpcwith_v3sign_ak_form_async(self, m): requestBody = 'key1=value&key2=1&key3=True' @@ -1099,6 +1197,19 @@ def bearer_request_callback(url, **request): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + loop.run_until_complete(client.call_api_async(params, request, runtime)) + assert False + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @httpretty.activate(allow_net_connect=False) def test_call_api_for_rpcwith_v3sign_anonymous_json(self): requestBody = '{"key1":"value","key2":1,"key3":true}' @@ -1306,6 +1417,18 @@ def bearer_request_callback(request: HTTPrettyRequest, uri: str, headers: dict): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + client.call_api(params, request, runtime) + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @aioresponses() def test_call_api_for_roawith_v3sign_ak_form_async(self, m): requestBody = 'key1=value&key2=1&key3=True' @@ -1406,6 +1529,19 @@ def bearer_request_callback(url, **request): self.assertEqual('A45EE076-334D-5012-9746-A8F828D20FD4', result.get("headers").get("x-acs-request-id")) self.assertEqual(200, result.get('statusCode')) + # anonymous error + config = self.create_anonymous_config() + config.protocol = 'HTTP' + config.signature_algorithm = 'v2' + config.endpoint = 'test.aliyuncs.com' + client = OpenApiClient(config) + try: + loop.run_until_complete(client.call_api_async(params, request, runtime)) + assert False + except TeaException as e: + self.assertEqual('Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.', e.message) + self.assertEqual('InvalidCredentials', e.code) + @httpretty.activate(allow_net_connect=False) def test_call_api_for_roawith_v3sign_anonymous_json(self): requestBody = '{"key1":"value","key2":1,"key3":true}' diff --git a/swift/Sources/AlibabacloudOpenApi/Client.swift b/swift/Sources/AlibabacloudOpenApi/Client.swift index 495be6f..b2af32b 100644 --- a/swift/Sources/AlibabacloudOpenApi/Client.swift +++ b/swift/Sources/AlibabacloudOpenApi/Client.swift @@ -221,6 +221,12 @@ open class Client { _request.headers["content-type"] = "application/x-www-form-urlencoded"; } if (!TeaUtils.Client.equalString(authType, "Anonymous")) { + if (TeaUtils.Client.isUnset(self._credential)) { + throw Tea.ReuqestError([ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]) + } var credentialType: String = try await getType() if (TeaUtils.Client.equalString(credentialType, "bearer")) { var bearerToken: String = try await getBearerToken() @@ -405,6 +411,12 @@ open class Client { _request.query = Tea.TeaConverter.merge([:], _request.query, request.query ?? [:]) } if (!TeaUtils.Client.equalString(authType, "Anonymous")) { + if (TeaUtils.Client.isUnset(self._credential)) { + throw Tea.ReuqestError([ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]) + } var credentialType: String = try await getType() if (TeaUtils.Client.equalString(credentialType, "bearer")) { var bearerToken: String = try await getBearerToken() @@ -590,6 +602,12 @@ open class Client { _request.query = Tea.TeaConverter.merge([:], _request.query, request.query ?? [:]) } if (!TeaUtils.Client.equalString(authType, "Anonymous")) { + if (TeaUtils.Client.isUnset(self._credential)) { + throw Tea.ReuqestError([ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]) + } var credentialType: String = try await getType() if (TeaUtils.Client.equalString(credentialType, "bearer")) { var bearerToken: String = try await getBearerToken() @@ -801,6 +819,12 @@ open class Client { } _request.headers["x-acs-content-sha256"] = hashedRequestPayload as! String; if (!TeaUtils.Client.equalString(params.authType, "Anonymous")) { + if (TeaUtils.Client.isUnset(self._credential)) { + throw Tea.ReuqestError([ + "code": "InvalidCredentials", + "message": "Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details." + ]) + } var authType: String = try await getType() if (TeaUtils.Client.equalString(authType, "bearer")) { var bearerToken: String = try await getBearerToken() diff --git a/ts/src/client.ts b/ts/src/client.ts index 460b632..b3b2b7f 100644 --- a/ts/src/client.ts +++ b/ts/src/client.ts @@ -435,6 +435,13 @@ export default class Client { } if (!Util.equalString(authType, "Anonymous")) { + if (Util.isUnset(this._credential)) { + throw $tea.newError({ + code: `InvalidCredentials`, + message: `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.`, + }); + } + let credentialModel = await this._credential.getCredential(); let credentialType = credentialModel.type; if (Util.equalString(credentialType, "bearer")) { @@ -651,6 +658,13 @@ export default class Client { } if (!Util.equalString(authType, "Anonymous")) { + if (Util.isUnset(this._credential)) { + throw $tea.newError({ + code: `InvalidCredentials`, + message: `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.`, + }); + } + let credentialModel = await this._credential.getCredential(); let credentialType = credentialModel.type; if (Util.equalString(credentialType, "bearer")) { @@ -865,6 +879,13 @@ export default class Client { } if (!Util.equalString(authType, "Anonymous")) { + if (Util.isUnset(this._credential)) { + throw $tea.newError({ + code: `InvalidCredentials`, + message: `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.`, + }); + } + let credentialModel = await this._credential.getCredential(); let credentialType = credentialModel.type; if (Util.equalString(credentialType, "bearer")) { @@ -1105,6 +1126,13 @@ export default class Client { request_.headers["x-acs-content-sha256"] = hashedRequestPayload; if (!Util.equalString(params.authType, "Anonymous")) { + if (Util.isUnset(this._credential)) { + throw $tea.newError({ + code: `InvalidCredentials`, + message: `Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details.`, + }); + } + let credentialModel = await this._credential.getCredential(); let authType = credentialModel.type; if (Util.equalString(authType, "bearer")) { diff --git a/ts/test/client.spec.ts b/ts/test/client.spec.ts index fe4e81e..1b8d4c3 100644 --- a/ts/test/client.spec.ts +++ b/ts/test/client.spec.ts @@ -141,6 +141,10 @@ function createBearerTokenConfig(): $OpenApi.Config { return config; } +function createAnonymousConfig(): $OpenApi.Config { + return new $OpenApi.Config(); +} + function createRuntimeOptions(): $Util.RuntimeOptions { let extendsParameters = new $Util.ExtendsParameters({ headers: { @@ -368,6 +372,19 @@ describe('$openapi', function () { regexp = /Action=TestAPI&Format=json&Version=2022-06-01&Timestamp=.+&SignatureNonce=.+&key1=value&key2=1&key3=true&BearerToken=token&SignatureType=BEARERTOKEN/; assert.ok(regexp.test(headers["raw-query"])); // assert.strictEqual(headers["authorization"], "bearer token"); + + // Anonymous error + config = createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = `127.0.0.1:${port}`; + client = new OpenApi(config); + try { + await client.callApi(params, request, runtime); + } catch (err) { + assert.strictEqual(err.message, "InvalidCredentials: Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."); + assert.strictEqual(err.code, "InvalidCredentials"); + } }); it("call api for RPC With V2Sign Anonymous JSON should ok", async function () { @@ -480,6 +497,19 @@ describe('$openapi', function () { assert.strictEqual(headers["x-acs-signature-type"], "BEARERTOKEN"); // assert.strictEqual(headers["authorization"], "bearer token"); + // Anonymous error + config = createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = `127.0.0.1:${port}`; + client = new OpenApi(config); + try { + await client.callApi(params, request, runtime); + } catch (err) { + assert.strictEqual(err.message, "InvalidCredentials: Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."); + assert.strictEqual(err.code, "InvalidCredentials"); + } + config = createBearerTokenConfig(); config.protocol = "HTTP"; config.signatureAlgorithm = "v2"; @@ -506,6 +536,19 @@ describe('$openapi', function () { assert.strictEqual(headers["x-acs-bearer-token"], "token"); assert.strictEqual(headers["x-acs-signature-type"], "BEARERTOKEN"); // assert.strictEqual(headers["authorization"], "bearer token"); + + // Anonymous error + config = createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = `127.0.0.1:${port}`; + client = new OpenApi(config); + try { + await client.callApi(params, request, runtime); + } catch (err) { + assert.strictEqual(err.message, "InvalidCredentials: Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."); + assert.strictEqual(err.code, "InvalidCredentials"); + } }); it("call api for ROA With V2Sign Anonymous JSON should ok", async function () { @@ -622,6 +665,19 @@ describe('$openapi', function () { const regexp = /SignatureType=BEARERTOKEN/; assert.ok(regexp.test(headers["raw-query"])); // assert.strictEqual(headers["authorization"], "bearer token"); + + // Anonymous error + config = createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = `127.0.0.1:${port}`; + client = new OpenApi(config); + try { + await client.callApi(params, request, runtime); + } catch (err) { + assert.strictEqual(err.message, "InvalidCredentials: Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."); + assert.strictEqual(err.code, "InvalidCredentials"); + } }); it("call api for RPC With V3Sign Anonymous JSON should ok", async function () { @@ -734,6 +790,19 @@ describe('$openapi', function () { assert.strictEqual(headers["x-acs-bearer-token"], "token"); assert.strictEqual(headers["x-acs-signature-type"], "BEARERTOKEN"); // assert.strictEqual(headers["authorization"], "bearer token"); + + // Anonymous error + config = createAnonymousConfig(); + config.protocol = "HTTP"; + config.signatureAlgorithm = "v2"; + config.endpoint = `127.0.0.1:${port}`; + client = new OpenApi(config); + try { + await client.callApi(params, request, runtime); + } catch (err) { + assert.strictEqual(err.message, "InvalidCredentials: Please set up the credentials correctly. If you are setting them through environment variables, please ensure that ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set correctly. See https://help.aliyun.com/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems for more details."); + assert.strictEqual(err.code, "InvalidCredentials"); + } }); it("call api for ROA With V3Sign Anonymous JSON should ok", async function () {