-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathrepository.go
319 lines (276 loc) · 9.14 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Package core_config provides functions to load core configuration.
// The package is for internal only.
package core_config
import (
"fmt"
"os"
"time"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/authentication/iam"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/authentication/vpc"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/configuration/config_helpers"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/bluemix/models"
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/common/rest"
)
type Repository interface {
APIEndpoint() string
HasAPIEndpoint() bool
IsPrivateEndpointEnabled() bool
IsAccessFromVPC() bool
ConsoleEndpoints() models.Endpoints
IAMEndpoint() string
IAMEndpoints() models.Endpoints
CloudName() string
CloudType() string
CurrentRegion() models.Region
HasTargetedRegion() bool
IAMToken() string
IAMRefreshToken() string
IsLoggedIn() bool
IsLoggedInWithServiceID() bool
IsLoggedInAsProfile() bool
IsLoggedInAsCRI() bool
UserEmail() string
// UserDisplayText is the human readable ID for logged-in users which include non-human IDs
UserDisplayText() string
IAMID() string
CurrentAccount() models.Account
HasTargetedAccount() bool
HasTargetedProfile() bool
HasTargetedComputeResource() bool
IMSAccountID() string
CurrentProfile() models.Profile
CurrentResourceGroup() models.ResourceGroup
// CRIType returns the type of compute resource the user logged in as, if applicable. Valid values are `IKS`, `VPC`, or `OTHER`
CRIType() string
HasTargetedResourceGroup() bool
PluginRepos() []models.PluginRepo
PluginRepo(string) (models.PluginRepo, bool)
IsSSLDisabled() bool
TypeOfSSO() string
AlphaCommandsEnabled() string
AssumedTrustedProfileId() string
FallbackAccount() models.Account
FallbackIAMToken() string
FallbackIAMRefreshToken() string
HTTPTimeout() int
CLIInfoEndpoint() string
CheckCLIVersionDisabled() bool
UpdateCheckInterval() time.Duration
UpdateRetryCheckInterval() time.Duration
UpdateNotificationInterval() time.Duration
// VPCCRITokenURL() returns the value specified by the environment variable 'IBMCLOUD_CR_VPC_URL', if set.
// Otherwise, the default VPC auth url specified by the constant `DefaultServerEndpoint` is returned
VPCCRITokenURL() string
// UsageSatsDisabled returns whether the usage statistics data collection is disabled or not
// Deprecated: use UsageSatsEnabled instead. We change to disable usage statistics by default,
// So this property will not be used anymore
UsageStatsDisabled() bool
// UsageSatsEnabled returns whether the usage statistics data collection is enabled or not
UsageStatsEnabled() bool
// UsageStatsEnabledLastUpdate returns last time when `UsageStatsEnabled` was updated
UsageStatsEnabledLastUpdate() time.Time
Locale() string
LoginAt() time.Time
Trace() string
ColorEnabled() string
SDKVersion() string
UnsetAPI()
RefreshIAMToken() (string, error)
SetAPIEndpoint(string)
SetPrivateEndpointEnabled(bool)
SetAccessFromVPC(bool)
SetConsoleEndpoints(models.Endpoints)
SetIAMEndpoints(models.Endpoints)
SetCloudType(string)
SetCloudName(string)
SetCRIType(string)
SetIsLoggedInAsCRI(bool)
SetRegion(models.Region)
SetIAMToken(string)
SetIAMRefreshToken(string)
SetFallbackIAMTokens(string, string)
SetFallbackAccount(string, string, string)
SetAssumedTrustedProfileId(string)
ClearSession()
SetAccount(models.Account)
SetProfile(models.Profile)
SetResourceGroup(models.ResourceGroup)
SetLoginAt(loginAt time.Time)
SetCheckCLIVersionDisabled(bool)
SetCLIInfoEndpoint(string)
SetPluginRepo(models.PluginRepo)
UnsetPluginRepo(string)
SetSSLDisabled(bool)
SetTypeOfSSO(string)
SetHTTPTimeout(int)
// SetUsageSatsDisabled disable or enable usage statistics data collection
// Deprecated: use SetUsageSatsEnabled instead
SetUsageStatsDisabled(bool)
// SetUsageSatsEnabled enable or disable usage statistics data collection
SetUsageStatsEnabled(bool)
SetUpdateCheckInterval(time.Duration)
SetUpdateRetryCheckInterval(time.Duration)
SetUpdateNotificationInterval(time.Duration)
SetLocale(string)
SetTrace(string)
SetColorEnabled(string)
SetAlphaCommandsEnabled(string)
CheckMessageOfTheDay() bool
SetMessageOfTheDayTime()
SetLastSessionUpdateTime()
LastSessionUpdateTime() (session int64)
SetPaginationURLs(paginationURLs []models.PaginationURL)
ClearPaginationURLs()
AddPaginationURL(lastIndex int, nextURL string)
PaginationURLs() []models.PaginationURL
}
// Deprecated
type ReadWriter interface {
Repository
}
type repository struct {
*bxConfig
}
func newRepository(bx *bxConfig) repository {
return repository{
bxConfig: bx,
}
}
func (c repository) IsLoggedIn() bool {
return c.bxConfig.IsLoggedIn()
}
func (c repository) IsLoggedInWithServiceID() bool {
return c.bxConfig.IsLoggedIn() && NewIAMTokenInfo(c.IAMToken()).SubjectType == SubjectTypeServiceID
}
func (c repository) IsLoggedInAsProfile() bool {
return c.bxConfig.IsLoggedIn() && NewIAMTokenInfo(c.IAMToken()).SubjectType == SubjectTypeTrustedProfile
}
func (c repository) VPCCRITokenURL() string {
if env := bluemix.EnvCRVpcUrl.Get(); env != "" {
return env
}
// default server endpoint is a constant value in vpc authenticator
return vpc.DefaultServerEndpoint
}
func (c repository) IAMEndpoint() string {
if c.IsPrivateEndpointEnabled() {
if c.IsAccessFromVPC() {
// return VPC endpoint
return c.IAMEndpoints().PrivateVPCEndpoint
} else {
// return CSE endpoint
return c.IAMEndpoints().PrivateEndpoint
}
}
return c.IAMEndpoints().PublicEndpoint
}
func (c repository) RefreshIAMToken() (string, error) {
var ret string
// confirm user is logged in as a VPC compute resource identity
isLoggedInAsCRI := c.IsLoggedInAsCRI()
criType := c.CRIType()
if isLoggedInAsCRI && criType == "VPC" {
token, err := c.fetchNewIAMTokenUsingVPCAuth()
if err != nil {
return "", err
}
ret = fmt.Sprintf("%s %s", token.TokenType, token.AccessToken)
c.SetIAMToken(ret)
// this should be empty for vpc vsi tokens
c.SetIAMRefreshToken(token.RefreshToken)
} else {
iamEndpoint := os.Getenv("IAM_ENDPOINT")
if iamEndpoint == "" {
iamEndpoint = c.IAMEndpoint()
}
if iamEndpoint == "" {
return "", fmt.Errorf("IAM endpoint is not set")
}
auth := iam.NewClient(iam.DefaultConfig(iamEndpoint), rest.NewClient())
token, err := auth.GetToken(iam.RefreshTokenRequest(c.IAMRefreshToken()))
if err != nil {
return "", err
}
ret = fmt.Sprintf("%s %s", token.TokenType, token.AccessToken)
c.SetIAMToken(ret)
c.SetIAMRefreshToken(token.RefreshToken)
}
c.SetLastSessionUpdateTime()
return ret, nil
}
func (c repository) fetchNewIAMTokenUsingVPCAuth() (*iam.Token, error) {
// create a vpc client using default configuration
client := vpc.NewClient(vpc.DefaultConfig(c.VPCCRITokenURL(), vpc.DefaultMetadataServiceVersion), rest.NewClient())
// fetch an instance identity token from the metadata server
identityToken, err := client.GetInstanceIdentityToken()
if err != nil {
return nil, err
}
// get the existing targeted IAM trusted profile ID of the CLI session
targetProfile := c.CurrentProfile()
profileID := targetProfile.ID
if profileID == "" {
return nil, fmt.Errorf("Trusted profile not set in configuration")
}
// prepare IAM token request using the existing targeted profile.
req, err := vpc.NewIAMAccessTokenRequest(profileID, "", identityToken.AccessToken)
if err != nil {
return nil, err
}
// get the new access token
iamToken, err := client.GetIAMAccessToken(req)
if err != nil {
return nil, err
}
return iamToken, nil
}
func (c repository) UserEmail() string {
return c.bxConfig.UserEmail()
}
func (c repository) SetSSLDisabled(disabled bool) {
c.bxConfig.SetSSLDisabled(disabled)
}
func (c repository) SetColorEnabled(enabled string) {
c.bxConfig.SetColorEnabled(enabled)
}
func (c repository) SetTrace(trace string) {
c.bxConfig.SetTrace(trace)
}
func (c repository) SetLocale(locale string) {
c.bxConfig.SetLocale(locale)
}
func (c repository) UnsetAPI() {
c.bxConfig.UnsetAPI()
}
func (c repository) ClearSession() {
c.bxConfig.ClearSession()
}
func (c repository) LastSessionUpdateTime() (session int64) {
return c.bxConfig.LastSessionUpdateTime()
}
func (c repository) SetLastSessionUpdateTime() {
c.bxConfig.SetLastSessionUpdateTime()
}
func (c repository) PaginationURLs() []models.PaginationURL {
return c.bxConfig.PaginationURLs()
}
func (c repository) AddPaginationURL(index int, url string) {
c.bxConfig.AddPaginationURL(index, url)
}
func (c repository) SetPaginationURLs(paginationURLs []models.PaginationURL) {
c.bxConfig.SetPaginationURLs(paginationURLs)
}
func (c repository) ClearPaginationURLs() {
c.bxConfig.ClearPaginationURLs()
}
func NewCoreConfig(errHandler func(error)) ReadWriter {
return NewCoreConfigFromPath(config_helpers.ConfigFilePath(), errHandler)
}
func NewCoreConfigFromPath(bxConfigPath string, errHandler func(error)) ReadWriter {
return NewCoreConfigFromPersistor(configuration.NewDiskPersistor(bxConfigPath), errHandler)
}
func NewCoreConfigFromPersistor(bxPersistor configuration.Persistor, errHandler func(error)) ReadWriter {
return newRepository(createBluemixConfigFromPersistor(bxPersistor, errHandler))
}