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

Add a .tsh/config file, add support for configuring custom http headers from the config file #10336

Merged
merged 1 commit into from
Mar 23, 2022

Conversation

lxea
Copy link
Contributor

@lxea lxea commented Feb 14, 2022

This adds a tsh config file to ~/.tsh/config/config.yaml. It includes support for configuring custom http headers as specified in #9838. Support for the aliases configuration option will be created in a seprate PR.

Current supported config file:

add_headers:
  - proxy: "*.example.com"
    headers:
      foo: bar
  - proxy: us-west-1.example.com
    headers:
      baz: qux

Also includes a change to stop tsh logout from blowing all of ~/.tsh away so config files can be retained.

@github-actions github-actions bot added the tsh tsh - Teleport's command line tool for logging into nodes running Teleport. label Feb 14, 2022
@ravicious
Copy link
Member

How can I test this myself? I wanted to see how tsh behaves when I provide invalid headers, but I wasn't able to send valid headers first.

I created a request bin on requestbin.com, I compiled tsh from your branch, then added ~/.tsh/config/config.yaml with this content:

add_headers:
  - proxy: *
    headers:
      foo: bar
  - proxy: enj0qqqty1z2.x.pipedream.net
    headers:
      baz: quux

When I do ./build/tsh login --proxy=enj0qqqty1z2.x.pipedream.net, I can see that the request to /webapi/ping doesn't include my headers. Maybe that's just an issue with this specific request.

Copy link
Member

@ravicious ravicious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to say that I'm new to both Teleport and Go, so I looked at the changes included in this PR just as GitHub shows them to me, but it'd be hard for me to say if there's a place we forgot to add the extra headers to.

Looks good in general, I just have a small question about hardcoding the path.

Also, when do we document such changes? Do other people write docs for them?

tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
@lxea
Copy link
Contributor Author

lxea commented Feb 14, 2022

How can I test this myself? I wanted to see how tsh behaves when I provide invalid headers, but I wasn't able to send valid headers first.

I created a request bin on requestbin.com, I compiled tsh from your branch, then added ~/.tsh/config/config.yaml with this content:

add_headers:
  - proxy: *
    headers:
      foo: bar
  - proxy: enj0qqqty1z2.x.pipedream.net
    headers:
      baz: quux

When I do ./build/tsh login --proxy=enj0qqqty1z2.x.pipedream.net, I can see that the request to /webapi/ping doesn't include my headers. Maybe that's just an issue with this specific request.

Is there a log at the beginning of the tsh output complaining about the .yaml file format? I think '*' needs to be in quotes.

Currently it just continues, but maybe it should fail if a config file is invalid, I'm not sure which would be preferred.

@ravicious
Copy link
Member

I scrolled up to see what was the output after running tsh and there was no log related to the error in config.

However, I did git pull and it updated your branch from ce1acad to a1c87ee and now everything works as expected: the extra headers are sent to /webapi/ping. If the config is invalid, tsh prints information about it, although I'm not sure if it should use stdout rather than stderr.

But yeah, I'm also not sure if it should fail if the config file has invalid YAML. I'd ask people more familiar with tsh than me.

In case the header itself is invalid, tsh fails only when making the actual request, but I think that's okay.

