Skip to content

Commit

Permalink
Merge pull request #1 from maximrub/alicloud-oidc-support
Browse files Browse the repository at this point in the history
Alicloud OIDC support
  • Loading branch information
maximrub authored Nov 29, 2022
2 parents aa7733c + a9c0ba0 commit f56571b
Show file tree
Hide file tree
Showing 10 changed files with 579 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package credentials

// OIDCCredential is a kind of credentials
type OIDCCredential struct {
RoleArn string
OIDCProviderArn string
OIDCTokenFilePath string
RoleSessionName string
RoleSessionExpiration int
}

// NewOIDCRoleArnCredential returns OIDCCredential
func NewOIDCRoleArnCredential(roleArn, OIDCProviderArn, OIDCTokenFilePath, RoleSessionName string, RoleSessionExpiration int) *OIDCCredential {
return &OIDCCredential{
RoleArn: roleArn,
OIDCProviderArn: OIDCProviderArn,
OIDCTokenFilePath: OIDCTokenFilePath,
RoleSessionName: RoleSessionName,
RoleSessionExpiration: RoleSessionExpiration,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func NewSignerWithCredential(credential Credential, commonApi func(request *requ
{
signer, err = signers.NewEcsRamRoleSigner(instance, commonApi)
}
case *credentials.OIDCCredential:
{
signer, err = signers.NewOIDCSigner(instance)
}
case *credentials.BaseCredential: // deprecated user interface
{
signer, err = signers.NewAccessKeySigner(instance.ToAccessKeyCredential())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package signers

import (
"encoding/json"
"fmt"
"github.com/jmespath/go-jmespath"
"io/ioutil"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/auth/credentials"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/errors"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/requests"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/responses"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/alicloud/alibaba-cloud-sdk-go/sdk/utils"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"time"
)

const (
defaultOIDCDurationSeconds = 3600
)

// OIDCSigner is kind of signer
type OIDCSigner struct {
*credentialUpdater
roleSessionName string
sessionCredential *SessionCredential
credential *credentials.OIDCCredential
}

// NewOIDCSigner returns OIDCSigner
func NewOIDCSigner(credential *credentials.OIDCCredential) (signer *OIDCSigner, err error) {
signer = &OIDCSigner{
credential: credential,
}

signer.credentialUpdater = &credentialUpdater{
credentialExpiration: credential.RoleSessionExpiration,
buildRequestMethod: signer.buildCommonRequest,
responseCallBack: signer.refreshCredential,
refreshApi: signer.refreshApi,
}

if len(credential.RoleSessionName) > 0 {
signer.roleSessionName = credential.RoleSessionName
} else {
signer.roleSessionName = "aliyun-go-sdk-" + strconv.FormatInt(time.Now().UnixNano()/1000, 10)
}
if credential.RoleSessionExpiration > 0 {
if credential.RoleSessionExpiration >= 900 && credential.RoleSessionExpiration <= 3600 {
signer.credentialExpiration = credential.RoleSessionExpiration
} else {
err = errors.NewClientError(errors.InvalidParamErrorCode, "Assume Role session duration should be in the range of 15min - 1Hr", nil)
}
} else {
signer.credentialExpiration = defaultOIDCDurationSeconds
}
return
}

// GetName returns "HMAC-SHA1"
func (*OIDCSigner) GetName() string {
return "HMAC-SHA1"
}

// GetType returns ""
func (*OIDCSigner) GetType() string {
return ""
}

// GetVersion returns "1.0"
func (*OIDCSigner) GetVersion() string {
return "1.0"
}

// GetAccessKeyId returns accessKeyId
func (signer *OIDCSigner) GetAccessKeyId() (accessKeyId string, err error) {
if signer.sessionCredential == nil || signer.needUpdateCredential() {
err = signer.updateCredential()
}
if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0) {
return "", err
}

return signer.sessionCredential.AccessKeyId, nil
}

// GetExtraParam returns params
func (signer *OIDCSigner) GetExtraParam() map[string]string {
if signer.sessionCredential == nil || signer.needUpdateCredential() {
signer.updateCredential()
}
if signer.sessionCredential == nil || len(signer.sessionCredential.StsToken) <= 0 {
return make(map[string]string)
}

return map[string]string{"SecurityToken": signer.sessionCredential.StsToken}
}

// Sign create signer
func (signer *OIDCSigner) Sign(stringToSign, secretSuffix string) string {
secret := signer.sessionCredential.AccessKeySecret + secretSuffix
return ShaHmac1(stringToSign, secret)
}

func (signer *OIDCSigner) buildCommonRequest() (request *requests.CommonRequest, err error) {
const endpoint = "sts.aliyuncs.com"
const stsApiVersion = "2015-04-01"
const action = "AssumeRoleWithOIDC"
request = requests.NewCommonRequest()
request.Scheme = requests.HTTPS
request.Domain = endpoint
request.Method = requests.POST
request.QueryParams["Action"] = action
request.QueryParams["Version"] = stsApiVersion
request.QueryParams["Format"] = "JSON"
request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601()
request.QueryParams["SignatureNonce"] = utils.GetUUIDV4()
request.FormParams["RoleArn"] = signer.credential.RoleArn
request.FormParams["OIDCProviderArn"] = signer.credential.OIDCProviderArn
request.FormParams["OIDCToken"] = signer.getOIDCToken(signer.credential.OIDCTokenFilePath)
request.QueryParams["RoleSessionName"] = signer.credential.RoleSessionName
request.Headers["host"] = endpoint
request.Headers["Accept-Encoding"] = "identity"
request.Headers["content-type"] = "application/x-www-form-urlencoded"
request.Headers["user-agent"] = fmt.Sprintf("AlibabaCloud (%s; %s) Golang/%s Core/%s TeaDSL/1", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), "0.01")
return
}

func (signer *OIDCSigner) getOIDCToken(OIDCTokenFilePath string) string {
tokenPath := OIDCTokenFilePath
_, err := os.Stat(tokenPath)
if os.IsNotExist(err) {
tokenPath = os.Getenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE")
if tokenPath == "" {
return ""
}
}

token, err := ioutil.ReadFile(tokenPath)
if err != nil {
return ""
}
return string(token)
}

func (signer *OIDCSigner) refreshApi(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
body := utils.GetUrlFormedMap(request.FormParams)
httpRequest, err := http.NewRequest(request.Method, fmt.Sprintf("%s://%s/?%s", strings.ToLower(request.Scheme), request.Domain, utils.GetUrlFormedMap(request.QueryParams)), strings.NewReader(body))
if err != nil {
fmt.Println("refresh RRSA token err", err)
return
}

httpRequest.Proto = "HTTP/1.1"
httpRequest.Host = request.Domain
for k, v := range request.Headers {
httpRequest.Header.Add(k, v)
}

httpClient := &http.Client{}
httpResponse, err := httpClient.Do(httpRequest)
if err != nil {
fmt.Println("refresh RRSA token err", err)
return
}

response = responses.NewCommonResponse()
err = responses.Unmarshal(response, httpResponse, "")

return
}

func (signer *OIDCSigner) refreshCredential(response *responses.CommonResponse) (err error) {
if response.GetHttpStatus() != http.StatusOK {
message := "refresh RRSA failed"
err = errors.NewServerError(response.GetHttpStatus(), response.GetHttpContentString(), message)
return
}

var data interface{}
err = json.Unmarshal(response.GetHttpContentBytes(), &data)
if err != nil {
fmt.Println("refresh RRSA token err, json.Unmarshal fail", err)
return
}
accessKeyId, err := jmespath.Search("Credentials.AccessKeyId", data)
if err != nil {
fmt.Println("refresh RRSA token err, fail to get AccessKeyId", err)
return
}
accessKeySecret, err := jmespath.Search("Credentials.AccessKeySecret", data)
if err != nil {
fmt.Println("refresh RRSA token err, fail to get AccessKeySecret", err)
return
}
securityToken, err := jmespath.Search("Credentials.SecurityToken", data)
if err != nil {
fmt.Println("refresh RRSA token err, fail to get SecurityToken", err)
return
}
expiration, err := jmespath.Search("Credentials.Expiration", data)
if err != nil {
fmt.Println("refresh RRSA token err, fail to get Expiration", err)
return
}

if accessKeyId == nil || accessKeySecret == nil || securityToken == nil {
return
}

expirationTime, err := time.Parse("2006-01-02T15:04:05Z", expiration.(string))
signer.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
signer.sessionCredential = &SessionCredential{
AccessKeyId: accessKeyId.(string),
AccessKeySecret: accessKeySecret.(string),
StsToken: securityToken.(string),
}

return
}

// GetSessionCredential returns SessionCredential
func (signer *OIDCSigner) GetSessionCredential() *SessionCredential {
return signer.sessionCredential
}

// Shutdown doesn't implement
func (signer *OIDCSigner) Shutdown() {}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,18 @@ func (client *Client) InitWithEcsRamRole(regionId, roleName string) (err error)
return client.InitWithOptions(regionId, config, credential)
}

// InitWithRRSA need regionId,roleARN,oidcProviderARN,oidcTokenFilePath and roleSessionName
func (client *Client) InitWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName string) (err error) {
config := client.InitClientConfig()
credential := &credentials.OIDCCredential{
RoleArn: roleARN,
OIDCProviderArn: oidcProviderARN,
OIDCTokenFilePath: oidcTokenFilePath,
RoleSessionName: roleSessionName,
}
return client.InitWithOptions(regionId, config, credential)
}

// InitClientConfig init client config
func (client *Client) InitClientConfig() (config *Config) {
if client.config != nil {
Expand Down Expand Up @@ -395,6 +407,13 @@ func NewClientWithEcsRamRole(regionId string, roleName string) (client *Client,
return
}

// NewClientWithRRSA create client with RRSA on ECS
func NewClientWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName string) (client *Client, err error) {
client = &Client{}
err = client.InitWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName)
return
}

// NewClientWithRsaKeyPair create client with key-pair
func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, sessionExpiration int) (client *Client, err error) {
client = &Client{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, se
err = client.InitWithRsaKeyPair(regionId, publicKeyId, privateKey, sessionExpiration)
return
}

// NewClientWithRRSA is a shortcut to create sdk client with RRSA
func NewClientWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName string) (client *Client, err error) {
client = &Client{}
err = client.InitWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, se
err = client.InitWithRsaKeyPair(regionId, publicKeyId, privateKey, sessionExpiration)
return
}

