Skip to content

Commit

Permalink
Add common lib for driver to handle local keypairs with cb-store
Browse files Browse the repository at this point in the history
  • Loading branch information
powerkimhub committed Dec 7, 2021
1 parent 97b6bd3 commit 5a5445d
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 0 deletions.
116 changes: 116 additions & 0 deletions cloud-control-manager/cloud-driver/common/InfoStore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// KeyPair <-> CB-Store Handler for Cloud Driver
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.11.

package common

import (
"fmt"

"github.com/cloud-barista/cb-store/utils"
"github.com/cloud-barista/cb-store"
icbs "github.com/cloud-barista/cb-store/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)

var store icbs.Store

const STORE_KEYPAIR_PREFIX string ="/driver-info-spaces/keypair"

func init() {
store = cbstore.GetStore()
}

// format
// /driver-info-spaces/keypair/{Param1}/{Param2}/{Param3} [Param4]
// /driver-info-spaces/keypair/{ProviderName}/{HashString}/{KeyPairNameId} [privateKey]
// ex) /driver-info-spaces/keypair/CLOUDIT/c4240bec42480e764a4381c10c92e2ce/keypair-0-c6ncl9aba5o081np93og [private key]

func insertInfo(providerName string, hashString string, keyPairNameId string, privateKey string) error {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString + "/" + keyPairNameId

err := store.Put(key, privateKey)
if err != nil {
//cblog.Error(err)
return err
}
return nil
}

// create KeyValue{KeyPairNameId, PrivateKey} List & return
func listInfo(providerName string, hashString string) ([]*irs.KeyValue, error) {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString
keyValueList, err := store.GetList(key, true)
if err != nil {
return nil, err
}
keyList := make([]*irs.KeyValue, len(keyValueList))
for count, kv := range keyValueList {
keyValue := &irs.KeyValue{
Key : utils.GetNodeValue(kv.Key, 5), // KeyPairNameId
Value : kv.Value, // private key
}
keyList[count] = keyValue
}

return keyList, nil
}

// create KeyValue{KeyPairNameId, PrivateKey} & return
func getInfo(providerName string, hashString string, keyPairNameId string) (*irs.KeyValue, error) {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString + "/" + keyPairNameId

// key is not the key of cb-store, so we have to use GetList()
keyValueList, err := store.GetList(key, true)
if err != nil {
return nil, err
}

if len(keyValueList) < 1 {
return nil, fmt.Errorf(keyPairNameId + ": does not exist!")
}

for _, kv := range keyValueList {
// keyValueList should have ~/keypair or ~/keypair-01
// so we have to check the sameness of keyPairNameId.
if utils.GetNodeValue(kv.Key, 5) == keyPairNameId {
keyValue := &irs.KeyValue{
Key : utils.GetNodeValue(kv.Key, 5), // KeyPairNameId
Value : kv.Value, // private key
}
return keyValue, nil
} // end of if
} // end of for

return nil, fmt.Errorf(keyPairNameId + ": does not exist!")
}

// 1. get the original Key.
// 2. delete the key.
func deleteInfo(providerName string, hashString string, keyPairNameId string) (bool, error) {
key := STORE_KEYPAIR_PREFIX + "/" + providerName + "/" + hashString + "/" + keyPairNameId

// key is not the key of cb-store, so we have to use GetList(
keyValueList, err := store.GetList(key, true)
if err != nil {
return false, err
}
for _, kv := range keyValueList {
// keyValueList should have ~/keypair or ~/keypair-01
// so we have to check the sameness of keyPairNameId.
if utils.GetNodeValue(kv.Key, 5) == keyPairNameId {
err = store.Delete(kv.Key)
if err != nil {
return false, err
}
return true, nil
}
}

return false, fmt.Errorf(keyPairNameId + ": does not exist!")
}

82 changes: 82 additions & 0 deletions cloud-control-manager/cloud-driver/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.12.
// by CB-Spider Team, 2021.08.

package common
Expand All @@ -15,9 +16,13 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"sync"
"io"
"io/ioutil"
"crypto/md5"

"golang.org/x/crypto/ssh"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)

