Skip to content

Commit 904ccb2

Browse files
authored
Merge pull request kubernetes#3620 from RainbowMango/pr_sdk_on_board
CA: upload huaweicloud new sdk
2 parents bfc69dc + a3eaf8d commit 904ccb2

File tree

384 files changed

+20783
-0
lines changed

Some content is hidden

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

384 files changed

+20783
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Copyright (c) Huawei Technologies Co., Ltd. 2020-present. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
15+
16+
17+
Copyright of core/signer/escape.go
18+
19+
Copyright (c) 2009 The Go Authors. All rights reserved.
20+
21+
Redistribution and use in source and binary forms, with or without
22+
modification, are permitted provided that the following conditions are
23+
met:
24+
25+
* Redistributions of source code must retain the above copyright
26+
notice, this list of conditions and the following disclaimer.
27+
* Redistributions in binary form must reproduce the above
28+
copyright notice, this list of conditions and the following disclaimer
29+
in the documentation and/or other materials provided with the
30+
distribution.
31+
* Neither the name of Google Inc. nor the names of its
32+
contributors may be used to endorse or promote products derived from
33+
this software without specific prior written permission.
34+
35+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46+
47+
48+
49+
Copyright of core/signer/signer.go
50+
51+
Copyright (c) 2014, Xianjie
52+
All rights reserved.
53+
54+
Redistribution and use in source and binary forms, with or without modification,
55+
are permitted provided that the following conditions are met:
56+
57+
* Redistributions of source code must retain the above copyright notice, this
58+
list of conditions and the following disclaimer.
59+
60+
* Redistributions in binary form must reproduce the above copyright notice, this
61+
list of conditions and the following disclaimer in the documentation and/or
62+
other materials provided with the distribution.
63+
64+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
65+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
66+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
67+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
68+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
69+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
70+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
71+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
73+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2020 Huawei Technologies Co.,Ltd.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
package basic
21+
22+
import (
23+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/auth/signer"
24+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/request"
25+
"strings"
26+
)
27+
28+
type Credentials struct {
29+
AK string
30+
SK string
31+
ProjectId string
32+
SecurityToken string
33+
}
34+
35+
func (s Credentials) ProcessAuthRequest(req *request.DefaultHttpRequest) (*request.DefaultHttpRequest, error) {
36+
reqBuilder := req.Builder()
37+
38+
if s.ProjectId != "" {
39+
reqBuilder.AddAutoFilledPathParam("project_id", s.ProjectId)
40+
reqBuilder.AddHeaderParam("X-Project-Id", s.ProjectId)
41+
}
42+
43+
if s.SecurityToken != "" {
44+
reqBuilder.AddHeaderParam("X-Security-Token", s.SecurityToken)
45+
}
46+
47+
if _, ok := req.GetHeaderParams()["Content-Type"]; ok {
48+
if !strings.Contains(req.GetHeaderParams()["Content-Type"], "application/json") {
49+
reqBuilder.AddHeaderParam("X-Sdk-Content-Sha256", "UNSIGNED-PAYLOAD")
50+
}
51+
}
52+
53+
r, err := reqBuilder.Build().ConvertRequest()
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
headerParams, err := signer.Sign(r, s.AK, s.SK)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
for key, value := range headerParams {
64+
req.AddHeaderParam(key, value)
65+
}
66+
return req, nil
67+
}
68+
69+
type CredentialsBuilder struct {
70+
Credentials Credentials
71+
}
72+
73+
func NewCredentialsBuilder() *CredentialsBuilder {
74+
return &CredentialsBuilder{Credentials: Credentials{}}
75+
}
76+
77+
func (builder *CredentialsBuilder) WithAk(ak string) *CredentialsBuilder {
78+
builder.Credentials.AK = ak
79+
return builder
80+
}
81+
82+
func (builder *CredentialsBuilder) WithSk(sk string) *CredentialsBuilder {
83+
builder.Credentials.SK = sk
84+
return builder
85+
}
86+
87+
func (builder *CredentialsBuilder) WithProjectId(projectId string) *CredentialsBuilder {
88+
builder.Credentials.ProjectId = projectId
89+
return builder
90+
}
91+
92+
func (builder *CredentialsBuilder) WithSecurityToken(token string) *CredentialsBuilder {
93+
builder.Credentials.SecurityToken = token
94+
return builder
95+
}
96+
97+
func (builder *CredentialsBuilder) Build() Credentials {
98+
return builder.Credentials
99+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2020 Huawei Technologies Co.,Ltd.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
package global
21+
22+
import (
23+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/auth/signer"
24+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/request"
25+
"strings"
26+
)
27+
28+
type Credentials struct {
29+
AK string
30+
SK string
31+
DomainId string
32+
SecurityToken string
33+
}
34+
35+
func (s Credentials) ProcessAuthRequest(req *request.DefaultHttpRequest) (*request.DefaultHttpRequest, error) {
36+
reqBuilder := req.Builder()
37+
38+
if s.DomainId != "" {
39+
reqBuilder.AddAutoFilledPathParam("domain_id", s.DomainId)
40+
reqBuilder.AddHeaderParam("X-Domain-Id", s.DomainId)
41+
}
42+
43+
if s.SecurityToken != "" {
44+
reqBuilder.AddHeaderParam("X-Security-Token", s.SecurityToken)
45+
}
46+
47+
if _, ok := req.GetHeaderParams()["Content-Type"]; ok {
48+
if !strings.Contains(req.GetHeaderParams()["Content-Type"], "application/json") {
49+
reqBuilder.AddHeaderParam("X-Sdk-Content-Sha256", "UNSIGNED-PAYLOAD")
50+
}
51+
}
52+
53+
r, err := reqBuilder.Build().ConvertRequest()
54+
if err != nil {
55+
return nil, err
56+
}
57+
headerParams, err := signer.Sign(r, s.AK, s.SK)
58+
if err != nil {
59+
return nil, err
60+
}
61+
for key, value := range headerParams {
62+
req.AddHeaderParam(key, value)
63+
}
64+
return req, nil
65+
}
66+
67+
type CredentialsBuilder struct {
68+
Credentials Credentials
69+
}
70+
71+
func NewCredentialsBuilder() *CredentialsBuilder {
72+
return &CredentialsBuilder{Credentials: Credentials{}}
73+
}
74+
75+
func (builder *CredentialsBuilder) WithAk(ak string) *CredentialsBuilder {
76+
builder.Credentials.AK = ak
77+
return builder
78+
}
79+
80+
func (builder *CredentialsBuilder) WithSk(sk string) *CredentialsBuilder {
81+
builder.Credentials.SK = sk
82+
return builder
83+
}
84+
85+
func (builder *CredentialsBuilder) WithDomainId(domainId string) *CredentialsBuilder {
86+
builder.Credentials.DomainId = domainId
87+
return builder
88+
}
89+
90+
func (builder *CredentialsBuilder) WithSecurityToken(token string) *CredentialsBuilder {
91+
builder.Credentials.SecurityToken = token
92+
return builder
93+
}
94+
95+
func (builder *CredentialsBuilder) Build() Credentials {
96+
return builder.Credentials
97+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2020 Huawei Technologies Co.,Ltd.
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one
4+
// or more contributor license agreements. See the NOTICE file
5+
// distributed with this work for additional information
6+
// regarding copyright ownership. The ASF licenses this file
7+
// to you under the Apache License, Version 2.0 (the
8+
// "License"); you may not use this file except in compliance
9+
// with the License. You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing,
14+
// software distributed under the License is distributed on an
15+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
// KIND, either express or implied. See the License for the
17+
// specific language governing permissions and limitations
18+
// under the License.
19+
20+
package auth
21+
22+
import (
23+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
24+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
25+
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/huaweicloud/huaweicloud-sdk-go-v3/core/request"
26+
"os"
27+
)
28+
29+
type ICredential interface {
30+
ProcessAuthRequest(httpRequest *request.DefaultHttpRequest) (*request.DefaultHttpRequest, error)
31+
}
32+
33+
func LoadCredentialFromEnv(defaultType string) ICredential {
34+
ak := os.Getenv("HUAWEICLOUD_SDK_AK")
35+
sk := os.Getenv("HUAWEICLOUD_SDK_SK")
36+
37+
if defaultType == "basic.Credentials" {
38+
projectId := os.Getenv("HUAWEICLOUD_SDK_PROJECT_ID")
39+
return basic.NewCredentialsBuilder().
40+
WithAk(ak).
41+
WithSk(sk).
42+
WithProjectId(projectId).
43+
Build()
44+
} else if defaultType == "global.Credentials" {
45+
domainId := os.Getenv("HUAWEICLOUD_SDK_DOMAIN_ID")
46+
return global.NewCredentialsBuilder().
47+
WithAk(ak).
48+
WithSk(sk).
49+
WithDomainId(domainId).
50+
Build()
51+
} else {
52+
return nil
53+
}
54+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// based on https://github.com/golang/go/blob/master/src/net/url/url.go
2+
// Copyright 2009 The Go Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package signer
7+
8+
func shouldEscape(c byte) bool {
9+
if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9' || c == '_' || c == '-' || c == '~' || c == '.' {
10+
return false
11+
}
12+
return true
13+
}
14+
15+
func escape(s string) string {
16+
hexCount := 0
17+
for i := 0; i < len(s); i++ {
18+
c := s[i]
19+
if shouldEscape(c) {
20+
hexCount++
21+
}
22+
}
23+
24+
if hexCount == 0 {
25+
return s
26+
}
27+
28+
t := make([]byte, len(s)+2*hexCount)
29+
j := 0
30+
for i := 0; i < len(s); i++ {
31+
switch c := s[i]; {
32+
case shouldEscape(c):
33+
t[j] = '%'
34+
t[j+1] = "0123456789ABCDEF"[c>>4]
35+
t[j+2] = "0123456789ABCDEF"[c&15]
36+
j += 3
37+
default:
38+
t[j] = s[i]
39+
j++
40+
}
41+
}
42+
return string(t)
43+
}

0 commit comments

Comments
 (0)