-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REC-110: refactor to remove CacheAlert
This is the beginning of a refactoring to move some LoadStorer implementations into appState. Although CacheAlert satisfied the LoadStorer interface type, it was a decorator that didn't actually load or store any tokens or provide useful or meaningful abstraction. It seems better to squash this into appState and remove unnecessary abstraction. This also makes the test a little more realistic. Also refactored fake token generation to make testing easier.
- Loading branch information
Showing
8 changed files
with
149 additions
and
183 deletions.
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
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,74 @@ | ||
// Copyright 2024 EngFlow Inc. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
// loadToken loads a token for the given cluster or returns an error equivalent | ||
// to fs.ErrNotExist if the token is not found in any store. | ||
// | ||
// loadToken may contain logic specific to this app and should be called | ||
// by commands instead of calling LoadStorer.Load directly. | ||
func (r *appState) loadToken(cluster string) (*oauth2.Token, error) { | ||
return r.tokenStore.Load(cluster) | ||
} | ||
|
||
// storeToken stores a token for the given cluster in one of the backends. | ||
// | ||
// storeToken may contain logic specific to this app and should be called | ||
// by commands instead of calling LoadStorer.Store directly. For example, | ||
// storeToken prints a message if the token's subject has changed. | ||
func (r *appState) storeToken(cluster string, token *oauth2.Token) error { | ||
oldToken, err := r.loadToken(cluster) | ||
if err == nil { | ||
r.warnIfSubjectChanged(cluster, oldToken, token) | ||
} | ||
return r.tokenStore.Store(cluster, token) | ||
} | ||
|
||
// warnIfSubjectChanged prints a warning on stderr if the new token belongs to | ||
// a different user than the previously stored token. The user is reminded to | ||
// shutdown Bazel since it caches tokens in memory to avoid running actions | ||
// with the old credential, which is probably still valid. | ||
func (r *appState) warnIfSubjectChanged(cluster string, oldToken, newToken *oauth2.Token) { | ||
// Disable claims validation, since expired tokens should be allowed to | ||
// parse. | ||
parser := jwt.NewParser(jwt.WithoutClaimsValidation()) | ||
oldClaims, newClaims := &jwt.RegisteredClaims{}, &jwt.RegisteredClaims{} | ||
// Unverified parsing, since issuing a warning vs. not is not a security | ||
// concern. | ||
if _, _, err := parser.ParseUnverified(oldToken.AccessToken, oldClaims); err != nil { | ||
return | ||
} | ||
if _, _, err := parser.ParseUnverified(newToken.AccessToken, newClaims); err != nil { | ||
return | ||
} | ||
if oldClaims.Subject != newClaims.Subject { | ||
fmt.Fprintf(r.stderr, "WARNING: Login identity has changed since last login to %q.\nPlease run `bazel shutdown` in current workspaces in order to ensure bazel picks up new credentials.\n", cluster) | ||
} | ||
} | ||
|
||
// deleteToken removes a token from all of the backends. | ||
// | ||
// deleteToken may contain logic specific to this app and should be called | ||
// by commands instead of calling LoadStorer.Delete directly. | ||
func (r *appState) deleteToken(cluster string) error { | ||
return r.tokenStore.Delete(cluster) | ||
} |
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.