You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ke...@apache.org on 2020/12/12 13:38:55 UTC

[skywalking-cli] 01/01: feature: support authorization when connecting to the OAP

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch feature/auth
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git

commit 606deec893c4310631ff230be340929bd288d7fc
Author: kezhenxu94 <ke...@apache.org>
AuthorDate: Sat Dec 12 21:38:31 2020 +0800

    feature: support authorization when connecting to the OAP
---
 Makefile                 |  2 +-
 README.md                |  3 +++
 cmd/main.go              | 18 ++++++++++++++++++
 go.mod                   |  4 ++--
 go.sum                   |  8 ++++----
 graphql/client/client.go | 11 +++++++++++
 6 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile
index 73535d2..daf42b4 100644
--- a/Makefile
+++ b/Makefile
@@ -85,7 +85,7 @@ $(PLATFORMS):
 lint: codegen tools
 	$(GO_LINT) run -v ./...
 
-.PHONE: test
+.PHONY: test
 test: clean codegen lint
 	$(GO_TEST) ./... -coverprofile=coverage.txt -covermode=atomic
 
diff --git a/README.md b/README.md
index 3af15ca..0c28b09 100644
--- a/README.md
+++ b/README.md
@@ -85,6 +85,9 @@ This section covers all the available commands in SkyWalking CLI and their usage
 | `--config` | from where the default options values will be loaded | `~/.skywalking.yml` |
 | `--debug` | enable debug mode, will print more detailed information at runtime | `false` |
 | `--base-url` | base url of GraphQL backend | `http://127.0.0.1:12800/graphql` |
+| `--username` | username of `Basic` authorization | `` |
+| `--password` | password of `Basic` authorization | `` |
+| `--authorization` | authorization header, can be something like `Basic base64<username:password>` or `Bearer jwt-token`, if `authorization` is set, `username` and `password` are ignored | `` |
 | `--display` | display style when printing the query result, supported styles are: `json`, `yaml`, `table`, `graph` | `json` |
 
 Note that not all display styles (except for `json` and `yaml`) are supported in all commands due to data formats incompatibilities and the limits of
diff --git a/cmd/main.go b/cmd/main.go
index 40e81ae..4a57536 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -66,6 +66,24 @@ func main() {
 			Value:    "http://127.0.0.1:12800/graphql",
 		}),
 		altsrc.NewStringFlag(cli.StringFlag{
+			Name:     "username",
+			Required: false,
+			Usage:    "username of basic authorization",
+			Value:    "",
+		}),
+		altsrc.NewStringFlag(cli.StringFlag{
+			Name:     "password",
+			Required: false,
+			Usage:    "password of basic authorization",
+			Value:    "",
+		}),
+		altsrc.NewStringFlag(cli.StringFlag{
+			Name:     "authorization",
+			Required: false,
+			Usage:    "authorization to the OAP backend",
+			Value:    "",
+		}),
+		altsrc.NewStringFlag(cli.StringFlag{
 			Name:     "timezone",
 			Required: false,
 			Hidden:   true,
diff --git a/go.mod b/go.mod
index f2c6782..caf1f12 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@ require (
 	github.com/machinebox/graphql v0.2.2
 	github.com/matryer/is v1.4.0 // indirect
 	github.com/mattn/go-runewidth v0.0.9
-	github.com/mitchellh/mapstructure v1.3.3 // indirect
+	github.com/mitchellh/mapstructure v1.4.0 // indirect
 	github.com/mum4k/termdash v0.12.1
 	github.com/olekukonko/tablewriter v0.0.2
 	github.com/pkg/errors v0.9.1 // indirect
@@ -22,5 +22,5 @@ require (
 	golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
 	google.golang.org/grpc v1.24.0
 	gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
-	gopkg.in/yaml.v2 v2.3.0
+	gopkg.in/yaml.v2 v2.4.0
 )
diff --git a/go.sum b/go.sum
index 9d30596..e44a9b5 100644
--- a/go.sum
+++ b/go.sum
@@ -147,8 +147,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
 github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
 github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
 github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
-github.com/mitchellh/mapstructure v1.3.3 h1:SzB1nHZ2Xi+17FP0zVQBHIZqvwRN9408fJO8h+eeNA8=
-github.com/mitchellh/mapstructure v1.3.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
+github.com/mitchellh/mapstructure v1.4.0 h1:7ks8ZkOP5/ujthUsT07rNv+nkLXCQWKNHuwzOAesEks=
+github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
 github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
 github.com/mum4k/termdash v0.12.1 h1:g3WAT602WIYII6Szhn2KsGoT4PffJZQoA4aAFxEllKc=
@@ -364,8 +364,8 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
 gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
-gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
-gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
diff --git a/graphql/client/client.go b/graphql/client/client.go
index a14f2f8..ebadd9c 100644
--- a/graphql/client/client.go
+++ b/graphql/client/client.go
@@ -19,6 +19,7 @@ package client
 
 import (
 	"context"
+	"encoding/base64"
 
 	"github.com/machinebox/graphql"
 	"github.com/urfave/cli"
@@ -36,6 +37,16 @@ func newClient(cliCtx *cli.Context) (client *graphql.Client) {
 
 // ExecuteQuery executes the `request` and parse to the `response`, returning `error` if there is any.
 func ExecuteQuery(cliCtx *cli.Context, request *graphql.Request, response interface{}) error {
+	username := cliCtx.GlobalString("username")
+	password := cliCtx.GlobalString("password")
+	authorization := cliCtx.GlobalString("authorization")
+	if authorization == "" && username != "" && password != "" {
+		authorization = "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
+	}
+	if authorization != "" {
+		request.Header.Set("Authorization", authorization)
+	}
+
 	client := newClient(cliCtx)
 	ctx := context.Background()
 	err := client.Run(ctx, request, response)