github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/util/configv3/json_config.go (about) 1 package configv3 2 3 import ( 4 "time" 5 ) 6 7 // JSONConfig represents .cf/config.json. 8 type JSONConfig struct { 9 AccessToken string `json:"AccessToken"` 10 APIVersion string `json:"APIVersion"` 11 AsyncTimeout int `json:"AsyncTimeout"` 12 AuthorizationEndpoint string `json:"AuthorizationEndpoint"` 13 CFOnK8s CFOnK8s `json:"CFOnK8s"` 14 ColorEnabled string `json:"ColorEnabled"` 15 ConfigVersion int `json:"ConfigVersion"` 16 DopplerEndpoint string `json:"DopplerEndPoint"` 17 Locale string `json:"Locale"` 18 LogCacheEndpoint string `json:"LogCacheEndPoint"` 19 MinCLIVersion string `json:"MinCLIVersion"` 20 MinRecommendedCLIVersion string `json:"MinRecommendedCLIVersion"` 21 NetworkPolicyV1Endpoint string `json:"NetworkPolicyV1Endpoint"` 22 TargetedOrganization Organization `json:"OrganizationFields"` 23 PluginRepositories []PluginRepository `json:"PluginRepos"` 24 RefreshToken string `json:"RefreshToken"` 25 RoutingEndpoint string `json:"RoutingAPIEndpoint"` 26 TargetedSpace Space `json:"SpaceFields"` 27 SSHOAuthClient string `json:"SSHOAuthClient"` 28 SkipSSLValidation bool `json:"SSLDisabled"` 29 Target string `json:"Target"` 30 Trace string `json:"Trace"` 31 UAAEndpoint string `json:"UaaEndpoint"` 32 UAAGrantType string `json:"UAAGrantType"` 33 UAAOAuthClient string `json:"UAAOAuthClient"` 34 UAAOAuthClientSecret string `json:"UAAOAuthClientSecret"` 35 } 36 37 // Organization contains basic information about the targeted organization. 38 type Organization struct { 39 GUID string `json:"GUID"` 40 Name string `json:"Name"` 41 } 42 43 // Space contains basic information about the targeted space. 44 type Space struct { 45 GUID string `json:"GUID"` 46 Name string `json:"Name"` 47 AllowSSH bool `json:"AllowSSH"` 48 } 49 50 // User represents the user information provided by the JWT access token. 51 type User struct { 52 Name string 53 GUID string 54 Origin string 55 IsClient bool 56 } 57 58 // AccessToken returns the access token for making authenticated API calls. 59 func (config *Config) AccessToken() string { 60 return config.ConfigFile.AccessToken 61 } 62 63 // APIVersion returns the CC API Version. 64 func (config *Config) APIVersion() string { 65 return config.ConfigFile.APIVersion 66 } 67 68 // AuthorizationEndpoint returns the authorization endpoint 69 func (config *Config) AuthorizationEndpoint() string { 70 return config.ConfigFile.AuthorizationEndpoint 71 } 72 73 // HasTargetedOrganization returns true if the organization is set. 74 func (config *Config) HasTargetedOrganization() bool { 75 return config.ConfigFile.TargetedOrganization.GUID != "" 76 } 77 78 // HasTargetedSpace returns true if the space is set. 79 func (config *Config) HasTargetedSpace() bool { 80 return config.ConfigFile.TargetedSpace.GUID != "" 81 } 82 83 // LogCacheEndpoint returns the log cache endpoint. 84 func (config *Config) LogCacheEndpoint() string { 85 return config.ConfigFile.LogCacheEndpoint 86 } 87 88 // MinCLIVersion returns the minimum CLI version required by the CC. 89 func (config *Config) MinCLIVersion() string { 90 return config.ConfigFile.MinCLIVersion 91 } 92 93 // OverallPollingTimeout returns the overall polling timeout for async 94 // operations. The time is based off of: 95 // 1. The config file's AsyncTimeout value (integer) is > 0 96 // 2. Defaults to the DefaultOverallPollingTimeout 97 func (config *Config) OverallPollingTimeout() time.Duration { 98 if config.ConfigFile.AsyncTimeout == 0 { 99 return DefaultOverallPollingTimeout 100 } 101 return time.Duration(config.ConfigFile.AsyncTimeout) * time.Minute 102 } 103 104 // NetworkPolicyV1Endpoint returns the endpoint for V1 of the networking API 105 func (config *Config) NetworkPolicyV1Endpoint() string { 106 return config.ConfigFile.NetworkPolicyV1Endpoint 107 } 108 109 // RefreshToken returns the refresh token for getting a new access token. 110 func (config *Config) RefreshToken() string { 111 return config.ConfigFile.RefreshToken 112 } 113 114 // RoutingEndpoint returns the endpoint for the router API 115 func (config *Config) RoutingEndpoint() string { 116 return config.ConfigFile.RoutingEndpoint 117 } 118 119 // SetAsyncTimeout sets the async timeout. 120 func (config *Config) SetAsyncTimeout(timeout int) { 121 config.ConfigFile.AsyncTimeout = timeout 122 } 123 124 // SetAccessToken sets the current access token. 125 func (config *Config) SetAccessToken(accessToken string) { 126 config.ConfigFile.AccessToken = accessToken 127 } 128 129 // SetColorEnabled sets the color enabled feature to true or false 130 func (config *Config) SetColorEnabled(enabled string) { 131 config.ConfigFile.ColorEnabled = enabled 132 } 133 134 // SetLocale sets the locale, or clears the field if requested 135 func (config *Config) SetLocale(locale string) { 136 if locale == "CLEAR" { 137 config.ConfigFile.Locale = "" 138 } else { 139 config.ConfigFile.Locale = locale 140 } 141 } 142 143 // SetMinCLIVersion sets the minimum CLI version required by the CC. 144 func (config *Config) SetMinCLIVersion(minVersion string) { 145 config.ConfigFile.MinCLIVersion = minVersion 146 } 147 148 // SetOrganizationInformation sets the currently targeted organization. 149 func (config *Config) SetOrganizationInformation(guid string, name string) { 150 config.ConfigFile.TargetedOrganization.GUID = guid 151 config.ConfigFile.TargetedOrganization.Name = name 152 } 153 154 // SetRefreshToken sets the current refresh token. 155 func (config *Config) SetRefreshToken(refreshToken string) { 156 config.ConfigFile.RefreshToken = refreshToken 157 } 158 159 // SetSpaceInformation sets the currently targeted space. 160 // The "AllowSSH" field is not returned by v3, and is never read from the config. 161 // Persist `true` to maintain compatibility in the config file. 162 // TODO: this field should be removed entirely in v7 163 func (config *Config) SetSpaceInformation(guid string, name string, allowSSH bool) { 164 config.V7SetSpaceInformation(guid, name) 165 config.ConfigFile.TargetedSpace.AllowSSH = allowSSH 166 } 167 168 type TargetInformationArgs struct { 169 Api string 170 ApiVersion string 171 Auth string 172 Doppler string 173 LogCache string 174 MinCLIVersion string 175 NetworkPolicyV1 string 176 Routing string 177 SkipSSLValidation bool 178 UAA string 179 CFOnK8s bool 180 } 181 182 // SetTargetInformation sets the currently targeted CC API and related other 183 // related API URLs. 184 func (config *Config) SetTargetInformation(args TargetInformationArgs) { 185 config.ConfigFile.Target = args.Api 186 config.ConfigFile.APIVersion = args.ApiVersion 187 config.SetMinCLIVersion(args.MinCLIVersion) 188 config.ConfigFile.DopplerEndpoint = args.Doppler 189 config.ConfigFile.LogCacheEndpoint = args.LogCache 190 config.ConfigFile.RoutingEndpoint = args.Routing 191 config.ConfigFile.SkipSSLValidation = args.SkipSSLValidation 192 config.ConfigFile.NetworkPolicyV1Endpoint = args.NetworkPolicyV1 193 194 config.ConfigFile.UAAEndpoint = args.UAA 195 config.ConfigFile.AuthorizationEndpoint = args.Auth 196 197 // NOTE: This gets written to the config file, but I do not believe it is currently 198 // ever read from there. 199 config.ConfigFile.AuthorizationEndpoint = args.Auth 200 201 config.ConfigFile.CFOnK8s.Enabled = args.CFOnK8s 202 203 config.UnsetOrganizationAndSpaceInformation() 204 } 205 206 // SetTokenInformation sets the current token/user information. 207 func (config *Config) SetTokenInformation(accessToken string, refreshToken string, sshOAuthClient string) { 208 config.ConfigFile.AccessToken = accessToken 209 config.ConfigFile.RefreshToken = refreshToken 210 config.ConfigFile.SSHOAuthClient = sshOAuthClient 211 } 212 213 // SetTrace sets the trace field to either true, false, or a path to a file. 214 func (config *Config) SetTrace(trace string) { 215 config.ConfigFile.Trace = trace 216 } 217 218 // SetUAAClientCredentials sets the client credentials. 219 func (config *Config) SetUAAClientCredentials(client string, clientSecret string) { 220 config.ConfigFile.UAAOAuthClient = client 221 config.ConfigFile.UAAOAuthClientSecret = clientSecret 222 } 223 224 // SetUAAEndpoint sets the UAA endpoint that is obtained from hitting 225 // <AuthorizationEndpoint>/login. 226 func (config *Config) SetUAAEndpoint(uaaEndpoint string) { 227 config.ConfigFile.UAAEndpoint = uaaEndpoint 228 } 229 230 // SetUAAGrantType sets the UAA grant type for logging in and refreshing the 231 // token. 232 func (config *Config) SetUAAGrantType(uaaGrantType string) { 233 config.ConfigFile.UAAGrantType = uaaGrantType 234 } 235 236 // SkipSSLValidation returns whether or not to skip SSL validation when 237 // targeting an API endpoint. 238 func (config *Config) SkipSSLValidation() bool { 239 return config.ConfigFile.SkipSSLValidation 240 } 241 242 // SSHOAuthClient returns the OAuth client id used for SSHing into 243 // application/process containers. 244 func (config *Config) SSHOAuthClient() string { 245 return config.ConfigFile.SSHOAuthClient 246 } 247 248 // Target returns the CC API URL. 249 func (config *Config) Target() string { 250 return config.ConfigFile.Target 251 } 252 253 // TargetedOrganization returns the currently targeted organization. 254 func (config *Config) TargetedOrganization() Organization { 255 return config.ConfigFile.TargetedOrganization 256 } 257 258 // TargetedOrganizationName returns the name of the targeted organization. 259 func (config *Config) TargetedOrganizationName() string { 260 return config.TargetedOrganization().Name 261 } 262 263 // TargetedSpace returns the currently targeted space. 264 func (config *Config) TargetedSpace() Space { 265 return config.ConfigFile.TargetedSpace 266 } 267 268 // UAAEndpoint returns the UAA endpoint 269 func (config *Config) UAAEndpoint() string { 270 return config.ConfigFile.UAAEndpoint 271 } 272 273 // UAAGrantType returns the grant type of the supplied UAA credentials. 274 func (config *Config) UAAGrantType() string { 275 return config.ConfigFile.UAAGrantType 276 } 277 278 // UAAOAuthClient returns the CLI's UAA client ID. 279 func (config *Config) UAAOAuthClient() string { 280 return config.ConfigFile.UAAOAuthClient 281 } 282 283 // UAAOAuthClientSecret returns the CLI's UAA client secret. 284 func (config *Config) UAAOAuthClientSecret() string { 285 return config.ConfigFile.UAAOAuthClientSecret 286 } 287 288 // UnsetOrganizationAndSpaceInformation resets the organization and space 289 // values to default. 290 func (config *Config) UnsetOrganizationAndSpaceInformation() { 291 config.SetOrganizationInformation("", "") 292 config.UnsetSpaceInformation() 293 } 294 295 // UnsetSpaceInformation resets the space values to default. 296 func (config *Config) UnsetSpaceInformation() { 297 config.SetSpaceInformation("", "", false) 298 } 299 300 // UnsetUserInformation resets the access token, refresh token, UAA grant type, 301 // UAA client credentials, and targeted org/space information. 302 func (config *Config) UnsetUserInformation() { 303 config.SetAccessToken("") 304 config.SetRefreshToken("") 305 config.SetUAAGrantType("") 306 config.SetUAAClientCredentials(DefaultUAAOAuthClient, DefaultUAAOAuthClientSecret) 307 config.SetKubernetesAuthInfo("") 308 309 config.UnsetOrganizationAndSpaceInformation() 310 } 311 312 // V7SetSpaceInformation sets the currently targeted space. 313 func (config *Config) V7SetSpaceInformation(guid string, name string) { 314 config.ConfigFile.TargetedSpace.GUID = guid 315 config.ConfigFile.TargetedSpace.Name = name 316 }