GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 6b4e63...3bee67 )
by
unknown
09:47
created

credentials.*OIDCCredential.GetBearerToken   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package credentials
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"io/ioutil"
7
	"os"
8
	"time"
9
10
	"github.com/alibabacloud-go/tea/tea"
11
	"github.com/aliyun/credentials-go/credentials/request"
12
	"github.com/aliyun/credentials-go/credentials/utils"
13
)
14
15
const defaultOIDCDurationSeconds = 3600
16
17
// OIDCCredential is a kind of credentials
18
type OIDCCredential struct {
19
	*credentialUpdater
20
	AccessKeyId           string
21
	AccessKeySecret       string
22
	RoleArn               string
23
	OIDCProviderArn       string
24
	OIDCTokenFilePath     string
25
	Policy                string
26
	RoleSessionName       string
27
	RoleSessionExpiration int
28
	sessionCredential     *sessionCredential
29
	runtime               *utils.Runtime
30
}
31
32
type OIDCResponse struct {
33
	Credentials *credentialsInResponse `json:"Credentials" xml:"Credentials"`
34
}
35
36
type OIDCcredentialsInResponse struct {
37
	AccessKeyId     string `json:"AccessKeyId" xml:"AccessKeyId"`
38
	AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"`
39
	SecurityToken   string `json:"SecurityToken" xml:"SecurityToken"`
40
	Expiration      string `json:"Expiration" xml:"Expiration"`
41
}
42
43
func newOIDCRoleArnCredential(accessKeyId, accessKeySecret, roleArn, OIDCProviderArn, OIDCTokenFilePath, RoleSessionName, policy string, RoleSessionExpiration int, runtime *utils.Runtime) *OIDCCredential {
44
	return &OIDCCredential{
45
		AccessKeyId:           accessKeyId,
46
		AccessKeySecret:       accessKeySecret,
47
		RoleArn:               roleArn,
48
		OIDCProviderArn:       OIDCProviderArn,
49
		OIDCTokenFilePath:     OIDCTokenFilePath,
50
		RoleSessionName:       RoleSessionName,
51
		Policy:                policy,
52
		RoleSessionExpiration: RoleSessionExpiration,
53
		credentialUpdater:     new(credentialUpdater),
54
		runtime:               runtime,
55
	}
56
}
57
58
// GetAccessKeyId reutrns OIDCCredential's AccessKeyId
59
// if AccessKeyId is not exist or out of date, the function will update it.
60
func (r *OIDCCredential) GetAccessKeyId() (*string, error) {
61
	if r.sessionCredential == nil || r.needUpdateCredential() {
62
		err := r.updateCredential()
63
		if err != nil {
64
			return tea.String(""), err
65
		}
66
	}
67
	return tea.String(r.sessionCredential.AccessKeyId), nil
68
}
69
70
// GetAccessSecret reutrns OIDCCredential's AccessKeySecret
71
// if AccessKeySecret is not exist or out of date, the function will update it.
72
func (r *OIDCCredential) GetAccessKeySecret() (*string, error) {
73
	if r.sessionCredential == nil || r.needUpdateCredential() {
74
		err := r.updateCredential()
75
		if err != nil {
76
			return tea.String(""), err
77
		}
78
	}
79
	return tea.String(r.sessionCredential.AccessKeySecret), nil
80
}
81
82
// GetSecurityToken reutrns OIDCCredential's SecurityToken
83
// if SecurityToken is not exist or out of date, the function will update it.
84
func (r *OIDCCredential) GetSecurityToken() (*string, error) {
85
	if r.sessionCredential == nil || r.needUpdateCredential() {
86
		err := r.updateCredential()
87
		if err != nil {
88
			return tea.String(""), err
89
		}
90
	}
91
	return tea.String(r.sessionCredential.SecurityToken), nil
92
}
93
94
// GetBearerToken is useless OIDCCredential
95
func (r *OIDCCredential) GetBearerToken() *string {
96
	return tea.String("")
97
}
98
99
// GetType reutrns OIDCCredential's type
100
func (r *OIDCCredential) GetType() *string {
101
	return tea.String("oidc_role_arn")
102
}
103
104
func (r *OIDCCredential) GetOIDCToken(OIDCTokenFilePath string) *string {
105
	tokenPath := OIDCTokenFilePath
106
	_, err := os.Stat(tokenPath)
107
	if os.IsNotExist(err) {
108
		tokenPath = os.Getenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE")
109
		if tokenPath == "" {
110
			return nil
111
		}
112
	}
113
	byt, err := ioutil.ReadFile(tokenPath)
114
	if err != nil {
115
		return nil
116
	}
117
	return tea.String(string(byt))
118
}
119
120
func (r *OIDCCredential) updateCredential() (err error) {
121
	if r.runtime == nil {
122
		r.runtime = new(utils.Runtime)
123
	}
124
	request := request.NewCommonRequest()
125
	request.Domain = "sts.aliyuncs.com"
126
	request.Scheme = "HTTPS"
127
	request.Method = "POST"
128
	request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601()
129
	request.QueryParams["Action"] = "AssumeRoleWithOIDC"
130
	request.QueryParams["Format"] = "JSON"
131
	request.BodyParams["RoleArn"] = r.RoleArn
132
	request.BodyParams["OIDCProviderArn"] = r.OIDCProviderArn
133
	token := r.GetOIDCToken(r.OIDCTokenFilePath)
134
	request.BodyParams["OIDCToken"] = tea.StringValue(token)
135
	if r.Policy != "" {
136
		request.QueryParams["Policy"] = r.Policy
137
	}
138
	request.QueryParams["RoleSessionName"] = r.RoleSessionName
139
	request.QueryParams["Version"] = "2015-04-01"
140
	request.QueryParams["SignatureNonce"] = utils.GetUUID()
141
	request.Headers["Host"] = request.Domain
142
	request.Headers["Accept-Encoding"] = "identity"
143
	request.Headers["content-type"] = "application/x-www-form-urlencoded"
144
	request.URL = request.BuildURL()
145
	content, err := doAction(request, r.runtime)
146
	if err != nil {
147
		return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error())
148
	}
149
	var resp *OIDCResponse
150
	err = json.Unmarshal(content, &resp)
151
	if err != nil {
152
		return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error())
153
	}
154
	if resp == nil || resp.Credentials == nil {
155
		return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty")
156
	}
157
	respCredentials := resp.Credentials
158
	if respCredentials.AccessKeyId == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" {
159
		return fmt.Errorf("refresh RoleArn sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyId, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration)
160
	}
161
162
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration)
163
	r.lastUpdateTimestamp = time.Now().Unix()
164
	r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
165
	r.sessionCredential = &sessionCredential{
166
		AccessKeyId:     respCredentials.AccessKeyId,
167
		AccessKeySecret: respCredentials.AccessKeySecret,
168
		SecurityToken:   respCredentials.SecurityToken,
169
	}
170
171
	return
172
}
173