diff --git a/src/Utils.cs b/src/CertUtils.cs similarity index 81% rename from src/Utils.cs rename to src/CertUtils.cs index 2ba7cdd..a98e62b 100644 --- a/src/Utils.cs +++ b/src/CertUtils.cs @@ -12,28 +12,8 @@ using Org.BouncyCastle.X509; namespace k8s { - public static class Utils + public static class CertUtils { - /// - /// Encode string in base64 format. - /// - /// string to be encoded. - /// Encoded string. - public static string Base64Encode(string text) - { - return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)); - } - - /// - /// Encode string in base64 format. - /// - /// string to be encoded. - /// Encoded string. - public static string Base64Decode(string text) - { - return Encoding.UTF8.GetString(Convert.FromBase64String(text)); - } - /// /// Load pem encoded cert file /// diff --git a/src/DotNetUtilities.cs b/src/DotNetUtilities.cs deleted file mode 100644 index 1540b52..0000000 --- a/src/DotNetUtilities.cs +++ /dev/null @@ -1,46 +0,0 @@ -namespace k8s { - using System; - using System.Security.Cryptography; - - using Org.BouncyCastle.Crypto.Parameters; - using Org.BouncyCastle.Math; - - // This class was derived from: - // https://github.com/bcgit/bc-csharp/blob/master/crypto/src/security/DotNetUtilities.cs - // Copyright (c) 2000 - 2017 The Legion of the Bouncy Castle Inc. (https://www.bouncycastle.org) - // - // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - public class DotNetUtilities - { - public static RSAParameters ToRSAParameters(RsaPrivateCrtKeyParameters privKey) - { - RSAParameters rp = new RSAParameters(); - rp.Modulus = privKey.Modulus.ToByteArrayUnsigned(); - rp.Exponent = privKey.PublicExponent.ToByteArrayUnsigned(); - rp.P = privKey.P.ToByteArrayUnsigned(); - rp.Q = privKey.Q.ToByteArrayUnsigned(); - rp.D = ConvertRSAParametersField(privKey.Exponent, rp.Modulus.Length); - rp.DP = ConvertRSAParametersField(privKey.DP, rp.P.Length); - rp.DQ = ConvertRSAParametersField(privKey.DQ, rp.Q.Length); - rp.InverseQ = ConvertRSAParametersField(privKey.QInv, rp.Q.Length); - return rp; - } - - private static byte[] ConvertRSAParametersField(BigInteger n, int size) - { - byte[] bs = n.ToByteArrayUnsigned(); - - if (bs.Length == size) - return bs; - - if (bs.Length > size) - throw new ArgumentException("Specified size too small", "size"); - - byte[] padded = new byte[size]; - Array.Copy(bs, 0, padded, size - bs.Length, bs.Length); - return padded; - } - } -} \ No newline at end of file diff --git a/src/Kubernetes.Auth.cs b/src/Kubernetes.Auth.cs index 5dea76f..f7e8bcc 100644 --- a/src/Kubernetes.Auth.cs +++ b/src/Kubernetes.Auth.cs @@ -77,7 +77,7 @@ namespace k8s (!string.IsNullOrWhiteSpace(config.ClientCertificateKeyData) || !string.IsNullOrWhiteSpace(config.ClientKeyFilePath))) { - var cert = Utils.GeneratePfx(config); + var cert = CertUtils.GeneratePfx(config); handler.ClientCertificates.Add(cert); } diff --git a/src/KubernetesClientConfiguration.ConfigFile.cs b/src/KubernetesClientConfiguration.ConfigFile.cs index 4fe3e09..1dc5b07 100644 --- a/src/KubernetesClientConfiguration.ConfigFile.cs +++ b/src/KubernetesClientConfiguration.ConfigFile.cs @@ -130,7 +130,7 @@ namespace k8s if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData)) { string data = clusterDetails.ClusterEndpoint.CertificateAuthorityData; - this.SslCaCert = new X509Certificate2(System.Text.Encoding.UTF8.GetBytes(Utils.Base64Decode(data))); + this.SslCaCert = new X509Certificate2(Convert.FromBase64String(data)); } else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority)) { @@ -215,4 +215,4 @@ namespace k8s return deserializer.Deserialize(kubeconfigContent); } } -} \ No newline at end of file +} diff --git a/src/KubernetesClientConfiguration.InCluster.cs b/src/KubernetesClientConfiguration.InCluster.cs index d2a6189..de4889b 100644 --- a/src/KubernetesClientConfiguration.InCluster.cs +++ b/src/KubernetesClientConfiguration.InCluster.cs @@ -28,8 +28,8 @@ namespace k8s { Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(), AccessToken = token, - SslCaCert = Utils.LoadPemFileCert(rootCAFile) + SslCaCert = CertUtils.LoadPemFileCert(rootCAFile) }; } } -} \ No newline at end of file +} diff --git a/src/Watcher.cs b/src/Watcher.cs index 8a84487..f0bc216 100644 --- a/src/Watcher.cs +++ b/src/Watcher.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Runtime.Serialization; using System.Threading; @@ -140,4 +140,4 @@ namespace k8s return Watch((HttpOperationResponse) response, onEvent, onError); } } -} \ No newline at end of file +} diff --git a/tests/AuthTests.cs b/tests/AuthTests.cs index c41047d..a1e96b3 100644 --- a/tests/AuthTests.cs +++ b/tests/AuthTests.cs @@ -1,9 +1,10 @@ -using System; +using System; using System.IO; using System.Linq; using System.Net; using System.Net.Http.Headers; using System.Security.Cryptography.X509Certificates; +using System.Text; using System.Threading.Tasks; using k8s.Models; using k8s.Tests.Mock; @@ -22,7 +23,7 @@ namespace k8s.Tests } [Fact] - public void TestAnonymous() + public void Anonymous() { using (var server = new MockKubeApiServer()) { @@ -55,7 +56,7 @@ namespace k8s.Tests } [Fact] - public void TestBasicAuth() + public void BasicAuth() { const string testName = "test_name"; const string testPassword = "test_password"; @@ -64,7 +65,7 @@ namespace k8s.Tests { var header = cxt.Request.Headers["Authorization"].FirstOrDefault(); - var expect = new AuthenticationHeaderValue("Basic", Utils.Base64Encode($"{testName}:{testPassword}")) + var expect = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{testName}:{testPassword}"))) .ToString(); if (header != expect) @@ -154,7 +155,7 @@ namespace k8s.Tests } [Fact] - public void TestCert() + public void Cert() { var serverCertificateData = File.ReadAllText("assets/apiserver-pfx-data.txt"); @@ -244,7 +245,7 @@ namespace k8s.Tests } [Fact] - public void TestToken() + public void Token() { const string token = "testingtoken"; @@ -314,4 +315,4 @@ namespace k8s.Tests } } } -} \ No newline at end of file +} diff --git a/tests/UtilTests.cs b/tests/CertUtilsTests.cs similarity index 86% rename from tests/UtilTests.cs rename to tests/CertUtilsTests.cs index 7fb42d6..3b3b83e 100644 --- a/tests/UtilTests.cs +++ b/tests/CertUtilsTests.cs @@ -5,7 +5,7 @@ using System.IO; namespace k8s.Tests { - public class UtilsTests + public class CertUtilsTests { /// /// This file contains a sample kubeconfig file @@ -22,7 +22,7 @@ namespace k8s.Tests var cfg = new KubernetesClientConfiguration(fi, "federal-context"); // Just validate that this doesn't throw and private key is non-null - var cert = Utils.GeneratePfx(cfg); + var cert = CertUtils.GeneratePfx(cfg); Assert.NotNull(cert.PrivateKey); } @@ -36,8 +36,8 @@ namespace k8s.Tests var cfg = new KubernetesClientConfiguration(fi, "victorian-context"); // Just validate that this doesn't throw and private key is non-null - var cert = Utils.GeneratePfx(cfg); + var cert = CertUtils.GeneratePfx(cfg); Assert.NotNull(cert.PrivateKey); } } -} \ No newline at end of file +} diff --git a/tests/KubernetesClientConfigurationTests.cs b/tests/KubernetesClientConfigurationTests.cs index ecc38e1..d21b847 100755 --- a/tests/KubernetesClientConfigurationTests.cs +++ b/tests/KubernetesClientConfigurationTests.cs @@ -83,7 +83,7 @@ namespace k8s.Tests [Theory] [InlineData("federal-context", "https://horse.org:4443")] [InlineData("queen-anne-context", "https://pig.org:443")] - public void ContextHostTest(string context, string host) + public void ContextHost(string context, string host) { var fi = new FileInfo(kubeConfigFileName); var cfg = new KubernetesClientConfiguration(fi, context); @@ -94,11 +94,10 @@ namespace k8s.Tests /// Checks if user-based token is loaded properly from the config file, per context /// /// - /// /// [Theory] [InlineData("queen-anne-context", "black-token")] - public void ContextUserTokenTest(string context, string token) + public void ContextUserToken(string context, string token) { var fi = new FileInfo(kubeConfigFileName); var cfg = new KubernetesClientConfiguration(fi, context); @@ -145,7 +144,7 @@ namespace k8s.Tests /// Test that an Exception is thrown when initializating a KubernetClientConfiguration whose config file Context is not present /// [Fact] - public void ContextNotFoundTest() + public void ContextNotFound() { var fi = new FileInfo(kubeConfigFileName); Assert.Throws(() => new KubernetesClientConfiguration(fi, "context-not-found")); diff --git a/tests/WatchTests.cs b/tests/WatchTests.cs index 31a3dac..7da5a64 100644 --- a/tests/WatchTests.cs +++ b/tests/WatchTests.cs @@ -42,7 +42,7 @@ namespace k8s.Tests } [Fact] - public void TestCannotWatch() + public void CannotWatch() { using (var server = new MockKubeApiServer()) { @@ -74,7 +74,7 @@ namespace k8s.Tests } [Fact] - public void TestSuriveBadLine() + public void SuriveBadLine() { using (var server = new MockKubeApiServer(async httpContext => { @@ -133,7 +133,7 @@ namespace k8s.Tests } [Fact] - public void TestDisposeWatch() + public void DisposeWatch() { using (var server = new MockKubeApiServer(async httpContext => { @@ -181,7 +181,7 @@ namespace k8s.Tests } [Fact] - public void TestWatchAllEvents() + public void WatchAllEvents() { using (var server = new MockKubeApiServer(async httpContext => { @@ -237,7 +237,7 @@ namespace k8s.Tests } [Fact] - public void TestWatchServerDisconnect() + public void WatchServerDisconnect() { Watcher watcher; Exception exceptionCatched = null; @@ -271,4 +271,4 @@ namespace k8s.Tests Assert.IsType(exceptionCatched); } } -} \ No newline at end of file +}