From d92e59baa3a4b6331f34348c7be2491cf57e31ee Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Mon, 11 Oct 2021 21:06:32 -0700 Subject: [PATCH 01/16] feat(cpd-authentication): add apikey for authentication step and update to use /v1/authorize --- .../CloudPakForDataAuthenticator.cs | 119 +++++++++++++++--- .../CloudPakForData/CloudPakForDataToken.cs | 2 +- .../CloudPakForDataTokenResponse.cs | 18 +-- 3 files changed, 107 insertions(+), 32 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 52ab0e58..46f0169e 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -19,7 +19,8 @@ using IBM.Cloud.SDK.Core.Util; using System; using System.Collections.Generic; - +using System.Net.Http; + namespace IBM.Cloud.SDK.Core.Authentication.Cp4d { /// @@ -30,12 +31,18 @@ public class CloudPakForDataAuthenticator : Authenticator public IClient Client { get; set; } // This is the suffix we'll need to add to the user-supplied URL to retrieve an access token. - private static string UrlSuffix = "/v1/preauth/validateAuth"; + private static string UrlSuffix = "/v1/authorize"; + + // These are keys for body request for cpd authorization token + private const string UsernameKey = "username"; + private const string PasswordKey = "password"; + private const string ApikeyKey = "api_key"; // Configuration properties for this authenticator. public string Url { get; private set; } public string Username { get; private set; } public string Password { get; private set; } + public string Apikey { get; private set; } public bool? DisableSslVerification { get; set; } public Dictionary Headers { get; set; } @@ -45,14 +52,28 @@ public class CloudPakForDataAuthenticator : Authenticator /// /// Constructs a CloudPakForDataAuthenticator with all properties. /// - /// The base URL associated with the token server. The path "/v1/preauth/validateAuth" will be appended to this value automatically. + /// The base URL associated with the token server. The path "/v1/authorize" will be appended to this value automatically. /// The username to be used when retrieving the access token /// The password to be used when retrieving the access token /// A flag indicating whether SSL hostname verification should be disabled /// A set of user-supplied headers to be included in token server interactions public CloudPakForDataAuthenticator(string url, string username, string password, bool? disableSslVerification = null, Dictionary headers = null) { - Init(url, username, password, disableSslVerification, headers); + Init(url, username, password, null, disableSslVerification, headers); + } + + /// + /// Constructs a CloudPakForDataAuthenticator with all properties. + /// + /// The base URL associated with the token server. The path "/v1/authorize" will be appended to this value automatically. + /// The username to be used when retrieving the access token + /// The password to be used when retrieving the access token + /// The apikey to be used when retrieving the access token + /// A flag indicating whether SSL hostname verification should be disabled + /// A set of user-supplied headers to be included in token server interactions + public CloudPakForDataAuthenticator(string url, string username, string? password, string? apikey, bool? disableSslVerification = null, Dictionary headers = null) + { + Init(url, username, password, apikey, disableSslVerification, headers); } /// @@ -64,16 +85,61 @@ public CloudPakForDataAuthenticator(Dictionary config) config.TryGetValue(PropNameUrl, out string url); config.TryGetValue(PropNameUsername, out string username); config.TryGetValue(PropNamePassword, out string password); + config.TryGetValue(PropNameApikey, out string apikey); config.TryGetValue(PropNameDisableSslVerification, out string disableSslVerficiationString); bool.TryParse(disableSslVerficiationString, out bool disableSslVerification); - Init(url, username, password, disableSslVerification); + Init(url, username, password, apikey, disableSslVerification); + } + + public CloudPakForDataAuthenticator SetUrl(string url) + { + this.Url = url; + return this; + } + + public CloudPakForDataAuthenticator SetUsername(string username) + { + this.Username = username; + return this; + } + + public CloudPakForDataAuthenticator setPassword(string password) + { + this.Password = this.Password; + return this; } - private void Init(string url, string username, string password, bool? disableSslVerification = null, Dictionary headers = null) + public CloudPakForDataAuthenticator setApikey(string apikey) + { + this.Apikey = this.Apikey; + return this; + } + + public CloudPakForDataAuthenticator SetDisableSslVerification(bool disableSslVerification) + { + this.DisableSslVerification = disableSslVerification; + return this; + } + + public CloudPakForDataAuthenticator SetHeaders(Dictionary headers) + { + this.Headers = headers; + return this; + } + + public CloudPakForDataAuthenticator Build() + { + Init(); + + return this; + } + + private void Init(string url, string username, string? password, string? apikey, bool? disableSslVerification = null, Dictionary headers = null) { Url = url; Username = username; Password = password; + Apikey = apikey; if (disableSslVerification != null) { @@ -85,12 +151,17 @@ private void Init(string url, string username, string password, bool? disableSsl Headers = headers; } - Validate(); + Init(); + } - Client = new IBMHttpClient() - { - ServiceUrl = Url + UrlSuffix - }; + public void Init() + { + Validate(); + + Client = new IBMHttpClient() + { + ServiceUrl = Url + UrlSuffix + }; } public override string AuthenticationType @@ -133,8 +204,9 @@ protected DetailedResponse RequestToken() try { - IClient client = Client.WithAuthentication(Username, Password); - var request = Client.GetAsync(Url + UrlSuffix); + var request = Client.PostAsync(Url + UrlSuffix); + request.WithHeader("Content-type", "application/x-www-form-urlencoded"); + if (DisableSslVerification != null) { Client.DisableSslVerification((bool)DisableSslVerification); @@ -145,6 +217,23 @@ protected DetailedResponse RequestToken() request.WithHeaders(Headers); } + List> content = new List>(); + KeyValuePair username = new KeyValuePair(UsernameKey, Username); + content.Add(username); + + if (string.IsNullOrEmpty(Password)) + { + KeyValuePair apikey = new KeyValuePair(ApikeyKey, Apikey); + content.Add(apikey); + } else + { + KeyValuePair password = new KeyValuePair(PasswordKey, Password); + content.Add(password); + } + + var formData = new FormUrlEncodedContent(content); + request.WithBodyContent(formData); + result = request.As().Result; if (result == null) { @@ -171,9 +260,9 @@ public override void Validate() throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username")); } - if (string.IsNullOrEmpty(Password)) + if (string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Apikey)) { - throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password")); + throw new ArgumentNullException("The Password or Apikey property is required but was not specified."); } if (CredentialUtils.HasBadStartOrEndChar(Url)) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataToken.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataToken.cs index 8e681fae..7b3c1bf1 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataToken.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataToken.cs @@ -42,7 +42,7 @@ public CloudPakForDataToken(string accessToken) public CloudPakForDataToken(CloudPakForDataTokenResponse response) { - AccessToken = response.AccessToken; + AccessToken = response.Token; long? iat = null; long? exp = null; diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataTokenResponse.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataTokenResponse.cs index 26b4db70..edecc942 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataTokenResponse.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataTokenResponse.cs @@ -27,22 +27,8 @@ namespace IBM.Cloud.SDK.Core.Authentication.Cp4d /// public class CloudPakForDataTokenResponse { - [JsonProperty("username", NullValueHandling = NullValueHandling.Ignore)] - public string Username { get; set; } - [JsonProperty("role", NullValueHandling = NullValueHandling.Ignore)] - public string Role { get; set; } - [JsonProperty("permissions", NullValueHandling = NullValueHandling.Ignore)] - public List Permissions { get; set; } - [JsonProperty("sub", NullValueHandling = NullValueHandling.Ignore)] - public string Sub { get; set; } - [JsonProperty("iss", NullValueHandling = NullValueHandling.Ignore)] - public string Iss { get; set; } - [JsonProperty("aud", NullValueHandling = NullValueHandling.Ignore)] - public string Aud { get; set; } - [JsonProperty("uid", NullValueHandling = NullValueHandling.Ignore)] - public string Uid { get; set; } - [JsonProperty("accessToken", NullValueHandling = NullValueHandling.Ignore)] - public string AccessToken { get; set; } + [JsonProperty("token", NullValueHandling = NullValueHandling.Ignore)] + public string Token { get; set; } [JsonProperty("_messageCode_", NullValueHandling = NullValueHandling.Ignore)] public string MessageCode { get; set; } [JsonProperty("message", NullValueHandling = NullValueHandling.Ignore)] From 8a851aee39b09ac1ba003cb5d7c250cbd95685ef Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Mon, 11 Oct 2021 22:40:13 -0700 Subject: [PATCH 02/16] test(cpd-tests): add testsing for CloudPakAuthentication --- .../CloudPakForDataAuthenticator.cs | 33 +++- .../CloudPak4DataAuthTests.cs | 168 +++++++++++++++++- 2 files changed, 193 insertions(+), 8 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 46f0169e..72e19e6b 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -49,6 +49,11 @@ public class CloudPakForDataAuthenticator : Authenticator // This field holds an access token and its expiration time. private CloudPakForDataToken tokenData; + // this empty constructor will be used by builder + private CloudPakForDataAuthenticator() + { + } + /// /// Constructs a CloudPakForDataAuthenticator with all properties. /// @@ -71,7 +76,7 @@ public CloudPakForDataAuthenticator(string url, string username, string password /// The apikey to be used when retrieving the access token /// A flag indicating whether SSL hostname verification should be disabled /// A set of user-supplied headers to be included in token server interactions - public CloudPakForDataAuthenticator(string url, string username, string? password, string? apikey, bool? disableSslVerification = null, Dictionary headers = null) + public CloudPakForDataAuthenticator(string url, string username, string password, string apikey, bool? disableSslVerification = null, Dictionary headers = null) { Init(url, username, password, apikey, disableSslVerification, headers); } @@ -127,6 +132,11 @@ public CloudPakForDataAuthenticator SetHeaders(Dictionary header return this; } + public static CloudPakForDataAuthenticator InitBuilder() + { + return new CloudPakForDataAuthenticator(); + } + public CloudPakForDataAuthenticator Build() { Init(); @@ -134,13 +144,21 @@ public CloudPakForDataAuthenticator Build() return this; } - private void Init(string url, string username, string? password, string? apikey, bool? disableSslVerification = null, Dictionary headers = null) + private void Init(string url, string username, string password = null, string apikey = null, bool? disableSslVerification = null, Dictionary headers = null) { Url = url; Username = username; - Password = password; - Apikey = apikey; + if (password != null) + { + Password = password; + } + + if (apikey != null) + { + Apikey = apikey; + } + if (disableSslVerification != null) { DisableSslVerification = disableSslVerification; @@ -275,10 +293,15 @@ public override void Validate() throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Username")); } - if (CredentialUtils.HasBadStartOrEndChar(Password)) + if (!string.IsNullOrEmpty(Password) && CredentialUtils.HasBadStartOrEndChar(Password)) { throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Password")); } + + if (!string.IsNullOrEmpty(Apikey) && CredentialUtils.HasBadStartOrEndChar(Apikey)) + { + throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "Apikey")); + } } } } diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index 410cb00f..bedc5016 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -42,6 +42,27 @@ public void TestConstructionRequried() Assert.IsTrue(authenticator.Url == url); Assert.IsTrue(authenticator.Username == username); Assert.IsTrue(authenticator.Password == password); + } + + [TestMethod] + public void TestConstructionApikeyRequried() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var apikey = "apikey"; + + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator( + url: url, + username: username, + password: null, + apikey: apikey); + + Assert.IsNotNull(authenticator); + Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); + Assert.IsTrue(authenticator.Url == url); + Assert.IsTrue(authenticator.Username == username); + Assert.IsTrue(authenticator.Apikey == apikey); + Assert.IsNull(authenticator.Password); } [TestMethod] @@ -66,7 +87,7 @@ public void TestConstructionDisableSslVerification() } [TestMethod] - public void TestConstructorHeaders() + public void TestConstructorHeadersWithPassword() { var url = "http://www.service-endpoint.com"; var username = "username"; @@ -91,10 +112,40 @@ public void TestConstructorHeaders() Assert.IsTrue(authenticator.Headers.ContainsKey(headerName)); Assert.IsTrue(authenticator.Headers.ContainsValue(headervalue)); Assert.IsTrue(retrievedHeaderValue == headervalue); + } + + [TestMethod] + public void TestConstructorHeadersWithApikey() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var apikey = "apikey"; + var headerName = "headerName"; + var headervalue = "headerValue"; + var headers = new Dictionary(); + headers.Add(headerName, headervalue); + + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator( + url: url, + username: username, + password: null, + apikey: apikey, + headers: headers + ); + + authenticator.Headers.TryGetValue(headerName, out string retrievedHeaderValue); + Assert.IsNotNull(authenticator); + Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); + Assert.IsTrue(authenticator.Username == username); + Assert.IsTrue(authenticator.Apikey == apikey); + Assert.IsTrue(authenticator.Headers.ContainsKey(headerName)); + Assert.IsTrue(authenticator.Headers.ContainsValue(headervalue)); + Assert.IsTrue(retrievedHeaderValue == headervalue); + Assert.IsNull(authenticator.Password); } [TestMethod] - public void TestConstructionDictionary() + public void TestConstructionDictionaryWithPassword() { var url = "http://www.service-endpoint.com"; var username = "username"; @@ -115,8 +166,35 @@ public void TestConstructionDictionary() Assert.IsTrue(authenticator.Username == username); Assert.IsTrue(authenticator.Password == password); Assert.IsTrue(authenticator.DisableSslVerification == disableSslVerification); + Assert.IsNull(authenticator.Apikey); + } + + [TestMethod] + public void TestConstructionDictionaryWithApikey() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var apikey = "apikey"; + var disableSslVerification = true; + + Dictionary config = new Dictionary(); + config.Add(Authenticator.PropNameUrl, url); + config.Add(Authenticator.PropNameUsername, username); + config.Add(Authenticator.PropNameApikey, apikey); + config.Add(Authenticator.PropNameDisableSslVerification, disableSslVerification.ToString()); + + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(config); + + Assert.IsNotNull(authenticator); + Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); + Assert.IsTrue(authenticator.Url == url); + Assert.IsTrue(authenticator.Username == username); + Assert.IsTrue(authenticator.Apikey == apikey); + Assert.IsTrue(authenticator.DisableSslVerification == disableSslVerification); + Assert.IsNull(authenticator.Password); } + [TestMethod] public void TestConstructionDictionaryMissingProperty() { @@ -139,6 +217,48 @@ public void TestConstructionDictionaryMissingProperty() Assert.IsTrue(authenticator.DisableSslVerification == false); } + [TestMethod] + public void TestBuilderRequired() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var password = "password"; + + CloudPakForDataAuthenticator authenticator = CloudPakForDataAuthenticator.InitBuilder() + .SetUrl(url) + .SetUsername(username) + .setPassword(password) + .Build(); + + Assert.IsNotNull(authenticator); + Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); + Assert.IsTrue(authenticator.Url == url); + Assert.IsTrue(authenticator.Username == username); + Assert.IsTrue(authenticator.Password == password); + Assert.IsTrue(authenticator.DisableSslVerification == false); + } + + [TestMethod] + public void TestBuilderWithApikeyRequired() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var apikey = "apikey"; + + CloudPakForDataAuthenticator authenticator = CloudPakForDataAuthenticator.InitBuilder() + .SetUrl(url) + .SetUsername(username) + .setApikey(apikey) + .Build(); + + Assert.IsNotNull(authenticator); + Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); + Assert.IsTrue(authenticator.Url == url); + Assert.IsTrue(authenticator.Username == username); + Assert.IsTrue(authenticator.Apikey == apikey); + Assert.IsTrue(authenticator.DisableSslVerification == false); + } + [TestMethod, ExpectedException(typeof(ArgumentNullException))] public void TestNullUrl() { @@ -158,12 +278,23 @@ public void TestNullUsername() } [TestMethod, ExpectedException(typeof(ArgumentNullException))] - public void TestNullPassword() + public void TestNullPasswordAndApikey() { var url = "http://www.service-endpoint.com"; var username = "username"; var password = default(string); CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(url, username, password); + } + + + [TestMethod, ExpectedException(typeof(ArgumentNullException))] + public void TestNullPasswordAndApikeyWithApikeyConstructor() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var password = default(string); + var apikey = default(string); + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(url, username, password, apikey); } [TestMethod, ExpectedException(typeof(ArgumentException))] @@ -182,8 +313,15 @@ public void TestBadUsernameBracketBeginning() public void TestBadPasswordBracketBeginning() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "{pasword"); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void TestBadApikeyBracketBeginning() + { + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "{apikey"); } + [TestMethod, ExpectedException(typeof(ArgumentException))] public void TestBadUrlBracketEnd() { @@ -200,6 +338,12 @@ public void TestBadUsernameBracketEnd() public void TestBadPasswordBracketEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "pasword}"); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void TestBadApikeyBracketEnd() + { + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "apikey}"); } [TestMethod, ExpectedException(typeof(ArgumentException))] @@ -218,6 +362,12 @@ public void TestBadUsernameBeginningBracketEnd() public void TestBadPasswordBeginningBracketEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "{pasword}"); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void TestBadApikeyBeginningBracketEnd() + { + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "{apikey}"); } [TestMethod, ExpectedException(typeof(ArgumentException))] @@ -254,6 +404,12 @@ public void TestBadUsernameQuoteEnd() public void TestBadPasswordQuoteEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "pasword\""); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void TestBadApikeyQuoteEnd() + { + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "apikey\""); } [TestMethod, ExpectedException(typeof(ArgumentException))] @@ -272,6 +428,12 @@ public void TestBadUsernameBeginningQuoteEnd() public void TestBadPasswordBeginningQuoteEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "\"pasword\""); + } + + [TestMethod, ExpectedException(typeof(ArgumentException))] + public void TestBadApikeyBeginningQuoteEnd() + { + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "\"apikey\""); } } } From d49dcfdc59b04569609ea017c364ffed3003384e Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Tue, 12 Oct 2021 08:37:03 -0700 Subject: [PATCH 03/16] fix(detect-secrets): update detect-secrets for false positive --- .secrets.baseline | 63 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/.secrets.baseline b/.secrets.baseline index 44660250..e22295c9 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": "package-lock.json|^.secrets.baseline$", "lines": null }, - "generated_at": "2021-03-04T18:54:10Z", + "generated_at": "2021-10-12T15:35:07Z", "plugins_used": [ { "name": "AWSKeyDetector" @@ -25,6 +25,7 @@ "name": "CloudantDetector" }, { + "ghe_instance": "github.ibm.com", "name": "GheDetector" }, { @@ -101,11 +102,43 @@ } ], "src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs": [ + { + "hashed_secret": "d7e417696b0fb23cfbb8c58cf748673382cbbc45", + "is_secret": false, + "is_verified": false, + "line_number": 113, + "type": "Secret Keyword", + "verified_result": null + }, + { + "hashed_secret": "0358c67856fb6a21c4767daf02fcb8fe4dc0a318", + "is_secret": false, + "is_verified": false, + "line_number": 119, + "type": "Secret Keyword", + "verified_result": null + }, { "hashed_secret": "69620d88f55564556dd0df45c97a051829c7b03e", "is_secret": false, "is_verified": false, - "line_number": 76, + "line_number": 154, + "type": "Secret Keyword", + "verified_result": null + }, + { + "hashed_secret": "f5cbae85fb47446511da4c9974e2da448caee7e1", + "is_secret": false, + "is_verified": false, + "line_number": 159, + "type": "Secret Keyword", + "verified_result": null + }, + { + "hashed_secret": "c2a6b03f190dfb2b4aa91f8af8d477a9bc3401dc", + "is_secret": false, + "is_verified": false, + "line_number": 248, "type": "Secret Keyword", "verified_result": null } @@ -216,6 +249,30 @@ "line_number": 38, "type": "Secret Keyword", "verified_result": null + }, + { + "hashed_secret": "500f6cbf87342a42e38738068db4785ab01cf4b6", + "is_secret": false, + "is_verified": false, + "line_number": 58, + "type": "Secret Keyword", + "verified_result": null + }, + { + "hashed_secret": "f75b33f87ffeacb3a4f793a09693e672e07449ff", + "is_secret": false, + "is_verified": false, + "line_number": 132, + "type": "Secret Keyword", + "verified_result": null + }, + { + "hashed_secret": "d4c3d66fd0c38547a3c7a4c6bdc29c36911bc030", + "is_secret": false, + "is_verified": false, + "line_number": 246, + "type": "Secret Keyword", + "verified_result": null } ], "test/IBM.Cloud.SDK.Core.Tests/ConfigBasedAuthenticatorFactoryTests.cs": [ @@ -401,7 +458,7 @@ } ] }, - "version": "0.13.1+ibm.31.dss", + "version": "0.13.1+ibm.46.dss", "word_list": { "file": null, "hash": null From 60fb13ffa5025c41113840643fcae647b2c3dd61 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Fri, 15 Oct 2021 13:43:22 -0700 Subject: [PATCH 04/16] fix(node-version): update appveyor.yml to use node version 15.6.0 --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 1ab55744..de6ae3c1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -17,6 +17,7 @@ environment: secure: gevsMhy8RTWMdf7MjOnIo5QaN6JpL9DPK6I+Go5ByZir5LDwyFsv9hO6nBjGTg8n #pragma: whitelist secret GITHUB_TOKEN: secure: +B2bs86RVtJtlbkB+cTf9bkqnNlFJi/PbBBPzR5jlUlLLZoOc+ZgqgQLwee4tCT+ #pragma: whitelist secret + nodejs_version: 15.6.0 install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version From 4c1c0264085681c91cff04d11ddab1b5e4ce16bf Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Sun, 17 Oct 2021 23:24:37 -0700 Subject: [PATCH 05/16] fix(renaming): rename function and vararaiables --- .../CloudPakForDataAuthenticator.cs | 14 +++++++------- .../CloudPak4DataAuthTests.cs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 72e19e6b..d96e29d7 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -34,9 +34,9 @@ public class CloudPakForDataAuthenticator : Authenticator private static string UrlSuffix = "/v1/authorize"; // These are keys for body request for cpd authorization token - private const string UsernameKey = "username"; - private const string PasswordKey = "password"; - private const string ApikeyKey = "api_key"; + private const string KeyUsername = "username"; + private const string KeyPassword = "password"; + private const string KeyApikey = "api_key"; // Configuration properties for this authenticator. public string Url { get; private set; } @@ -236,16 +236,16 @@ protected DetailedResponse RequestToken() } List> content = new List>(); - KeyValuePair username = new KeyValuePair(UsernameKey, Username); + KeyValuePair username = new KeyValuePair(KeyUsername, Username); content.Add(username); if (string.IsNullOrEmpty(Password)) { - KeyValuePair apikey = new KeyValuePair(ApikeyKey, Apikey); + KeyValuePair apikey = new KeyValuePair(KeyApikey, Apikey); content.Add(apikey); } else { - KeyValuePair password = new KeyValuePair(PasswordKey, Password); + KeyValuePair password = new KeyValuePair(KeyPassword, Password); content.Add(password); } @@ -280,7 +280,7 @@ public override void Validate() if (string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Apikey)) { - throw new ArgumentNullException("The Password or Apikey property is required but was not specified."); + throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password or Apikey")); } if (CredentialUtils.HasBadStartOrEndChar(Url)) diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index bedc5016..38e51565 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -45,7 +45,7 @@ public void TestConstructionRequried() } [TestMethod] - public void TestConstructionApikeyRequried() + public void TestConstructionWithApikeyAndNullPassword() { var url = "http://www.service-endpoint.com"; var username = "username"; From f4399a457264975422e9fdbf24c4079fbfe82e04 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Sun, 17 Oct 2021 23:26:03 -0700 Subject: [PATCH 06/16] chore(detect-secrets): ran audit for detect-secrets --- .secrets.baseline | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.secrets.baseline b/.secrets.baseline index e22295c9..948a2d14 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -3,7 +3,7 @@ "files": "package-lock.json|^.secrets.baseline$", "lines": null }, - "generated_at": "2021-10-12T15:35:07Z", + "generated_at": "2021-10-18T06:25:05Z", "plugins_used": [ { "name": "AWSKeyDetector" From d3291fe40488acf931e8d270b3ca7368b9986480 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Mon, 18 Oct 2021 16:34:52 -0700 Subject: [PATCH 07/16] fix(cp4d): fixed setter method with the correct naming --- appveyor.yml | 1 - .../CloudPakForDataAuthenticator.cs | 30 +++++-------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index de6ae3c1..1ab55744 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -17,7 +17,6 @@ environment: secure: gevsMhy8RTWMdf7MjOnIo5QaN6JpL9DPK6I+Go5ByZir5LDwyFsv9hO6nBjGTg8n #pragma: whitelist secret GITHUB_TOKEN: secure: +B2bs86RVtJtlbkB+cTf9bkqnNlFJi/PbBBPzR5jlUlLLZoOc+ZgqgQLwee4tCT+ #pragma: whitelist secret - nodejs_version: 15.6.0 install: # Get the latest stable version of Node.js or io.js - ps: Install-Product node $env:nodejs_version diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index d96e29d7..d165bda2 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -65,20 +65,6 @@ private CloudPakForDataAuthenticator() public CloudPakForDataAuthenticator(string url, string username, string password, bool? disableSslVerification = null, Dictionary headers = null) { Init(url, username, password, null, disableSslVerification, headers); - } - - /// - /// Constructs a CloudPakForDataAuthenticator with all properties. - /// - /// The base URL associated with the token server. The path "/v1/authorize" will be appended to this value automatically. - /// The username to be used when retrieving the access token - /// The password to be used when retrieving the access token - /// The apikey to be used when retrieving the access token - /// A flag indicating whether SSL hostname verification should be disabled - /// A set of user-supplied headers to be included in token server interactions - public CloudPakForDataAuthenticator(string url, string username, string password, string apikey, bool? disableSslVerification = null, Dictionary headers = null) - { - Init(url, username, password, apikey, disableSslVerification, headers); } /// @@ -98,37 +84,37 @@ public CloudPakForDataAuthenticator(Dictionary config) public CloudPakForDataAuthenticator SetUrl(string url) { - this.Url = url; + Url = url; return this; } public CloudPakForDataAuthenticator SetUsername(string username) { - this.Username = username; + Username = username; return this; } - public CloudPakForDataAuthenticator setPassword(string password) + public CloudPakForDataAuthenticator SetPassword(string password) { - this.Password = this.Password; + Password = password; return this; } - public CloudPakForDataAuthenticator setApikey(string apikey) + public CloudPakForDataAuthenticator SetApikey(string apikey) { - this.Apikey = this.Apikey; + Apikey = apikey; return this; } public CloudPakForDataAuthenticator SetDisableSslVerification(bool disableSslVerification) { - this.DisableSslVerification = disableSslVerification; + DisableSslVerification = disableSslVerification; return this; } public CloudPakForDataAuthenticator SetHeaders(Dictionary headers) { - this.Headers = headers; + Headers = headers; return this; } From 5e6154435877b8bc83a4db7c6af9e33c8448365b Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Mon, 18 Oct 2021 21:20:13 -0700 Subject: [PATCH 08/16] test(unit-test): remove unused tests --- .../CloudPak4DataAuthTests.cs | 74 +------------------ 1 file changed, 4 insertions(+), 70 deletions(-) diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index 38e51565..bf61f36a 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -54,8 +54,7 @@ public void TestConstructionWithApikeyAndNullPassword() CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator( url: url, username: username, - password: null, - apikey: apikey); + password: null); Assert.IsNotNull(authenticator); Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); @@ -113,36 +112,6 @@ public void TestConstructorHeadersWithPassword() Assert.IsTrue(authenticator.Headers.ContainsValue(headervalue)); Assert.IsTrue(retrievedHeaderValue == headervalue); } - - [TestMethod] - public void TestConstructorHeadersWithApikey() - { - var url = "http://www.service-endpoint.com"; - var username = "username"; - var apikey = "apikey"; - var headerName = "headerName"; - var headervalue = "headerValue"; - var headers = new Dictionary(); - headers.Add(headerName, headervalue); - - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator( - url: url, - username: username, - password: null, - apikey: apikey, - headers: headers - ); - - authenticator.Headers.TryGetValue(headerName, out string retrievedHeaderValue); - Assert.IsNotNull(authenticator); - Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); - Assert.IsTrue(authenticator.Username == username); - Assert.IsTrue(authenticator.Apikey == apikey); - Assert.IsTrue(authenticator.Headers.ContainsKey(headerName)); - Assert.IsTrue(authenticator.Headers.ContainsValue(headervalue)); - Assert.IsTrue(retrievedHeaderValue == headervalue); - Assert.IsNull(authenticator.Password); - } [TestMethod] public void TestConstructionDictionaryWithPassword() @@ -227,7 +196,7 @@ public void TestBuilderRequired() CloudPakForDataAuthenticator authenticator = CloudPakForDataAuthenticator.InitBuilder() .SetUrl(url) .SetUsername(username) - .setPassword(password) + .SetPassword(password) .Build(); Assert.IsNotNull(authenticator); @@ -248,7 +217,7 @@ public void TestBuilderWithApikeyRequired() CloudPakForDataAuthenticator authenticator = CloudPakForDataAuthenticator.InitBuilder() .SetUrl(url) .SetUsername(username) - .setApikey(apikey) + .SetApikey(apikey) .Build(); Assert.IsNotNull(authenticator); @@ -285,17 +254,7 @@ public void TestNullPasswordAndApikey() var password = default(string); CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(url, username, password); } - - - [TestMethod, ExpectedException(typeof(ArgumentNullException))] - public void TestNullPasswordAndApikeyWithApikeyConstructor() - { - var url = "http://www.service-endpoint.com"; - var username = "username"; - var password = default(string); - var apikey = default(string); - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(url, username, password, apikey); - } + [TestMethod, ExpectedException(typeof(ArgumentException))] public void TestBadUrlBracketBeginning() @@ -313,15 +272,8 @@ public void TestBadUsernameBracketBeginning() public void TestBadPasswordBracketBeginning() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "{pasword"); - } - - [TestMethod, ExpectedException(typeof(ArgumentException))] - public void TestBadApikeyBracketBeginning() - { - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "{apikey"); } - [TestMethod, ExpectedException(typeof(ArgumentException))] public void TestBadUrlBracketEnd() { @@ -338,12 +290,6 @@ public void TestBadUsernameBracketEnd() public void TestBadPasswordBracketEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "pasword}"); - } - - [TestMethod, ExpectedException(typeof(ArgumentException))] - public void TestBadApikeyBracketEnd() - { - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "apikey}"); } [TestMethod, ExpectedException(typeof(ArgumentException))] @@ -362,12 +308,6 @@ public void TestBadUsernameBeginningBracketEnd() public void TestBadPasswordBeginningBracketEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "{pasword}"); - } - - [TestMethod, ExpectedException(typeof(ArgumentException))] - public void TestBadApikeyBeginningBracketEnd() - { - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "{apikey}"); } [TestMethod, ExpectedException(typeof(ArgumentException))] @@ -405,12 +345,6 @@ public void TestBadPasswordQuoteEnd() { CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", "pasword\""); } - - [TestMethod, ExpectedException(typeof(ArgumentException))] - public void TestBadApikeyQuoteEnd() - { - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator("url", "username", null, "apikey\""); - } [TestMethod, ExpectedException(typeof(ArgumentException))] public void TestBadUrlBeginningQuoteEnd() From 055eef4de1699892794ca45e736a054a4366e7c0 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Wed, 20 Oct 2021 08:09:24 -0700 Subject: [PATCH 09/16] test(unit-testing): fix unit testing and remove unused testing function --- .../CloudPakForDataAuthenticator.cs | 2 +- .../CloudPak4DataAuthTests.cs | 26 ++----------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index d165bda2..3a276d50 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -264,7 +264,7 @@ public override void Validate() throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Username")); } - if (string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(Apikey)) + if (string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(Apikey)) { throw new ArgumentNullException(string.Format(ErrorMessagePropMissing, "Password or Apikey")); } diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index bf61f36a..1214c17f 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -42,26 +42,6 @@ public void TestConstructionRequried() Assert.IsTrue(authenticator.Url == url); Assert.IsTrue(authenticator.Username == username); Assert.IsTrue(authenticator.Password == password); - } - - [TestMethod] - public void TestConstructionWithApikeyAndNullPassword() - { - var url = "http://www.service-endpoint.com"; - var username = "username"; - var apikey = "apikey"; - - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator( - url: url, - username: username, - password: null); - - Assert.IsNotNull(authenticator); - Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); - Assert.IsTrue(authenticator.Url == url); - Assert.IsTrue(authenticator.Username == username); - Assert.IsTrue(authenticator.Apikey == apikey); - Assert.IsNull(authenticator.Password); } [TestMethod] @@ -203,8 +183,7 @@ public void TestBuilderRequired() Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); Assert.IsTrue(authenticator.Url == url); Assert.IsTrue(authenticator.Username == username); - Assert.IsTrue(authenticator.Password == password); - Assert.IsTrue(authenticator.DisableSslVerification == false); + Assert.IsTrue(authenticator.Password == password); } [TestMethod] @@ -224,8 +203,7 @@ public void TestBuilderWithApikeyRequired() Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); Assert.IsTrue(authenticator.Url == url); Assert.IsTrue(authenticator.Username == username); - Assert.IsTrue(authenticator.Apikey == apikey); - Assert.IsTrue(authenticator.DisableSslVerification == false); + Assert.IsTrue(authenticator.Apikey == apikey); } [TestMethod, ExpectedException(typeof(ArgumentNullException))] From b9558903a64fd02640f4bddcace853dcb6fd2476 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Wed, 20 Oct 2021 18:43:02 -0700 Subject: [PATCH 10/16] fix(cpd-constuctor): revert the code for ConstructionDictionary --- .../CloudPakForDataAuthenticator.cs | 15 ++++------- .../CloudPak4DataAuthTests.cs | 26 ------------------- 2 files changed, 5 insertions(+), 36 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 3a276d50..6bdcccec 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -64,7 +64,7 @@ private CloudPakForDataAuthenticator() /// A set of user-supplied headers to be included in token server interactions public CloudPakForDataAuthenticator(string url, string username, string password, bool? disableSslVerification = null, Dictionary headers = null) { - Init(url, username, password, null, disableSslVerification, headers); + Init(url, username, password, disableSslVerification, headers); } /// @@ -76,10 +76,9 @@ public CloudPakForDataAuthenticator(Dictionary config) config.TryGetValue(PropNameUrl, out string url); config.TryGetValue(PropNameUsername, out string username); config.TryGetValue(PropNamePassword, out string password); - config.TryGetValue(PropNameApikey, out string apikey); config.TryGetValue(PropNameDisableSslVerification, out string disableSslVerficiationString); bool.TryParse(disableSslVerficiationString, out bool disableSslVerification); - Init(url, username, password, apikey, disableSslVerification); + Init(url, username, password, disableSslVerification); } public CloudPakForDataAuthenticator SetUrl(string url) @@ -130,7 +129,7 @@ public CloudPakForDataAuthenticator Build() return this; } - private void Init(string url, string username, string password = null, string apikey = null, bool? disableSslVerification = null, Dictionary headers = null) + private void Init(string url, string username, string password, bool? disableSslVerification = null, Dictionary headers = null) { Url = url; Username = username; @@ -139,11 +138,6 @@ private void Init(string url, string username, string password = null, string ap { Password = password; } - - if (apikey != null) - { - Apikey = apikey; - } if (disableSslVerification != null) { @@ -229,7 +223,8 @@ protected DetailedResponse RequestToken() { KeyValuePair apikey = new KeyValuePair(KeyApikey, Apikey); content.Add(apikey); - } else + } + else { KeyValuePair password = new KeyValuePair(KeyPassword, Password); content.Add(password); diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index 1214c17f..7b388259 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -116,34 +116,8 @@ public void TestConstructionDictionaryWithPassword() Assert.IsTrue(authenticator.Password == password); Assert.IsTrue(authenticator.DisableSslVerification == disableSslVerification); Assert.IsNull(authenticator.Apikey); - } - - [TestMethod] - public void TestConstructionDictionaryWithApikey() - { - var url = "http://www.service-endpoint.com"; - var username = "username"; - var apikey = "apikey"; - var disableSslVerification = true; - - Dictionary config = new Dictionary(); - config.Add(Authenticator.PropNameUrl, url); - config.Add(Authenticator.PropNameUsername, username); - config.Add(Authenticator.PropNameApikey, apikey); - config.Add(Authenticator.PropNameDisableSslVerification, disableSslVerification.ToString()); - - CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(config); - - Assert.IsNotNull(authenticator); - Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); - Assert.IsTrue(authenticator.Url == url); - Assert.IsTrue(authenticator.Username == username); - Assert.IsTrue(authenticator.Apikey == apikey); - Assert.IsTrue(authenticator.DisableSslVerification == disableSslVerification); - Assert.IsNull(authenticator.Password); } - [TestMethod] public void TestConstructionDictionaryMissingProperty() { From 65502b0b9e2dc528bcb9a55cc68b49150fbc91f0 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Thu, 21 Oct 2021 12:46:09 -0700 Subject: [PATCH 11/16] fix(cpd-authenticator): update apikey for CloudPakForDataAuthenticator which use Dictinary config --- .../CloudPakForDataAuthenticator.cs | 31 ++++++++++++++++++- .../CloudPak4DataAuthTests.cs | 26 ++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 6bdcccec..fc064b19 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -76,9 +76,10 @@ public CloudPakForDataAuthenticator(Dictionary config) config.TryGetValue(PropNameUrl, out string url); config.TryGetValue(PropNameUsername, out string username); config.TryGetValue(PropNamePassword, out string password); + config.TryGetValue(PropNameApikey, out string apikey); config.TryGetValue(PropNameDisableSslVerification, out string disableSslVerficiationString); bool.TryParse(disableSslVerficiationString, out bool disableSslVerification); - Init(url, username, password, disableSslVerification); + Init(url, username, password, apikey, disableSslVerification); } public CloudPakForDataAuthenticator SetUrl(string url) @@ -149,6 +150,34 @@ private void Init(string url, string username, string password, bool? disableSsl Headers = headers; } + Init(); + } + + private void Init(string url, string username, string password, string apikey, bool? disableSslVerification = null, Dictionary headers = null) + { + Url = url; + Username = username; + + if (password != null) + { + Password = password; + } + + if (apikey != null) + { + Apikey = apikey; + } + + if (disableSslVerification != null) + { + DisableSslVerification = disableSslVerification; + } + + if (headers != null) + { + Headers = headers; + } + Init(); } diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index 7b388259..1214c17f 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -116,8 +116,34 @@ public void TestConstructionDictionaryWithPassword() Assert.IsTrue(authenticator.Password == password); Assert.IsTrue(authenticator.DisableSslVerification == disableSslVerification); Assert.IsNull(authenticator.Apikey); + } + + [TestMethod] + public void TestConstructionDictionaryWithApikey() + { + var url = "http://www.service-endpoint.com"; + var username = "username"; + var apikey = "apikey"; + var disableSslVerification = true; + + Dictionary config = new Dictionary(); + config.Add(Authenticator.PropNameUrl, url); + config.Add(Authenticator.PropNameUsername, username); + config.Add(Authenticator.PropNameApikey, apikey); + config.Add(Authenticator.PropNameDisableSslVerification, disableSslVerification.ToString()); + + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator(config); + + Assert.IsNotNull(authenticator); + Assert.IsTrue(authenticator.AuthenticationType == Authenticator.AuthTypeCp4d); + Assert.IsTrue(authenticator.Url == url); + Assert.IsTrue(authenticator.Username == username); + Assert.IsTrue(authenticator.Apikey == apikey); + Assert.IsTrue(authenticator.DisableSslVerification == disableSslVerification); + Assert.IsNull(authenticator.Password); } + [TestMethod] public void TestConstructionDictionaryMissingProperty() { From 11f3c02398ca2ca88c2709e23f2db3e4d4e065c9 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Thu, 21 Oct 2021 15:19:22 -0700 Subject: [PATCH 12/16] fix(semantic-release): update semantic-release version --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index b854d396..0b3c04d8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -224,7 +224,7 @@ test_script: { Write-Output "branchName is main and not a pull request - running semantic release" - npx semantic-release@15.11.0 + npx semantic-release@15.14.0 } From 0ca8e45c34fda2a5929dcb42a197e5804c3c2d5b Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Fri, 22 Oct 2021 10:46:30 -0700 Subject: [PATCH 13/16] fix(cpd-authenticator): renaming builder pattern functions and update http post to use json type --- .../CloudPakForDataAuthenticator.cs | 39 ++++++++----------- .../CloudPak4DataAuthTests.cs | 16 ++++---- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index fc064b19..57cd12fb 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -17,9 +17,12 @@ using IBM.Cloud.SDK.Core.Http; using IBM.Cloud.SDK.Core.Util; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Net.Http; +using System.Text; namespace IBM.Cloud.SDK.Core.Authentication.Cp4d { @@ -50,7 +53,7 @@ public class CloudPakForDataAuthenticator : Authenticator private CloudPakForDataToken tokenData; // this empty constructor will be used by builder - private CloudPakForDataAuthenticator() + public CloudPakForDataAuthenticator() { } @@ -82,47 +85,42 @@ public CloudPakForDataAuthenticator(Dictionary config) Init(url, username, password, apikey, disableSslVerification); } - public CloudPakForDataAuthenticator SetUrl(string url) + public CloudPakForDataAuthenticator WithUrl(string url) { Url = url; return this; } - public CloudPakForDataAuthenticator SetUsername(string username) + public CloudPakForDataAuthenticator WithUserName(string username) { Username = username; return this; } - public CloudPakForDataAuthenticator SetPassword(string password) + public CloudPakForDataAuthenticator WithPassword(string password) { Password = password; return this; } - public CloudPakForDataAuthenticator SetApikey(string apikey) + public CloudPakForDataAuthenticator WithApikey(string apikey) { Apikey = apikey; return this; } - public CloudPakForDataAuthenticator SetDisableSslVerification(bool disableSslVerification) + public CloudPakForDataAuthenticator WithDisableSslVerification(bool disableSslVerification) { DisableSslVerification = disableSslVerification; return this; } - public CloudPakForDataAuthenticator SetHeaders(Dictionary headers) + public CloudPakForDataAuthenticator WithHeaders(Dictionary headers) { Headers = headers; return this; } - public static CloudPakForDataAuthenticator InitBuilder() - { - return new CloudPakForDataAuthenticator(); - } - public CloudPakForDataAuthenticator Build() { Init(); @@ -232,7 +230,7 @@ protected DetailedResponse RequestToken() try { var request = Client.PostAsync(Url + UrlSuffix); - request.WithHeader("Content-type", "application/x-www-form-urlencoded"); + request.WithHeader("Content-type", "Content-Type: application/json"); if (DisableSslVerification != null) { @@ -244,23 +242,20 @@ protected DetailedResponse RequestToken() request.WithHeaders(Headers); } - List> content = new List>(); - KeyValuePair username = new KeyValuePair(KeyUsername, Username); - content.Add(username); + JObject bodyObject = new JObject(); + bodyObject[KeyUsername] = Username; if (string.IsNullOrEmpty(Password)) { - KeyValuePair apikey = new KeyValuePair(KeyApikey, Apikey); - content.Add(apikey); + bodyObject[KeyApikey] = Apikey; } else { - KeyValuePair password = new KeyValuePair(KeyPassword, Password); - content.Add(password); + bodyObject[KeyPassword] = Password; } - var formData = new FormUrlEncodedContent(content); - request.WithBodyContent(formData); + var httpContent = new StringContent(JsonConvert.SerializeObject(bodyObject), Encoding.UTF8, HttpMediaType.APPLICATION_JSON); + request.WithBodyContent(httpContent); result = request.As().Result; if (result == null) diff --git a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs index 1214c17f..a0c1249b 100644 --- a/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs +++ b/test/IBM.Cloud.SDK.Core.Tests/CloudPak4DataAuthTests.cs @@ -173,10 +173,10 @@ public void TestBuilderRequired() var username = "username"; var password = "password"; - CloudPakForDataAuthenticator authenticator = CloudPakForDataAuthenticator.InitBuilder() - .SetUrl(url) - .SetUsername(username) - .SetPassword(password) + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator() + .WithUrl(url) + .WithUserName(username) + .WithPassword(password) .Build(); Assert.IsNotNull(authenticator); @@ -193,10 +193,10 @@ public void TestBuilderWithApikeyRequired() var username = "username"; var apikey = "apikey"; - CloudPakForDataAuthenticator authenticator = CloudPakForDataAuthenticator.InitBuilder() - .SetUrl(url) - .SetUsername(username) - .SetApikey(apikey) + CloudPakForDataAuthenticator authenticator = new CloudPakForDataAuthenticator() + .WithUrl(url) + .WithUserName(username) + .WithApikey(apikey) .Build(); Assert.IsNotNull(authenticator); From 14691dd5a37c37d3b586aceb19cf2f15e6ed021a Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Fri, 22 Oct 2021 13:06:55 -0700 Subject: [PATCH 14/16] fix(cpd-authenticate): remove unused condition --- .../CloudPakForData/CloudPakForDataAuthenticator.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 57cd12fb..77953915 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -132,11 +132,6 @@ private void Init(string url, string username, string password, bool? disableSsl { Url = url; Username = username; - - if (password != null) - { - Password = password; - } if (disableSslVerification != null) { From 18b32e480a32a3a8a73925a65b974bb0a182a6e6 Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Fri, 22 Oct 2021 13:35:15 -0700 Subject: [PATCH 15/16] fix(cpd-authentication): fix init function for initalize with password --- .../CloudPakForData/CloudPakForDataAuthenticator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 77953915..2ec76c60 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -132,6 +132,7 @@ private void Init(string url, string username, string password, bool? disableSsl { Url = url; Username = username; + Password = password; if (disableSslVerification != null) { From cb672187652e5ca9e0e7acadadd181d43c58621e Mon Sep 17 00:00:00 2001 From: Nanwarin Chantarutai Date: Fri, 22 Oct 2021 13:36:57 -0700 Subject: [PATCH 16/16] fix(cpd-authenticator): add default value for password and apikey as null --- .../CloudPakForData/CloudPakForDataAuthenticator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 2ec76c60..c7cd1167 100644 --- a/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/src/IBM.Cloud.SDK.Core/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -147,7 +147,7 @@ private void Init(string url, string username, string password, bool? disableSsl Init(); } - private void Init(string url, string username, string password, string apikey, bool? disableSslVerification = null, Dictionary headers = null) + private void Init(string url, string username, string password = null, string apikey = null, bool? disableSslVerification = null, Dictionary headers = null) { Url = url; Username = username;