diff --git a/Server/Directory.Packages.props b/Server/Directory.Packages.props index b35431f..cd0baba 100644 --- a/Server/Directory.Packages.props +++ b/Server/Directory.Packages.props @@ -37,6 +37,10 @@ + + + + diff --git a/Server/src/SOR.Cdn/CdnOptions.cs b/Server/src/SOR.Cdn/CdnOptions.cs index d100ba7..bbb46ef 100644 --- a/Server/src/SOR.Cdn/CdnOptions.cs +++ b/Server/src/SOR.Cdn/CdnOptions.cs @@ -1,14 +1,16 @@ namespace SOR.Cdn; /// -/// Configuration for the CDN client. +/// Configuration for the S3-compatible CDN client (Ceph RadosGW). /// /// appsettings.json section: "Cdn" /// /// In production, inject via k8s secret as environment variables: -/// Cdn__PublicBaseUrl → public URL prefix served to clients (e.g. https://cdn.sor.gg) -/// Cdn__UploadBaseUrl → internal upload endpoint (e.g. https://upload.sor.gg) -/// Cdn__ApiKey → API key for upload/delete operations +/// Cdn__PublicBaseUrl → public URL prefix served to clients (e.g. https://cdn.aeternalisgames.com) +/// Cdn__EndpointUrl → S3 endpoint URL (e.g. http://172.10.1.60:7480) +/// Cdn__AccessKey → S3 access key ID +/// Cdn__SecretKey → S3 secret access key +/// Cdn__BucketName → S3 bucket name (e.g. sor-assets) /// public sealed class CdnOptions { @@ -16,19 +18,26 @@ public sealed class CdnOptions /// /// Public base URL for serving CDN assets. - /// Returned URLs are /{path}. - /// Example: https://cdn.sor.gg + /// Returned URLs are /{folder}/{fileName}. + /// Example: https://cdn.aeternalisgames.com /// public string PublicBaseUrl { get; set; } = string.Empty; /// - /// Base URL of the CDN upload service. - /// Example: https://upload.sor.gg + /// S3-compatible endpoint URL (Ceph RadosGW). + /// Example: http://172.10.1.60:7480 /// - public string UploadBaseUrl { get; set; } = string.Empty; + public string EndpointUrl { get; set; } = string.Empty; + + /// S3 access key ID. + public string AccessKey { get; set; } = string.Empty; + + /// S3 secret access key. + public string SecretKey { get; set; } = string.Empty; /// - /// API key sent as the X-Api-Key request header on all write operations. + /// S3 bucket name where assets are stored. + /// Example: sor-assets /// - public string ApiKey { get; set; } = string.Empty; + public string BucketName { get; set; } = string.Empty; } diff --git a/Server/src/SOR.Cdn/HttpCdnClient.cs b/Server/src/SOR.Cdn/HttpCdnClient.cs index f87b62d..7eb6326 100644 --- a/Server/src/SOR.Cdn/HttpCdnClient.cs +++ b/Server/src/SOR.Cdn/HttpCdnClient.cs @@ -1,35 +1,28 @@ namespace SOR.Cdn; -using System.Net.Http.Json; +using Amazon.S3; +using Amazon.S3.Model; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; /// -/// HTTP-based CDN client. +/// S3-compatible CDN client backed by Ceph RadosGW. /// -/// Expected CDN server API: +/// Uploads objects with public-read ACL; the public URL is built as: +/// {PublicBaseUrl}/{folder}/{fileName} /// -/// POST {UploadBaseUrl}/upload?folder={folder} -/// Header: X-Api-Key: {ApiKey} -/// Body: multipart/form-data, field name "file" -/// Returns: 200 { "path": "cards/abc/art.webp", "url": "https://cdn.sor.gg/cards/abc/art.webp" } -/// -/// DELETE {UploadBaseUrl}/files/{path} -/// Header: X-Api-Key: {ApiKey} -/// Returns: 200 or 404 (both treated as success — idempotent) +/// Requires ForcePathStyle=true for Ceph (virtual-hosted bucket style is not supported +/// without explicit DNS configuration). /// -public sealed class HttpCdnClient : ICdnClient +public sealed class S3CdnClient : ICdnClient { - private readonly HttpClient _http; + private readonly IAmazonS3 _s3; private readonly CdnOptions _options; - private readonly ILogger _logger; + private readonly ILogger _logger; - public HttpCdnClient( - HttpClient http, - IOptions options, - ILogger logger) + public S3CdnClient(IAmazonS3 s3, IOptions options, ILogger logger) { - _http = http; + _s3 = s3; _options = options.Value; _logger = logger; } @@ -41,60 +34,39 @@ public sealed class HttpCdnClient : ICdnClient Stream data, CancellationToken ct = default) { - var uploadUrl = $"{_options.UploadBaseUrl.TrimEnd('/')}/upload" + - $"?folder={Uri.EscapeDataString(folder)}"; + var key = $"{folder.Trim('/')}/{fileName}"; - using var multipart = new MultipartFormDataContent(); - var fileContent = new StreamContent(data); - fileContent.Headers.ContentType = - new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); - multipart.Add(fileContent, "file", fileName); - - using var request = new HttpRequestMessage(HttpMethod.Post, uploadUrl) - { - Content = multipart, - }; - request.Headers.Add("X-Api-Key", _options.ApiKey); - - HttpResponseMessage response; try { - response = await _http.SendAsync(request, ct); + var request = new PutObjectRequest + { + BucketName = _options.BucketName, + Key = key, + InputStream = data, + ContentType = contentType, + CannedACL = S3CannedACL.PublicRead, + DisableDefaultChecksumValidation = true, + }; + + await _s3.PutObjectAsync(request, ct); + + var publicUrl = $"{_options.PublicBaseUrl.TrimEnd('/')}/{key}"; + _logger.LogInformation("CDN upload succeeded: {Url}", publicUrl); + return new CdnUploadResult(true, key, publicUrl, null); } catch (Exception ex) { - _logger.LogError(ex, "CDN upload request failed for {Folder}/{FileName}", folder, fileName); + _logger.LogError(ex, "CDN upload failed for {Folder}/{FileName}", folder, fileName); return new CdnUploadResult(false, null, null, ex.Message); } - - if (!response.IsSuccessStatusCode) - { - var body = await response.Content.ReadAsStringAsync(ct); - _logger.LogWarning("CDN upload HTTP {Status} for {Folder}/{FileName}: {Body}", - (int)response.StatusCode, folder, fileName, body); - return new CdnUploadResult(false, null, null, $"HTTP {(int)response.StatusCode}"); - } - - var json = await response.Content.ReadFromJsonAsync(ct); - if (json is null) - return new CdnUploadResult(false, null, null, "Empty or invalid response from CDN"); - - _logger.LogInformation("CDN upload succeeded: {Url}", json.Url); - return new CdnUploadResult(true, json.Path, json.Url, null); } public async Task DeleteAsync(string path, CancellationToken ct = default) { - var deleteUrl = $"{_options.UploadBaseUrl.TrimEnd('/')}/files/{Uri.EscapeDataString(path)}"; - - using var request = new HttpRequestMessage(HttpMethod.Delete, deleteUrl); - request.Headers.Add("X-Api-Key", _options.ApiKey); - try { - var response = await _http.SendAsync(request, ct); - // 200 = deleted, 404 = already gone — both are fine. - return response.IsSuccessStatusCode || response.StatusCode == System.Net.HttpStatusCode.NotFound; + await _s3.DeleteObjectAsync(_options.BucketName, path, ct); + return true; } catch (Exception ex) { @@ -102,7 +74,4 @@ public sealed class HttpCdnClient : ICdnClient return false; } } - - // Response shape from the CDN upload endpoint. - private sealed record CdnUploadResponse(string Path, string Url); } diff --git a/Server/src/SOR.Cdn/SOR.Cdn.csproj b/Server/src/SOR.Cdn/SOR.Cdn.csproj index 95750a8..c511ff9 100644 --- a/Server/src/SOR.Cdn/SOR.Cdn.csproj +++ b/Server/src/SOR.Cdn/SOR.Cdn.csproj @@ -4,7 +4,11 @@ - + + + + + diff --git a/Server/src/SOR.Cdn/ServiceRegistration.cs b/Server/src/SOR.Cdn/ServiceRegistration.cs index e6a3d3c..da31aac 100644 --- a/Server/src/SOR.Cdn/ServiceRegistration.cs +++ b/Server/src/SOR.Cdn/ServiceRegistration.cs @@ -1,16 +1,18 @@ namespace SOR.Cdn; +using Amazon.S3; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; public static class ServiceRegistration { /// - /// Registers (backed by ) and reads + /// Registers (backed by ) and reads /// configuration from the "Cdn" section. /// /// Production: override via k8s secret env vars: - /// Cdn__PublicBaseUrl, Cdn__UploadBaseUrl, Cdn__ApiKey + /// Cdn__PublicBaseUrl, Cdn__EndpointUrl, Cdn__AccessKey, Cdn__SecretKey, Cdn__BucketName /// public static IServiceCollection AddCdnClient( this IServiceCollection services, @@ -18,8 +20,21 @@ public static class ServiceRegistration { services.Configure(configuration.GetSection(CdnOptions.Section)); - // Typed HttpClient — IHttpClientFactory manages connection pooling + lifetime. - services.AddHttpClient(); + // Build AmazonS3Client from CdnOptions; registered as Singleton because + // AmazonS3Client manages its own connection pool. + services.AddSingleton(sp => + { + var opts = sp.GetRequiredService>().Value; + var config = new AmazonS3Config + { + ServiceURL = opts.EndpointUrl, + ForcePathStyle = true, // required for Ceph RadosGW + AuthenticationRegion = "us-east-1", + }; + return new AmazonS3Client(opts.AccessKey, opts.SecretKey, config); + }); + + services.AddSingleton(); return services; } diff --git a/deploy/k8s/deployment-admin.yaml b/deploy/k8s/deployment-admin.yaml index f1e15bf..ab44c4b 100644 --- a/deploy/k8s/deployment-admin.yaml +++ b/deploy/k8s/deployment-admin.yaml @@ -37,22 +37,32 @@ spec: name: sor-app-secrets key: ConnectionStrings__Default - # CDN — needed for asset uploads in the admin portal. + # CDN (Ceph RadosGW S3-compatible) — needed for asset uploads in the admin portal. - name: Cdn__PublicBaseUrl valueFrom: secretKeyRef: name: sor-app-secrets key: Cdn__PublicBaseUrl - - name: Cdn__UploadBaseUrl + - name: Cdn__EndpointUrl valueFrom: secretKeyRef: name: sor-app-secrets - key: Cdn__UploadBaseUrl - - name: Cdn__ApiKey + key: Cdn__EndpointUrl + - name: Cdn__AccessKey valueFrom: secretKeyRef: name: sor-app-secrets - key: Cdn__ApiKey + key: Cdn__AccessKey + - name: Cdn__SecretKey + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__SecretKey + - name: Cdn__BucketName + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__BucketName # ASP.NET Core Data Protection — persists Blazor auth cookies across restarts. # TODO: generate a strong random value and store in sor-app-secrets. diff --git a/deploy/k8s/deployment-host.yaml b/deploy/k8s/deployment-host.yaml index eb111a5..b24336a 100644 --- a/deploy/k8s/deployment-host.yaml +++ b/deploy/k8s/deployment-host.yaml @@ -48,22 +48,32 @@ spec: name: redis-credentials key: connection-string - # CDN + # CDN (Ceph RadosGW S3-compatible) - name: Cdn__PublicBaseUrl valueFrom: secretKeyRef: name: sor-app-secrets key: Cdn__PublicBaseUrl - - name: Cdn__UploadBaseUrl + - name: Cdn__EndpointUrl valueFrom: secretKeyRef: name: sor-app-secrets - key: Cdn__UploadBaseUrl - - name: Cdn__ApiKey + key: Cdn__EndpointUrl + - name: Cdn__AccessKey valueFrom: secretKeyRef: name: sor-app-secrets - key: Cdn__ApiKey + key: Cdn__AccessKey + - name: Cdn__SecretKey + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__SecretKey + - name: Cdn__BucketName + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__BucketName # ── Health checks ────────────────────────────────────────────────── livenessProbe: diff --git a/deploy/secrets/README.md b/deploy/secrets/README.md index 76885f2..113a16d 100644 --- a/deploy/secrets/README.md +++ b/deploy/secrets/README.md @@ -33,14 +33,16 @@ In your Gitea repository → Settings → Secrets, add: ## Secrets Reference -| Secret Name | Key | Used By | Notes | -|--------------------|----------------------------|-----------------|-------------------------------------------| -| sor-app-secrets | ConnectionStrings__Default | host + admin | Postgres connection string | -| sor-app-secrets | Cdn__PublicBaseUrl | host + admin | e.g. https://cdn.aeternalisgames.com | -| sor-app-secrets | Cdn__UploadBaseUrl | host + admin | CDN upload endpoint | -| sor-app-secrets | Cdn__ApiKey | host + admin | CDN API key | -| redis-credentials | connection-string | host only | **Already exists** in stone-server ns | -| harbor-credentials | .dockerconfigjson | both pods | Harbor image pull secret | +| Secret Name | Key | Used By | Notes | +|--------------------|----------------------------|-----------------|-------------------------------------------------| +| sor-app-secrets | ConnectionStrings__Default | host + admin | Postgres connection string | +| sor-app-secrets | Cdn__PublicBaseUrl | host + admin | e.g. https://cdn.aeternalisgames.com | +| sor-app-secrets | Cdn__EndpointUrl | host + admin | Ceph RadosGW endpoint, e.g. http://172.10.1.60:7480 | +| sor-app-secrets | Cdn__AccessKey | host + admin | S3 access key ID | +| sor-app-secrets | Cdn__SecretKey | host + admin | S3 secret access key | +| sor-app-secrets | Cdn__BucketName | host + admin | S3 bucket name, e.g. sor-assets | +| redis-credentials | connection-string | host only | **Already exists** in stone-server ns | +| harbor-credentials | .dockerconfigjson | both pods | Harbor image pull secret | --- diff --git a/deploy/secrets/sor-app-secrets.yaml.template b/deploy/secrets/sor-app-secrets.yaml.template index f9b3a2c..4f3fb39 100644 --- a/deploy/secrets/sor-app-secrets.yaml.template +++ b/deploy/secrets/sor-app-secrets.yaml.template @@ -9,7 +9,11 @@ stringData: # Format: Host=HOST;Port=5432;Database=SOR;Username=USER;Password=PASSWORD; ConnectionStrings__Default: "Host=172.10.1.127;Port=5432;Database=SOR;Username=SOR-DBA;Password=CHANGEME" - # CDN — public URL served to clients, upload endpoint, and API key. + # CDN (Ceph RadosGW S3-compatible). + # PublicBaseUrl: the public-facing URL prefix returned to clients for asset links. + # EndpointUrl: internal S3 endpoint (e.g. http://172.10.1.60:7480). Cdn__PublicBaseUrl: "https://CHANGEME" - Cdn__UploadBaseUrl: "https://CHANGEME" - Cdn__ApiKey: "CHANGEME" + Cdn__EndpointUrl: "http://CHANGEME:7480" + Cdn__AccessKey: "CHANGEME" + Cdn__SecretKey: "CHANGEME" + Cdn__BucketName: "sor-assets"