tool/tsh/tsh.go Outdated
Comment on lines 603 to 606
confOptions, err := loadConfig()
if err != nil {
fmt.Println("Failed to load tsh config: ", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the config file completely, tsh logs this message:

Failed to load tsh config: open /Users/rav/.tsh/config/config.yaml: no such file or directory

I'm pretty sure we would like to avoid logging this, right? It seems like having no config file should let tsh continue as if confOptions was nil.

We could take inspiration from kubeconfig.Load:

// Load tries to read a kubeconfig file and if it can't, returns an error.
// One exception, missing files result in empty configs, not an error.
func Load(path string) (*clientcmdapi.Config, error) {

A design decision I don't agree with, but at least the behavior would be consistent across different config files. ;)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes - sorry didn't see this comment. I guess it's consistent but it's a poor user experience when replacing a binary on a patch change.

@russjones russjones linked an issue Feb 16, 2022 that may be closed by this pull request
@russjones russjones requested a review from r0mant February 17, 2022 01:41
Copy link
Member

@ravicious ravicious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the arg change that I pointed out, but the rest of the code looks fine to me, minus those failing tests.

Leaving an approve so that I don't obstruct your progress there after someone more familiar with the project reviews the PR.

lib/reversetunnel/transport.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@r0mant r0mant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also might be useful to share a dev build with a customer to make sure it addresses their use-case and we didn't miss any requests headers should be injected into.

api/client/webclient/webclient.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
tool/tsh/tsh_test.go Outdated Show resolved Hide resolved
lib/client/keystore.go Outdated Show resolved Hide resolved
lib/client/keystore.go Outdated Show resolved Hide resolved
lib/reversetunnel/transport.go Outdated Show resolved Hide resolved
@lxea
Copy link
Contributor Author

lxea commented Feb 18, 2022

Also might be useful to share a dev build with a customer to make sure it addresses their use-case and we didn't miss any requests headers should be injected into.

@r0mant This makes sense, I did try to be thorough while trying to find places headers could be inserted from tsh commands.

@lxea lxea force-pushed the lxea/tsh-config branch 3 times, most recently from 37e8151 to 66cb586 Compare February 18, 2022 15:09
lib/web/apiserver.go Outdated Show resolved Hide resolved
@lxea lxea force-pushed the lxea/tsh-config branch 3 times, most recently from 5210182 to c7a29d2 Compare February 28, 2022 09:49
api/client/webclient/webclient.go Show resolved Hide resolved
Comment on lines +28 to +30
// .tsh config must go in a subdir as all .yaml files in .tsh get
// parsed automatically by the profile loader and results in yaml
// unmarshal errors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the profile loader be updated to ignore config.yaml file instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be but I think this would mean clusters couldn't be named config which doesn't seem ideal

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. Let me check with the product team on this. I think another option could be to just do something like ~/.tshrc which seems consistent with where e.g. shells keep their preferences and it would have another benefit that we won't have to touch ~/.tsh directory. But let's check with product first.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lxea We have decided that keeping it under ~/.tsh/config/config.yaml is ok for now. Let's merge this as-is.

tool/tsh/tshconfig.go Outdated Show resolved Hide resolved
tool/tsh/tsh.go Outdated Show resolved Hide resolved
api/client/webclient/webclient.go Outdated Show resolved Hide resolved
@lxea lxea force-pushed the lxea/tsh-config branch from c7a29d2 to c51e76a Compare March 8, 2022 10:49
Comment on lines +28 to +30
// .tsh config must go in a subdir as all .yaml files in .tsh get
// parsed automatically by the profile loader and results in yaml
// unmarshal errors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lxea We have decided that keeping it under ~/.tsh/config/config.yaml is ok for now. Let's merge this as-is.

@lxea lxea force-pushed the lxea/tsh-config branch from c51e76a to f962118 Compare March 23, 2022 12:41
@lxea lxea enabled auto-merge (rebase) March 23, 2022 12:43
@lxea lxea force-pushed the lxea/tsh-config branch from f962118 to bbec705 Compare March 23, 2022 13:09
@lxea lxea force-pushed the lxea/tsh-config branch from bbec705 to d6051b2 Compare March 23, 2022 13:14
@zmb3
Copy link
Collaborator

zmb3 commented Mar 23, 2022

@lxea please don't forget to open a docs PR documenting this new config file

@endzyme
Copy link

endzyme commented Mar 30, 2022

When this file is missing the tsh version command returns a error. I would have expected maybe a warning vs an outright failure.

@lxea
Copy link
Contributor Author

lxea commented Mar 31, 2022

@endzyme This is fixed here: #11495

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tsh tsh - Teleport's command line tool for logging into nodes running Teleport.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tsh configuration file
6 participants