Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
572 changes: 0 additions & 572 deletions doc/index.html

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
github.com/antlr4-go/antlr/v4 v4.13.1 // indirect
github.com/klauspost/compress v1.19.2 // indirect
github.com/minio/minlz v1.2.0 // indirect
github.com/stretchr/objx v0.5.3 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa // indirect
golang.org/x/sys v0.47.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ github.com/minio/minlz v1.2.0 h1:6IOBuiHg04QxvbFfgFLT/9sMaO/UhL7S+ApW1mK8q5A=
github.com/minio/minlz v1.2.0/go.mod h1:Ls9H7nlkASeCcdl5thjVD5Eraj6z+zGa7xtq57jIKD4=
github.com/rodaine/protogofakeit v0.1.1 h1:ZKouljuRM3A+TArppfBqnH8tGZHOwM/pjvtXe9DaXH8=
github.com/rodaine/protogofakeit v0.1.1/go.mod h1:pXn/AstBYMaSfc1/RqH3N82pBuxtWgejz1AlYpY1mI0=
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
Expand Down
82 changes: 0 additions & 82 deletions go/client/client-interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ package client

import (
"context"
"fmt"
"log/slog"
"sync"
"sync/atomic"
"time"

"connectrpc.com/connect"
apiv1models "github.com/fi-ts/api/go/fits/api/v1"
)

// authinterceptor adds the required auth headers
Expand Down Expand Up @@ -71,79 +65,3 @@ func (i *loggingInterceptor) WrapStreamingClient(next connect.StreamingClientFun
func (i *loggingInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return next
}

type tokenRenewingInterceptor struct {
config *DialConfig
client *client

renewing atomic.Bool

sync.Mutex
}

func (i *tokenRenewingInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
return connect.UnaryFunc(func(ctx context.Context, request connect.AnyRequest) (connect.AnyResponse, error) {
err := i.renewTokenIfNeeded()
if err != nil {
return nil, err
}
return next(ctx, request)
})
}

func (i *tokenRenewingInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
return next
}

func (i *tokenRenewingInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
return next
}

func (i *tokenRenewingInterceptor) renewTokenIfNeeded() error {
if i.config.expiresAt.IsZero() {
return nil
}
if i.renewing.Load() {
return nil
}
if i.config.Log == nil {
i.config.Log = slog.Default()
}

replaceBefore := i.config.expiresAt.Sub(i.config.issuedAt) / tokenRenewChecksDuringLifetime

if time.Until(i.config.expiresAt) > replaceBefore {
return nil
}

i.renewing.Store(true)
defer i.renewing.Store(false)

i.config.Log.Info("call token refresh, current token expires soon", "expires", i.config.expiresAt.String())

i.Lock()
defer i.Unlock()

resp, err := i.client.Apiv1().Token().Refresh(context.Background(), &apiv1models.TokenServiceRefreshRequest{})
if err != nil {
return fmt.Errorf("unable to refresh token %w", err)
}

i.config.Token = resp.Secret
err = i.config.parse()
if err != nil {
return fmt.Errorf("unable to parse token %w", err)
}

if i.config.TokenRenewal.PersistTokenFn == nil {
return nil
}

err = i.config.TokenRenewal.PersistTokenFn(i.config.Token)
if err != nil {
return fmt.Errorf("unable to persist token %w", err)
}

i.config.Log.Info("token refreshed, new token expires in", "expires", i.config.expiresAt.String())
return nil
}
22 changes: 0 additions & 22 deletions go/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func Test_Client(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
tokenString, err := generateToken(2 * time.Second)
require.NoError(t, err)
var renewedToken string

c, err := client.New(&client.DialConfig{
BaseURL: "http://localhost",
Expand All @@ -50,17 +49,6 @@ func Test_Client(t *testing.T) {
})
},
},
{
WantRequest: &apiv1.TokenServiceRefreshRequest{},
WantResponse: func() connect.AnyResponse {
tokenString, err := generateToken(2 * time.Second)
require.NoError(t, err)

return connect.NewResponse(&apiv1.TokenServiceRefreshResponse{
Secret: tokenString,
})
},
},
{
WantRequest: &apiv1.VersionServiceGetRequest{},
WantResponse: func() connect.AnyResponse {
Expand All @@ -71,12 +59,6 @@ func Test_Client(t *testing.T) {
},
}),
},
TokenRenewal: &client.TokenRenewal{
PersistTokenFn: func(token string) error {
renewedToken = token
return nil
},
},
Log: log,
})