// NewClientWithRRSA is a shortcut to create sdk client with RRSA
func NewClientWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName string) (client *Client, err error) {
client = &Client{}
err = client.InitWithRRSA(regionId, roleARN, oidcProviderARN, oidcTokenFilePath, roleSessionName)
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newAutoScalingWrapper(cfg *cloudConfig) (*autoScalingWrapper, error) {
asw := &autoScalingWrapper{
cfg: cfg,
}
if cfg.STSEnabled == true {
if cfg.STSEnabled {
go func(asw *autoScalingWrapper, cfg *cloudConfig) {
timer := time.NewTicker(refreshClientInterval)
defer timer.Stop()
Expand All @@ -76,7 +76,7 @@ func newAutoScalingWrapper(cfg *cloudConfig) (*autoScalingWrapper, error) {

func getEssClient(cfg *cloudConfig) (client *ess.Client, err error) {
region := cfg.getRegion()
if cfg.STSEnabled == true {
if cfg.STSEnabled {
auth, err := cfg.getSTSToken()
if err != nil {
klog.Errorf("Failed to get sts token from metadata,Because of %s", err.Error())
Expand All @@ -86,6 +86,11 @@ func getEssClient(cfg *cloudConfig) (client *ess.Client, err error) {
if err != nil {
klog.Errorf("Failed to create client with sts in metadata because of %s", err.Error())
}
} else if cfg.RRSAEnabled {
client, err = ess.NewClientWithRRSA(region, cfg.RoleARN, cfg.OIDCProviderARN, cfg.OIDCTokenFilePath, cfg.RoleSessionName)
if err != nil {
klog.Errorf("Failed to create ess client with RRSA, because of %s", err.Error())
}
} else {
client, err = ess.NewClientWithAccessKey(region, cfg.AccessKeyID, cfg.AccessKeySecret)
if err != nil {
Expand Down
Loading

0 comments on commit f56571b

Please sign in to comment.