set uncloud group on the unix socket to allow configured non-root users to access it

This commit is contained in:
Pavel Sviderski
2024-09-10 16:13:20 +10:00
parent 9e44d30328
commit ddf7d23351
6 changed files with 59 additions and 28 deletions
+21 -23
View File
@@ -2,7 +2,6 @@ package cli
import (
"context"
"crypto/ed25519"
"errors"
"fmt"
"net/netip"
@@ -13,6 +12,8 @@ import (
"uncloud/internal/secret"
)
const defaultClusterName = "default"
var (
ErrNotFound = errors.New("not found")
)
@@ -31,19 +32,10 @@ func New(configPath string) (*CLI, error) {
}, nil
}
func (cli *CLI) CreateCluster(
name string, privateKey ed25519.PrivateKey, userPrivateKey secret.Secret,
) (*client.ClusterClient, error) {
func (cli *CLI) CreateCluster(name string, userPrivateKey secret.Secret) (*client.ClusterClient, error) {
if _, ok := cli.config.Clusters[name]; ok {
return nil, fmt.Errorf("cluster %q already exists", name)
}
if privateKey == nil {
var err error
_, privateKey, err = ed25519.GenerateKey(nil)
if err != nil {
return nil, fmt.Errorf("generate cluster secret: %w", err)
}
}
if userPrivateKey == nil {
user, err := client.NewUser(nil)
if err != nil {
@@ -54,7 +46,6 @@ func (cli *CLI) CreateCluster(
cli.config.Clusters[name] = &config.Cluster{
Name: name,
Secret: privateKey.Seed(),
UserPrivateKey: userPrivateKey,
}
if err := cli.config.Save(); err != nil {
@@ -65,7 +56,7 @@ func (cli *CLI) CreateCluster(
}
func (cli *CLI) CreateDefaultCluster() (*client.ClusterClient, error) {
c, err := cli.CreateCluster("default", nil, nil)
c, err := cli.CreateCluster("default", nil)
if err != nil {
return nil, err
}
@@ -142,18 +133,15 @@ func (cli *CLI) initRemoteMachine(
_ = c.Close()
}()
var cluster *client.ClusterClient
if clusterName == "" {
cluster, err = cli.CreateDefaultCluster()
} else {
cluster, err = cli.CreateCluster(clusterName, nil, nil)
clusterName = defaultClusterName
}
if err != nil {
return err
if _, ok := cli.config.Clusters[clusterName]; ok {
return fmt.Errorf("cluster %q already exists", clusterName)
}
user, err := cluster.User()
user, err := client.NewUser(nil)
if err != nil {
return fmt.Errorf("get cluster user: %w", err)
return fmt.Errorf("generate cluster user: %w", err)
}
// TODO: download and install the latest uncloudd binary by running the install shell script from GitHub.
@@ -174,13 +162,23 @@ func (cli *CLI) initRemoteMachine(
if err != nil {
return fmt.Errorf("init cluster: %w", err)
}
fmt.Printf("Cluster %q initialised with machine %q\n", cluster.Name(), resp.Machine.Name)
fmt.Printf("Cluster %q initialised with machine %q\n", clusterName, resp.Machine.Name)
_, err = cli.CreateCluster(clusterName, user.PrivateKey())
if err != nil {
return fmt.Errorf("save cluster to config: %w", err)
}
// Set the current cluster to the just created one if it is the only cluster in the config.
if len(cli.config.Clusters) == 1 {
if err = cli.SetCurrentCluster(clusterName); err != nil {
return fmt.Errorf("set current cluster: %w", err)
}
}
// Save the machine's SSH connection details in the cluster config.
connCfg := config.MachineConnection{
SSH: config.NewSSHDestination(remoteMachine.User, remoteMachine.Host, remoteMachine.Port),
}
cli.config.Clusters[cluster.Name()].Machines = append(cli.config.Clusters[cluster.Name()].Machines, connCfg)
cli.config.Clusters[clusterName].Machines = append(cli.config.Clusters[clusterName].Machines, connCfg)
if err = cli.config.Save(); err != nil {
return fmt.Errorf("save config: %w", err)
}
+5 -1
View File
@@ -57,7 +57,11 @@ func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
addr = strings.TrimPrefix(addr, "unix://")
conn, dErr := c.client.DialContext(ctx, "unix", addr)
if dErr != nil {
return nil, fmt.Errorf("connect to machine API socket %s through SSH tunnel: %w", addr, dErr)
return nil, fmt.Errorf(
"connect to machine API socket %s through SSH tunnel (is the Uncloud daemon running "+
"on the remote machine and does the SSH user have permissions to access the socket?): %w",
addr, dErr,
)
}
return conn, nil
},
-1
View File
@@ -5,7 +5,6 @@ import "uncloud/internal/secret"
type Cluster struct {
Name string `toml:"-"`
Machines []MachineConnection `toml:"machines"`
Secret secret.Secret `toml:"secret"`
// UserPrivateKey is the user's WireGuard private key used to connect to cluster machines.
UserPrivateKey secret.Secret `toml:"user_private_key"`
}