Skip to content

Commit b7a134b

Browse files
committed
convert pem keys automatically
1 parent e457281 commit b7a134b

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

v1/providers/shadeform/instance.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@ package v1
22

33
import (
44
"context"
5+
"crypto/rsa"
6+
"crypto/x509"
7+
"encoding/pem"
58
"errors"
69
"fmt"
710
"strings"
811

12+
"golang.org/x/crypto/ssh"
13+
914
"github.com/alecthomas/units"
1015
v1 "github.com/brevdev/cloud/v1"
1116
openapi "github.com/brevdev/cloud/v1/providers/shadeform/gen/shadeform"
@@ -124,9 +129,14 @@ func (c *ShadeformClient) CreateInstance(ctx context.Context, attrs v1.CreateIns
124129
func (c *ShadeformClient) addSSHKey(ctx context.Context, keyPairName string, publicKey string) (string, error) {
125130
authCtx := c.makeAuthContext(ctx)
126131

132+
key, err := convertPEMKeyToOpenSSH(publicKey)
133+
if err != nil {
134+
return "", fmt.Errorf("failed to convert PEM key to RSA key: %w", err)
135+
}
136+
127137
request := openapi.AddSshKeyRequest{
128138
Name: keyPairName,
129-
PublicKey: publicKey,
139+
PublicKey: key,
130140
}
131141

132142
resp, httpResp, err := c.client.DefaultAPI.SshKeysAdd(authCtx).AddSshKeyRequest(request).Execute()
@@ -144,6 +154,33 @@ func (c *ShadeformClient) addSSHKey(ctx context.Context, keyPairName string, pub
144154
return resp.Id, nil
145155
}
146156

157+
func convertPEMKeyToOpenSSH(pemKey string) (string, error) {
158+
// Decode PEM
159+
block, _ := pem.Decode([]byte(pemKey))
160+
if block == nil {
161+
return "", fmt.Errorf("failed to decode PEM key")
162+
}
163+
164+
// Parse into rsa.PublicKey
165+
pubAny, err := x509.ParsePKIXPublicKey(block.Bytes)
166+
if err != nil {
167+
return "", fmt.Errorf("parse error: %w", err)
168+
}
169+
170+
pub, ok := pubAny.(*rsa.PublicKey)
171+
if !ok {
172+
return "", fmt.Errorf("not an RSA public key")
173+
}
174+
175+
// Convert to OpenSSH format
176+
sshPub, err := ssh.NewPublicKey(pub)
177+
if err != nil {
178+
return "", fmt.Errorf("ssh key error: %w", err)
179+
}
180+
181+
return string(ssh.MarshalAuthorizedKey(sshPub)), nil
182+
}
183+
147184
func (c *ShadeformClient) GetInstance(ctx context.Context, instanceID v1.CloudProviderInstanceID) (*v1.Instance, error) {
148185
authCtx := c.makeAuthContext(ctx)
149186

v1/providers/shadeform/instancetype.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (c *ShadeformClient) convertShadeformInstanceTypeToV1InstanceType(shadeform
154154

155155
for _, region := range shadeformInstanceType.Availability {
156156
instanceTypes = append(instanceTypes, v1.InstanceType{
157-
// ID: v1.InstanceTypeID(c.getInstanceTypeID(instanceType, region.Region)), // TODO: ID format is very particular within devplane, and will be generated automatically if omitted here
157+
ID: v1.InstanceTypeID(c.getInstanceTypeID(instanceType, region.Region)),
158158
Type: instanceType,
159159
VCPU: shadeformInstanceType.Configuration.Vcpus,
160160
Memory: units.Base2Bytes(shadeformInstanceType.Configuration.MemoryInGb) * units.GiB,
@@ -171,7 +171,7 @@ func (c *ShadeformClient) convertShadeformInstanceTypeToV1InstanceType(shadeform
171171
},
172172
SupportedStorage: []v1.Storage{ // TODO: add storage
173173
{
174-
Type: "ssd",
174+
Type: "ssd",
175175
Count: 1,
176176
Size: units.GiB * units.Base2Bytes(shadeformInstanceType.Configuration.StorageInGb),
177177
},

0 commit comments

Comments
 (0)