-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore : csr approval example using json patch net package #816
Merged
tg123
merged 4 commits into
kubernetes-client:master
from
kibernetik542:crs-approval-example
Apr 1, 2022
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Net; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Text; | ||
using System.Text.Json; | ||
using Json.Patch; | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace csrApproval | ||
{ | ||
internal class Program | ||
{ | ||
private static string GenerateCertificate(string name) | ||
{ | ||
var sanBuilder = new SubjectAlternativeNameBuilder(); | ||
sanBuilder.AddIpAddress(IPAddress.Loopback); | ||
sanBuilder.AddIpAddress(IPAddress.IPv6Loopback); | ||
sanBuilder.AddDnsName("localhost"); | ||
sanBuilder.AddDnsName(Environment.MachineName); | ||
|
||
var distinguishedName = new X500DistinguishedName(name); | ||
|
||
using var rsa = RSA.Create(4096); | ||
var request = new CertificateRequest(distinguishedName, rsa, HashAlgorithmName.SHA256,RSASignaturePadding.Pkcs1); | ||
|
||
request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DigitalSignature , false)); | ||
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new ("1.3.6.1.5.5.7.3.1") }, false)); | ||
request.CertificateExtensions.Add(sanBuilder.Build()); | ||
var csr = request.CreateSigningRequest(); | ||
var pemKey = "-----BEGIN CERTIFICATE REQUEST-----\r\n" + | ||
Convert.ToBase64String(csr) + | ||
"\r\n-----END CERTIFICATE REQUEST-----"; | ||
|
||
return pemKey; | ||
} | ||
|
||
private static async Task Main(string[] args) | ||
{ | ||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); | ||
IKubernetes client = new Kubernetes(config); | ||
Console.WriteLine("Starting Request!"); | ||
var name = "demo"; | ||
var x509 = GenerateCertificate(name); | ||
var encodedCsr= Encoding.UTF8.GetBytes(x509); | ||
|
||
var request = new V1CertificateSigningRequest | ||
{ | ||
ApiVersion = "certificates.k8s.io/v1", | ||
Kind = "CertificateSigningRequest", | ||
Metadata = new V1ObjectMeta | ||
{ | ||
Name = name | ||
}, | ||
Spec = new V1CertificateSigningRequestSpec | ||
{ | ||
Request = encodedCsr, | ||
SignerName = "kubernetes.io/kube-apiserver-client", | ||
Usages = new List<string> { "client auth" }, | ||
ExpirationSeconds = 600 // minimum should be 10 minutes | ||
} | ||
}; | ||
|
||
await client.CreateCertificateSigningRequestAsync(request); | ||
|
||
var serializeOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, | ||
WriteIndented = true | ||
}; | ||
var readCert = await client.ReadCertificateSigningRequestAsync(name); | ||
var old = JsonSerializer.SerializeToDocument(readCert, serializeOptions); | ||
|
||
var replace = new List<V1CertificateSigningRequestCondition> | ||
{ | ||
new("True", "Approved", DateTime.UtcNow, DateTime.UtcNow, "This certificate was approved by k8s client", "Approve") | ||
}; | ||
readCert.Status.Conditions = replace; | ||
|
||
var expected = JsonSerializer.SerializeToDocument(readCert, serializeOptions); | ||
|
||
var patch = old.CreatePatch(expected); | ||
await client.PatchCertificateSigningRequestApprovalAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
kibernetik542 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<TargetFramework>net6.0</TargetFramework> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. target can be removed as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="JsonPatch.Net" Version="1.1.2" /> | ||
<PackageReference Include="KubernetesClient" Version="7.1.9" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional
https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added using top level statement