// generate a KeyPair with 4KB length
Expand Down Expand Up @@ -58,6 +63,83 @@ func GenKeyPair() ([]byte, []byte, error) {
return privateKeyBytes, publicKeyBytes, nil
}


// Lock to store and read private key
var rwMutex sync.RWMutex

// ex)
// privateKey, publicKey, err := GenKeyPair()
//
// srcList[0] = credentialInfo.IdentityEndpoint
// srcList[1] = credentialInfo.AuthToken
// srcList[2] = credentialInfo.TenantId
// strHash, err := GenHash(srcList)
//
// AddKey("CLOUDIT", strHash, keyPairReqInfo.IId.NameId, privateKey)
func AddKey(providerName string, hashString string, keyPairNameId string, privateKey string) error {

rwMutex.Lock()
defer rwMutex.Unlock()

err := insertInfo(providerName, hashString, keyPairNameId, privateKey)
if err != nil {
return err
}
return nil
}

// return: []KeyValue{Key:KeyPairNameId, Value:PrivateKey}
func ListKey(providerName string, hashString string) ([]*irs.KeyValue, error) {

rwMutex.Lock()
defer rwMutex.Unlock()

keyValueList, err := listInfo(providerName, hashString)
if err != nil {
return nil, err
}

return keyValueList, nil
}

// return: KeyValue{Key:KeyPairNameId, Value:PrivateKey}
func GetKey(providerName string, hashString string, keyPairNameId string) (*irs.KeyValue, error) {

rwMutex.Lock()
defer rwMutex.Unlock()

keyValue, err := getInfo(providerName, hashString, keyPairNameId)
if err != nil {
return nil, err
}
return keyValue, nil
}

func DelKey(providerName string, hashString string, keyPairNameId string) error {

rwMutex.Lock()
defer rwMutex.Unlock()

_, err := deleteInfo(providerName, hashString, keyPairNameId)
if err != nil {
return err
}
return nil
}

func GenHash(sourceList []string) (string, error) {
var keyString string
for _, str := range sourceList {
keyString += str
}
hasher := md5.New()
_, err := io.WriteString(hasher, keyString)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}

// save a key to a file
func SaveKey(keyBytes []byte, targetFile string) error {
err := ioutil.WriteFile(targetFile, keyBytes, 0600)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2021.11.

package validatetest

import (
cdcom "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/common"
"testing"
"log"
)

func TestAddListGetDelete(t *testing.T) {

privateKey, _, err := cdcom.GenKeyPair()

strList:= []string{
"IdentityEndpoint-01",
"AuthToken-01",
"TenantId-01",
}
strHash, err := cdcom.GenHash(strList)

keyPairNameId := "keypair-0-c6ncl9aba5o081np93og"


// (1) insert-1
err = cdcom.AddKey("CLOUDIT", strHash, keyPairNameId, string(privateKey))
if err != nil {
log.Fatal("something failed!")
}

// (1) insert-2
privateKey, _, err = cdcom.GenKeyPair()
keyPairNameId = "keypair-1-c6ncl9aba5o081np93og"
err = cdcom.AddKey("CLOUDIT", strHash, keyPairNameId, string(privateKey))
if err != nil {
log.Fatal("something failed!")
}


// (2) list
keyValueList, err := cdcom.ListKey("CLOUDIT", strHash)
if err != nil {
log.Fatal("something failed!")
}
log.Println(keyValueList)


// (3) get
keyValue, err := cdcom.GetKey("CLOUDIT", strHash, keyPairNameId)
if err != nil {
log.Fatal("something failed!")
}
log.Println(keyValue)


// (4) delete
err = cdcom.DelKey("CLOUDIT", strHash, keyPairNameId)
if err != nil {
log.Fatal("something failed!")
}
}

0 comments on commit 5a5445d

Please sign in to comment.