Skip to content

Commit

Permalink
Add auth token refresh to RichClient
Browse files Browse the repository at this point in the history
There was no logic to refresh auth token needed for longer-time tasks
(e.g. tests). Adding this method to RichCLient.

To be able use Token Refresh method, it was needed keep
whole api.Login struct as part of client to have refresh token.

Related to konveyor/go-konveyor-tests#71

Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed Jan 12, 2024
1 parent 951cb48 commit 54b1667
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
6 changes: 4 additions & 2 deletions addon/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ Tackle hub/addon integration.
package addon

import (
"os"

logapi "github.com/go-logr/logr"
"github.com/jortel/go-utils/logr"
"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/settings"
"github.com/konveyor/tackle2-hub/task"
"golang.org/x/sys/unix"
"os"
)

var (
Expand Down Expand Up @@ -138,7 +140,7 @@ func (h *Adapter) Run(addon func() error) {
// newAdapter builds a new Addon Adapter object.
func newAdapter() (adapter *Adapter) {
richClient := binding.New(Settings.Addon.Hub.URL)
richClient.Client.SetToken(Settings.Addon.Hub.Token)
richClient.Client.SetToken(api.Login{Token: Settings.Addon.Hub.Token})
adapter = &Adapter{
client: richClient.Client,
Task: Task{
Expand Down
11 changes: 7 additions & 4 deletions binding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s Path) Inject(p Params) (out string) {

//
// NewClient Constructs a new client
func NewClient(url, token string) (client *Client) {
func NewClient(url string, token api.Login) (client *Client) {
client = &Client{
baseURL: url,
token: token,
Expand All @@ -90,7 +90,7 @@ type Client struct {
// baseURL for the nub.
baseURL string
// addon API token
token string
token api.Login
// transport
transport http.RoundTripper
// Retry limit.
Expand All @@ -101,7 +101,7 @@ type Client struct {

//
// SetToken sets hub token on client
func (r *Client) SetToken(token string) {
func (r *Client) SetToken(token api.Login) {
r.token = token
}

Expand Down Expand Up @@ -648,6 +648,9 @@ func (r *Client) IsDir(path string, must bool) (b bool, err error) {
// Send the request.
// Resilient against transient hub availability.
func (r *Client) send(rb func() (*http.Request, error)) (response *http.Response, err error) {
// refresh token before or after actual API request when its expiration time gets under a threshold (~1 minute, expecting 5 mins token lifetime and waiting loops in e.g. tests with sleep in few to few tents of seconds)
// _or_
// start a goroutine on client creation with token refresh
var request *http.Request
if r.Error != nil {
err = r.Error
Expand All @@ -662,7 +665,7 @@ func (r *Client) send(rb func() (*http.Request, error)) (response *http.Response
if err != nil {
return
}
request.Header.Set(api.Authorization, r.token)
request.Header.Set(api.Authorization, r.token.Token)
client := http.Client{Transport: r.transport}
response, err = client.Do(request)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions binding/richclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type RichClient struct {
func New(baseUrl string) (r *RichClient) {
//
// Build REST client.
client := NewClient(baseUrl, "")
client := NewClient(baseUrl, api.Login{})

//
// Build RichClient.
Expand Down Expand Up @@ -143,6 +143,18 @@ func (r *RichClient) Login(user, password string) (err error) {
if err != nil {
return
}
r.Client.SetToken(login.Token)
r.Client.SetToken(login)
return
}

//
// Refresh client token.
func (r *RichClient) RefreshToken() (err error) {
login := api.Login{Refresh: r.Client.token.Refresh}
err = r.Client.Post(api.AuthRefreshRoot, &login)
if err != nil {
return
}
r.Client.SetToken(login)
return
}
7 changes: 7 additions & 0 deletions test/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"fmt"
"os"
"time"

"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/settings"
Expand All @@ -22,6 +23,12 @@ func PrepareRichClient() (richClient *binding.RichClient) {
// Prepare RichClient and login to Hub API
richClient = binding.New(settings.Settings.Addon.Hub.URL)
err := richClient.Login(os.Getenv(Username), os.Getenv(Password))

// Start goroutine with token refresh.
go func () {
time.Sleep(1 * time.Minute) // TODO: base sleep time on token expiration
richClient.RefreshToken()
}()

if err != nil {
panic(fmt.Sprintf("Cannot login to API: %v.", err.Error()))
Expand Down

0 comments on commit 54b1667

Please sign in to comment.