Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TT-13513 TT-12767 TT-12768 ensure to save oauth clients locally when pulled from rpc #6740

Merged
Merged
2 changes: 1 addition & 1 deletion certs/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func NewSlaveCertManager(localStorage, rpcStorage storage.Handler, secret string
}

mdcbStorage := storage.NewMdcbStorage(localStorage, rpcStorage, log)
mdcbStorage.CallbackonPullfromRPC = &callbackOnPullCertFromRPC
mdcbStorage.CallbackOnPullCertificateFromRPC = &callbackOnPullCertFromRPC
sredxny marked this conversation as resolved.
Show resolved Hide resolved

cm.storage = mdcbStorage
return cm
Expand Down
100 changes: 68 additions & 32 deletions storage/mdcb_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ import (
)

type MdcbStorage struct {
local Handler
rpc Handler
logger *logrus.Entry
CallbackonPullfromRPC *func(key string, val string) error
local Handler
rpc Handler
logger *logrus.Entry
CallbackOnPullCertificateFromRPC *func(key string, val string) error
sredxny marked this conversation as resolved.
Show resolved Hide resolved
sredxny marked this conversation as resolved.
Show resolved Hide resolved
}

const (
resourceOauthClient = "Oauth Client"
sredxny marked this conversation as resolved.
Show resolved Hide resolved
resourceCertificate = "Certificate"
resourceApiKey = "ApiKey"
resourceKey = "Key"
)

func NewMdcbStorage(local, rpc Handler, log *logrus.Entry) *MdcbStorage {
return &MdcbStorage{
local: local,
Expand All @@ -23,45 +30,27 @@ func NewMdcbStorage(local, rpc Handler, log *logrus.Entry) *MdcbStorage {
}

func (m MdcbStorage) GetKey(key string) (string, error) {
var val string
var err error

if m.local == nil {
return m.rpc.GetKey(key)
}

val, err = m.local.GetKey(key)
if err != nil {
m.logger.Infof("Retrieving key from rpc.")
val, err = m.rpc.GetKey(key)

if err != nil {
resourceType := getResourceType(key)
m.logger.Errorf("cannot retrieve %v from rpc: %v", resourceType, err.Error())
return val, err
}

if m.CallbackonPullfromRPC != nil {
err := (*m.CallbackonPullfromRPC)(key, val)
if err != nil {
m.logger.Error(err)
}
if m.local != nil {
val, err := m.getFromLocal(key)
if err == nil {
return val, nil
}
m.logger.Debugf("Key not present locally, pulling from rpc layer: %v", err)
}

return val, err
return m.getFromRPCAndCache(key)
}

func getResourceType(key string) string {
switch {
case strings.Contains(key, "oauth-clientid."):
return "Oauth Client"
return resourceOauthClient
case strings.HasPrefix(key, "cert"):
return "certificate"
return resourceCertificate
case strings.HasPrefix(key, "apikey"):
return "api key"
return resourceApiKey
default:
return "key"
return resourceKey
}
}

Expand Down Expand Up @@ -256,3 +245,50 @@ func (m MdcbStorage) Exists(key string) (bool, error) {

return foundLocal && foundRpc, nil
}

// cacheCertificate saves locally resourceCertificate after pull from rpc
func (m MdcbStorage) cacheCertificate(key, val string) error {
var err error
if m.CallbackOnPullCertificateFromRPC != nil {
err = (*m.CallbackOnPullCertificateFromRPC)(key, val)
if err != nil {
m.logger.WithError(err).Error("cannot save resourceCertificate locally")
}
}
return err
sredxny marked this conversation as resolved.
Show resolved Hide resolved
}

// cacheOAuthClient saved oauth data in local storage after pull from rpc
func (m MdcbStorage) cacheOAuthClient(key, val string) error {
return m.local.SetKey(key, val, 0)
}

// processResourceByType based on the type of key it will trigger the proper
// caching mechanism
func (m MdcbStorage) processResourceByType(key, val string) error {

resourceType := getResourceType(key)
switch resourceType {
case resourceOauthClient:
return m.cacheOAuthClient(key, val)
case resourceCertificate:
return m.cacheCertificate(key, val)
}
return nil
}

// getFromRPCAndCache pulls a resource from rpc and stores it in local redis for caching
func (m MdcbStorage) getFromRPCAndCache(key string) (string, error) {
val, err := m.rpc.GetKey(key)
if err != nil {
return "", err
}

err = m.processResourceByType(key, val)
return val, err
}

// getFromLocal get a key from local storage
func (m MdcbStorage) getFromLocal(key string) (string, error) {
return m.local.GetKey(key)
}
Loading
Loading