-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(backend) Enable auth between pesistence agent and pipelineAPI (ReportServer) #9699
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package client | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type TokenRefresherInterface interface { | ||
GetToken() string | ||
RefreshToken() error | ||
} | ||
|
||
const SaTokenFile = "/var/run/secrets/kubeflow/tokens/persistenceagent-sa-token" | ||
|
||
type FileReader interface { | ||
ReadFile(filename string) ([]byte, error) | ||
} | ||
|
||
type tokenRefresher struct { | ||
mu sync.RWMutex | ||
seconds *time.Duration | ||
token string | ||
fileReader *FileReader | ||
} | ||
|
||
type FileReaderImpl struct{} | ||
|
||
func (r *FileReaderImpl) ReadFile(filename string) ([]byte, error) { | ||
return os.ReadFile(filename) | ||
} | ||
|
||
func NewTokenRefresher(seconds time.Duration, fileReader FileReader) *tokenRefresher { | ||
if fileReader == nil { | ||
fileReader = &FileReaderImpl{} | ||
} | ||
|
||
tokenRefresher := &tokenRefresher{ | ||
seconds: &seconds, | ||
fileReader: &fileReader, | ||
} | ||
|
||
return tokenRefresher | ||
} | ||
|
||
func (tr *tokenRefresher) StartTokenRefreshTicker() error { | ||
err := tr.RefreshToken() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ticker := time.NewTicker(*tr.seconds) | ||
go func() { | ||
for range ticker.C { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was unsure about the use of the |
||
tr.RefreshToken() | ||
} | ||
}() | ||
return err | ||
} | ||
|
||
func (tr *tokenRefresher) GetToken() string { | ||
tr.mu.RLock() | ||
defer tr.mu.RUnlock() | ||
return tr.token | ||
} | ||
|
||
func (tr *tokenRefresher) RefreshToken() error { | ||
tr.mu.Lock() | ||
defer tr.mu.Unlock() | ||
b, err := (*tr.fileReader).ReadFile(SaTokenFile) | ||
if err != nil { | ||
log.Errorf("Error reading persistence agent service account token '%s': %v", SaTokenFile, err) | ||
return err | ||
} | ||
tr.token = string(b) | ||
return nil | ||
} |
111 changes: 111 additions & 0 deletions
111
backend/src/agent/persistence/client/token_refresher_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"log" | ||
"syscall" | ||
"testing" | ||
"time" | ||
) | ||
|
||
const refreshInterval = 2 * time.Second | ||
|
||
type FileReaderFake struct { | ||
Data string | ||
Err error | ||
readCounter int | ||
} | ||
|
||
func (m *FileReaderFake) ReadFile(filename string) ([]byte, error) { | ||
if m.Err != nil { | ||
return nil, m.Err | ||
} | ||
content := fmt.Sprintf("%s-%v", m.Data, m.readCounter) | ||
m.readCounter++ | ||
return []byte(content), nil | ||
} | ||
|
||
func Test_token_refresher(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
baseToken string | ||
wanted string | ||
refreshedToken string | ||
err error | ||
}{ | ||
{ | ||
name: "TestTokenRefresher_GetToken_Success", | ||
baseToken: "rightToken", | ||
wanted: "rightToken-0", | ||
err: nil, | ||
}, | ||
{ | ||
name: "TestTokenRefresher_GetToken_Failed_PathError", | ||
baseToken: "rightToken", | ||
wanted: "rightToken-0", | ||
err: &fs.PathError{Err: syscall.ENOENT}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// setup | ||
fakeFileReader := &FileReaderFake{ | ||
Data: tt.baseToken, | ||
Err: tt.err, | ||
} | ||
tr := NewTokenRefresher(refreshInterval, fakeFileReader) | ||
err := tr.StartTokenRefreshTicker() | ||
if err != nil { | ||
got, sameType := err.(*fs.PathError) | ||
if sameType != true { | ||
t.Errorf("%v(): got = %v, wanted %v", tt.name, got, tt.err) | ||
} | ||
return | ||
} | ||
if err != nil { | ||
log.Fatalf("Error starting Service Account Token Refresh Ticker: %v", err) | ||
} | ||
|
||
if got := tr.GetToken(); got != tt.wanted { | ||
t.Errorf("%v(): got %v, wanted %v", tt.name, got, tt.wanted) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTokenRefresher_GetToken_After_TickerRefresh_Success(t *testing.T) { | ||
fakeFileReader := &FileReaderFake{ | ||
Data: "Token", | ||
Err: nil, | ||
} | ||
tr := NewTokenRefresher(1*time.Second, fakeFileReader) | ||
err := tr.StartTokenRefreshTicker() | ||
if err != nil { | ||
log.Fatalf("Error starting Service Account Token Refresh Ticker: %v", err) | ||
} | ||
time.Sleep(1200 * time.Millisecond) | ||
expectedToken := "Token-1" | ||
|
||
if got := tr.GetToken(); got != expectedToken { | ||
t.Errorf("%v(): got %v, wanted 'refreshed baseToken' %v", t.Name(), got, expectedToken) | ||
} | ||
} | ||
|
||
func TestTokenRefresher_GetToken_After_ForceRefresh_Success(t *testing.T) { | ||
fakeFileReader := &FileReaderFake{ | ||
Data: "Token", | ||
Err: nil, | ||
} | ||
tr := NewTokenRefresher(refreshInterval, fakeFileReader) | ||
err := tr.StartTokenRefreshTicker() | ||
if err != nil { | ||
log.Fatalf("Error starting Service Account Token Refresh Ticker: %v", err) | ||
} | ||
tr.RefreshToken() | ||
expectedToken := "Token-1" | ||
|
||
if got := tr.GetToken(); got != expectedToken { | ||
t.Errorf("%v(): got %v, wanted 'refreshed baseToken' %v", t.Name(), got, expectedToken) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I see you're following an existing pattern, for my education how would user discover such error? Would this be surfaced back by apiserver?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! Sorry for my late response.
The user could observe the status of the Run and inspect the logs of the persistent agent. The errors are not directly displayed on the UI.
If a "transient" error occurs the Persistent Agent will retry with an exponential delay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, If an error occurs on token refresh, I just log the error and let the worker thread retries. Any other suggestions on what to do if the refresh token fails?