diff --git a/cmd/cli/proxy.go b/cmd/cli/proxy.go index 31ebcaa..07a826c 100644 --- a/cmd/cli/proxy.go +++ b/cmd/cli/proxy.go @@ -47,7 +47,7 @@ func StartProxy( for _, endpoints := range config.ProxyGateway.EnpointsProxy { - h.ProxyGateway(endpoints, key, securityType) + h.ProxyGateway(endpoints, engine, key, securityType) } if config.ProxySSL.Enable { diff --git a/internal/proxy/domain/proxy.go b/internal/proxy/domain/proxy.go index 3600634..887dcb7 100644 --- a/internal/proxy/domain/proxy.go +++ b/internal/proxy/domain/proxy.go @@ -17,5 +17,5 @@ type Endpoint struct { // ProxyRepository interface type ProxyRepository interface { SaveKEY(string, string, string) error - GetKEY(string) (string, error) + GetKEY(string, string) (string, error) } diff --git a/internal/proxy/domain/repositoryDB.go b/internal/proxy/domain/repositoryDB.go index d19f559..fddebe0 100644 --- a/internal/proxy/domain/repositoryDB.go +++ b/internal/proxy/domain/repositoryDB.go @@ -1,10 +1,12 @@ package proxy import ( + "context" "fmt" "os" badger "github.com/dgraph-io/badger/v3" + "github.com/go-redis/redis/v8" "github.com/kenriortega/ngonx/pkg/errors" "github.com/kenriortega/ngonx/pkg/logger" ) @@ -12,6 +14,7 @@ import ( // ProxyRepositoryStorage struct repository storage type ProxyRepositoryStorage struct { clientBadger *badger.DB + clientRdb *redis.Client } // NewProxyRepository return a new ProxyRepositoryStorage @@ -22,6 +25,8 @@ func NewProxyRepository(clients ...interface{}) ProxyRepositoryStorage { switch c := c.(type) { case *badger.DB: proxyRepositoryDB.clientBadger = c + case *redis.Client: + proxyRepositoryDB.clientRdb = c } } return proxyRepositoryDB @@ -45,6 +50,11 @@ func (r ProxyRepositoryStorage) SaveKEY(engine, key, apikey string) error { } return nil + case "redis": + if _, err := r.clientRdb.HSet(context.TODO(), key, apikey).Result(); err != nil { + logger.LogError(err.Error()) + } + // r.clientRdb.Expire(context.TODO(), key, 24*time.Hour) case "local": f, err := os.Create("./apikey") @@ -69,24 +79,33 @@ func (r ProxyRepositoryStorage) SaveKEY(engine, key, apikey string) error { } // GetKEY get key from the database -func (r ProxyRepositoryStorage) GetKEY(key string) (string, error) { +func (r ProxyRepositoryStorage) GetKEY(engine, key string) (string, error) { var apikey string - fmt.Println(key) - if err := r.clientBadger.View(func(txn *badger.Txn) error { - item, err := txn.Get([]byte(key)) - if err != nil { - return errors.ErrGetkeyTX - } - if err := item.Value(func(value []byte) error { - apikey = string(value) + + switch engine { + case "badger": + if err := r.clientBadger.View(func(txn *badger.Txn) error { + item, err := txn.Get([]byte(key)) + if err != nil { + return errors.ErrGetkeyTX + } + if err := item.Value(func(value []byte) error { + apikey = string(value) + return nil + }); err != nil { + return errors.ErrGetkeyValue + } + return nil }); err != nil { - return errors.ErrGetkeyValue + return "", errors.ErrGetkeyView } - - return nil - }); err != nil { - return "", errors.ErrGetkeyView + case "redis": + value, err := r.clientRdb.Get(context.TODO(), key).Result() + if err == redis.Nil || err != nil { + return "", err + } + apikey = value } return apikey, nil diff --git a/internal/proxy/handlers/proxy.go b/internal/proxy/handlers/proxy.go index ae6713c..7d60f4c 100644 --- a/internal/proxy/handlers/proxy.go +++ b/internal/proxy/handlers/proxy.go @@ -47,7 +47,7 @@ func (ph *ProxyHandler) SaveSecretKEY(engine, key, apikey string) { } // ProxyGateway handler for management all request -func (ph *ProxyHandler) ProxyGateway(endpoints domain.ProxyEndpoint, key, securityType string) { +func (ph *ProxyHandler) ProxyGateway(endpoints domain.ProxyEndpoint, engine, key, securityType string) { for _, endpoint := range endpoints.Endpoints { target, err := url.Parse( @@ -69,7 +69,7 @@ func (ph *ProxyHandler) ProxyGateway(endpoints domain.ProxyEndpoint, key, securi err := checkJWTSecretKeyFromRequest(req, key) proxy.ModifyResponse = modifyResponse(err) case "apikey": - err := checkAPIKEYSecretKeyFromRequest(req, ph, key) + err := checkAPIKEYSecretKeyFromRequest(req, ph, engine, key) proxy.ModifyResponse = modifyResponse(err) } @@ -154,8 +154,8 @@ func checkJWTSecretKeyFromRequest(req *http.Request, key string) error { } // checkAPIKEYSecretKeyFromRequest check apikey from request -func checkAPIKEYSecretKeyFromRequest(req *http.Request, ph *ProxyHandler, key string) error { - apikey, err := ph.Service.GetKEY(key) +func checkAPIKEYSecretKeyFromRequest(req *http.Request, ph *ProxyHandler, engine, key string) error { + apikey, err := ph.Service.GetKEY(engine, key) header := req.Header.Get("X-API-KEY") // pass to constants if err != nil { logger.LogError(errors.ErrGetkeyView.Error()) diff --git a/internal/proxy/services/proxy.go b/internal/proxy/services/proxy.go index add0c50..4237ac9 100644 --- a/internal/proxy/services/proxy.go +++ b/internal/proxy/services/proxy.go @@ -7,7 +7,7 @@ import ( // ProxyService interface service for proxy repository funcionalities type ProxyService interface { SaveSecretKEY(string, string, string) error - GetKEY(string) (string, error) + GetKEY(string, string) (string, error) } // DefaultProxyService struct for management proxy repository @@ -31,8 +31,8 @@ func (s DefaultProxyService) SaveSecretKEY(engine, key, apikey string) (string, } // GetKEY get key -func (s DefaultProxyService) GetKEY(key string) (string, error) { - result, err := s.repo.GetKEY(key) +func (s DefaultProxyService) GetKEY(engine, key string) (string, error) { + result, err := s.repo.GetKEY(engine, key) if err != nil { return "failed", err } diff --git a/internal/proxy/services/proxy_test.go b/internal/proxy/services/proxy_test.go index f53b87d..c954291 100644 --- a/internal/proxy/services/proxy_test.go +++ b/internal/proxy/services/proxy_test.go @@ -21,7 +21,7 @@ func Test_SaveSecretKEY(t *testing.T) { func Test_GetKEY(t *testing.T) { clientBadger := badgerdb.GetBadgerDB(false) proxyRepository = domain.NewProxyRepository(clientBadger) - result, err := proxyRepository.GetKEY("key") + result, err := proxyRepository.GetKEY("badger", "key") if err != nil { t.Error("Error to created key") } diff --git a/ngonx.yaml b/ngonx.yaml index 4b4605c..d7a73c3 100644 --- a/ngonx.yaml +++ b/ngonx.yaml @@ -17,7 +17,7 @@ proxy: crt_file: ./ssl/cert.pem key_file: ./ssl/key.pem cache_proxy: - engine: badger # local|badgerDB|redis + engine: badger # badgerDB|redis key: secretKey security: type: apikey # apikey|jwt|none