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

tsh: ignore empty or non-existing config files #11495

Merged
Merged
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
12 changes: 5 additions & 7 deletions tool/tsh/tsh.go
Original file line number Diff line number Diff line change
@@ -687,14 +687,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():
20 changes: 12 additions & 8 deletions tool/tsh/tshconfig.go
Original file line number Diff line number Diff line change
@@ -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"
)
@@ -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)
}