-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
patch kubeconfig if token cannot be deleted via api #12962
patch kubeconfig if token cannot be deleted via api #12962
Conversation
[test] |
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.
Not sure the logic makes sense to me -- oc logout
should always remove the token from your .kube/config
but should delete it using the API as a best-effort attempt.
If this does end up being the accepted logic, you should break this out into a separate method at the very least, four levels of nested logic smells and and doesn't read well for someone trying to glance over RunLogout()
.
b8347c2
to
44902a7
Compare
@stevekuznetsov thanks for the feedback, I extracted out existing code to patch config into a separate function, which is now being called before attempting to delete using api. PTAL |
pkg/cmd/cli/cmd/login/logout.go
Outdated
// before deleting using api. | ||
tokenDeleted, err := o.deleteTokenFromConfig() | ||
if err != nil { | ||
return err |
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.
Is the ordering here important? Not sure we want to be in a situation where we have deleted the token from config but failed before trying to delete it using the API.
pkg/cmd/cli/cmd/login/logout.go
Outdated
value.Token = "" | ||
newConfig.AuthInfos[key] = value | ||
tokenDeleted = true |
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.
Doesn't this just mean we modified the in-memory copy of the config? Whereas the kclientcmd.ModifyConfig
call later is what actually modifies the config on disk?
a6ab352
to
c93f914
Compare
re[test] |
0168440
to
350cf33
Compare
pkg/cmd/cli/cmd/login/logout.go
Outdated
return err | ||
errs := []error{} | ||
serverErr := client.OAuthAccessTokens().Delete(token) | ||
if serverErr != nil { |
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.
NewAggregate
does this for you
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.
I need to not append nil
to the errs
slice at the end in order to print a successful message or not by checking if len(errs) == 0 ...
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.
Just check if the output of Flatten
is nil
or not
pkg/cmd/cli/cmd/login/logout.go
Outdated
fmt.Fprintf(o.ErrOut, "%s\n\n", "An error ocurred deleting the token on the server. The token has been removed from your local configuration.") | ||
} | ||
|
||
if len(errs) > 0 { |
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.
Flatten
does this for you
350cf33
to
5af4679
Compare
pkg/cmd/cli/cmd/login/logout.go
Outdated
} else if serverErr != nil { | ||
// if token was not able to be deleted using api but was successfully removed | ||
// from client config, print brief explanation that token was still removed locally | ||
fmt.Fprintf(o.ErrOut, "%s\n\n", "An error ocurred deleting the token on the server. The token has been removed from your local configuration.") |
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 this also applies to cases outside of being logged in with a serviceaccount token, I am not sure how I feel about returning an error message everytime a user tries to log out when using a serviceaccount token. cc @fabianofranz
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.
I would make it a log with low level, but not print it by default.
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.
@fabianofranz Thanks, updated this line to glog
instead. It will still print the server error (if any) however, just in case it is unrelated to the token not being found:
$ oc logout --loglevel=1
I0216 09:42:03.042611 13554 logout.go:137] An error ocurred deleting the token on the server. The token has been removed from your local configuration.
Error from server (NotFound): oauthaccesstokens "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRlZmF1bHQtdG9rZW4tNjU3bjQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGVmYXVsdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjVmMzk0Y2I0LWYzYWYtMTFlNi1iNDUxLTUwN2I5ZGFjOTZlMSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0OmRlZmF1bHQifQ.c7TiHikwCsobsMTIYhB6O-FjRca6TJmccpN4jem4oKWh9UTE8SxjqnCtrjZe_-zJ0eZ-GovMgiaa5L0xcilcvnj7htKeT3XeycKcUzxYCGJj884UL-Ouu4dh0-vJElvEMu6AlpwxmaES99-aJbghaTOssIIVFQdP9-AsYA2iVS1BBbeCWLw7yrE1gRXVhSjRhntpUwr-OBGO7fV8_ky96FjmUltADw0cynZr6P_NN_DWZYrEhaK7TuAbc-14K9uzLqcAiG_RLJfR7fOUvjPBp9kQDfQ13PczYhvMhXxluMma7WyXOrsB9ia2D2dXJ2SIA4a6tuL_8K_7UPaiO-an9Q" not found
Flake was
@Kargakis @mfojtik failed when deploying, looks familiar but has never happened in gce before |
de163de
to
93976dd
Compare
e973706
to
f37ba7b
Compare
@fabianofranz thanks! |
re[test] |
@stevekuznetsov anything else here? |
pkg/cmd/cli/cmd/login/logout.go
Outdated
} else if serverErr != nil { | ||
// if token was not able to be deleted using api but was successfully removed | ||
// from client config, log brief explanation that token was still removed locally | ||
glog.V(1).Infof("%s\n\n", "An error ocurred deleting the token on the server. The token has been removed from your local configuration.") |
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.
You're doing a glog.V(1).Infof
with the flattened error below, so you'll always get a log about the delete failure. You should just do this here:
if configErr := deleteTokenFromConfig(*o.StartingKubeConfig, o.PathOptions, token); configErr == nil {
glog.V(1).Info("Removed token from your local configuration.\n\n")
}
errs = append(errs, configErr)
pkg/cmd/cli/cmd/login/logout.go
Outdated
|
||
for key, value := range newConfig.AuthInfos { | ||
if value.Token == token { | ||
return nil |
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.
The logic now is -- we fail to delete from API server, and we fail to delete from config, but we print the logout message and return nil
so we exit successfully. Are both of these operations meant to be best-effort?
pkg/cmd/cli/cmd/login/logout.go
Outdated
// don't break, its possible that more than one user stanza has the same token. | ||
} | ||
} | ||
|
||
if err := kclientcmd.ModifyConfig(o.PathOptions, newConfig, true); err != nil { | ||
if err := kclientcmd.ModifyConfig(pathOptions, config, true); err != nil { |
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.
Don't do
if err != nil {
return err
}
return nil
just do
return err
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.
This is outstanding @juanvallejo
Updated this to print |
7a16da2
to
1d346e7
Compare
re[test] |
1d346e7
to
98d990d
Compare
pkg/cmd/cli/cmd/login/logout.go
Outdated
if err := client.OAuthAccessTokens().Delete(token); err != nil { | ||
return err | ||
serverErr := client.OAuthAccessTokens().Delete(token) | ||
if serverErr != nil { |
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.
You don't need to leak serverErr
out of this scope anymore
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.
Thanks, fixed
98d990d
to
a280ea5
Compare
remove token from config on error / success from api req log server error to prevent failure on sa logout
a280ea5
to
bf1f4e2
Compare
@stevekuznetsov thanks for the feedback, review comments addressed |
pkg/cmd/cli/cmd/login/logout.go
Outdated
newConfig := *o.StartingKubeConfig | ||
configErr := deleteTokenFromConfig(*o.StartingKubeConfig, o.PathOptions, token) | ||
if configErr == nil { | ||
glog.V(1).Infof("Removed token from your local configuration.\n\n") |
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.
Don't \n
in glog
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.
One small nit, otherwise LGTM
Evaluated for origin test up to 3c0eb53 |
continuous-integration/openshift-jenkins/test SUCCESS (https://ci.openshift.redhat.com/jenkins/job/test_pull_requests_origin_future/365/) (Base Commit: 3f36d2e) |
|
Evaluated for origin merge up to 3c0eb53 |
continuous-integration/openshift-jenkins/merge SUCCESS (https://ci.openshift.redhat.com/jenkins/job/test_pull_requests_origin_future/365/) (Base Commit: 3ef5377) (Image: devenv-rhel7_5945) |
Fixes #7011
Related bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1422252
Deletes a token in a kubeconfig user stanza even if the token could not be deleted via the API.
cc @openshift/cli-review @stevekuznetsov @deads2k