command_ssh_config

package
v0.39.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SshConfigCmd = &cli.Command{
	Name:        "ssh-config",
	Usage:       "Operate on the .ssh/config file",
	Description: "Operations to perform management of the .ssh/config file.",
	MaxArgs:     cli.NoArgs,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:         "tls-skip-verify",
			Usage:        "Skip TLS verification when talking to server.",
			ConfigPath:   []string{"tls.skip_verify"},
			EnvVars:      []string{config.CONFIG_ENV_PREFIX + "_TLS_SKIP_VERIFY"},
			DefaultValue: true,
			Global:       true,
		},
		&cli.StringFlag{
			Name:         "alias",
			Aliases:      []string{"a"},
			Usage:        "The server alias to use.",
			DefaultValue: "default",
			Global:       true,
		},
	},
	Commands: []*cli.Command{
		SshConfigUpdateCmd,
		SshConfigRemoveCmd,
	},
}
View Source
var SshConfigRemoveCmd = &cli.Command{
	Name:        "remove",
	Usage:       "Remove all entries from .ssh/config",
	Description: "Remove any knot space configurations from the .ssh/config file.",
	MaxArgs:     cli.NoArgs,
	Run: func(ctx context.Context, cmd *cli.Command) error {
		alias := cmd.GetString("alias")
		util.UpdateSSHConfig("", alias)
		fmt.Println(".ssh/config has been updated")
		return nil
	},
}
View Source
var SshConfigUpdateCmd = &cli.Command{
	Name:  "update",
	Usage: "Update the .ssh/config file",
	Description: `Update the .ssh/config file with all spaces that expose SSH.

Spaces already in the file keep their agent forwarding setting, including changes made by hand. New spaces you own get agent forwarding as set by --agent-forwarding; spaces shared with you never get it. Use --reset to apply these defaults to every space again.`,
	MaxArgs: cli.NoArgs,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:         "agent-forwarding",
			Usage:        "Enable SSH agent forwarding for new spaces you own.",
			ConfigPath:   []string{"ssh.agent_forwarding"},
			EnvVars:      []string{config.CONFIG_ENV_PREFIX + "_SSH_AGENT_FORWARDING"},
			DefaultValue: true,
		},
		&cli.BoolFlag{
			Name:  "reset",
			Usage: "Apply the agent forwarding defaults to spaces already in the file.",
		},
		&cli.StringFlag{
			Name:         "binary",
			Usage:        "Optional path to the knot binary.",
			DefaultValue: "knot",
		},
	},
	Run: func(ctx context.Context, cmd *cli.Command) error {
		alias := cmd.GetString("alias")
		cfg := config.GetServerAddr(alias, cmd)
		client, err := apiclient.NewClient(cfg.HttpServer, cfg.ApiToken, cmd.GetBool("tls-skip-verify"))
		if err != nil {
			return fmt.Errorf("couldn't create the API client: %w", err)
		}

		user, err := client.WhoAmI(context.Background())
		if err != nil {
			return fmt.Errorf("couldn't get the current user: %w", err)
		}

		spaces, _, err := client.GetSpaces(context.Background(), user.Id, false)
		if err != nil {
			return fmt.Errorf("couldn't list spaces: %w", err)
		}

		configFile := ""
		if cmd.HasFlag("config") {
			absPath, err := filepath.Abs(cmd.GetString("config"))
			if err != nil {
				return fmt.Errorf("couldn't resolve the path of the SSH config file: %w", err)
			}
			configFile = " --config=" + absPath
		}

		existing := map[string]bool{}
		if !cmd.GetBool("reset") {
			existing, err = util.ReadSSHConfigAgentForwarding(alias)
			if err != nil {
				return fmt.Errorf("couldn't read the SSH config file: %w", err)
			}
		}

		sshConfig := ""
		knotParams := ""
		machineAlias := alias
		if machineAlias == "default" {
			machineAlias = ""
		} else {
			machineAlias = "." + machineAlias
			knotParams = "--alias " + alias + " "
		}
		for _, space := range spaces.Spaces {
			if space.TemplateHasSSH {
				host := "knot." + space.Name + machineAlias
				fmt.Println("Adding " + host + " to .ssh/config")

				forward, ok := existing[host]
				if !ok {
					forward = space.UserId == user.Id && cmd.GetBool("agent-forwarding")
				}

				sshConfig += "Host " + host + "\n"
				sshConfig += "  HostName " + host + "\n"
				sshConfig += "  StrictHostKeyChecking=no\n"
				sshConfig += "  LogLevel ERROR\n"
				sshConfig += "  UserKnownHostsFile=/dev/null\n"
				if forward {
					sshConfig += "  ForwardAgent yes\n"
				}
				sshConfig += "  ProxyCommand " + cmd.GetString("binary") + " forward ssh " + knotParams + space.Name + configFile + "\n"
			}
		}

		util.UpdateSSHConfig(sshConfig, alias)
		fmt.Println(".ssh/config has been updated")
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL