Skip to content

Commit

Permalink
Add ReadTokenV2() to read the new token
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Feb 23, 2024
1 parent 77c699c commit a3032af
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 3 deletions.
57 changes: 57 additions & 0 deletions tsuru/config/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package config

import (
"io"
"os"

"github.com/tsuru/tsuru/fs/fstest"
"gopkg.in/check.v1"
Expand Down Expand Up @@ -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"))
Expand Down
41 changes: 38 additions & 3 deletions tsuru/config/token_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package config

import (
"encoding/json"
"os"

"github.com/tsuru/tsuru/fs"
"golang.org/x/oauth2"
)

Expand All @@ -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 {
Expand All @@ -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
}
65 changes: 65 additions & 0 deletions tsuru/config/token_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a3032af

Please sign in to comment.