From 13c145ad85461576226e48e7a5ef755c1b1abcd0 Mon Sep 17 00:00:00 2001 From: Manuel <5877862+manuelsc@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:27:00 +0100 Subject: [PATCH] (NOBIDS) allow apple and google secrets being passed directly without file --- exporter/appsubscription_oracle.go | 33 ++++++++++++++++++++++++------ notify/firebase.go | 11 +++------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/exporter/appsubscription_oracle.go b/exporter/appsubscription_oracle.go index 4b1e8a4189..82ba8f9c14 100644 --- a/exporter/appsubscription_oracle.go +++ b/exporter/appsubscription_oracle.go @@ -114,9 +114,19 @@ func VerifyReceipt(googleClient *playstore.Client, appleClient *api.StoreClient, } func initGoogle() (*playstore.Client, error) { - jsonKey, err := os.ReadFile(utils.Config.Frontend.AppSubsGoogleJSONPath) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("Can not read google json key file %v", utils.Config.Frontend.AppSubsGoogleJSONPath)) + if len(utils.Config.Frontend.AppSubsGoogleJSONPath) == 0 { + return nil, errors.New("google app subs json path not set") + } + + var jsonKey []byte + var err error + if strings.Contains(utils.Config.Frontend.AppSubsGoogleJSONPath, ".json") { + jsonKey, err = os.ReadFile(utils.Config.Frontend.AppSubsGoogleJSONPath) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("Can not read google json key file %v", utils.Config.Frontend.AppSubsGoogleJSONPath)) + } + } else { + jsonKey = []byte(utils.Config.Frontend.AppSubsGoogleJSONPath) } client, err := playstore.New(jsonKey) @@ -124,10 +134,21 @@ func initGoogle() (*playstore.Client, error) { } func initApple() (*api.StoreClient, error) { - keyContent, err := os.ReadFile(utils.Config.Frontend.Apple.Certificate) - if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("can not load apple certificate for file %v", utils.Config.Frontend.Apple.Certificate)) + if len(utils.Config.Frontend.Apple.Certificate) == 0 { + return nil, errors.New("apple certificate path not set") } + + var keyContent []byte + var err error + if strings.Contains(utils.Config.Frontend.Apple.Certificate, "BEGIN PRIVATE KEY") { + keyContent = []byte(utils.Config.Frontend.Apple.Certificate) + } else { + keyContent, err = os.ReadFile(utils.Config.Frontend.Apple.Certificate) + if err != nil { + return nil, errors.Wrap(err, fmt.Sprintf("can not load apple certificate for file %v", utils.Config.Frontend.Apple.Certificate)) + } + } + return api.NewStoreClient(&api.StoreConfig{ KeyContent: keyContent, // Loads a .p8 certificate KeyID: utils.Config.Frontend.Apple.KeyID, // Your private key ID from App Store Connect (Ex: 2X9R4HXF34) diff --git a/notify/firebase.go b/notify/firebase.go index 55e9400378..a2b4eab708 100644 --- a/notify/firebase.go +++ b/notify/firebase.go @@ -3,7 +3,6 @@ package notify import ( "context" "eth2-exporter/utils" - "fmt" "strings" "time" @@ -36,14 +35,10 @@ func SendPushBatch(messages []*messaging.Message) error { ctx := context.Background() var opt option.ClientOption - if strings.HasPrefix(credentialsPath, "projects/") { - x, err := utils.AccessSecretVersion(credentialsPath) - if err != nil { - return fmt.Errorf("error getting firebase config from secret store: %v", err) - } - opt = option.WithCredentialsJSON([]byte(*x)) - } else { + if strings.Contains(credentialsPath, ".json") && len(credentialsPath) < 200 { opt = option.WithCredentialsFile(credentialsPath) + } else { + opt = option.WithCredentialsJSON([]byte(credentialsPath)) } app, err := firebase.NewApp(context.Background(), nil, opt)