Skip to content

Commit

Permalink
Merge branch 'master' into release-2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
alexec committed Jan 30, 2020
2 parents ed223e9 + b6a2be8 commit af14e45
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ commands:
- dep_ensure
- run:
name: Create KUBECONFIG
# Copy kubeconfig file, and add a fake user for "argo --token xxx" testing
command: |
mkdir -p ~/.kube
cat /etc/rancher/k3s/k3s.yaml | sed "s/127.0.0.1/$(hostname)/g" > ~/.kube/config
echo "- name: fake_token_user" >> ~/.kube/config
echo " user:" >> ~/.kube/config
echo " token: xxxxxx" >> ~/.kube/config
- run:
name: Watch Docker events, to help diagnose failures
command: docker events
Expand Down Expand Up @@ -211,4 +215,4 @@ workflows:
- e2e-no-db
- e2e-postgres
- e2e-mysql
- docker-build
- docker-build
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ STATIC_BUILD ?= true
CI ?= false
DB ?= postgres
K3D := $(shell if [ "`kubectl config current-context`" = "k3s-default" ]; then echo true; else echo false; fi)
ARGO_TOKEN = $(shell kubectl -n argo get secret -o name | grep argo-server | xargs kubectl -n argo get -o jsonpath='{.data.token}' | base64 --decode)

override LDFLAGS += \
-X ${PACKAGE}.version=$(VERSION) \
Expand Down Expand Up @@ -362,7 +363,16 @@ up:
# Wait for pods to be ready
kubectl -n argo wait --for=condition=Ready pod --all -l app --timeout 2m
# Token
kubectl -n argo get `kubectl -n argo get secret -o name | grep argo-server` -o jsonpath='{.data.token}' | base64 --decode
make env

# this is a convenience to get the login token, you can use it as follows
# eval $(make env)
# argo token

.PHONY: env
env:
export ARGO_SERVER=localhost:2746
export ARGO_TOKEN=$(ARGO_TOKEN)

.PHONY: pf
pf:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Currently **officially** using Argo:
1. [BioBox Analytics](https://biobox.io)
1. [BlackRock](https://www.blackrock.com/)
1. [Canva](https://www.canva.com/)
1. [Capital One](https://www.capitalone.com/tech/)
1. [CCRi](https://www.ccri.com/)
1. [Codec](https://www.codec.ai/)
1. [Commodus Tech](https://www.commodus.tech)
Expand Down
4 changes: 2 additions & 2 deletions cmd/argo/commands/client/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func GetClientConn() *grpc.ClientConn {

func GetContext() context.Context {
token := GetBearerToken()
if token == "" {
if len(token) == 0 {
return context.Background()
}
return metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+GetBearerToken()))
return metadata.NewOutgoingContext(context.Background(), metadata.Pairs("authorization", "Bearer "+token))
}

func GetBearerToken() string {
Expand Down
19 changes: 19 additions & 0 deletions test/e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ func (s *CLISuite) TestToken() {
})
}

func (s *CLISuite) TestTokenArg() {
s.Given().RunCli([]string{"list", "--user", "fake_token_user", "--token", "badtoken"}, func(t *testing.T, output string, err error) {
assert.Error(t, err)
})

var goodToken string
s.Run("GetSAToken", func(t *testing.T) {
token, err := s.GetServiceAccountToken()
assert.NoError(t, err)
goodToken = token
})

s.Given().RunCli([]string{"list", "--user", "fake_token_user", "--token", goodToken}, func(t *testing.T, output string, err error) {
assert.NoError(t, err)
assert.Contains(t, output, "NAME")
assert.Contains(t, output, "STATUS")
})
}

func (s *CLISuite) TestRoot() {
s.Given().RunCli([]string{"submit", "smoke/basic.yaml"}, func(t *testing.T, output string, err error) {
assert.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions util/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ func GetRestConfig(token string) (*restclient.Config, error) {
// convert the REST config into a bearer token
func GetBearerToken(in *restclient.Config) (string, error) {

if len(in.BearerToken) > 0 {
return in.BearerToken, nil
}

if token := getEnvToken(); token != "" {
return token, nil
}

if in == nil {
return "", errors.Errorf("RestClient can't be nil")
}
if in.ExecProvider != nil {
tc, err := in.TransportConfig()
if err != nil {
Expand Down Expand Up @@ -99,7 +100,7 @@ func GetBearerToken(in *restclient.Config) (string, error) {
return strings.TrimPrefix(token, "Bearer "), nil
}
}
return in.BearerToken, nil
return "", errors.Errorf("Can not find a token")
}

// Get the Auth token from environment variable
Expand Down
27 changes: 16 additions & 11 deletions util/kubeconfig/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,34 @@ func Test_getDefaultTokenVersion(t *testing.T) {

t.Run("No token", func(t *testing.T) {
restConfig, err := clientcmd.DefaultClientConfig.ClientConfig()
os.Unsetenv("ARGO_TOKEN")
assert.NoError(t, err)
token, err := GetBearerToken(restConfig)
assert.NoError(t, err)
assert.Equal(t, restConfig.BearerToken, token)
restConfig.BearerToken = ""
envToken := os.Getenv("ARGO_TOKEN")
os.Unsetenv("ARGO_TOKEN")
defer os.Setenv("ARGO_TOKEN", envToken)
_, err = GetBearerToken(restConfig)
assert.Error(t, err)
})
t.Run("Env token", func(t *testing.T) {
t.Run("Existing token", func(t *testing.T) {
restConfig, err := clientcmd.DefaultClientConfig.ClientConfig()
assert.NoError(t, err)
restConfig.BearerToken = "test12"
envToken := os.Getenv("ARGO_TOKEN")
os.Setenv("ARGO_TOKEN", "test")
defer os.Unsetenv("ARGO_TOKEN")
defer os.Setenv("ARGO_TOKEN", envToken)
token, err := GetBearerToken(restConfig)
assert.NoError(t, err)
assert.Equal(t, "test", token)
assert.Equal(t, "test12", token)
})
t.Run("RestConfig token", func(t *testing.T) {
t.Run("Env token", func(t *testing.T) {
restConfig, err := clientcmd.DefaultClientConfig.ClientConfig()
os.Unsetenv("ARGO_TOKEN")
assert.NoError(t, err)
restConfig.BearerToken = "test"
restConfig.BearerToken = ""
envToken := os.Getenv("ARGO_TOKEN")
os.Setenv("ARGO_TOKEN", "test")
defer os.Setenv("ARGO_TOKEN", envToken)
token, err := GetBearerToken(restConfig)
assert.NoError(t, err)
assert.Equal(t, restConfig.BearerToken, token)
assert.Equal(t, "test", token)
})
}

0 comments on commit af14e45

Please sign in to comment.