Merge pull request #51 from tg123/minor_style_fix
cleanup unused code and functions and fix unittest naming style
This commit is contained in:
@@ -12,28 +12,8 @@ using Org.BouncyCastle.X509;
|
||||
|
||||
namespace k8s
|
||||
{
|
||||
public static class Utils
|
||||
public static class CertUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Encode string in base64 format.
|
||||
/// </summary>
|
||||
/// <param name="text">string to be encoded.</param>
|
||||
/// <returns>Encoded string.</returns>
|
||||
public static string Base64Encode(string text)
|
||||
{
|
||||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encode string in base64 format.
|
||||
/// </summary>
|
||||
/// <param name="text">string to be encoded.</param>
|
||||
/// <returns>Encoded string.</returns>
|
||||
public static string Base64Decode(string text)
|
||||
{
|
||||
return Encoding.UTF8.GetString(Convert.FromBase64String(text));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load pem encoded cert file
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace k8s
|
||||
{
|
||||
Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(),
|
||||
AccessToken = token,
|
||||
SslCaCert = Utils.LoadPemFileCert(rootCAFile)
|
||||
SslCaCert = CertUtils.LoadPemFileCert(rootCAFile)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading;
|
||||
|
||||
@@ -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";
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ using System.IO;
|
||||
|
||||
namespace k8s.Tests
|
||||
{
|
||||
public class UtilsTests
|
||||
public class CertUtilsTests
|
||||
{
|
||||
/// <summary>
|
||||
/// 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,7 +36,7 @@ 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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="username"></param>
|
||||
/// <param name="token"></param>
|
||||
[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
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ContextNotFoundTest()
|
||||
public void ContextNotFound()
|
||||
{
|
||||
var fi = new FileInfo(kubeConfigFileName);
|
||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -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 =>
|
||||
{
|
||||
@@ -163,7 +163,7 @@ namespace k8s.Tests
|
||||
);
|
||||
|
||||
// wait at least an event
|
||||
Thread.Sleep(TimeSpan.FromMilliseconds(300));
|
||||
Thread.Sleep(TimeSpan.FromMilliseconds(500));
|
||||
|
||||
Assert.NotEmpty(events);
|
||||
Assert.True(watcher.Watching);
|
||||
@@ -173,7 +173,7 @@ namespace k8s.Tests
|
||||
events.Clear();
|
||||
|
||||
// make sure wait event called
|
||||
Thread.Sleep(TimeSpan.FromMilliseconds(300));
|
||||
Thread.Sleep(TimeSpan.FromMilliseconds(500));
|
||||
Assert.Empty(events);
|
||||
Assert.False(watcher.Watching);
|
||||
|
||||
@@ -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<Corev1Pod> watcher;
|
||||
Exception exceptionCatched = null;
|
||||
|
||||
Reference in New Issue
Block a user