Skip to content

Commit

Permalink
(#577) Tilde expand context paths
Browse files Browse the repository at this point in the history
Here we move tilde expansion to be as late as possible.

This prevents cli actions like `nats context edit` from rewriting
the value of the path to file, and instead only expanding the tilde
when needed.
  • Loading branch information
ploubser committed Oct 9, 2024
1 parent 7ac2448 commit 15be8f3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ require (
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.uber.org/automaxprocs v1.6.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/time v0.7.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/nats-io/jwt/v2 v2.7.0 h1:J+ZnaaMGQi3xSB8iOhVM5ipiWCDrQvgEoitTwWFyOYw=
github.com/nats-io/jwt/v2 v2.7.0/go.mod h1:ZdWS1nZa6WMZfFwwgpEaqBV8EPGVgOTDHN/wTbz0Y5A=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240919192050-816061f4f441 h1:zRz6lR7kpJGsScHQldbr0LFSzC5BWiX1r08oDgp42Tw=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20240919192050-816061f4f441/go.mod h1:7ME9V++zVk2hoBe5VOvq/WMQuOuNeyhG63bOwWWokZY=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20241004162106-225cd3dd6eca h1:EcjPbDziop5sCVOpWOhUXTcQPAg1of3p+MgczwJubUo=
github.com/nats-io/nats-server/v2 v2.11.0-dev.0.20241004162106-225cd3dd6eca/go.mod h1:LkVD8QXFfIxYsQCn3r0NHJk48PIg2XY1RqVBDTQwYuU=
github.com/nats-io/nats.go v1.37.0 h1:07rauXbVnnJvv1gfIyghFEo6lUcYRY0WXc3x7x0vUxE=
Expand All @@ -56,8 +54,6 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
Expand Down
35 changes: 18 additions & 17 deletions natscontext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ func (c *Context) NATSOptions(opts ...nats.Option) ([]nats.Option, error) {
}
nopts = append(nopts, nats.UserJWT(userCB, sigCB))
} else {
nopts = append(nopts, nats.UserCredentials(c.Creds()))
nopts = append(nopts, nats.UserCredentials(expandHomedir(c.Creds())))
}

case c.NKey() != "":
nko, err := nats.NkeyOptionFromSeed(c.NKey())
nko, err := nats.NkeyOptionFromSeed(expandHomedir(c.NKey()))
if err != nil {
return nil, err
}
Expand All @@ -369,15 +369,15 @@ func (c *Context) NATSOptions(opts ...nats.Option) ([]nats.Option, error) {
}

if c.Token() != "" {
nopts = append(nopts, nats.Token(c.Token()))
nopts = append(nopts, nats.Token(expandHomedir(c.Token())))
}

if c.Certificate() != "" && c.Key() != "" {
nopts = append(nopts, nats.ClientCert(c.Certificate(), c.Key()))
nopts = append(nopts, nats.ClientCert(expandHomedir(c.Certificate()), expandHomedir(c.Key())))
}

if c.CA() != "" {
nopts = append(nopts, nats.RootCAs(c.CA()))
nopts = append(nopts, nats.RootCAs(expandHomedir(c.CA())))
}

if c.SocksProxy() != "" {
Expand Down Expand Up @@ -502,18 +502,6 @@ func (c *Context) loadActiveContext() error {
// performing environment variable expansion for the path of the cerds.
c.config.Creds = os.ExpandEnv(c.config.Creds)

usr, err := user.Current()
if err != nil {
return err
}

// expand tilde to current user's home directory
c.config.Creds = strings.Replace(c.config.Creds, "~", usr.HomeDir, 1)
c.config.NKey = strings.Replace(c.config.NKey, "~", usr.HomeDir, 1)
c.config.Cert = strings.Replace(c.config.Cert, "~", usr.HomeDir, 1)
c.config.Key = strings.Replace(c.config.Key, "~", usr.HomeDir, 1)
c.config.CA = strings.Replace(c.config.CA, "~", usr.HomeDir, 1)

if c.config.NSCLookup != "" {
err := c.resolveNscLookup()
if err != nil {
Expand Down Expand Up @@ -564,6 +552,19 @@ func (c *Context) resolveNscLookup() error {
return nil
}

func expandHomedir(path string) string {
if path[0] != '~' {
return path
}

usr, err := user.Current()
if err != nil {
return path
}

return strings.Replace(path, "~", usr.HomeDir, 1)
}

func validName(name string) bool {
return name != "" && !strings.Contains(name, "..") && !strings.Contains(name, string(os.PathSeparator))
}
Expand Down
17 changes: 0 additions & 17 deletions natscontext/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,4 @@ func TestContext(t *testing.T) {
if err != nil || (config.Name != "gotest" && config.ServerURL() != "demo.nats.io" && config.Token() != "use-nkeys!") {
t.Fatalf("could not load context file: %s", err)
}

// test tilde expansion
if config.NKey() == "~/.keys/nats/example/prod.nkey" {
t.Fatalf("invalid expansion of HOME directory - %s", config.NKey())
}
if config.Creds() == "~/.keys/nats/example/creds" {
t.Fatalf("invalid expansion of HOME directory - %s", config.Creds())
}
if config.Certificate() == "~/.keys/nats/example/public.crt" {
t.Fatalf("invalid expansion of HOME directory - %s", config.Certificate())
}
if config.Key() == "~/.keys/nats/example/public.key" {
t.Fatalf("invalid expansion of HOME directory - %s", config.Key())
}
if config.CA() == "~/.keys/nats/example/public.ca" {
t.Fatalf("invalid expansion of HOME directory - %s", config.CA())
}
}
23 changes: 23 additions & 0 deletions natscontext/expansion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package natscontext

import "testing"

func TestContext(t *testing.T) {
// Do nothing if the string does not start with ~
t1 := expandHomedir("foo")
if t1 != "foo" {
t.Fatalf("failed to expand home directory for string 'foo': %s", t1)
}

// Expand ~ to HOMEDIR if string starts with ~
t2 := expandHomedir("~/foo")
if t2 == "~/foo" {
t.Fatalf("failed to expand home directory for string 'foo': %s", t2)
}

// Do nothing if there is a ~ but it is not the first character of the string
t3 := expandHomedir("/~/foo")
if t3 != "/~/foo" {
t.Fatalf("failed to expand home directory for string 'foo': %s", t3)
}
}

0 comments on commit 15be8f3

Please sign in to comment.