From a3032afbe89c93b824de4cf2b4ed2012b106cfb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Fri, 23 Feb 2024 17:01:29 -0300 Subject: [PATCH] Add ReadTokenV2() to read the new token --- tsuru/config/token_test.go | 57 ++++++++++++++++++++++++++++++ tsuru/config/token_v2.go | 41 ++++++++++++++++++++-- tsuru/config/token_v2_test.go | 65 +++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 3 deletions(-) diff --git a/tsuru/config/token_test.go b/tsuru/config/token_test.go index 74491a39..4310ee36 100644 --- a/tsuru/config/token_test.go +++ b/tsuru/config/token_test.go @@ -6,6 +6,7 @@ package config import ( "io" + "os" "github.com/tsuru/tsuru/fs/fstest" "gopkg.in/check.v1" @@ -48,6 +49,62 @@ func (s *S) TestWriteTokenWithTarget(c *check.C) { c.Assert(string(b), check.Equals, "abc") } +func (s *S) TestReadToken(c *check.C) { + os.Unsetenv("TSURU_TOKEN") + rfs := &fstest.RecordingFs{} + SetFileSystem(rfs) + initTestTarget() + f, err := Filesystem().Create(JoinWithUserDir(".tsuru", "token.d", "test")) + c.Assert(err, check.IsNil) + f.WriteString("mytoken") + defer func() { + ResetFileSystem() + }() + token, err := ReadToken() + c.Assert(err, check.IsNil) + c.Assert(token, check.Equals, "mytoken") + tokenPath := JoinWithUserDir(".tsuru", "token.d", "test") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, true) + tokenPath = JoinWithUserDir(".tsuru", "token") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, false) +} + +func (s *S) TestReadTokenFallback(c *check.C) { + os.Unsetenv("TSURU_TOKEN") + rfs := &fstest.RecordingFs{} + SetFileSystem(rfs) + defer func() { + ResetFileSystem() + }() + + initTestTarget() + f, err := Filesystem().Create(JoinWithUserDir(".tsuru", "token")) + c.Assert(err, check.IsNil) + f.WriteString("mytoken") + token, err := ReadToken() + c.Assert(err, check.IsNil) + c.Assert(token, check.Equals, "mytoken") + tokenPath := JoinWithUserDir(".tsuru", "token.d", "test") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, true) + tokenPath = JoinWithUserDir(".tsuru", "token") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, true) +} + +func (s *S) TestReadTokenFileNotFound(c *check.C) { + os.Unsetenv("TSURU_TOKEN") + errFs := &fstest.FileNotFoundFs{} + SetFileSystem(errFs) + defer func() { + ResetFileSystem() + }() + token, err := ReadToken() + c.Assert(err, check.IsNil) + tokenPath := JoinWithUserDir(".tsuru", "token") + c.Assert(err, check.IsNil) + c.Assert(errFs.HasAction("open "+tokenPath), check.Equals, true) + c.Assert(token, check.Equals, "") +} + func initTestTarget() { f, _ := Filesystem().Create(JoinWithUserDir(".tsuru", "target")) f.Write([]byte("http://localhost")) diff --git a/tsuru/config/token_v2.go b/tsuru/config/token_v2.go index 4dda3093..e39dde63 100644 --- a/tsuru/config/token_v2.go +++ b/tsuru/config/token_v2.go @@ -6,7 +6,9 @@ package config import ( "encoding/json" + "os" + "github.com/tsuru/tsuru/fs" "golang.org/x/oauth2" ) @@ -15,17 +17,22 @@ type TokenV2 struct { OAuth2Token *oauth2.Token `json:"oauth2_token"` } +const ( + tokenV2Filename = "token-v2.json" + tokenV2Directory = "token-v2.d" +) + func WriteTokenV2(token TokenV2) error { tokenPaths := []string{ - JoinWithUserDir(".tsuru", "token-v2.json"), + JoinWithUserDir(".tsuru", tokenV2Filename), } targetLabel, err := GetTargetLabel() if err == nil { - err := Filesystem().MkdirAll(JoinWithUserDir(".tsuru", "token-v2.d"), 0700) + err := Filesystem().MkdirAll(JoinWithUserDir(".tsuru", tokenV2Directory), 0700) if err != nil { return err } - tokenPaths = append(tokenPaths, JoinWithUserDir(".tsuru", "token-v2.d", targetLabel+".json")) + tokenPaths = append(tokenPaths, JoinWithUserDir(".tsuru", tokenV2Directory, targetLabel+".json")) } for _, tokenPath := range tokenPaths { @@ -46,3 +53,31 @@ func WriteTokenV2(token TokenV2) error { } return nil } + +func ReadTokenV2() (*TokenV2, error) { + tokenPaths := []string{ + JoinWithUserDir(".tsuru", tokenV2Filename), + } + targetLabel, err := GetTargetLabel() + if err == nil { + tokenPaths = append([]string{JoinWithUserDir(".tsuru", tokenV2Directory, targetLabel+".json")}, tokenPaths...) + } + for _, tokenPath := range tokenPaths { + var tkFile fs.File + tkFile, err = Filesystem().Open(tokenPath) + if err == nil { + defer tkFile.Close() + + t := TokenV2{} + err = json.NewDecoder(tkFile).Decode(&t) + if err != nil { + return nil, err + } + return &t, nil + } + } + if os.IsNotExist(err) { + return nil, nil + } + return nil, err +} diff --git a/tsuru/config/token_v2_test.go b/tsuru/config/token_v2_test.go index cb5c5cd9..9f885c9a 100644 --- a/tsuru/config/token_v2_test.go +++ b/tsuru/config/token_v2_test.go @@ -85,3 +85,68 @@ func (s *S) TestWriteTokenV2WithTarget(c *check.C) { c.Assert(t.OAuth2Token.Expiry, check.Not(check.IsNil)) c.Assert(t.OAuth2Token.RefreshToken, check.Equals, "321") } + +func (s *S) TestReadTokenV2(c *check.C) { + rfs := &fstest.RecordingFs{} + SetFileSystem(rfs) + defer func() { + ResetFileSystem() + }() + initTestTarget() + + f, err := Filesystem().Create(JoinWithUserDir(".tsuru", "token-v2.d", "test.json")) + c.Assert(err, check.IsNil) + f.WriteString(`{ + "scheme": "oidc", + "oauth2_token": { + "access_token": "321", + "refresh_token": "123" + } + }`) + + token, err := ReadTokenV2() + c.Assert(err, check.IsNil) + c.Assert(token, check.DeepEquals, &TokenV2{ + Scheme: "oidc", + OAuth2Token: &oauth2.Token{ + AccessToken: "321", + RefreshToken: "123", + }, + }) + tokenPath := JoinWithUserDir(".tsuru", "token-v2.d", "test.json") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, true) + tokenPath = JoinWithUserDir(".tsuru", "token-v2.json") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, false) +} + +func (s *S) TestReadTokenV2Fallback(c *check.C) { + rfs := &fstest.RecordingFs{} + SetFileSystem(rfs) + defer func() { + ResetFileSystem() + }() + + initTestTarget() + f, err := Filesystem().Create(JoinWithUserDir(".tsuru", "token-v2.json")) + c.Assert(err, check.IsNil) + f.WriteString(`{ + "scheme": "oidc", + "oauth2_token": { + "access_token": "321", + "refresh_token": "123" + } + }`) + token, err := ReadTokenV2() + c.Assert(err, check.IsNil) + c.Assert(token, check.DeepEquals, &TokenV2{ + Scheme: "oidc", + OAuth2Token: &oauth2.Token{ + AccessToken: "321", + RefreshToken: "123", + }, + }) + tokenPath := JoinWithUserDir(".tsuru", "token-v2.d", "test.json") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, true) + tokenPath = JoinWithUserDir(".tsuru", "token-v2.json") + c.Assert(rfs.HasAction("open "+tokenPath), check.Equals, true) +}