Skip to content

Commit

Permalink
refactor: solve this issue #14
Browse files Browse the repository at this point in the history
  • Loading branch information
kenriortega committed Aug 23, 2021
1 parent 0e2cbb6 commit 21febfd
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/domain/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
47 changes: 33 additions & 14 deletions internal/proxy/domain/repositoryDB.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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"
)

// ProxyRepositoryStorage struct repository storage
type ProxyRepositoryStorage struct {
clientBadger *badger.DB
clientRdb *redis.Client
}

// NewProxyRepository return a new ProxyRepositoryStorage
Expand All @@ -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
Expand All @@ -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")

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/proxy/handlers/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}

Expand Down Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions internal/proxy/services/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/proxy/services/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion ngonx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21febfd

Please sign in to comment.