-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Read user environment when creating child session #1020
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,8 @@ func Run(cmdlineArgs []string, testRun bool) (executedCommand string, conf *serv | |
"Specify gops addr to listen on").Hidden().StringVar(&ccf.GopsAddr) | ||
start.Flag("diag-addr", | ||
"Start diangonstic endpoint on this address").Hidden().StringVar(&ccf.DiagnosticAddr) | ||
start.Flag("permit-user-env", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make this a flag that accepts values with paths to read, defaulting to |
||
"Enables reading of ~/.tsh/environment when creating a session").BoolVar(&ccf.PermitUserEnvironment) | ||
|
||
// define start's usage info (we use kingpin's "alias" field for this) | ||
start.Alias(usageNotes + usageExamples) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not make this a boolean flag, rather than a list with files to read with default to
.tsh/environment
so folks can actually set to something else if they want, e.g. will close the issue #1011