command_ssh_config

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 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.",
	MaxArgs:     cli.NoArgs,
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:         "agent-forwarding",
			Usage:        "Enable SSH Agent Forwarding.",
			ConfigPath:   []string{"ssh.agent_forwarding"},
			EnvVars:      []string{config.CONFIG_ENV_PREFIX + "_SSH_AGENT_FORWARDING"},
			DefaultValue: true,
		},
		&cli.StringFlag{
			Name:         "binary",
			Usage:        "Option 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("Failed to create API client: %w", err)
		}

		user, err := client.WhoAmI(context.Background())
		if err != nil {
			return fmt.Errorf("Error getting user: %w", err)
		}

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

		configFile := ""
		if cmd.HasFlag("config") {
			absPath, err := filepath.Abs(cmd.GetString("config"))
			if err != nil {
				return fmt.Errorf("Failed to resolve absolute path for config file: %w", err)
			}
			configFile = " --config=" + absPath
		}

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

				sshConfig += "Host knot." + space.Name + machineAlias + "\n"
				sshConfig += "  HostName knot." + space.Name + machineAlias + "\n"
				sshConfig += "  StrictHostKeyChecking=no\n"
				sshConfig += "  LogLevel ERROR\n"
				sshConfig += "  UserKnownHostsFile=/dev/null\n"
				if cmd.GetBool("agent-forwarding") {
					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