From 1246472852b3348e6da93390f962c777cc9bff71 Mon Sep 17 00:00:00 2001 From: Pratik Patel Date: Tue, 17 Nov 2026 16:43:21 -0800 Subject: [PATCH] feat(sfcompute): bootstrap v2 instances with resource SSH CAs --- v1/providers/sfcomputev2/instance.go | 21 ++++--- v1/providers/sfcomputev2/ssh_ca_test.go | 83 +++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 v1/providers/sfcomputev2/ssh_ca_test.go diff --git a/v1/providers/sfcomputev2/instance.go b/v1/providers/sfcomputev2/instance.go index 722107b..eb3bb65 100644 --- a/v1/providers/sfcomputev2/instance.go +++ b/v1/providers/sfcomputev2/instance.go @@ -8,13 +8,12 @@ import ( "net/http" "regexp" "slices" - "strings" "time" "github.com/alecthomas/units" "github.com/brevdev/cloud/internal/errors" + "github.com/brevdev/cloud/internal/sshbootstrap" v1 "github.com/brevdev/cloud/v1" - "golang.org/x/crypto/ssh" ) // SFC instance names must match `[a-zA-Z0-9][a-zA-Z0-9._-]{0,254}`: start with an @@ -45,7 +44,7 @@ func makeSFCName(refID string, tags v1.Tags) string { } func (c *SFCClientV2) CreateInstance(ctx context.Context, attrs v1.CreateInstanceAttrs) (*v1.Instance, error) { - cloudInit, err := sshKeyCloudInit(attrs.PublicKey) + cloudInit, err := sshCloudInit(attrs) if err != nil { return nil, errors.WrapAndTrace(err) } @@ -94,16 +93,22 @@ func (c *SFCClientV2) CreateInstance(ctx context.Context, attrs v1.CreateInstanc v1.LogField("instanceSku", sku), ) + instance.SSHCertificateBootstrap = attrs.SSHCertificateAuthority != nil return instance, nil } -func sshKeyCloudInit(sshKey string) (string, error) { - key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(sshKey)) +func sshCloudInit(attrs v1.CreateInstanceAttrs) (string, error) { + var key string + var err error + if attrs.SSHCertificateAuthority != nil { + key, err = sshbootstrap.AuthorizedKey(*attrs.SSHCertificateAuthority, defaultSSHUsername) + } else { + key, err = sshbootstrap.CanonicalPublicKey(attrs.PublicKey) + } if err != nil { - return "", fmt.Errorf("invalid SSH public key: %w", err) + return "", fmt.Errorf("invalid SSH bootstrap key: %w", err) } - sshKey = strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))) - script := fmt.Sprintf("#cloud-config\nssh_authorized_keys:\n - %s", sshKey) + script := fmt.Sprintf("#cloud-config\nssh_authorized_keys:\n - %q", key) return base64.StdEncoding.EncodeToString([]byte(script)), nil } diff --git a/v1/providers/sfcomputev2/ssh_ca_test.go b/v1/providers/sfcomputev2/ssh_ca_test.go new file mode 100644 index 0000000..76338d8 --- /dev/null +++ b/v1/providers/sfcomputev2/ssh_ca_test.go @@ -0,0 +1,83 @@ +package v2 + +import ( + "crypto/ed25519" + "crypto/rand" + "encoding/base64" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + v1 "github.com/brevdev/cloud/v1" + "github.com/stretchr/testify/require" + "golang.org/x/crypto/ssh" + "gopkg.in/yaml.v3" +) + +func testPublicKey(t *testing.T) string { + t.Helper() + public, _, err := ed25519.GenerateKey(rand.Reader) + require.NoError(t, err) + key, err := ssh.NewPublicKey(public) + require.NoError(t, err) + return strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))) +} + +func TestCreateInstanceSSHCertificateBootstrap(t *testing.T) { + userKey, caKey := testPublicKey(t), testPublicKey(t) + for _, certificate := range []bool{false, true} { + args := v1.CreateInstanceAttrs{ + RefID: "instance-ref", + PublicKey: userKey + " legacy-comment", + } + if certificate { + args.SSHCertificateAuthority = &v1.SSHCertificateAuthority{ + PublicKey: caKey, + PrincipalPrefix: "brev:v1:vm:env-sfc:login:", + } + } + var posted *createInstanceRequest + server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { + switch request.Method + " " + request.URL.Path { + case "GET /integrations/brev/v1/pools/sfc:pool:account:workspace:default": + writeJSON(t, writer, poolResponse{AllocationSchedule: allocationSchedule{ + ByInstanceSKU: map[string][]scheduleEntry{"sku": {{StartAt: 0, NodeCount: 1}}}, + }}) + case "GET /integrations/brev/v1/instances": + writeJSON(t, writer, listInstancesResponse{}) + case "POST /integrations/brev/v1/instances": + require.NoError(t, json.NewDecoder(request.Body).Decode(&posted)) + writeJSON(t, writer, instanceResponse{ + ID: "vm-id", + Status: instanceStatusAwaitingAllocation, + Tags: posted.Tags, + }) + default: + http.NotFound(writer, request) + } + })) + t.Cleanup(server.Close) + instance, err := newTestSFCClient(server.URL).CreateInstance(t.Context(), args) + require.NoError(t, err) + require.Equal(t, certificate, instance.SSHCertificateBootstrap) + require.Equal(t, args.RefID, instance.RefID) + userData, err := base64.StdEncoding.DecodeString(*posted.CloudInitUserData) + require.NoError(t, err) + var config struct { + Keys []string `yaml:"ssh_authorized_keys"` + } + require.NoError(t, yaml.Unmarshal(userData, &config)) + require.Len(t, config.Keys, 1) + if !certificate { + require.Equal(t, []string{userKey}, config.Keys, "retain legacy key canonicalization") + continue + } + key, _, options, _, err := ssh.ParseAuthorizedKey([]byte(config.Keys[0])) + require.NoError(t, err) + require.Equal(t, caKey, strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key)))) + require.ElementsMatch(t, []string{"cert-authority", `principals="brev:v1:vm:env-sfc:login:ubuntu"`}, options) + require.NotContains(t, string(userData), userKey) + } +}