-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logout ability (
argocd logout
) (#1582)
- Loading branch information
Showing
7 changed files
with
285 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package commands | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/argoproj/argo-cd/util/localconfig" | ||
) | ||
|
||
const testConfig = `contexts: | ||
- name: argocd.example.com:443 | ||
server: argocd.example.com:443 | ||
user: argocd.example.com:443 | ||
- name: localhost:8080 | ||
server: localhost:8080 | ||
user: localhost:8080 | ||
current-context: localhost:8080 | ||
servers: | ||
- server: argocd.example.com:443 | ||
- plain-text: true | ||
server: localhost:8080 | ||
users: | ||
- auth-token: vErrYS3c3tReFRe$hToken | ||
name: argocd.example.com:443 | ||
refresh-token: vErrYS3c3tReFRe$hToken | ||
- auth-token: vErrYS3c3tReFRe$hToken | ||
name: localhost:8080` | ||
|
||
const testConfigFilePath = "./testdata/config" | ||
|
||
func TestContextDelete(t *testing.T) { | ||
|
||
// Write the test config file | ||
err := ioutil.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
localConfig, err := localconfig.ReadLocalConfig(testConfigFilePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, localConfig.CurrentContext, "localhost:8080") | ||
assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "localhost:8080", Server: "localhost:8080", User: "localhost:8080"}) | ||
|
||
err = deleteContext("localhost:8080", testConfigFilePath) | ||
assert.NoError(t, err) | ||
|
||
localConfig, err = localconfig.ReadLocalConfig(testConfigFilePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, localConfig.CurrentContext, "argocd.example.com:443") | ||
assert.NotContains(t, localConfig.Contexts, localconfig.ContextRef{Name: "localhost:8080", Server: "localhost:8080", User: "localhost:8080"}) | ||
assert.NotContains(t, localConfig.Servers, localconfig.Server{PlainText: true, Server: "localhost:8080"}) | ||
assert.NotContains(t, localConfig.Users, localconfig.User{AuthToken: "vErrYS3c3tReFRe$hToken", Name: "localhost:8080"}) | ||
assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "argocd.example.com:443", Server: "argocd.example.com:443", User: "argocd.example.com:443"}) | ||
|
||
// Write the file again so that no conflicts are made in git | ||
err = ioutil.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
} |
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,50 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/argoproj/argo-cd/errors" | ||
argocdclient "github.com/argoproj/argo-cd/pkg/apiclient" | ||
"github.com/argoproj/argo-cd/util/localconfig" | ||
) | ||
|
||
// NewLogoutCommand returns a new instance of `argocd logout` command | ||
func NewLogoutCommand(globalClientOpts *argocdclient.ClientOptions) *cobra.Command { | ||
var command = &cobra.Command{ | ||
Use: "logout CONTEXT", | ||
Short: "Log out from Argo CD", | ||
Long: "Log out from Argo CD", | ||
Run: func(c *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
c.HelpFunc()(c, args) | ||
os.Exit(1) | ||
} | ||
context := args[0] | ||
|
||
localCfg, err := localconfig.ReadLocalConfig(globalClientOpts.ConfigPath) | ||
errors.CheckError(err) | ||
if localCfg == nil { | ||
log.Fatalf("Nothing to logout from") | ||
} | ||
|
||
ok := localCfg.RemoveToken(context) | ||
if !ok { | ||
log.Fatalf("Context %s does not exist", context) | ||
} | ||
|
||
err = localconfig.ValidateLocalConfig(*localCfg) | ||
if err != nil { | ||
log.Fatalf("Error in logging out: %s", err) | ||
} | ||
err = localconfig.WriteLocalConfig(*localCfg, globalClientOpts.ConfigPath) | ||
errors.CheckError(err) | ||
|
||
fmt.Printf("Logged out from '%s'\n", context) | ||
}, | ||
} | ||
return command | ||
} |
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,39 @@ | ||
package commands | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/argoproj/argo-cd/pkg/apiclient" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/argoproj/argo-cd/util/localconfig" | ||
) | ||
|
||
func TestLogout(t *testing.T) { | ||
|
||
// Write the test config file | ||
err := ioutil.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
localConfig, err := localconfig.ReadLocalConfig(testConfigFilePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, localConfig.CurrentContext, "localhost:8080") | ||
assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "localhost:8080", Server: "localhost:8080", User: "localhost:8080"}) | ||
|
||
command := NewLogoutCommand(&apiclient.ClientOptions{ConfigPath: testConfigFilePath}) | ||
command.Run(nil, []string{"localhost:8080"}) | ||
|
||
localConfig, err = localconfig.ReadLocalConfig(testConfigFilePath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, localConfig.CurrentContext, "localhost:8080") | ||
assert.NotContains(t, localConfig.Users, localconfig.User{AuthToken: "vErrYS3c3tReFRe$hToken", Name: "localhost:8080"}) | ||
assert.Contains(t, localConfig.Contexts, localconfig.ContextRef{Name: "argocd.example.com:443", Server: "argocd.example.com:443", User: "argocd.example.com:443"}) | ||
|
||
// Write the file again so that no conflicts are made in git | ||
err = ioutil.WriteFile(testConfigFilePath, []byte(testConfig), os.ModePerm) | ||
assert.NoError(t, err) | ||
|
||
} |
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,18 @@ | ||
contexts: | ||
- name: argocd.example.com:443 | ||
server: argocd.example.com:443 | ||
user: argocd.example.com:443 | ||
- name: localhost:8080 | ||
server: localhost:8080 | ||
user: localhost:8080 | ||
current-context: localhost:8080 | ||
servers: | ||
- server: argocd.example.com:443 | ||
- plain-text: true | ||
server: localhost:8080 | ||
users: | ||
- auth-token: vErrYS3c3tReFRe$hToken | ||
name: argocd.example.com:443 | ||
refresh-token: vErrYS3c3tReFRe$hToken | ||
- auth-token: vErrYS3c3tReFRe$hToken | ||
name: localhost:8080 |
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