From a3bc32164df4c95b67e8055e550424b5848c89db Mon Sep 17 00:00:00 2001 From: potsables Date: Mon, 18 Feb 2019 15:43:52 -0800 Subject: [PATCH] api/v2: add in primary and secondary krab clients --- api/v2/api.go | 11 +++++++---- api/v2/routes_utils.go | 14 +++++++++++--- api/v2/types.go | 7 +++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/api/v2/api.go b/api/v2/api.go index e8af18548..f97044167 100644 --- a/api/v2/api.go +++ b/api/v2/api.go @@ -9,7 +9,6 @@ import ( "strconv" "time" - clients "github.com/RTradeLtd/Temporal/grpc-clients" gClients "github.com/RTradeLtd/Temporal/grpc-clients" "github.com/streadway/amqp" @@ -47,7 +46,7 @@ var ( type API struct { ipfs rtfs.Manager ipfsCluster *rtfscluster.ClusterManager - keys *clients.KaasClient + keys keys r *gin.Engine cfg *config.TemporalConfig dbm *database.Manager @@ -170,7 +169,11 @@ func new(cfg *config.TemporalConfig, router *gin.Engine, l *zap.SugaredLogger, c Blockchain: networkVersion, Token: cfg.APIKeys.ChainRider, }) - keys, err := gClients.NewKaasClient(cfg.Services, false) + kb1, err := gClients.NewKaasClient(cfg.Services, false) + if err != nil { + return nil, err + } + kb2, err := gClients.NewKaasClient(cfg.Services, true) if err != nil { return nil, err } @@ -219,7 +222,7 @@ func new(cfg *config.TemporalConfig, router *gin.Engine, l *zap.SugaredLogger, c return &API{ ipfs: ipfs, ipfsCluster: ipfsCluster, - keys: keys, + keys: keys{kb1: kb1, kb2: kb2}, cfg: cfg, service: "api", r: router, diff --git a/api/v2/routes_utils.go b/api/v2/routes_utils.go index 94c465e0e..42a87f264 100644 --- a/api/v2/routes_utils.go +++ b/api/v2/routes_utils.go @@ -122,7 +122,7 @@ func (api *API) exportKey(c *gin.Context) { return } // get private key from krab keystore - resp, err := api.keys.GetPrivateKey(context.Background(), &pb.KeyGet{Name: keyName}) + resp, err := api.keys.kb1.GetPrivateKey(context.Background(), &pb.KeyGet{Name: keyName}) if err != nil { api.LogError(c, err, eh.KeyExportError)(http.StatusBadRequest) return @@ -133,8 +133,16 @@ func (api *API) exportKey(c *gin.Context) { api.LogError(c, err, eh.KeyExportError)(http.StatusBadRequest) return } - // after successful parsing delete key from krab - if resp, err := api.keys.DeletePrivateKey(context.Background(), &pb.KeyDelete{Name: keyName}); err != nil { + // after successful parsing delete key from krab primary + if resp, err := api.keys.kb1.DeletePrivateKey(context.Background(), &pb.KeyDelete{Name: keyName}); err != nil { + api.LogError(c, err, "failed to delete key")(http.StatusBadRequest) + return + } else if resp.Status != "private key deleted" { + Fail(c, errors.New("failed to delete private key")) + return + } + // after successful parsing delete key from krab fallback + if resp, err := api.keys.kb2.DeletePrivateKey(context.Background(), &pb.KeyDelete{Name: keyName}); err != nil { api.LogError(c, err, "failed to delete key")(http.StatusBadRequest) return } else if resp.Status != "private key deleted" { diff --git a/api/v2/types.go b/api/v2/types.go index f761003b9..ea8e68c17 100644 --- a/api/v2/types.go +++ b/api/v2/types.go @@ -1,6 +1,7 @@ package v2 import ( + clients "github.com/RTradeLtd/Temporal/grpc-clients" "github.com/RTradeLtd/Temporal/queue" ) @@ -20,3 +21,9 @@ type queues struct { dash *queue.Manager eth *queue.Manager } + +// kaas key managers +type keys struct { + kb1 *clients.KaasClient + kb2 *clients.KaasClient +}