From a40c7e1b0a6bb0bda000d8340f99219afc75c011 Mon Sep 17 00:00:00 2001 From: jonaslagoni Date: Sat, 18 Feb 2023 16:15:04 +0100 Subject: [PATCH] fix: credential files not being loaded --- Oxide.Ext.GamingApi/GamingApiNats.cs | 82 ++++++++++++++-------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/Oxide.Ext.GamingApi/GamingApiNats.cs b/Oxide.Ext.GamingApi/GamingApiNats.cs index 81c7be9..30e27cf 100644 --- a/Oxide.Ext.GamingApi/GamingApiNats.cs +++ b/Oxide.Ext.GamingApi/GamingApiNats.cs @@ -71,39 +71,31 @@ private GamingApiNats(LoggingInterface logger) opts.Url = this.GetNatsHost(); string authenticationType = GetNatsAuthenticationType(); if(authenticationType == "jwt") { - string NatsJwtUserFile = this.GetNatsJwtUserFile(); - if (NatsJwtUserFile == null) + EventHandler jwtEh = (sender, args) => { - EventHandler jwtEh = (sender, args) => - { - // Obtain a user JWT... - string jwt = this.GetNatsJwtUser(); + // Obtain a user JWT... + string jwt = this.GetNatsJwtUser(); - this.Logger.Info("NATS: Loading jwt token : " + jwt); - // You must set the JWT in the args to hand off - // to the client library. - args.JWT = jwt; - }; + this.Logger.Info("NATS: Loading jwt token : " + jwt); + // You must set the JWT in the args to hand off + // to the client library. + args.JWT = jwt; + }; - EventHandler sigEh = (sender, args) => - { - // get a private key seed from your environment. - string seed = this.GetNatsJwtSeed(); - this.Logger.Info("NATS: Loading jwt seed : " + seed.Substring(0, 3) + "...."); + EventHandler sigEh = (sender, args) => + { + // get a private key seed from your environment. + string seed = this.GetNatsJwtSeed(); + this.Logger.Info("NATS: Loading jwt seed : " + seed.Substring(0, 3) + "...."); - // Generate a NkeyPair - NkeyPair kp = Nkeys.FromSeed(seed); + // Generate a NkeyPair + NkeyPair kp = Nkeys.FromSeed(seed); - // Sign the nonce and return the result in the SignedNonce - // args property. This must be set. - args.SignedNonce = kp.Sign(args.ServerNonce); - }; - opts.SetUserCredentialHandlers(jwtEh, sigEh); - } else - { - string NatsJwtSeedFile = this.GetNatsJwtSeedFile(); - opts.SetUserCredentials(NatsJwtUserFile, NatsJwtSeedFile); - } + // Sign the nonce and return the result in the SignedNonce + // args property. This must be set. + args.SignedNonce = kp.Sign(args.ServerNonce); + }; + opts.SetUserCredentialHandlers(jwtEh, sigEh); } else { // NKey authentication @@ -163,7 +155,7 @@ private string GetNatsAuthenticationType() private string GetNatsNkeyUser() { - var envName = $"GAMINGAPI_NATS_NKEY_USER"; + var envName = $"GAMINGAPI_NATS_NKEY_USER"; var value = Environment.GetEnvironmentVariable(envName); this.Logger.Info($"NATS: {envName} loading"); if (value == null) @@ -196,15 +188,19 @@ public string PrintByteArray(byte[] bytes) sb.Append("}"); return sb.ToString(); } - private string GetNatsJwtUserFile() - { - var envName = $"GAMINGAPI_NATS_JWT_USER_FILE"; - var fileName = Environment.GetEnvironmentVariable(envName); - return fileName; - } private string GetNatsJwtUser() { var envName = $"GAMINGAPI_NATS_JWT_USER"; + var envFileName = envName + "_FILE"; + + var fileName = Environment.GetEnvironmentVariable(envFileName); + if (fileName != null) + { + this.Logger.Info($"NATS: {envFileName} loading from file"); + string contents = File.ReadAllText(@fileName); + this.Logger.Info($"NATS: {envFileName} length was - {PrintByteArray(Encoding.UTF8.GetBytes(contents))}"); + return contents.Trim(); + } var value = Environment.GetEnvironmentVariable(envName); this.Logger.Info($"NATS: {envName} loading"); @@ -216,15 +212,19 @@ private string GetNatsJwtUser() return value; } - private string GetNatsJwtSeedFile() - { - var envName = $"GAMINGAPI_NATS_JWT_SEED_FILE"; - var fileName = Environment.GetEnvironmentVariable(envName); - return fileName; - } private string GetNatsJwtSeed() { var envName = $"GAMINGAPI_NATS_JWT_SEED"; + var envFileName = envName + "_FILE"; + + var fileName = Environment.GetEnvironmentVariable(envFileName); + if (fileName != null) + { + this.Logger.Info($"NATS: {envFileName} loading from file"); + string contents = File.ReadAllText(@fileName); + this.Logger.Info($"NATS: {envFileName} length was - {PrintByteArray(Encoding.UTF8.GetBytes(contents))}"); + return contents.Trim(); + } var value = Environment.GetEnvironmentVariable(envName); if (value == null) {