From 26181327da6587d4a0ef0bf1ad6d1a1ab28f6cdb Mon Sep 17 00:00:00 2001 From: Radoslav Radev Date: Fri, 17 May 2024 17:48:25 +0200 Subject: [PATCH] moved to ENV vars for some core config fields --- Backend/APIGateway/Auth/OwnAuthHandler.cs | 8 +++++++- Backend/Account/Program.cs | 3 ++- Backend/Account/Services/AuthTokenGenerator.cs | 9 +++++++-- Backend/Account/Services/StripePaymentService.cs | 7 ++++++- .../openvidstreamer-fe/src/Pages/Upload/UploadPage.tsx | 1 + K6Tests/StreamVideo.js | 2 +- OpenVidStreamerKubernetesFiles/account-deployment.yaml | 10 +++++++++- OpenVidStreamerKubernetesFiles/apigateway-service.yaml | 6 +++--- OpenVidStreamerKubernetesFiles/render-deployment.yaml | 3 +-- OpenVidStreamerKubernetesFiles/render-hpa.yaml | 2 +- 10 files changed, 38 insertions(+), 13 deletions(-) diff --git a/Backend/APIGateway/Auth/OwnAuthHandler.cs b/Backend/APIGateway/Auth/OwnAuthHandler.cs index 47067d2..1c2281e 100644 --- a/Backend/APIGateway/Auth/OwnAuthHandler.cs +++ b/Backend/APIGateway/Auth/OwnAuthHandler.cs @@ -19,6 +19,9 @@ namespace OpenVidStreamer.APIGateway.Auth; public class OwnAuthHandler : AuthenticationHandler { + + + private readonly string _jwtSecret; IConfiguration _configuration; @@ -30,6 +33,7 @@ public OwnAuthHandler(IOptionsMonitor options, : base(options, logger, encoder, clock) { _configuration = configuration; + _jwtSecret = Environment.GetEnvironmentVariable("JwtSecret") ?? configuration["CustomJWT:Secret"]; } @@ -63,6 +67,8 @@ protected override Task HandleAuthenticateAsync() } try { + + var validationParameters = new TokenValidationParameters { ValidateIssuer = true, @@ -70,7 +76,7 @@ protected override Task HandleAuthenticateAsync() ValidateLifetime = true, ValidIssuer = _configuration["CustomJWT:Issuer"], ValidAudience = _configuration["CustomJWT:Audience"], - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["CustomJWT:Secret"])) + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSecret)) }; var handler = new JwtSecurityTokenHandler(); diff --git a/Backend/Account/Program.cs b/Backend/Account/Program.cs index 8445bc5..00b8536 100644 --- a/Backend/Account/Program.cs +++ b/Backend/Account/Program.cs @@ -21,7 +21,8 @@ builder.Services.AddScoped(); builder.Services.AddHealthChecks(); -StripeConfiguration.ApiKey = builder.Configuration["Stripe:SecretKey"]; +var stripeKey = Environment.GetEnvironmentVariable("StripeSecretKey") ?? builder.Configuration["Stripe:SecretKey"]; +StripeConfiguration.ApiKey = stripeKey; var app = builder.Build(); diff --git a/Backend/Account/Services/AuthTokenGenerator.cs b/Backend/Account/Services/AuthTokenGenerator.cs index 92c449d..b20f6b9 100644 --- a/Backend/Account/Services/AuthTokenGenerator.cs +++ b/Backend/Account/Services/AuthTokenGenerator.cs @@ -10,6 +10,11 @@ public static class AuthTokenGenerator { public static string GenerateOwnAuthToken(string accId, IConfiguration configuration,bool hasActiveSubscription = false) { + string _jwtSecret = Environment.GetEnvironmentVariable("JwtSecret") ?? configuration["CustomJWT:Secret"]; + string _jwtExpiration = Environment.GetEnvironmentVariable("JwtExpiration") ?? + configuration["CustomJWT:ExpirationInHours"]; + + // Define token claims var claims = new List { @@ -27,14 +32,14 @@ public static string GenerateOwnAuthToken(string accId, IConfiguration configura // Generate token - var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["CustomJWT:Secret"])); + var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSecret)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: configuration["CustomJWT:Issuer"], audience: configuration["CustomJWT:Audience"], claims: claims, - expires: DateTime.Now.AddMinutes(Convert.ToInt32(configuration["CustomJWT:ExpirationInHours"])), // Token expiry time + expires: DateTime.Now.AddMinutes(Convert.ToInt32(_jwtExpiration)), // Token expiry time signingCredentials: creds); return new JwtSecurityTokenHandler().WriteToken(token); diff --git a/Backend/Account/Services/StripePaymentService.cs b/Backend/Account/Services/StripePaymentService.cs index 2916a61..287071b 100644 --- a/Backend/Account/Services/StripePaymentService.cs +++ b/Backend/Account/Services/StripePaymentService.cs @@ -9,6 +9,9 @@ namespace Account.Services; public class StripePaymentService { + + private readonly string _stripeRedirectUrl; + private readonly DatabaseContext _accountDbContext; private readonly IAsyncPolicy _retryPolicy = Policy .HandleResult(paymentIntent => paymentIntent.Status == "requires_action" || paymentIntent.Status == "requires_payment_method") @@ -22,6 +25,8 @@ public StripePaymentService(DatabaseContext accountDbContext, IConfiguration con { _accountDbContext = accountDbContext; _configuration = configuration; + _stripeRedirectUrl = Environment.GetEnvironmentVariable("StripeRedirectUrl") ?? + _configuration.GetValue("Stripe:RedirectUrl"); } public async Task ProcessPaymentAsync(IncomingPaymentDTO incomingPayment, string accId) @@ -50,7 +55,7 @@ public async Task ProcessPaymentAsync(IncomingPaymentDTO incoming Description = $"OpenVidStreamer - Payment of monthly subscription for AccountNumber: {accId}", Confirm = true, UseStripeSdk = true, - ReturnUrl = _configuration.GetValue("Stripe:RedirectUrl"), + ReturnUrl = _stripeRedirectUrl, Customer = customer.Id }; diff --git a/Frontend/openvidstreamer-fe/src/Pages/Upload/UploadPage.tsx b/Frontend/openvidstreamer-fe/src/Pages/Upload/UploadPage.tsx index e271a3e..05fd215 100644 --- a/Frontend/openvidstreamer-fe/src/Pages/Upload/UploadPage.tsx +++ b/Frontend/openvidstreamer-fe/src/Pages/Upload/UploadPage.tsx @@ -64,6 +64,7 @@ const UploadPage = () => { onUploadProgress: progressEvent => { const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total); setUploadPercentage(percentCompleted); + console.log("Upload progress: " + percentCompleted + "%") } }); alert('Video uploaded successfully!'); diff --git a/K6Tests/StreamVideo.js b/K6Tests/StreamVideo.js index 2eb9233..f8fd3e8 100644 --- a/K6Tests/StreamVideo.js +++ b/K6Tests/StreamVideo.js @@ -23,7 +23,7 @@ export let options = { export default function () { const params = { headers: { - 'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cG4iOiIwOGRjNDIwNy1mZWMwLTRlOTctOGY4MC0zYWU1MDUyYTMwNWEiLCJzdWIiOiIwOGRjNDIwNy1mZWMwLTRlOTctOGY4MC0zYWU1MDUyYTMwNWEiLCJqdGkiOiIyNWYwNjgxZC04NzMwLTQ4ZDgtODIyMC0xMzYyMzAwNGVhMmIiLCJleHAiOjE3MTQ1NzE3MTAsImlzcyI6Ik9wZW5WaWRTdHJlYW1lckFjY291bnRTZXJ2aWNlIiwiYXVkIjoiT3BlblZpZFN0cmVhbWVyRkUifQ.LV4iObB6OXV66kdx9caprcWawHbgAHG0HmyYEvuuxmk' + 'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1cG4iOiIwOGRjNDIwNy1mZWMwLTRlOTctOGY4MC0zYWU1MDUyYTMwNWEiLCJzdWIiOiIwOGRjNDIwNy1mZWMwLTRlOTctOGY4MC0zYWU1MDUyYTMwNWEiLCJqdGkiOiIwNWEyM2JiMS02NThhLTRjODMtOTgyNS1lMzM4MWZjNGViOTQiLCJleHAiOjE3MTQ5ODY2MTAsImlzcyI6Ik9wZW5WaWRTdHJlYW1lckFjY291bnRTZXJ2aWNlIiwiYXVkIjoiT3BlblZpZFN0cmVhbWVyRkUifQ.j5DQGFXBZovCMj_vQVmOyd9fv0DwtyiMhbZ0pe6wwok' } }; diff --git a/OpenVidStreamerKubernetesFiles/account-deployment.yaml b/OpenVidStreamerKubernetesFiles/account-deployment.yaml index 5dacd46..3d13a02 100644 --- a/OpenVidStreamerKubernetesFiles/account-deployment.yaml +++ b/OpenVidStreamerKubernetesFiles/account-deployment.yaml @@ -26,4 +26,12 @@ spec: fieldPath: status.podIP - name: servicePort value: "8081" - + # CHANGE ALL ENV VARS BELLOW !!!!! + - name: StripeSecretKey + value: "sk_test_51J4J7sk_test_51OpbAqLhuzbscNjkorQ4sXh7YsruJX0mYn3seAyim6fAaxZ2vnawug50BZknBpdbKCLOTNu1eT3pXWG84bDKLhab00kyOhVEW2" + - name: StripeRedirectUrl + value: "http://145.220.74.148:3000/paymentProcessed" + - name: JwtSecret + value: "rxio0SNqgU2yYEvOyZJ1greSMC75JBU0D6IxBZBxIXm+xzSr2ZZ+ZV/PHoV7sNYg7f9PCHulGu+QHG5qaSNpTQ==" + - name: JwtExpiration + value: "72" diff --git a/OpenVidStreamerKubernetesFiles/apigateway-service.yaml b/OpenVidStreamerKubernetesFiles/apigateway-service.yaml index 9979870..f7516de 100644 --- a/OpenVidStreamerKubernetesFiles/apigateway-service.yaml +++ b/OpenVidStreamerKubernetesFiles/apigateway-service.yaml @@ -3,10 +3,10 @@ kind: Service metadata: name: apigateway-service spec: - type: LoadBalancer + type: NodePort selector: app: apigateway ports: - - protocol: TCP - port: 8000 + - port: 8000 targetPort: 8000 + nodePort: 31800 diff --git a/OpenVidStreamerKubernetesFiles/render-deployment.yaml b/OpenVidStreamerKubernetesFiles/render-deployment.yaml index 1db6fd0..98328e5 100644 --- a/OpenVidStreamerKubernetesFiles/render-deployment.yaml +++ b/OpenVidStreamerKubernetesFiles/render-deployment.yaml @@ -2,8 +2,7 @@ kind: Deployment metadata: name: render-deployment -spec: - replicas: 2 +spec: selector: matchLabels: app: render diff --git a/OpenVidStreamerKubernetesFiles/render-hpa.yaml b/OpenVidStreamerKubernetesFiles/render-hpa.yaml index 56b80d9..8d629d8 100644 --- a/OpenVidStreamerKubernetesFiles/render-hpa.yaml +++ b/OpenVidStreamerKubernetesFiles/render-hpa.yaml @@ -15,4 +15,4 @@ spec: name: cpu target: type: Utilization - averageUtilization: 95 + averageUtilization: 10