Expand All @@ -85,22 +67,18 @@ func Test_Client(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, v)
require.Equal(t, "1.0", v.Version.Version)
require.Empty(t, renewedToken)

time.Sleep(1 * time.Second)
v, err = c.Apiv1().Version().Get(t.Context(), &apiv1.VersionServiceGetRequest{})
require.NoError(t, err)
require.NotNil(t, v)
require.Equal(t, "1.0", v.Version.Version)
require.Empty(t, renewedToken)

time.Sleep(3 * time.Second)
v, err = c.Apiv1().Version().Get(t.Context(), &apiv1.VersionServiceGetRequest{})
require.NoError(t, err)
require.NotNil(t, v)
require.Equal(t, "1.0", v.Version.Version)
require.NotEmpty(t, renewedToken)
require.NotEqual(t, renewedToken, tokenString, "haven't changed")
})
}

Expand Down
40 changes: 5 additions & 35 deletions go/client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
)

const (
tokenRenewChecksDuringLifetime = 4
tokenFileRereadDuration = 5 * time.Minute
TokenEnvName = "FCO_APIV1_TOKEN"
TokenFileEnvName = "FCO_APIV1_TOKEN_FILE"
BaseURLEnvName = "FCO_APIV1_URL"
TokenEnvName = "FCO_APIV1_TOKEN"
TokenFileEnvName = "FCO_APIV1_TOKEN_FILE"
BaseURLEnvName = "FCO_APIV1_URL"
)

type (
Expand All @@ -34,33 +32,20 @@ type (
// If Tokenfile is specified, Token cannot be specified.
// Token renewal must be done from outside
TokenFile string
// Duration between token file re-reads, optional, defaults to 5min if not specified.
TokenFileRereadDuration time.Duration

// Optional client Interceptors
Interceptors []connect.Interceptor

UserAgent string
// TokenRenewal defines if and how the token should be renewed
TokenRenewal *TokenRenewal

// Transport optional, can be used to configure how the http transport works.
Transport http.RoundTripper

Log *slog.Logger

expiresAt time.Time
issuedAt time.Time
tokenFileLastRead time.Time
expiresAt time.Time
issuedAt time.Time
}

TokenRenewal struct {
// PersistTokenFn is called to persist the newly fetched token
// token will not be persisted if not specified
PersistTokenFn PersistTokenFn
}

PersistTokenFn func(token string) error
)

func New(config *DialConfig) (Client, error) {
Expand All @@ -77,19 +62,11 @@ func New(config *DialConfig) (Client, error) {
if config.Token != "" {
authInterceptor := &authInterceptor{config: config}
c.interceptors = append(c.interceptors, authInterceptor)

if config.TokenRenewal != nil {
tokenRenewingInterceptor := &tokenRenewingInterceptor{config: config, client: c}
c.interceptors = append(c.interceptors, tokenRenewingInterceptor)
}
}

if config.TokenFile != "" {
authInterceptor := &authInterceptor{config: config}
c.interceptors = append(c.interceptors, authInterceptor)

tokenRenewingInterceptor := &tokenRenewingInterceptor{config: config, client: c}
c.interceptors = append(c.interceptors, tokenRenewingInterceptor)
}

if config.Log != nil {
Expand Down Expand Up @@ -136,18 +113,11 @@ func (dc *DialConfig) parse() error {
}

if dc.Token == "" && dc.TokenFile != "" {
if dc.TokenFileRereadDuration == 0 {
dc.TokenFileRereadDuration = tokenFileRereadDuration
}
if dc.TokenFileRereadDuration < time.Minute {
return fmt.Errorf("token file re-read duration must be greater than 1min")
}
content, err := os.ReadFile(dc.TokenFile)
if err != nil {
return err
}
dc.Token = string(content)
dc.tokenFileLastRead = time.Now()
}

if dc.Token == "" && dc.TokenFile == "" {
Expand Down
31 changes: 0 additions & 31 deletions go/client/token-persistent-funcs.go

This file was deleted.

28 changes: 0 additions & 28 deletions go/client/token-persistent-funcs_test.go

This file was deleted.

Loading
Loading