Skip to content

Commit

Permalink
tsh: ignore empty or non-existing config files (#11495) (#11571)
Browse files Browse the repository at this point in the history
If the newly created config.yaml didnt exist we would load the default
values and continue the flow
However, we were not resetting the `err`'s value and would have an ERROR
message at the end and an invalid exit code

Most of the commands would reset that variable to the output of the
command's execution
One of them was not: `tsh version`
The version command has no return value, so the program would execute
as expected until the last statement: `trace.Wrap(error)` which would
re-use the `err` variable whose value is the result of the `loadConfig`
method.

We could either reset the `err`s value inside the `PrintVersion` switch
case block or reset it right after we check for `IsNotFound`.

We ended up picking the first option as it seems cleaner

```
 # before
$ make full > /dev/null; build/tsh version
Teleport v10.0.0-dev git:v8.0.0-alpha.1-899-g335adf1f4 go1.18
ERROR: open /home/marco/.tsh/config/config.yaml: no such file or directory

 # after
$ make full > /dev/null; build/tsh version
Teleport v10.0.0-dev git:v8.0.0-alpha.1-899-g335adf1f4 go1.18
```
  • Loading branch information
marcoandredinis authored Mar 30, 2022
1 parent 584f90f commit 7253530
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
12 changes: 5 additions & 7 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,14 +675,12 @@ func Run(args []string, opts ...cliOption) error {

setEnvFlags(&cf, os.Getenv)

confOptions, err := loadConfig(cf.HomePath)
if err != nil && !trace.IsNotFound(err) {
return trace.Wrap(err, "failed to load tsh config from %s",
filepath.Join(profile.FullProfilePath(cf.HomePath), tshConfigPath))
}
if confOptions != nil {
cf.ExtraProxyHeaders = confOptions.ExtraHeaders
fullConfigPath := filepath.Join(profile.FullProfilePath(cf.HomePath), tshConfigPath)
confOptions, err := loadConfig(fullConfigPath)
if err != nil {
return trace.Wrap(err, "failed to load tsh config from %s", fullConfigPath)
}
cf.ExtraProxyHeaders = confOptions.ExtraHeaders

switch command {
case ver.FullCommand():
Expand Down
20 changes: 12 additions & 8 deletions tool/tsh/tshconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ limitations under the License.
package main

import (
"errors"
"io/fs"
"os"
"path/filepath"

"github.com/gravitational/teleport/api/profile"
"github.com/gravitational/trace"
"gopkg.in/yaml.v2"
)
Expand All @@ -46,14 +46,18 @@ type ExtraProxyHeaders struct {
Headers map[string]string `yaml:"headers,omitempty"`
}

func loadConfig(homePath string) (*TshConfig, error) {
confPath := filepath.Join(profile.FullProfilePath(homePath), tshConfigPath)
configFile, err := os.Open(confPath)
func loadConfig(fullConfigPath string) (*TshConfig, error) {
bs, err := os.ReadFile(fullConfigPath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return &TshConfig{}, nil
}
return nil, trace.ConvertSystemError(err)
}
defer configFile.Close()

cfg := TshConfig{}
err = yaml.NewDecoder(configFile).Decode(&cfg)
return &cfg, trace.Wrap(err)
if err := yaml.Unmarshal(bs, &cfg); err != nil {
return nil, trace.ConvertSystemError(err)
}
return &cfg, nil
}
43 changes: 43 additions & 0 deletions tool/tsh/tshconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2022 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 main

import (
"os"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
)

func TestLoadConfigNonExistingFile(t *testing.T) {
fullFilePath := "/tmp/doesntexist." + uuid.NewString()
gotConfig, gotErr := loadConfig(fullFilePath)
require.NoError(t, gotErr)
require.Equal(t, &TshConfig{}, gotConfig)
}

func TestLoadConfigEmptyFile(t *testing.T) {
file, err := os.CreateTemp("", "test-telelport")
file.Write([]byte(" "))
require.NoError(t, err)
defer os.Remove(file.Name())

gotConfig, gotErr := loadConfig(file.Name())
require.NoError(t, gotErr)
require.Equal(t, &TshConfig{}, gotConfig)
}

0 comments on commit 7253530

Please sign in to comment.