-
Notifications
You must be signed in to change notification settings - Fork 49
Update secret functionality #342
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
Merged
Merged
Conversation
This file contains hidden or 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 test for UpdateSecret function.
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #342 +/- ##
==========================================
- Coverage 92.15% 92.10% -0.05%
==========================================
Files 116 116
Lines 11622 11680 +58
Branches 907 909 +2
==========================================
+ Hits 10710 10758 +48
- Misses 705 715 +10
Partials 207 207 ☔ View full report in Codecov by Sentry. |
Member
|
Thank you @simone-fariselli |
Update the secrets for all the pool connections Signed-off-by: Gabriele Santomaggio <[email protected]>
Member
|
Note: you need Tested with: using System.Buffers;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using RabbitMQ.Stream.Client;
using RabbitMQ.Stream.Client.Reliable;
namespace example;
public class KeyCloak
{
private static async Task<string> NewAccessToken()
{
var data = new[]
{
new KeyValuePair<string?, string?>("client_id", "producer"),
new KeyValuePair<string?, string?>("client_secret", "kbOFBXI9tANgKUq8vXHLhT6YhbivgXxn"),
new KeyValuePair<string?, string?>("grant_type", "client_credentials"),
};
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("http://localhost:8080/realms/test/protocol/openid-connect/token"),
Headers =
{
{
HttpRequestHeader.ContentType.ToString(), "application/x-www-form-urlencoded"
},
},
Content = new FormUrlEncodedContent(data)
};
var client = new HttpClient();
var response = await client.SendAsync(httpRequestMessage);
var responseString = await response.Content.ReadAsStringAsync();
var json = System.Text.Json.JsonDocument.Parse(responseString);
var r = json.RootElement.GetProperty("access_token").GetString();
return r ?? throw new Exception("no access token");
}
public static async Task Start()
{
var accessToken = await NewAccessToken();
Console.WriteLine(accessToken);
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder => builder
.AddSimpleConsole(options =>
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "[HH:mm:ss] ";
options.ColorBehavior = LoggerColorBehavior.Default;
})
.AddFilter(level => level >= LogLevel.Debug)
);
var loggerFactory = serviceCollection.BuildServiceProvider()
.GetService<ILoggerFactory>();
if (loggerFactory != null)
{
var producerLogger = loggerFactory.CreateLogger<Producer>();
var consumerLogger = loggerFactory.CreateLogger<Consumer>();
var system = await StreamSystem.Create(new StreamSystemConfig()
{
UserName = "producer",
Password = accessToken,
VirtualHost = "/",
}, loggerFactory.CreateLogger<StreamSystem>());
const string stream = "test-keycloak";
await system.CreateStream(new StreamSpec(stream)
{
MaxLengthBytes = 1_000_000,
});
var start = DateTime.Now;
var completed = new TaskCompletionSource<bool>();
_ = Task.Run(async () =>
{
while (completed.Task.Status != TaskStatus.RanToCompletion)
{
await Task.Delay(TimeSpan.FromSeconds(1));
if (start.AddSeconds(50) >= DateTime.Now) continue;
Console.WriteLine($"{DateTime.Now} - Updating the secret....");
await system.UpdateSecret(await NewAccessToken()).ConfigureAwait(false);
start = DateTime.Now;
}
});
var consumer = await Consumer.Create(new ConsumerConfig(system, stream)
{
OffsetSpec = new OffsetTypeFirst(),
MessageHandler = (_, _, _, message) =>
{
Console.WriteLine(
$"{DateTime.Now} - Received: {Encoding.UTF8.GetString(message.Data.Contents.ToArray())} ");
return Task.CompletedTask;
}
});
var producer = await Producer.Create(new ProducerConfig(system, stream));
for (var i = 0; i < 10 * 60; i++)
{
await producer.Send(new Message(Encoding.UTF8.GetBytes($"Hello KeyCloak! {i}")));
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine($"{DateTime.Now} - Sent: Hello KeyCloak! {i}");
}
completed.SetResult(true);
Console.WriteLine("Closing...");
await consumer.Close();
await producer.Close();
await system.Close();
Console.WriteLine("Closed.");
}
}
} |
Gsantomaggio
approved these changes
Feb 1, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
add update secret functionality.
add test for UpdateSecret function.
Solves #340 with the introduction of the UpdateSecret method in StreamSystem.
Usage example: