Skip to content
Snippets Groups Projects
Commit 5d87da64 authored by Damyan Mitev's avatar Damyan Mitev :beach:
Browse files

refator error handling

parent c71ba842
No related branches found
No related tags found
2 merge requests!57Refactor EMA error handling,!56refator error handling
...@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package handler package handler
import ( import (
"code.vereign.com/code/viam-apis/errors"
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
...@@ -29,7 +30,6 @@ import ( ...@@ -29,7 +30,6 @@ import (
keyutils "code.vereign.com/code/key-storage-agent/utils" keyutils "code.vereign.com/code/key-storage-agent/utils"
"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"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
...@@ -41,89 +41,75 @@ func (s *KeyStorageServerImpl) GenerateKeyPair(ctx context.Context, ...@@ -41,89 +41,75 @@ func (s *KeyStorageServerImpl) GenerateKeyPair(ctx context.Context,
client := keyutils.CreateDataStorageClient(auth) client := keyutils.CreateDataStorageClient(auth)
defer client.CloseClient() defer client.CloseClient()
generateKeyPairResponse := &api.GenerateKeyPairResponse{}
uuid, err := keyutils.GenerateUnusedUUID(client) uuid, err := keyutils.GenerateUnusedUUID(client)
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList, return nil, err
"500", api.StatusType_ERROR, err.Error())
} }
privateKeyBytes, publicKeyBytes, err := generateKeyPair(int(in.KeySize)) privateKeyBytes, publicKeyBytes, err := generateKeyPair(int(in.KeySize))
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList, return nil, err
"500", api.StatusType_ERROR, err.Error())
} }
aesKeyBytes, err := generateRandomSequence(256) aesKeyBytes, err := generateRandomSequence(256)
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList, return nil, err
"500", api.StatusType_ERROR, err.Error())
} }
encryptedPrivateKeyBytes, privateKeyNonce, err := aesEncrypt(aesKeyBytes, privateKeyBytes) encryptedPrivateKeyBytes, privateKeyNonce, err := aesEncrypt(aesKeyBytes, privateKeyBytes)
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList, return nil, err
"500", api.StatusType_ERROR, err.Error())
return generateKeyPairResponse, nil
} }
encryptedPrivateKey := &api.Key{Content: encryptedPrivateKeyBytes} encryptedPrivateKey := &api.Key{Content: encryptedPrivateKeyBytes}
result, errors, err := client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), encryptedPrivateKey) _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), encryptedPrivateKey)
generateKeyPairResponse.StatusList = keyutils.HandlePutDataErrors(generateKeyPairResponse.StatusList, errors, err) if err != nil {
return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE))
if generateKeyPairResponse.StatusList == nil || len(generateKeyPairResponse.StatusList) == 0 {
publicKey := &api.Key{Content: publicKeyBytes}
result, errors, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PUBLIC), publicKey)
generateKeyPairResponse.StatusList = keyutils.HandlePutDataErrors(generateKeyPairResponse.StatusList, errors, err)
} }
//duplicate logic of ReserveKeyUUID
if generateKeyPairResponse.StatusList == nil || len(generateKeyPairResponse.StatusList) == 0 { publicKey := &api.Key{Content: publicKeyBytes}
emptyKey := &api.Key{Content: []byte{}} _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PUBLIC), publicKey)
result, errors, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_CERTIFICATE), emptyKey) if err != nil {
generateKeyPairResponse.StatusList = keyutils.HandlePutDataErrors(generateKeyPairResponse.StatusList, errors, err) return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_PUBLIC))
} }
if generateKeyPairResponse.StatusList == nil || len(generateKeyPairResponse.StatusList) == 0 { //duplicate logic of ReserveKeyUUID
encryptedAesKeyBytes, err := rsaEncryptWithServerKey(s.VereignCertPEM, aesKeyBytes, []byte("aeskeys"))
if err != nil {
log.Printf("Error: %v", err)
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList,
"500", api.StatusType_ERROR, err.Error())
return generateKeyPairResponse, nil
}
encryptedAesKey := &api.Key{Content: encryptedAesKeyBytes} emptyKey := &api.Key{Content: []byte{}}
_, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_CERTIFICATE), emptyKey)
if err != nil {
return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_CERTIFICATE))
}
result, errors, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_AES), encryptedAesKey) encryptedAesKeyBytes, err := rsaEncryptWithServerKey(s.VereignCertPEM, aesKeyBytes, []byte("aeskeys"))
generateKeyPairResponse.StatusList = keyutils.HandlePutDataErrors(generateKeyPairResponse.StatusList, errors, err) if err != nil {
return nil, errors.WrapInternal(err, "Could not encrypt")
} }
if generateKeyPairResponse.StatusList == nil || len(generateKeyPairResponse.StatusList) == 0 { encryptedAesKey := &api.Key{Content: encryptedAesKeyBytes}
encryptedPrivateKeyNonceBytes, err := rsaEncryptWithServerKey(s.VereignCertPEM, privateKeyNonce, []byte("nonce"))
if err != nil {
log.Printf("Error: %v", err)
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList,
"500", api.StatusType_ERROR, err.Error())
return generateKeyPairResponse, nil
}
encryptedNonce := &api.Key{Content: encryptedPrivateKeyNonceBytes} _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_AES), encryptedAesKey)
if err != nil {
return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_AES))
}
result, errors, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_NONCE), encryptedNonce) encryptedPrivateKeyNonceBytes, err := rsaEncryptWithServerKey(s.VereignCertPEM, privateKeyNonce, []byte("nonce"))
generateKeyPairResponse.StatusList = keyutils.HandlePutDataErrors(generateKeyPairResponse.StatusList, errors, err) if err != nil {
return nil, errors.WrapInternal(err, "Could not encrypt private key")
} }
if generateKeyPairResponse.StatusList == nil || len(generateKeyPairResponse.StatusList) == 0 { encryptedNonce := &api.Key{Content: encryptedPrivateKeyNonceBytes}
generateKeyPairResponse.Uuid = uuid
generateKeyPairResponse.StatusList = utils.AddStatus(generateKeyPairResponse.StatusList, _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_NONCE), encryptedNonce)
"200", api.StatusType_INFO, result) if err != nil {
return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_NONCE))
} }
generateKeyPairResponse := &api.GenerateKeyPairResponse{Uuid:uuid}
return generateKeyPairResponse, nil return generateKeyPairResponse, nil
} }
......
...@@ -18,8 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -18,8 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package handler package handler
import ( import (
"code.vereign.com/code/viam-apis/errors"
"encoding/base64" "encoding/base64"
"errors"
"log" "log"
"strings" "strings"
...@@ -29,7 +29,6 @@ import ( ...@@ -29,7 +29,6 @@ import (
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"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
...@@ -73,7 +72,7 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest ...@@ -73,7 +72,7 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest
client := keyutils.CreateDataStorageClient(auth) client := keyutils.CreateDataStorageClient(auth)
defer client.CloseClient() defer client.CloseClient()
getKeyResponse := &api.GetKeyResponse{}
if in.KeyType == api.KeyType_CERTIFICATE && in.Uuid == "root" { if in.KeyType == api.KeyType_CERTIFICATE && in.Uuid == "root" {
key := &api.Key{} key := &api.Key{}
...@@ -89,44 +88,28 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest ...@@ -89,44 +88,28 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest
key.Content = s.VereignCertPEM key.Content = s.VereignCertPEM
key.Revoked = false key.Revoked = false
getKeyResponse.Key = key getKeyResponse := &api.GetKeyResponse{Key:key}
return getKeyResponse, nil return getKeyResponse, nil
} }
if in.KeyType == api.KeyType_KT_EMPTY { if in.KeyType == api.KeyType_KT_EMPTY {
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList, return nil, errors.NewUser("KeyType cannot be empty")
"400", api.StatusType_ERROR, "KeyType cannot be empty")
return getKeyResponse, errors.New("KeyType cannot be empty")
} }
key := &api.Key{} key := &api.Key{}
hasData, errorsString, err := client.GetData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), key) hasData, _, err := client.GetData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), key)
if err != nil { if err != nil {
log.Printf("grpc call GetData to DataStorage failed: %s", err) log.Printf("grpc call GetData to DataStorage failed: %s", err)
getKeyResponse.Key = nil return nil, err
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList,
"500", api.StatusType_ERROR, err.Error())
return getKeyResponse, err
}
if errorsString != "" {
getKeyResponse.Key = nil
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList,
"500", api.StatusType_ERROR, errorsString)
return nil, errors.New(errorsString)
} }
if !hasData { if !hasData {
log.Println("No such key " + in.Uuid) log.Println("No such key " + in.Uuid)
getKeyResponse.Key = nil return nil, errors.NewUser("No such key " + in.Uuid)
getKeyResponse.StatusList = utils.AddStatus(getKeyResponse.StatusList,
"500", api.StatusType_ERROR, err.Error())
return getKeyResponse, err
} }
getKeyResponse.Key = key getKeyResponse := &api.GetKeyResponse{Key:key}
return getKeyResponse, nil return getKeyResponse, nil
} }
...@@ -136,18 +119,12 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest ...@@ -136,18 +119,12 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest
client := keyutils.CreateDataStorageClient(auth) client := keyutils.CreateDataStorageClient(auth)
defer client.CloseClient() defer client.CloseClient()
setKeyResponse := &api.SetKeyResponse{}
if in.Uuid == "root" { if in.Uuid == "root" {
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList, return nil, errors.NewUser("Can not set root CA keys")
"400", api.StatusType_ERROR, "Can not set root CA keys")
return setKeyResponse, errors.New("Can not set root CA keys")
} }
if in.KeyType == api.KeyType_KT_EMPTY { if in.KeyType == api.KeyType_KT_EMPTY {
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList, return nil, errors.NewUser("KeyType cannot be empty")
"400", api.StatusType_ERROR, "KeyType cannot be empty")
return setKeyResponse, errors.New("KeyType cannot be empty")
} }
key := &api.Key{} key := &api.Key{}
...@@ -156,24 +133,17 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest ...@@ -156,24 +133,17 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest
_, _, err := client.GetData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), key) _, _, err := client.GetData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), key)
if err != nil { if err != nil {
log.Printf("grpc call GetData to DataStorage failed: %s", err) log.Printf("grpc call GetData to DataStorage failed: %s", err)
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList, return nil, errors.WrapInternalFormat(err, "grpc call GetData to DataStorage failed: %s", err)
"500", api.StatusType_ERROR, err.Error())
return setKeyResponse, err
} }
if len(key.Content) > 0 { if len(key.Content) > 0 {
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList, return nil, errors.NewUser("Key is already set")
"400", api.StatusType_ERROR, "Key is already set")
return setKeyResponse, errors.New("Key is already set")
} }
} }
result, errors, err := client.PutData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), in.Key) _, _, err := client.PutData("keys", in.Uuid+"/"+api.KeyType.String(in.KeyType), in.Key)
setKeyResponse.StatusList = keyutils.HandlePutDataErrors(setKeyResponse.StatusList, errors, err) if err != nil {
return nil, err
if setKeyResponse.StatusList == nil || len(setKeyResponse.StatusList) == 0 {
setKeyResponse.StatusList = utils.AddStatus(setKeyResponse.StatusList,
"200", api.StatusType_INFO, result)
} }
if in.KeyType == api.KeyType_PUBLIC { if in.KeyType == api.KeyType_PUBLIC {
...@@ -182,10 +152,11 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest ...@@ -182,10 +152,11 @@ 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 PutString: %s", err) log.Printf("can't PutString: %s", err)
return nil, err return nil, errors.WrapInternal(err, "can't PutString")
} }
} }
setKeyResponse := &api.SetKeyResponse{}
return setKeyResponse, nil return setKeyResponse, nil
} }
...@@ -195,38 +166,32 @@ func (s *KeyStorageServerImpl) ReserveKeyUUID(ctx context.Context, in *api.Reser ...@@ -195,38 +166,32 @@ func (s *KeyStorageServerImpl) ReserveKeyUUID(ctx context.Context, in *api.Reser
client := keyutils.CreateDataStorageClient(auth) client := keyutils.CreateDataStorageClient(auth)
defer client.CloseClient() defer client.CloseClient()
reserveKeyUUIDResponse := &api.ReserveKeyUUIDResponse{}
uuid, err := keyutils.GenerateUnusedUUID(client) uuid, err := keyutils.GenerateUnusedUUID(client)
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
reserveKeyUUIDResponse.StatusList = utils.AddStatus(reserveKeyUUIDResponse.StatusList, return nil, err
"500", api.StatusType_INFO, err.Error())
} }
emptyKey := &api.Key{ emptyKey := &api.Key{
Content: []byte{}, Content: []byte{},
} }
result, errors, err := client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), emptyKey) _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), emptyKey)
reserveKeyUUIDResponse.StatusList = keyutils.HandlePutDataErrors(reserveKeyUUIDResponse.StatusList, errors, err) if err != nil {
return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE))
if reserveKeyUUIDResponse.StatusList == nil || len(reserveKeyUUIDResponse.StatusList) == 0 {
result, errors, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PUBLIC), emptyKey)
reserveKeyUUIDResponse.StatusList = keyutils.HandlePutDataErrors(reserveKeyUUIDResponse.StatusList, errors, err)
} }
if reserveKeyUUIDResponse.StatusList == nil || len(reserveKeyUUIDResponse.StatusList) == 0 { _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PUBLIC), emptyKey)
result, errors, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_CERTIFICATE), emptyKey) if err != nil {
reserveKeyUUIDResponse.StatusList = keyutils.HandlePutDataErrors(reserveKeyUUIDResponse.StatusList, errors, err) return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_PUBLIC))
} }
if reserveKeyUUIDResponse.StatusList == nil || len(reserveKeyUUIDResponse.StatusList) == 0 { _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(api.KeyType_CERTIFICATE), emptyKey)
reserveKeyUUIDResponse.Uuid = uuid if err != nil {
reserveKeyUUIDResponse.StatusList = utils.AddStatus(reserveKeyUUIDResponse.StatusList, return nil, errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(api.KeyType_CERTIFICATE))
"200", api.StatusType_INFO, result)
} }
reserveKeyUUIDResponse := &api.ReserveKeyUUIDResponse{Uuid:uuid}
return reserveKeyUUIDResponse, nil return reserveKeyUUIDResponse, nil
} }
...@@ -302,5 +267,5 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK ...@@ -302,5 +267,5 @@ func (s *KeyStorageServerImpl) GetKeyId(ctx context.Context, in *api.GetKeyIdByK
func (s *KeyStorageServerImpl) GetVersionKSA(ctx context.Context, in *api.GetVersionKSAMessage) (*api.GetVersionKSAResponseMessage, error) { func (s *KeyStorageServerImpl) GetVersionKSA(ctx context.Context, in *api.GetVersionKSAMessage) (*api.GetVersionKSAResponseMessage, error) {
log.Println("Version: " + version) log.Println("Version: " + version)
return &api.GetVersionKSAResponseMessage{Version: version, Errors: ""}, nil return &api.GetVersionKSAResponseMessage{Version: version}, nil
} }
...@@ -20,105 +20,104 @@ package handler ...@@ -20,105 +20,104 @@ package handler
import ( import (
keyutils "code.vereign.com/code/key-storage-agent/utils" keyutils "code.vereign.com/code/key-storage-agent/utils"
"code.vereign.com/code/viam-apis/data-storage-agent/client" "code.vereign.com/code/viam-apis/data-storage-agent/client"
"code.vereign.com/code/viam-apis/errors"
"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"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
func (s *KeyStorageServerImpl) SetAuthorized(ctx context.Context, in *api.SetAuthorizedRequest) (*api.SetAuthorizedResponse, error) { func (s *KeyStorageServerImpl) SetAuthorized(ctx context.Context, in *api.SetAuthorizedRequest) (*api.SetAuthorizedResponse, error) {
setAuthorizedResponse := &api.SetAuthorizedResponse{}
setAuthorizedResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Unauthorized = !in.Value }) err := s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Unauthorized = !in.Value })
if setAuthorizedResponse.StatusList != nil { if err != nil {
return setAuthorizedResponse, nil return nil, errors.WrapInternal(err, "Could not authorize keys")
} }
setAuthorizedResponse.StatusList = utils.AddStatus(setAuthorizedResponse.StatusList, "200", api.StatusType_INFO, "Keys authorization updated")
setAuthorizedResponse := &api.SetAuthorizedResponse{}
return setAuthorizedResponse, nil return setAuthorizedResponse, nil
} }
func (s *KeyStorageServerImpl) Destroy(ctx context.Context, in *api.DestroyRequest) (*api.DestroyResponse, error) { func (s *KeyStorageServerImpl) Destroy(ctx context.Context, in *api.DestroyRequest) (*api.DestroyResponse, error) {
destroyResponse := &api.DestroyResponse{}
destroyResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Destroyed = true }) err := s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Destroyed = true })
if destroyResponse.StatusList != nil { if err != nil {
return destroyResponse, nil return nil, errors.WrapInternal(err, "Could not destroy keys")
} }
destroyResponse.StatusList = utils.AddStatus(destroyResponse.StatusList, "200", api.StatusType_INFO, "Keys destroyed")
destroyResponse := &api.DestroyResponse{}
return destroyResponse, nil return destroyResponse, nil
} }
func (s *KeyStorageServerImpl) Revoke(ctx context.Context, in *api.RevokeRequest) (*api.RevokeResponse, error) { func (s *KeyStorageServerImpl) Revoke(ctx context.Context, in *api.RevokeRequest) (*api.RevokeResponse, error) {
revokeResponse := &api.RevokeResponse{}
revokeResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Revoked = true }) err := s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Revoked = true })
if revokeResponse.StatusList != nil { if err != nil {
return revokeResponse, nil return nil, errors.WrapInternal(err, "Could not revoke keys")
} }
revokeResponse.StatusList = utils.AddStatus(revokeResponse.StatusList, "200", api.StatusType_INFO, "Keys revoked")
revokeResponse := &api.RevokeResponse{}
return revokeResponse, nil return revokeResponse, nil
} }
func (s *KeyStorageServerImpl) Suspend(ctx context.Context, in *api.SuspendRequest) (*api.SuspendResponse, error) { func (s *KeyStorageServerImpl) Suspend(ctx context.Context, in *api.SuspendRequest) (*api.SuspendResponse, error) {
suspendResponse := &api.SuspendResponse{}
suspendResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Suspended = true }) err := s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Suspended = true })
if suspendResponse.StatusList != nil { if err != nil {
return suspendResponse, nil return nil, errors.WrapInternal(err, "Could not suspend keys")
} }
suspendResponse.StatusList = utils.AddStatus(suspendResponse.StatusList, "200", api.StatusType_INFO, "Keys suspended")
suspendResponse := &api.SuspendResponse{}
return suspendResponse, nil return suspendResponse, nil
} }
func (s *KeyStorageServerImpl) Resume(ctx context.Context, in *api.ResumeRequest) (*api.ResumeResponse, error) { func (s *KeyStorageServerImpl) Resume(ctx context.Context, in *api.ResumeRequest) (*api.ResumeResponse, error) {
resumeResponse := &api.ResumeResponse{}
resumeResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Suspended = false }) err := s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Suspended = false })
if resumeResponse.StatusList != nil { if err != nil {
return resumeResponse, nil return nil, errors.WrapInternal(err, "Could not resume keys")
} }
resumeResponse.StatusList = utils.AddStatus(resumeResponse.StatusList, "200", api.StatusType_INFO, "Keys resumed")
resumeResponse := &api.ResumeResponse{}
return resumeResponse, nil return resumeResponse, nil
} }
func (s *KeyStorageServerImpl) Rename(ctx context.Context, in *api.RenameRequest) (*api.RenameResponse, error) { func (s *KeyStorageServerImpl) Rename(ctx context.Context, in *api.RenameRequest) (*api.RenameResponse, error) {
renameResponse := &api.RenameResponse{}
renameResponse.StatusList = s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Name = in.Name }) err := s.updateAll(ctx, in.Uuid, func(k *api.Key) { k.Name = in.Name })
if renameResponse.StatusList != nil { if err != nil {
return renameResponse, nil return nil, errors.WrapInternal(err, "Could not rename keys")
} }
renameResponse.StatusList = utils.AddStatus(renameResponse.StatusList, "200", api.StatusType_INFO, "Keys renamed")
renameResponse := &api.RenameResponse{}
return renameResponse, nil return renameResponse, nil
} }
func (s *KeyStorageServerImpl) updateAll(ctx context.Context, uuid string, update func(*api.Key)) []*api.Status { func (s *KeyStorageServerImpl) updateAll(ctx context.Context, uuid string, updateFunc func(*api.Key)) error {
auth := s.CreateAuthentication(ctx) auth := s.CreateAuthentication(ctx)
client := keyutils.CreateDataStorageClient(auth) client := keyutils.CreateDataStorageClient(auth)
defer client.CloseClient() defer client.CloseClient()
for _, kType := range []api.KeyType{api.KeyType_PRIVATE, api.KeyType_PUBLIC, api.KeyType_CERTIFICATE} { for _, kType := range []api.KeyType{api.KeyType_PRIVATE, api.KeyType_PUBLIC, api.KeyType_CERTIFICATE} {
statusList := updateKey(client, uuid, kType, update) err := updateKey(client, uuid, kType, updateFunc)
if statusList != nil { if err != nil {
return statusList return err
} }
} }
return nil return nil
} }
func updateKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType, update func(*api.Key)) []*api.Status { func updateKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType, updateFunc func(*api.Key)) error {
key, statusList := keyutils.GetKey(client, uuid, keyType) key, err := keyutils.GetKey(client, uuid, keyType)
if statusList != nil { if err != nil {
return statusList return err
} }
update(key) updateFunc(key)
_, errors, err := client.PutData("keys", uuid+"/"+api.KeyType.String(keyType), key) _, _, err = client.PutData("keys", uuid+"/"+api.KeyType.String(keyType), key)
statusList = keyutils.HandlePutDataErrors(statusList, errors, err) if err != nil {
if statusList != nil && len(statusList) > 0 { return errors.WrapInternalFormat(err, "Could not store key %s", uuid+"/"+api.KeyType.String(keyType))
return statusList
} }
return nil return nil
......
...@@ -18,9 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -18,9 +18,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package utils package utils
import ( import (
"code.vereign.com/code/viam-apis/errors"
"crypto/rand" "crypto/rand"
"crypto/x509" "crypto/x509"
"errors"
"fmt" "fmt"
"io" "io"
"log" "log"
...@@ -35,7 +35,6 @@ import ( ...@@ -35,7 +35,6 @@ import (
dsclient "code.vereign.com/code/viam-apis/data-storage-agent/client" dsclient "code.vereign.com/code/viam-apis/data-storage-agent/client"
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"
) )
func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) { func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) {
...@@ -47,7 +46,10 @@ func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) { ...@@ -47,7 +46,10 @@ func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) {
key := &api.Key{} key := &api.Key{}
hasData, _, err := client.GetData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), key) hasData, _, err := client.GetData("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE), key)
if err != nil || !hasData { if err != nil {
return "", errors.WrapInternal(err, "Could not generate unused UUID")
}
if !hasData {
return uuid, nil return uuid, nil
} }
if count >= 10 { if count >= 10 {
...@@ -57,6 +59,7 @@ func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) { ...@@ -57,6 +59,7 @@ func GenerateUnusedUUID(client *client.DataStorageClientImpl) (string, error) {
} }
} }
//TODO create one single function to create UUIDs
func NewUUID() (string, error) { func NewUUID() (string, error) {
uuid := make([]byte, 16) uuid := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, uuid) n, err := io.ReadFull(rand.Reader, uuid)
...@@ -70,16 +73,16 @@ func NewUUID() (string, error) { ...@@ -70,16 +73,16 @@ func NewUUID() (string, error) {
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
} }
func HandlePutDataErrors(statusList []*api.Status, errors string, err error) []*api.Status { //func HandlePutDataErrors(statusList []*api.Status, errors string, err error) []*api.Status {
if err != nil { // if err != nil {
log.Printf("Error: %v", err) // log.Printf("Error: %v", err)
statusList = utils.AddStatus(statusList, "500", api.StatusType_ERROR, err.Error()) // statusList = utils.AddStatus(statusList, "500", api.StatusType_ERROR, err.Error())
} else if errors != "" { // } else if errors != "" {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errors) // statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errors)
} // }
//
return statusList // return statusList
} //}
func ReadCertificateFromPEM(pemString []byte) (*x509.Certificate, error) { func ReadCertificateFromPEM(pemString []byte) (*x509.Certificate, error) {
certificatePemBlock, err := ReadPemBlockFromBytes(pemString) certificatePemBlock, err := ReadPemBlockFromBytes(pemString)
...@@ -133,28 +136,18 @@ func ReadPemBlockFromFile(fileName string) (*pem.Block, error) { ...@@ -133,28 +136,18 @@ func ReadPemBlockFromFile(fileName string) (*pem.Block, error) {
return certificatePemBlock, nil return certificatePemBlock, nil
} }
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, error) {
statusList := []*api.Status{}
key := &api.Key{} key := &api.Key{}
_, errorsString, err := client.GetData("keys", uuid+"/"+api.KeyType.String(keyType), key) hasData, _, err := client.GetData("keys", uuid+"/"+api.KeyType.String(keyType), key)
if err != nil { if err != nil {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errorsString) return nil, errors.WrapInternalFormat(err, "Could not get key %s", uuid+"/"+api.KeyType.String(keyType))
return nil, statusList
}
/*
if errorsString != "" {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errorsString)
return nil, statusList
} }
if !hasData { if !hasData {
statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errorsString) err = errors.New("No data")
return nil, statusList return nil, errors.WrapUserFormat(err, "Could not get key %s", uuid+"/"+api.KeyType.String(keyType))
} }
*/
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