From 0628d85fd70ef38cf58179be8497949d7b787466 Mon Sep 17 00:00:00 2001 From: Andy Ford Date: Mon, 18 Dec 2023 20:13:32 +0000 Subject: [PATCH] feat: get public key directly from environment --- cmd/ecfmp-discord/main.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/ecfmp-discord/main.go b/cmd/ecfmp-discord/main.go index 10cabef..4e0485b 100644 --- a/cmd/ecfmp-discord/main.go +++ b/cmd/ecfmp-discord/main.go @@ -54,18 +54,25 @@ func main() { // Create the discord scheduler scheduler := discord.NewDiscordScheduler(mongo, publisher) - // Get the public key from the environment - publicKeyFile := os.Getenv("AUTH_JWT_PUBLIC_KEY_FILE") - if publicKeyFile == "" { - log.Fatalf("AUTH_JWT_PUBLIC_KEY_FILE is not set") - } + // Try the public key from the environment directly + publicKey := os.Getenv("AUTH_JWT_PUBLIC_KEY") - // Get the public key by reading the file, throw error if file doesnt exist - publicKey, err := os.ReadFile(publicKeyFile) - if err != nil { - log.Fatalf("failed to read public key file: %v", err) + // If the public key is empty, try to get it from a file + if publicKey == "" { + publicKeyFile := os.Getenv("AUTH_JWT_PUBLIC_KEY_FILE") + if publicKeyFile == "" { + log.Fatalf("AUTH_JWT_PUBLIC_KEY_FILE is not set") + } + + // Get the public key by reading the file, throw error if file doesnt exist + fromFile, err := os.ReadFile(publicKeyFile) + if err != nil { + log.Fatalf("failed to read public key file: %v", err) + } + + publicKey = string(fromFile) } - interceptor := grpc.NewJwtAuthInterceptor(publicKey, os.Getenv("AUTH_JWT_AUDIENCE")) + interceptor := grpc.NewJwtAuthInterceptor([]byte(publicKey), os.Getenv("AUTH_JWT_AUDIENCE")) grpcServer := grpc.NewServer(mongo, scheduler, interceptor) log.Info("Discord server starting...")