Skip to content
Snippets Groups Projects
Commit fe9f2da2 authored by Olgun Cengiz's avatar Olgun Cengiz :drum:
Browse files

replaced DoGetDataCall

parent 12f75754
No related branches found
No related tags found
2 merge requests!48Master,!4716 refactor deprecated functions
...@@ -22,14 +22,13 @@ import ( ...@@ -22,14 +22,13 @@ import (
"errors" "errors"
"log" "log"
"strings" "strings"
"code.vereign.com/code/viam-apis/clientutils" "code.vereign.com/code/viam-apis/clientutils"
"github.com/golang/protobuf/proto"
keyutils "code.vereign.com/code/key-storage-agent/utils" keyutils "code.vereign.com/code/key-storage-agent/utils"
"code.vereign.com/code/viam-apis/authentication" "code.vereign.com/code/viam-apis/authentication"
"code.vereign.com/code/viam-apis/key-storage-agent/api" "code.vereign.com/code/viam-apis/key-storage-agent/api"
"code.vereign.com/code/viam-apis/utils" "code.vereign.com/code/viam-apis/utils"
"code.vereign.com/code/viam-apis/system"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
...@@ -99,25 +98,33 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest ...@@ -99,25 +98,33 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest
return getKeyResponse, nil return getKeyResponse, nil
} }
data, err := client.DoGetDataCall("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType)) key := &api.Key{}
hasData, errorsString, err := client.GetData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), key)
if err != nil { if err != nil {
log.Printf("grpc call DoGetDataCall to DataStorage failed: %s", err) log.Printf("grpc call GetData to DataStorage failed: %s", err)
getKeyResponse.Key = nil getKeyResponse.Key = nil
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList, getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList,
"500", api.StatusType_ERROR, err.Error()) "500", api.StatusType_ERROR, err.Error())
return getKeyResponse, nil return getKeyResponse, nil
} }
if data.Errors != "" { if errorsString != "" {
getKeyResponse.Key = nil getKeyResponse.Key = nil
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList, getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList,
"500", api.StatusType_ERROR, data.Errors) "500", api.StatusType_ERROR, errorsString)
} else { }
key := &api.Key{}
proto.Unmarshal(data.Data.Data, key) if !hasData {
getKeyResponse.Key = key log.Println("No such key " + in.Uuid)
getKeyResponse.Key = nil
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList,
"500", api.StatusType_ERROR, err.Error())
return getKeyResponse, nil
} }
getKeyResponse.Key = key
return getKeyResponse, nil return getKeyResponse, nil
} }
...@@ -141,23 +148,17 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest ...@@ -141,23 +148,17 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest
return setKeyResponse, nil return setKeyResponse, nil
} }
data, err := client.DoGetDataCall("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType)) key := &api.Key{}
_, _, err := client.GetData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), key)
if err != nil { if err != nil {
log.Printf("grpc call DoGetDataCall to DataStorage failed: %s", err) log.Printf("grpc call GetData to DataStorage failed: %s", err)
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList, setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList,
"500", api.StatusType_ERROR, err.Error()) "500", api.StatusType_ERROR, err.Error())
return setKeyResponse, nil return setKeyResponse, nil
} }
if data.Errors != "" { if len(key.Content) > 0 {
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList,
"400", api.StatusType_ERROR, data.Errors)
return setKeyResponse, nil
}
key := &api.Key{}
proto.Unmarshal(data.Data.Data, key)
if key != nil && key.Content != nil && len(key.Content) > 0 {
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList, setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList,
"400", api.StatusType_ERROR, "Key is already set") "400", api.StatusType_ERROR, "Key is already set")
return setKeyResponse, nil return setKeyResponse, nil
...@@ -176,7 +177,7 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest ...@@ -176,7 +177,7 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest
_, _, err = client.PutString(keyToKeyIdTable, keyContent, in.Uuid) _, _, err = client.PutString(keyToKeyIdTable, keyContent, in.Uuid)
if err != nil { if err != nil {
log.Printf("can't DoPutDataCallWithString: %s", err) log.Printf("can't PutString: %s", err)
return nil, err return nil, err
} }
} }
...@@ -236,10 +237,11 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK ...@@ -236,10 +237,11 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK
keyIdResponse := &api.GetKeyIdByKeyResponse{} keyIdResponse := &api.GetKeyIdByKeyResponse{}
response, err := dataStorageClient.DoGetDataCall(keyToKeyIdTable, in.PublicKey) stringMessage := &system.String{}
if err == nil && response.Data != nil && response.Data.Data != nil {
keyID := string(response.Data.Data) hasData, errorsString, err := dataStorageClient.GetData(keyToKeyIdTable, in.PublicKey, stringMessage)
keyIdResponse.KeyId = keyID if err == nil && errorsString == "" && hasData {
keyIdResponse.KeyId = stringMessage.Value
return keyIdResponse, nil return keyIdResponse, nil
} else { } else {
if err != nil { if err != nil {
...@@ -258,17 +260,21 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK ...@@ -258,17 +260,21 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK
for _, checkID := range entity.AuthenticationKeys { for _, checkID := range entity.AuthenticationKeys {
key := &api.Key{} key := &api.Key{}
data, err := dataStorageClient.DoGetDataCall("keys", checkID+"/"+api.KeyType_PUBLIC.String()) hasData, errorsString, err := dataStorageClient.GetData("keys", checkID+"/"+api.KeyType_PUBLIC.String(), key)
if err != nil { if err != nil {
log.Printf("grpc call GetKeyId to DataStorage failed: %s", err) log.Printf("grpc call GetData to DataStorage failed: %s", err)
return nil, err return nil, err
} }
if data.Errors != "" { if errorsString != "" {
return nil, errors.New(data.Errors) log.Printf("Error: %s", errorsString)
} else { return nil, errors.New(errorsString)
proto.Unmarshal(data.Data.Data, key) }
if !hasData {
log.Println("No such checkID " + checkID)
return nil, errors.New("No such checkID " + checkID)
} }
keyFromStorage := base64.StdEncoding.EncodeToString(key.Content) keyFromStorage := base64.StdEncoding.EncodeToString(key.Content)
...@@ -279,7 +285,7 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK ...@@ -279,7 +285,7 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK
_, _, err = dataStorageClient.PutString(keyToKeyIdTable, in.PublicKey, checkID) _, _, err = dataStorageClient.PutString(keyToKeyIdTable, in.PublicKey, checkID)
if err != nil { if err != nil {
log.Printf("can't DoPutDataCallWithString: %s", err) log.Printf("can't PutString: %s", err)
return nil, err return nil, err
} }
......
...@@ -36,7 +36,6 @@ import ( ...@@ -36,7 +36,6 @@ import (
emclient "code.vereign.com/code/viam-apis/entities-management-agent/client" emclient "code.vereign.com/code/viam-apis/entities-management-agent/client"
"code.vereign.com/code/viam-apis/key-storage-agent/api" "code.vereign.com/code/viam-apis/key-storage-agent/api"
"code.vereign.com/code/viam-apis/utils" "code.vereign.com/code/viam-apis/utils"
"github.com/golang/protobuf/proto"
) )
func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) { func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) {
...@@ -45,9 +44,10 @@ func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) { ...@@ -45,9 +44,10 @@ func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) {
uuid, err := NewUUID() uuid, err := NewUUID()
// check that uuid is not used // check that uuid is not used
data, _ := client.DoGetDataCall("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE)) key := &api.Key{}
if data == nil || data.Errors != "" || err != nil { hasData, errorsString, err := client.GetData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), key)
if err != nil && errorsString != "" && !hasData {
return uuid, nil return uuid, nil
} }
if count >= 10 { if count >= 10 {
...@@ -136,14 +136,23 @@ func ReadPemBlockFromFile(fileName string) (*pem.Block, error) { ...@@ -136,14 +136,23 @@ func ReadPemBlockFromFile(fileName string) (*pem.Block, error) {
func GetKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType) (*api.Key, []*api.Status) { func GetKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType) (*api.Key, []*api.Status) {
statusList := []*api.Status{} statusList := []*api.Status{}
data, _ := client.DoGetDataCall("keys", uuid+"/"+api.KeyType.String(keyType)) key := &api.Key{}
if data.Errors != "" {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, data.Errors) hasData, errorsString, err := client.GetData("keys", uuid+"/"+api.KeyType.String(keyType), key)
if err != nil {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errorsString)
return nil, statusList return nil, statusList
} }
key := &api.Key{} if errorsString != "" {
proto.Unmarshal(data.Data.Data, key) statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errorsString)
return nil, statusList
}
if !hasData {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errorsString)
return nil, statusList
}
return key, nil return key, nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment