Skip to content

Commit

Permalink
implement small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Jan 16, 2025
1 parent bd3bbaf commit 57da20b
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 63 deletions.
2 changes: 1 addition & 1 deletion bootstrapper/internal/joinclient/joinclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (c *JoinClient) startNodeAndJoin(ticket *joinproto.IssueJoinTicketResponse,
}

if err := c.fileHandler.Write(constants.SSHCAKeyPath, ticket.EmergencyCaKey, file.OptMkdirAll); err != nil {
return fmt.Errorf("writing ca key: %w", err)
return fmt.Errorf("writing ssh ca key: %w", err)
}

state := nodestate.NodeState{
Expand Down
22 changes: 10 additions & 12 deletions cli/internal/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ import (
"golang.org/x/crypto/ssh"
)

var permissions = ssh.Permissions{
Extensions: map[string]string{
"permit-port-forwarding": "yes",
"permit-pty": "yes",
},
}

// NewSSHCmd returns a new cobra.Command for the ssh command.
func NewSSHCmd() *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -41,7 +34,7 @@ func NewSSHCmd() *cobra.Command {
Args: cobra.ExactArgs(0),
RunE: runSSH,
}
cmd.Flags().String("key", "", "The path to an existing ssh public key.")
cmd.Flags().String("key", "", "the path to an existing ssh public key.")
must(cmd.MarkFlagRequired("key"))
return cmd
}
Expand Down Expand Up @@ -72,7 +65,7 @@ func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogg

// NOTE(miampf): Since other KMS aren't fully implemented yet, this commands assumes that the cKMS is used and derives the key accordingly.
var mastersecret uri.MasterSecret
if err = fh.ReadJSON(fmt.Sprintf("%s.json", constants.ConstellationMasterSecretStoreName), &mastersecret); err != nil {
if err = fh.ReadJSON(constants.MasterSecretFilename, &mastersecret); err != nil {
return fmt.Errorf("reading master secret: %s", err)
}

Expand All @@ -81,12 +74,12 @@ func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogg
if err != nil {
return fmt.Errorf("setting up KMS: %s", err)
}
key, err := kms.GetDEK(ctx, crypto.DEKPrefix+constants.SSHCAKeySuffix, ed25519.SeedSize)
sshCAKeySeed, err := kms.GetDEK(ctx, crypto.DEKPrefix+constants.SSHCAKeySuffix, ed25519.SeedSize)
if err != nil {
return fmt.Errorf("retrieving key from KMS: %s", err)
}

ca, err := crypto.GenerateEmergencySSHCAKey(key)
ca, err := crypto.GenerateEmergencySSHCAKey(sshCAKeySeed)
if err != nil {
return fmt.Errorf("generating ssh emergency CA key: %s", err)
}
Expand All @@ -109,7 +102,12 @@ func generateKey(ctx context.Context, keyPath string, fh file.Handler, debugLogg
ValidAfter: uint64(time.Now().Unix()),
ValidBefore: uint64(time.Now().Add(24 * time.Hour).Unix()),
ValidPrincipals: []string{"root"},
Permissions: permissions,
Permissions: ssh.Permissions{
Extensions: map[string]string{
"permit-port-forwarding": "yes",
"permit-pty": "yes",
},
},
}
if err := certificate.SignCert(rand.Reader, ca); err != nil {
return fmt.Errorf("signing certificate: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func GenerateRandomBytes(length int) ([]byte, error) {
}

// GenerateEmergencySSHCAKey creates a CA that is used to sign keys for emergency ssh access.
func GenerateEmergencySSHCAKey(key []byte) (ssh.Signer, error) {
_, priv, err := ed25519.GenerateKey(bytes.NewReader(key))
func GenerateEmergencySSHCAKey(seed []byte) (ssh.Signer, error) {
_, priv, err := ed25519.GenerateKey(bytes.NewReader(seed))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/crypto/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ func TestGenerateEmergencySSHCAKey(t *testing.T) {

_, err := GenerateEmergencySSHCAKey(tc.key)
if tc.wantErr {
assert.NotNil(err)
assert.Error(err)
} else {
assert.Nil(err)
assert.NoError(err)
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions joinservice/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func (s *Server) IssueJoinTicket(ctx context.Context, req *joinproto.IssueJoinTi
}

log.Info("Requesting emergency SSH CA derivation key")
ssheCADerivationKey, err := s.dataKeyGetter.GetDataKey(ctx, constants.SSHCAKeySuffix, ed25519.SeedSize)
sshCAKeySeed, err := s.dataKeyGetter.GetDataKey(ctx, constants.SSHCAKeySuffix, ed25519.SeedSize)
if err != nil {
log.With(slog.Any("error", err)).Error("Failed to get emergency SSH CA derivation key")
return nil, status.Errorf(codes.Internal, "getting emergency SSH CA derivation key: %s", err)
log.With(slog.Any("error", err)).Error("Failed to get seed material to derive SSH CA key")
return nil, status.Errorf(codes.Internal, "getting emergency SSH CA seed material: %s", err)
}
ca, err := crypto.GenerateEmergencySSHCAKey(ssheCADerivationKey)
ca, err := crypto.GenerateEmergencySSHCAKey(sshCAKeySeed)
if err != nil {
log.With(slog.Any("error", err)).Error("Failed to derive ssh CA key from derivation key")
log.With(slog.Any("error", err)).Error("Failed to derive ssh CA key from seed material")
return nil, status.Errorf(codes.Internal, "generating ssh emergency CA key: %s", err)
}

Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *Server) IssueJoinTicket(ctx context.Context, req *joinproto.IssueJoinTi
KubeletCert: kubeletCert,
ControlPlaneFiles: controlPlaneFiles,
KubernetesComponents: components,
EmergencyCaKey: ssh.MarshalAuthorizedKey(ca.PublicKey()),
AuthorizedCaPublicKey: ssh.MarshalAuthorizedKey(ca.PublicKey()),
}, nil
}

Expand Down
77 changes: 39 additions & 38 deletions joinservice/joinproto/join.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions joinservice/joinproto/join.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ message IssueJoinTicketResponse {
string kubernetes_version = 9;
// kubernetes_components is a list of components to install on the node.
repeated components.Component kubernetes_components = 10;
// emergency_ca_key is an ssh ca key that can be used to connect to a node in case of an emergency.
bytes emergency_ca_key = 11;
// authorized_ca_public_key is an ssh ca key that can be used to connect to a node in case of an emergency.
bytes authorized_ca_public_key = 11;
}

message control_plane_cert_or_key {
Expand Down

0 comments on commit 57da20b

Please sign in to comment.