Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add canonically-cased config options #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ type KV struct {
position Position
}

// CanonicalKey returns k's Key, using the canonical casing from https://man.openbsd.org/ssh_config.
func (k *KV) CanonicalKey() string {
return GetCanonicalCase(k.Key)
}

// Pos returns k's Position.
func (k *KV) Pos() Position {
return k.position
Expand All @@ -586,7 +591,7 @@ func (k *KV) String() string {
if k.hasEquals {
equals = " = "
}
line := fmt.Sprintf("%s%s%s%s", strings.Repeat(" ", int(k.leadingSpace)), k.Key, equals, k.Value)
line := fmt.Sprintf("%s%s%s%s", strings.Repeat(" ", k.leadingSpace), k.CanonicalKey(), equals, k.Value)
if k.Comment != "" {
line += " #" + k.Comment
}
Expand Down
150 changes: 150 additions & 0 deletions config_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package ssh_config

import (
"strings"
)

// Current as of OpenSSH 8.2
// Source: https://man.openbsd.org/ssh_config
var configOptions = []string{
"Host",
"Match",
"AddKeysToAgent",
"AddressFamily",
"BatchMode",
"BindAddress",
"BindInterface",
"CanonicalDomains",
"CanonicalizeFallbackLocal",
"CanonicalizeHostname",
"CanonicalizeMaxDots",
"CanonicalizePermittedCNAMEs",
"CASignatureAlgorithms",
"CertificateFile",
"CheckHostIP",
"Ciphers",
"ClearAllForwardings",
"Compression",
"ConnectionAttempts",
"ConnectTimeout",
"ControlMaster",
"ControlPath",
"ControlPersist",
"DynamicForward",
"EnableSSHKeysign",
"EscapeChar",
"ExitOnForwardFailure",
"FingerprintHash",
"ForkAfterAuthentication",
"ForwardAgent",
"ForwardX11",
"ForwardX11Timeout",
"ForwardX11Trusted",
"GatewayPorts",
"GlobalKnownHostsFile",
"GSSAPIAuthentication",
"GSSAPIDelegateCredentials",
"HashKnownHosts",
"HostbasedAcceptedAlgorithms",
"HostbasedAuthentication",
"HostKeyAlgorithms",
"HostKeyAlias",
"Hostname",
"IdentitiesOnly",
"IdentityAgent",
"IdentityFile",
"IgnoreUnknown",
"Include",
"IPQoS",
"KbdInteractiveAuthentication",
"KbdInteractiveDevices",
"KexAlgorithms",
"KnownHostsCommand",
"LocalCommand",
"LocalForward",
"LogLevel",
"LogVerbose",
"MACs",
"NoHostAuthenticationForLocalhost",
"NumberOfPasswordPrompts",
"PasswordAuthentication",
"PermitLocalCommand",
"PermitRemoteOpen",
"PKCS11Provider",
"Port",
"PreferredAuthentications",
"ProxyCommand",
"ProxyJump",
"ProxyUseFdpass",
"PubkeyAcceptedAlgorithms",
"PubkeyAuthentication",
"RekeyLimit",
"RemoteCommand",
"RemoteForward",
"RequestTTY",
"RevokedHostKeys",
"SecurityKeyProvider",
"SendEnv",
"ServerAliveCountMax",
"ServerAliveInterval",
"SessionType",
"SetEnv",
"StdinNull",
"StreamLocalBindMask",
"StreamLocalBindUnlink",
"StrictHostKeyChecking",
"SyslogFacility",
"TCPKeepAlive",
"Tunnel",
"TunnelDevice",
"UpdateHostKeys",
"User",
"UserKnownHostsFile",
"VerifyHostKeyDNS",
"VisualHostKey",
"XAuthLocation",
}

// // getConfigOptions retrieves a sorted list of all the current config options
// // from the official source: https://man.openbsd.org/ssh_config and filtered
// // with "github.com/PuerkitoBio/goquery".
// // The result is sorted and therefore may be searched with sort.SearchStrings()
// func getConfigOptions() (opts []string, err error) {
// SSHConfigURL := "https://man.openbsd.org/ssh_config"

// resp, err := http.Get(SSHConfigURL)
// if err != nil {
// return opts, fmt.Errorf("getting %s: %w", SSHConfigURL, err)
// }
// defer resp.Body.Close()

// doc, err := goquery.NewDocumentFromReader(resp.Body)
// if err != nil {
// return opts, fmt.Errorf("parsing %s: %w", SSHConfigURL, err)
// }

// doc.Find("dt > a > code.Cm").Each(func(i int, c *goquery.Selection) {
// opts = append(opts, c.Text())
// })

// sort.Strings(opts)
// return opts, nil
// }

var configOptionsMap = map[string]string{}

func init() {
for _, opt := range configOptions {
configOptionsMap[strings.ToLower(opt)] = opt
}
}

// GetCanonicalCase checks for the given key in the known ssh config keys
// and returns with proper casing if found, otherwise returns what was given.
func GetCanonicalCase(key string) string {
if v, ok := configOptionsMap[strings.ToLower(key)]; ok {
return v
}

return key
}