-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for allowing the reading of a users environment when
creating a new child session from ~/.tsh/environment.
- Loading branch information
Showing
11 changed files
with
240 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strings" | ||
|
||
"github.com/gravitational/teleport" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/gravitational/trace" | ||
) | ||
|
||
// ReadEnvironmentFile will read environment variables from a passed in location. | ||
// Lines that start with "#" or empty lines are ignored. Assignments are in the | ||
// form name=value and no variable expansion occurs. | ||
func ReadEnvironmentFile(filename string) ([]string, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return nil, trace.ConvertSystemError(err) | ||
} | ||
defer file.Close() | ||
|
||
var lineno int | ||
var envs []string | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(scanner.Text()) | ||
|
||
// follow the lead of OpenSSH and don't allow more than 1,000 environment variables | ||
// https://github.com/openssh/openssh-portable/blob/master/session.c#L873-L874 | ||
lineno = lineno + 1 | ||
if lineno > teleport.MaxEnvironmentFileLines { | ||
return nil, trace.BadParameter("too many lines in environment file %v", filename) | ||
} | ||
|
||
// empty lines or lines that start with # are ignored | ||
if line == "" || line[0] == '#' { | ||
continue | ||
} | ||
|
||
// split on first =, if not found, log it and continue | ||
idx := strings.Index(line, "=") | ||
if idx == -1 { | ||
log.Debugf("Bad line %v while reading %v: no = separator found", lineno, filename) | ||
continue | ||
} | ||
|
||
// split key and value and make sure that key has a name | ||
key := line[:idx] | ||
value := line[idx+1:] | ||
if strings.TrimSpace(key) == "" { | ||
log.Debugf("Bad line %v while reading %v: key without name", lineno, filename) | ||
continue | ||
} | ||
|
||
envs = append(envs, key+"="+value) | ||
} | ||
|
||
err = scanner.Err() | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
return envs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Copyright 2017 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"gopkg.in/check.v1" | ||
) | ||
|
||
type EnvironmentSuite struct{} | ||
|
||
var _ = check.Suite(&EnvironmentSuite{}) | ||
var _ = fmt.Printf | ||
|
||
func (s *EnvironmentSuite) SetUpSuite(c *check.C) { | ||
InitLoggerForTests() | ||
} | ||
func (s *EnvironmentSuite) TearDownSuite(c *check.C) {} | ||
func (s *EnvironmentSuite) SetUpTest(c *check.C) {} | ||
func (s *EnvironmentSuite) TearDownTest(c *check.C) {} | ||
|
||
func (s *EnvironmentSuite) TestReadEnvironmentFile(c *check.C) { | ||
// contents of environment file | ||
rawenv := []byte(` | ||
foo=bar | ||
# comment | ||
foo=bar=baz | ||
# comment 2 | ||
= | ||
foo= | ||
=bar | ||
`) | ||
|
||
// create a temp file with an environment in it | ||
f, err := ioutil.TempFile("", "teleport-environment-") | ||
c.Assert(err, check.IsNil) | ||
defer os.Remove(f.Name()) | ||
_, err = f.Write(rawenv) | ||
c.Assert(err, check.IsNil) | ||
err = f.Close() | ||
c.Assert(err, check.IsNil) | ||
|
||
// read in the temp file | ||
env, err := ReadEnvironmentFile(f.Name()) | ||
c.Assert(err, check.IsNil) | ||
|
||
// check we parsed it correctly | ||
c.Assert(env, check.DeepEquals, []string{"foo=bar", "foo=bar=baz", "foo="}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters