Skip to content

Commit a69d716

Browse files
authored
cmd: add signature command to calculate ucloud signature (#63)
1 parent 08672be commit a69d716

58 files changed

Lines changed: 5363 additions & 309 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.40 (2022-08-31)
2+
3+
* add `signature` command to calculate signature quickly.
4+
15
## 0.1.39 (2022-06-13)
26

37
* fix init failure when user donot have a default project in ucloud console.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export VERSION=0.1.39
1+
export VERSION=0.1.40
22
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
33

44
.PHONY : install

cmd/pathx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func NewCmdUGA3Create(out io.Writer) *cobra.Command {
174174
bindRegion(createPathxReq, flags)
175175
bindZone(createPathxReq, flags)
176176

177-
createPathxReq.Bandwidth = flags.String("bandwidth", "",
177+
createPathxReq.Bandwidth = flags.Int("bandwidth", 0,
178178
"Required. Shared bandwidth of the resource")
179179
flags.String("area-code", "",
180180
"Optional. When it is empty,the nearest zone will be selected based on the origin-domain and origin-ip. "+

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func addChildren(root *cobra.Command) {
129129
root.AddCommand(NewCmdMemcache())
130130
root.AddCommand(NewCmdExt())
131131
root.AddCommand(NewCmdAPI(out))
132+
root.AddCommand(NewCmdSignature())
132133
for _, c := range root.Commands() {
133134
if c.Name() != "init" && c.Name() != "gendoc" && c.Name() != "config" {
134135
c.PersistentFlags().StringVar(&global.PublicKey, "public-key", global.PublicKey, "Set public-key to override the public-key in local config file")

cmd/signature.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"net/url"
7+
"strings"
8+
9+
"github.com/fatih/color"
10+
"github.com/spf13/cobra"
11+
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
12+
)
13+
14+
func NewCmdSignature() *cobra.Command {
15+
var (
16+
rawParams []string
17+
privateKey string
18+
rawURL string
19+
)
20+
cmd := &cobra.Command{
21+
Use: "signature",
22+
Short: "Calculate ucloud signature",
23+
Long: "Calculate ucloud signature",
24+
25+
Aliases: []string{"sign"},
26+
27+
Run: func(cmd *cobra.Command, args []string) {
28+
var params map[string]interface{}
29+
if rawURL != "" {
30+
// Parse params from exists url
31+
parsedURL, err := url.Parse(rawURL)
32+
if err != nil {
33+
fmt.Printf("error: failed to parse url %q: %v\n", rawURL, err)
34+
return
35+
}
36+
query := parsedURL.Query()
37+
params = make(map[string]interface{}, len(query))
38+
for key, values := range query {
39+
if key == "Signature" {
40+
fmt.Println("error: the `Signature` cannot be placed in url")
41+
return
42+
}
43+
if len(values) == 0 {
44+
continue
45+
}
46+
val := values[0]
47+
params[key] = val
48+
}
49+
}
50+
if len(rawParams) > 0 {
51+
if params == nil {
52+
params = make(map[string]interface{}, len(rawParams))
53+
}
54+
for _, rawParam := range rawParams {
55+
kv := strings.Split(rawParam, "=")
56+
if len(kv) != 2 {
57+
fmt.Printf("error: param %q is invalid\n", rawParam)
58+
return
59+
}
60+
params[kv[0]] = kv[1]
61+
}
62+
}
63+
if len(params) == 0 {
64+
fmt.Println("error: missing param")
65+
return
66+
}
67+
68+
r := auth.CalculateSignature(params, privateKey)
69+
70+
var colorParamBuf bytes.Buffer
71+
for _, key := range r.SortedKeys {
72+
val := params[key]
73+
colorParamBuf.WriteString(color.GreenString(key))
74+
colorParamBuf.WriteString(color.CyanString("%v", val))
75+
}
76+
colorParamBuf.WriteString(color.MagentaString(privateKey))
77+
fmt.Println("")
78+
fmt.Printf("ParamStr: %s\n", colorParamBuf.String())
79+
fmt.Println("")
80+
81+
fmt.Printf("Signature: %s\n", color.BlueString(r.Sign))
82+
},
83+
}
84+
85+
flags := cmd.Flags()
86+
flags.SortFlags = false
87+
flags.StringArrayVarP(&rawParams, "param", "m", nil, "Request params")
88+
flags.StringVarP(&privateKey, "private-key", "k", "", "Private key")
89+
flags.StringVarP(&rawURL, "url", "u", "", "Request url without signature")
90+
cmd.MarkFlagRequired("private-key")
91+
92+
return cmd
93+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ module github.com/ucloud/ucloud-cli
33
go 1.12
44

55
require (
6+
github.com/fatih/color v1.13.0
67
github.com/kr/pretty v0.1.0 // indirect
78
github.com/satori/go.uuid v1.2.0
89
github.com/sirupsen/logrus v1.3.0
910
github.com/spf13/cobra v0.0.3
1011
github.com/spf13/pflag v1.0.3
11-
github.com/ucloud/ucloud-sdk-go v0.21.21
12+
github.com/ucloud/ucloud-sdk-go v0.21.42
1213
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
1314
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
1415
)

go.sum

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF
66
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
10+
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
911
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
1012
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1113
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
@@ -22,6 +24,11 @@ github.com/lixiaojun629/cobra v0.0.10/go.mod h1:6VKYqzoixuRlMBmzm3rHPS0sRYVhT3zX
2224
github.com/lixiaojun629/pflag v1.0.5 h1:plFJ2SBJd2S2Fc7ZwwFZ3682IvxBiUkhRuJS40OvEMs=
2325
github.com/lixiaojun629/pflag v1.0.5/go.mod h1:uchrjsiFxJj1XOBpO4YJCZwpqXHsCHovxY91tyFoUrg=
2426
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
27+
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
28+
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
29+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
30+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
31+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
2532
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
2633
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
2734
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
@@ -42,8 +49,8 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
4249
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4350
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
4451
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
45-
github.com/ucloud/ucloud-sdk-go v0.21.21 h1:c4UtP8dR3vfYY9bzxaOtRc3L1jdv9R7GteomTqI3vV8=
46-
github.com/ucloud/ucloud-sdk-go v0.21.21/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
52+
github.com/ucloud/ucloud-sdk-go v0.21.42 h1:WGFpTRrnAL6yEZVJsZxNH5kSMNQNCYBsB8pq159QRG4=
53+
github.com/ucloud/ucloud-sdk-go v0.21.42/go.mod h1:dyLmFHmUfgb4RZKYQP9IArlvQ2pxzFthfhwxRzOEPIw=
4754
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
4855
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
4956
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -56,6 +63,9 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
5663
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5764
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5865
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
66+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
67+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
68+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5969
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
6070
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6171
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

vendor/github.com/fatih/color/LICENSE.md

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fatih/color/README.md

Lines changed: 178 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)