@@ -2,10 +2,15 @@ package v1
22
33import (
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
124129func (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+
147184func (c * ShadeformClient ) GetInstance (ctx context.Context , instanceID v1.CloudProviderInstanceID ) (* v1.Instance , error ) {
148185 authCtx := c .makeAuthContext (ctx )
149186
0 commit comments