Skip to content

Commit

Permalink
[Azure] - Local 키페어 관리 로직 제거 (이슈 cloud-barista#480)
Browse files Browse the repository at this point in the history
Local 키페어 관리 로직 제거  (이슈 cloud-barista#480)
- Azure SSH-key 리소스 적용 로직 추가  및 local 키페어 로직 삭제
  • Loading branch information
tjeom-inno committed Nov 2, 2021
1 parent 78882af commit 7284a5f
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 182 deletions.
20 changes: 19 additions & 1 deletion cloud-control-manager/cloud-driver/drivers/azure/AzureDriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (driver *AzureDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (ico
if err != nil {
return nil, err
}
Ctx, sshKeyClient, err := getSshKeyClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}

iConn := azcon.AzureCloudConnection{
CredentialInfo: connectionInfo.CredentialInfo,
Expand All @@ -126,6 +130,7 @@ func (driver *AzureDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (ico
VMImageClient: VMImageClient,
DiskClient: DiskClient,
VmSpecClient: VmSpecClient,
SshKeyClient: sshKeyClient,
}
return &iConn, nil
}
Expand Down Expand Up @@ -269,6 +274,20 @@ func getSubnetClient(credential idrv.CredentialInfo) (context.Context, *network.
return ctx, &subnetClient, nil
}

func getSshKeyClient(credential idrv.CredentialInfo) (context.Context, *compute.SSHPublicKeysClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}

sshClientClient := compute.NewSSHPublicKeysClient(credential.SubscriptionId)
sshClientClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)

return ctx, &sshClientClient, nil
}

func getVMImageClient(credential idrv.CredentialInfo) (context.Context, *compute.VirtualMachineImagesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
Expand Down Expand Up @@ -310,4 +329,3 @@ func getVmSpecClient(credential idrv.CredentialInfo) (context.Context, *compute.

return ctx, &vmSpecClient, nil
}

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type AzureCloudConnection struct {
SubnetClient *network.SubnetsClient
DiskClient *compute.DisksClient
VmSpecClient *compute.VirtualMachineSizesClient
SshKeyClient *compute.SSHPublicKeysClient
}

func (cloudConn *AzureCloudConnection) CreateImageHandler() (irs.ImageHandler, error) {
Expand Down Expand Up @@ -71,7 +72,7 @@ func (cloudConn *AzureCloudConnection) CreateSecurityHandler() (irs.SecurityHand

func (cloudConn *AzureCloudConnection) CreateKeyPairHandler() (irs.KeyPairHandler, error) {
cblogger.Info("Azure Cloud Driver: called CreateKeyPairHandler()!")
keypairHandler := azrs.AzureKeyPairHandler{cloudConn.CredentialInfo, cloudConn.Region}
keypairHandler := azrs.AzureKeyPairHandler{cloudConn.CredentialInfo, cloudConn.Region, cloudConn.Ctx, cloudConn.SshKeyClient}
return &keypairHandler, nil
}

Expand All @@ -98,6 +99,7 @@ func (cloudConn *AzureCloudConnection) CreateVMHandler() (irs.VMHandler, error)
NicClient: cloudConn.VNicClient,
PublicIPClient: cloudConn.PublicIPClient,
DiskClient: cloudConn.DiskClient,
SshKeyClient: cloudConn.SshKeyClient,
}
return &vmHandler, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package resources

import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"os"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
"sync"
"time"

Expand Down Expand Up @@ -90,49 +87,6 @@ func GetCallLogScheme(region idrv.RegionInfo, resourceType call.RES_TYPE, resour
return &subnetCIDR, nil
}*/

// KeyPair 해시 생성 함수
func CreateHashString(credentialInfo idrv.CredentialInfo) (string, error) {
keyString := credentialInfo.ClientId + credentialInfo.ClientSecret + credentialInfo.TenantId + credentialInfo.SubscriptionId
hasher := md5.New()
_, err := io.WriteString(hasher, keyString)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}

// Public KeyPair 정보 가져오기
func GetPublicKey(credentialInfo idrv.CredentialInfo, keyPairName string) (string, error) {
keyPairPath := os.Getenv("CBSPIDER_ROOT") + CBKeyPairPath
hashString, err := CreateHashString(credentialInfo)
if err != nil {
return "", err
}

publicKeyPath := keyPairPath + hashString + "--" + keyPairName + ".pub"
publicKeyBytes, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return "", err
}
return string(publicKeyBytes), nil
}

// Private KeyPair 정보 가져오기
/*func GetPrivateKey(credentialInfo idrv.CredentialInfo, keyPairName string) (string, error) {
keyPairPath := os.Getenv("CBSPIDER_ROOT") + CBKeyPairPath
hashString, err := CreateHashString(credentialInfo)
if err != nil {
return "", err
}
privateKeyPath := keyPairPath + hashString + "--" + keyPairName + ".ppk"
privateKeyBytes, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
return "", err
}
return string(privateKeyBytes), nil
}*/

func GetVNicIdByName(credentialInfo idrv.CredentialInfo, regionInfo idrv.RegionInfo, vNicName string) string {
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkInterfaces/%s", credentialInfo.SubscriptionId, regionInfo.ResourceGroup, vNicName)
}
Expand All @@ -142,6 +96,16 @@ func GetPublicIPIdByName(credentialInfo idrv.CredentialInfo, regionInfo idrv.Reg
}

func GetSecGroupIdByName(credentialInfo idrv.CredentialInfo, regionInfo idrv.RegionInfo, secGroupName string) string {
// "SecurityGroupIds": ["/subscriptions/cb592624-b77b-4a8f-bb13-0e5a48cae40f/resourceGroups/CB-GROUP/providers/Microsoft.Network/networkSecurityGroups/CB-SecGroup"],
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/networkSecurityGroups/%s", credentialInfo.SubscriptionId, regionInfo.ResourceGroup, secGroupName)
}

func GetSshKeyIdByName(credentialInfo idrv.CredentialInfo, regionInfo idrv.RegionInfo, keyName string) string{
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/sshPublicKeys/%s", credentialInfo.SubscriptionId, regionInfo.ResourceGroup, keyName)
}

func CheckIIDValidation(IId irs.IID) bool {
if IId.NameId == "" && IId.SystemId == "" {
return false
}
return true
}
Loading

0 comments on commit 7284a5f

Please sign in to comment.