public async Task PublishAsync <T>(T message) where T : class { //var json = JsonConvert.SerializeObject(message); var topic = message.GetType().FullName; var cloudEvent = new CloudEvent(topic, this.Source) { DataContentType = new ContentType(MediaTypeNames.Application.Json), Data = message }; var content = new CloudEventContent( cloudEvent, ContentMode.Binary, new JsonEventFormatter() ); var json = await content.ReadAsStringAsync(); _logger.LogInformation("Busk - Publishing event {Topic}: {Body}", cloudEvent.Type, json); var url = Url.Combine(this.Endpoint, Magic.VirtualPath.Publish); var response = await _httpClient.PostAsync(url, content); }
public async Task PublishAsync(CloudEvent cloudEvent) { _logger.LogInformation( "Publishing event {Type} with id {Id} from {Source} with data: {Data}", cloudEvent.Type, cloudEvent.Id, cloudEvent.Source, JsonConvert.SerializeObject(cloudEvent.Data) ); var httpClientHandler = new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Manual, ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true }; var httpClient = new HttpClient(httpClientHandler); var content = new CloudEventContent( cloudEvent, ContentMode.Binary, new JsonEventFormatter() ); var json = await content.ReadAsStringAsync(); _logger.LogWarning("Publishing event {CloudEventContent}", json); foreach (var endpoint in await _storageProvider.GetSubscriptionsAsync(cloudEvent.Type)) { var response = await httpClient.PostAsync(endpoint, content); } }
private async void SendCloudEventForCertificate(string name) { _logger.LogInformation("Sending cloud event for secret {Name}", name); var cloudEvent = new CloudEvent("Microsoft.KeyVault.SecretExpired", new Uri("https://labrats.shawnross.dev/async-function/keyvault-credential-rotator")) { DataContentType = new ContentType(MediaTypeNames.Application.Json), Data = new CertificateExpiryEvent { ObjectType = "Secret", ObjectName = name } }; _logger.LogInformation("Creating content type"); var content = new CloudEventContent(cloudEvent, ContentMode.Structured, new JsonEventFormatter()); var httpClient = new HttpClient(); _logger.LogInformation("Sending request"); var result = await httpClient.PostAsync("https://labrats.shawnross.dev/async-function/keyvault-secret-rotate", // I know this is kind of scuffed, but this is the best I can do currently with the limited timespan. new StringContent("[" + await content.ReadAsStringAsync() + "]", Encoding.UTF8, "application/json")); _logger.LogInformation("Request {Request}", await result.RequestMessage.Content.ReadAsStringAsync()); if (result.IsSuccessStatusCode) { _logger.LogInformation("Successfully rotated secret"); } else { var resultString = await result.Content.ReadAsStringAsync(); _logger.LogError("Result unsuccessful: \n{Result}", resultString); } }