diff --git a/cmd/validator/main.go b/cmd/validator/main.go new file mode 100644 index 0000000..ef5c048 --- /dev/null +++ b/cmd/validator/main.go @@ -0,0 +1,208 @@ +package main + +import ( + "context" + "fmt" + "gitea.pena/PenaSide/common/encrypt" + "gitea.pena/PenaSide/customer/internal/models" + "github.com/caarlos0/env/v8" + "github.com/gofiber/fiber/v2" + "github.com/twmb/franz-go/pkg/kgo" + "log" + "net" + "regexp" + "time" +) + +func main() { + config, err := loadConfig() + if err != nil { + log.Fatalf("error loading config: %v", err) + } + + err = validateNotEmpty(config) + if err != nil { + log.Fatalf("error validating config: %v", err) + } + + urls := []string{ + config.AuthMicroservice, + config.HubadminMicroservice, + config.CurrencyMicroservice, + config.DiscountMicroservice, + config.CodewordMicroservice, + config.PaymentMicroservice, + config.VerificationMicroservice, + config.TemplategenMicroserviceURL, + config.TrashLogHost, + config.AdminURL, + config.ExternalCfg.MailClientCfg.ApiUrl, + } + if err = validateURLs(urls); err != nil { + log.Fatalf("error validating urls: %v", err) + } + + // todo validate jwt + + if err = validateKafka(config.KafkaBrokers, config.KafkaTopic); err != nil { + log.Fatalf("error validating kafka: %v", err) + } + + if err = validateMail(config.ExternalCfg.MailClientCfg); err != nil { + log.Fatalf("error validating smtp: %v", err) + } + + if err = validateEncryptKeys(&config.ExternalCfg.EncryptCommon); err != nil { + log.Fatalf("error validating encrypted: %v", err) + } + + if err = validateTG(config.NotificationBotToken, config.NotificationRsPayChannel, config.NotificationChannel); err != nil { + log.Fatalf("error validating tg enviroments: %v", err) + } + + //todo mongo +} + +func loadConfig() (*models.Config, error) { + var config models.Config + if err := env.Parse(&config); err != nil { + return nil, err + } + return &config, nil +} + +func validateURLs(urls []string) error { + for index, u := range urls { + if u == "" { + return fmt.Errorf("empty url, index: %d", index) + } + + if ip := net.ParseIP(u); ip == nil { + return fmt.Errorf("invalid url: %s", u) + } + // todo check the liveness of these URLs, many services do not support + } + return nil +} + +func validateKafka(brokers []string, topic string) error { + if len(brokers) == 0 { + return fmt.Errorf("kafka brokers is empty") + } + if topic == "" { + return fmt.Errorf("kafka topic is empty") + } + + for _, addr := range brokers { + if addr == "" { + return fmt.Errorf("empty kafka broker") + } + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + kafkaTariffClient, err := kgo.NewClient( + kgo.SeedBrokers(brokers...), + kgo.ConsumeResetOffset(kgo.NewOffset().AtStart()), + kgo.DefaultProduceTopic(topic), + kgo.ConsumeTopics(topic), + ) + if err != nil { + return err + } + + defer kafkaTariffClient.Close() + + err = kafkaTariffClient.Ping(ctx) + if err != nil { + return err + } + + return nil +} + +func validateMail(cfg models.MailClientCfg) error { + if cfg.MailAddress == "" { + return fmt.Errorf("mail address is empty") + } + + if cfg.ApiUrl == "" { + return fmt.Errorf("mail api url is empty") + } + + if cfg.ApiKey == "" { + return fmt.Errorf("mail api key is empty") + } + + if cfg.Sender == "" { + return fmt.Errorf("mail sender is empty") + } + + client := fiber.AcquireClient() + req := client.Get("https://api.smtp.bz/v1/user") + req.Set("Authorization", cfg.ApiKey) + + code, _, _ := req.Bytes() + if code != fiber.StatusOK { + return fmt.Errorf("invalid smtp code, no auth: %d", code) + } + + return nil +} + +func validateEncryptKeys(e *encrypt.Encrypt) error { + codeWord := "ДАЙТЕ Jazz-у!" + shifr, err := e.EncryptStr(codeWord) + if err != nil { + return err + } + + deShifr, err := e.DecryptStr(shifr) + if err != nil { + return err + } + + if deShifr != codeWord { + return fmt.Errorf("invalid encrypt key") + } + return nil +} + +func validateNotEmpty(config *models.Config) error { + if config.ModuleLogger == "" { + return fmt.Errorf("ModuleLogger is empty") + } + if config.ExternalCfg.EncryptCommon.PrivKey == "" { + return fmt.Errorf("invalid private encrypt key") + } + if config.ExternalCfg.EncryptCommon.PubKey == "" { + return fmt.Errorf("invalid publice encrypt key") + } + + return nil +} + +func validateTG(notificationBotToken string, notificationRsPayChannel int64, notificationChannel int64) error { + if notificationBotToken == "" { + return fmt.Errorf("notificationBotToken is empty") + } + + // todo обдумать еще регулярку + pattern := `^\d+:.+$` + ok, err := regexp.MatchString(pattern, notificationBotToken) + if err != nil { + return fmt.Errorf("error validating notificationBotToken: %w", err) + } + if !ok { + return fmt.Errorf("invalid notificationBotToken format") + } + + if notificationChannel == 0 { + return fmt.Errorf("notificationChannel is not set") + } + if notificationRsPayChannel == 0 { + return fmt.Errorf("notificationRsPayChannel is not set") + } + + return nil +} diff --git a/cmd/validator/main_test.go b/cmd/validator/main_test.go new file mode 100644 index 0000000..23589c1 --- /dev/null +++ b/cmd/validator/main_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "gitea.pena/PenaSide/customer/internal/models" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestValidateTG(t *testing.T) { + err := validateTG("7127966184:AAG1steOCH4wDvHRe9QcsXJPS4dWRyRYsqg", -1002177203276, -1002177203276) + assert.NoError(t, err) +} + +func TestValidateMail(t *testing.T) { + err := validateMail(models.MailClientCfg{ + ApiUrl: "https://api.smtp.bz/v1/smtp/send", + Sender: "noreply@mailing.pena.digital", + ApiKey: "P0YsjUB137upXrr1NiJefHmXVKW1hmBWlpev", + MailAddress: "sells@pena.digital", + }) + assert.NoError(t, err) +} diff --git a/go.mod b/go.mod index 45f8c40..e7da33a 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( gitea.pena/PenaSide/common v0.0.0-20241126121130-cf56ae1e3fb2 gitea.pena/PenaSide/linters-golang v0.0.0-20241119212350-2759fa93724a gitea.pena/PenaSide/trashlog v0.0.0-20241119225515-2fd267647ca4 + github.com/caarlos0/env/v8 v8.0.0 github.com/go-resty/resty/v2 v2.11.0 github.com/gofiber/fiber/v2 v2.52.1 github.com/golang-jwt/jwt/v5 v5.2.0 diff --git a/go.sum b/go.sum index 457f5bc..edf27c0 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer5 github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/bkaradzic/go-lz4 v1.0.0 h1:RXc4wYsyz985CkXXeX04y4VnZFGG8Rd43pRaHsOXAKk= github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4= +github.com/caarlos0/env/v8 v8.0.0 h1:POhxHhSpuxrLMIdvTGARuZqR4Jjm8AYmoi/JKlcScs0= +github.com/caarlos0/env/v8 v8.0.0/go.mod h1:7K4wMY9bH0esiXSSHlfHLX5xKGQMnkH5Fk4TDSSSzfo= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 h1:F1EaeKL/ta07PY/k9Os/UFtwERei2/XzGemhpGnBKNg= diff --git a/internal/app/app.go b/internal/app/app.go index eac256e..59ebb6b 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -107,6 +107,14 @@ func Run(config *models.Config, logger *zap.Logger, build Build) (appErr error) return err } + if _, err = notificationBot.Send(tb.ChatID(config.NotificationRsPayChannel), fmt.Sprintf("PING MSG from %s", config.ModuleLogger)); err != nil { + return err + } + + if _, err = notificationBot.Send(tb.ChatID(config.NotificationChannel), fmt.Sprintf("PING MSG from %s", config.ModuleLogger)); err != nil { + return err + } + clients := initialize.NewClients(initialize.ClientsDeps{ Logger: logger, AuthURL: config.AuthMicroservice,