Skip to content
Snippets Groups Projects
utils.go 3 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
    Copyright (c) 2018 Vereign AG [https://www.vereign.com]
    
    This is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program. If not, see <http://www.gnu.org/licenses/>.
    */
    
    package handler
    
    import (
    	"crypto/rand"
    	"crypto/x509"
    	"errors"
    	"fmt"
    	"io"
    
    	"code.vereign.com/code/viam-apis/data-storage-agent/client"
    	"code.vereign.com/code/viam-apis/key-storage-agent/api"
    	"code.vereign.com/code/viam-apis/utils"
    
    	"encoding/pem"
    	"io/ioutil"
    
    )
    
    func generateUnusedUUID(client *client.DataStorageClientImpl) (string, error) {
    	count := 0
    	for {
    		uuid, err := newUUID()
    
    		// check that uuid is not used
    		data, _ := client.DoGetDataCall("keys", uuid+"/"+api.KeyType.String(api.KeyType_PRIVATE))
    
    
    Gospodin Bodurov's avatar
    Gospodin Bodurov committed
    		if data == nil || data.Errors != "" || err != nil {
    
    			return uuid, nil
    		}
    		if count >= 10 {
    			return "", errors.New("Could not generate unused UUID in 10 tries")
    		}
    		count++
    	}
    }
    
    func newUUID() (string, error) {
    	uuid := make([]byte, 16)
    	n, err := io.ReadFull(rand.Reader, uuid)
    	if n != len(uuid) || err != nil {
    		return "", err
    	}
    
    	uuid[8] = uuid[8]&^0xc0 | 0x80
    	uuid[6] = uuid[6]&^0xf0 | 0x40
    
    	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 {
    	if err != nil {
    		statusList = utils.AddStatus(statusList, "500", api.StatusType_ERROR, err.Error())
    	} else if errors != "" {
    		statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, errors)
    	}
    
    	return statusList
    }
    
    func readCertificateFromFile(fileName string) (*x509.Certificate, error) {
    	certificatePemBlock, err := readPemBlockFromFile(fileName)
    	if err != nil {
    		return nil, err
    	}
    
    	certificate, err := x509.ParseCertificate(certificatePemBlock.Bytes)
    	if err != nil {
    		return nil, err
    	}
    
    	return certificate, nil
    }
    
    func readPemBlockFromFile(fileName string) (*pem.Block, error) {
    	fileBytes, err := ioutil.ReadFile(fileName)
    	if err != nil {
    		return nil, err
    	}
    
    	certificatePemBlock, _ := pem.Decode(fileBytes)
    
    	return certificatePemBlock, nil
    }
    
    
    func getKey(client *client.DataStorageClientImpl, uuid string, keyType api.KeyType) (*api.Key, []*api.Status) {
    	statusList := []*api.Status{}
    
    	data, _ := client.DoGetDataCall("keys", uuid+"/"+api.KeyType.String(keyType))
    	if data.Errors != "" {
    		statusList = utils.AddStatus(statusList, "400", api.StatusType_ERROR, data.Errors)
    		return nil, statusList
    	}
    
    	key := &api.Key{}
    	proto.Unmarshal(data.Data.Data, key)
    
    	return key, nil
    }