|
| 1 | +/* |
| 2 | + Copyright NetFoundry Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package datapipe |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "github.com/gliderlabs/ssh" |
| 22 | + "github.com/michaelquigley/pfxlog" |
| 23 | + "github.com/openziti/identity" |
| 24 | + gossh "golang.org/x/crypto/ssh" |
| 25 | + "os" |
| 26 | + "path" |
| 27 | + "strconv" |
| 28 | + "strings" |
| 29 | +) |
| 30 | + |
| 31 | +type LocalAccessType string |
| 32 | + |
| 33 | +const ( |
| 34 | + LocalAccessTypeNone LocalAccessType = "" |
| 35 | + LocalAccessTypePort LocalAccessType = "local-port" |
| 36 | + LocalAccessTypeEmbeddedSshServer LocalAccessType = "embedded-ssh-server" |
| 37 | +) |
| 38 | + |
| 39 | +type Config struct { |
| 40 | + Enabled bool |
| 41 | + LocalAccessType LocalAccessType // values: 'none', 'localhost:port', 'embedded' |
| 42 | + DestinationPort uint16 |
| 43 | + AuthorizedKeysFile string |
| 44 | + HostKey gossh.Signer |
| 45 | + ShellPath string |
| 46 | +} |
| 47 | + |
| 48 | +func (self *Config) IsLocalAccessAllowed() bool { |
| 49 | + return self.Enabled && self.LocalAccessType != LocalAccessTypeNone |
| 50 | +} |
| 51 | + |
| 52 | +func (self *Config) IsLocalPort() bool { |
| 53 | + return self.LocalAccessType == LocalAccessTypePort |
| 54 | +} |
| 55 | + |
| 56 | +func (self *Config) IsEmbedded() bool { |
| 57 | + return self.LocalAccessType == LocalAccessTypeEmbeddedSshServer |
| 58 | +} |
| 59 | + |
| 60 | +func (self *Config) LoadConfig(m map[interface{}]interface{}) error { |
| 61 | + log := pfxlog.Logger() |
| 62 | + if v, ok := m["enabled"]; ok { |
| 63 | + if enabled, ok := v.(bool); ok { |
| 64 | + self.Enabled = enabled |
| 65 | + } else { |
| 66 | + self.Enabled = strings.EqualFold("true", fmt.Sprintf("%v", v)) |
| 67 | + } |
| 68 | + } |
| 69 | + if v, ok := m["enableExperimentalFeature"]; ok { |
| 70 | + if enabled, ok := v.(bool); ok { |
| 71 | + if !enabled { |
| 72 | + self.Enabled = false |
| 73 | + } |
| 74 | + } else if !strings.EqualFold("true", fmt.Sprintf("%v", v)) { |
| 75 | + self.Enabled = false |
| 76 | + } |
| 77 | + } else { |
| 78 | + self.Enabled = false |
| 79 | + } |
| 80 | + |
| 81 | + if self.Enabled { |
| 82 | + log.Infof("mgmt.pipe enabled") |
| 83 | + if v, ok := m["destination"]; ok { |
| 84 | + if destination, ok := v.(string); ok { |
| 85 | + if strings.HasPrefix(destination, "127.0.0.1:") { |
| 86 | + self.LocalAccessType = LocalAccessTypePort |
| 87 | + portStr := strings.TrimPrefix(destination, "127.0.0.1:") |
| 88 | + port, err := strconv.ParseUint(portStr, 10, 16) |
| 89 | + if err != nil { |
| 90 | + log.WithError(err).Warn("mgmt.pipe is enabled, but destination not valid. Must be '127.0.0.1:<port>' or 'embedded'") |
| 91 | + self.Enabled = false |
| 92 | + return nil |
| 93 | + } |
| 94 | + self.DestinationPort = uint16(port) |
| 95 | + } else if destination == "embedded-ssh-server" { |
| 96 | + self.LocalAccessType = LocalAccessTypeEmbeddedSshServer |
| 97 | + |
| 98 | + if v, ok = m["authorizedKeysFile"]; ok { |
| 99 | + if keysFile, ok := v.(string); ok { |
| 100 | + self.AuthorizedKeysFile = keysFile |
| 101 | + } else { |
| 102 | + log.Warnf("mgmt.pipe is enabled, but 'embedded' destination configured and authorizedKeysFile configuration is not type string, but %T", v) |
| 103 | + self.Enabled = false |
| 104 | + return nil |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if v, ok = m["shell"]; ok { |
| 109 | + if s, ok := v.(string); ok { |
| 110 | + self.ShellPath = s |
| 111 | + } else { |
| 112 | + log.Warnf("mgmt.pipe is enabled, but 'embedded' destination configured and shell configuration is not type string, but %T", v) |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + log.Warn("mgmt.pipe is enabled, but destination not valid. Must be 'localhost:port' or 'embedded'") |
| 117 | + self.Enabled = false |
| 118 | + return nil |
| 119 | + } |
| 120 | + } |
| 121 | + } else { |
| 122 | + self.Enabled = false |
| 123 | + log.Warn("mgmt.pipe is enabled, but destination not specified. mgmt.pipe disabled.") |
| 124 | + return nil |
| 125 | + } |
| 126 | + } else { |
| 127 | + log.Infof("mgmt.pipe disabled") |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func (self *Config) NewSshRequestHandler(identity *identity.TokenId) (*SshRequestHandler, error) { |
| 133 | + if self.HostKey == nil { |
| 134 | + signer, err := gossh.NewSignerFromKey(identity.Cert().PrivateKey) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + self.HostKey = signer |
| 139 | + } |
| 140 | + |
| 141 | + keysFile := self.AuthorizedKeysFile |
| 142 | + if keysFile == "" { |
| 143 | + homeDir, err := os.UserHomeDir() |
| 144 | + if err != nil { |
| 145 | + return nil, fmt.Errorf("could not set up ssh request handler, failing get home dir, trying to load default authorized keys (%w)", err) |
| 146 | + } |
| 147 | + keysFile = path.Join(homeDir, ".ssh", "authorized_keys") |
| 148 | + } |
| 149 | + |
| 150 | + keysFileContents, err := os.ReadFile(keysFile) |
| 151 | + if err != nil { |
| 152 | + return nil, fmt.Errorf("could not set up ssh request handler, failed to load authorized keys from '%s' (%w)", keysFile, err) |
| 153 | + } |
| 154 | + |
| 155 | + authorizedKeys := map[string]struct{}{} |
| 156 | + entryIdx := 0 |
| 157 | + for len(keysFileContents) > 0 { |
| 158 | + pubKey, _, _, rest, err := gossh.ParseAuthorizedKey(keysFileContents) |
| 159 | + if err != nil { |
| 160 | + return nil, fmt.Errorf("could not set up ssh request handler, failed to load authorized key at index %d from '%s' (%w)", entryIdx, keysFile, err) |
| 161 | + } |
| 162 | + |
| 163 | + authorizedKeys[string(pubKey.Marshal())] = struct{}{} |
| 164 | + keysFileContents = rest |
| 165 | + entryIdx++ |
| 166 | + } |
| 167 | + |
| 168 | + publicKeyOption := ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool { |
| 169 | + _, found := authorizedKeys[string(key.Marshal())] |
| 170 | + return found |
| 171 | + }) |
| 172 | + |
| 173 | + return &SshRequestHandler{ |
| 174 | + config: self, |
| 175 | + options: []ssh.Option{publicKeyOption}, |
| 176 | + }, nil |
| 177 | +} |
0 commit comments