github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/util/configv3/json_config.go (about) 1 package configv3 2 3 import ( 4 "time" 5 6 "github.com/SermoDigital/jose/jws" 7 ) 8 9 // JSONConfig represents .cf/config.json. 10 type JSONConfig struct { 11 ConfigVersion int `json:"ConfigVersion"` 12 Target string `json:"Target"` 13 APIVersion string `json:"APIVersion"` 14 AuthorizationEndpoint string `json:"AuthorizationEndpoint"` 15 DopplerEndpoint string `json:"DopplerEndPoint"` 16 UAAEndpoint string `json:"UaaEndpoint"` 17 RoutingEndpoint string `json:"RoutingAPIEndpoint"` 18 AccessToken string `json:"AccessToken"` 19 SSHOAuthClient string `json:"SSHOAuthClient"` 20 UAAOAuthClient string `json:"UAAOAuthClient"` 21 UAAOAuthClientSecret string `json:"UAAOAuthClientSecret"` 22 UAAGrantType string `json:"UAAGrantType"` 23 RefreshToken string `json:"RefreshToken"` 24 TargetedOrganization Organization `json:"OrganizationFields"` 25 TargetedSpace Space `json:"SpaceFields"` 26 SkipSSLValidation bool `json:"SSLDisabled"` 27 AsyncTimeout int `json:"AsyncTimeout"` 28 Trace string `json:"Trace"` 29 ColorEnabled string `json:"ColorEnabled"` 30 Locale string `json:"Locale"` 31 PluginRepositories []PluginRepository `json:"PluginRepos"` 32 MinCLIVersion string `json:"MinCLIVersion"` 33 MinRecommendedCLIVersion string `json:"MinRecommendedCLIVersion"` 34 } 35 36 // Organization contains basic information about the targeted organization. 37 type Organization struct { 38 GUID string `json:"GUID"` 39 Name string `json:"Name"` 40 QuotaDefinition QuotaDefinition `json:"QuotaDefinition"` 41 } 42 43 // QuotaDefinition contains information about the organization's quota. 44 type QuotaDefinition struct { 45 GUID string `json:"guid"` 46 Name string `json:"name"` 47 MemoryLimit int `json:"memory_limit"` 48 InstanceMemoryLimit int `json:"instance_memory_limit"` 49 TotalRoutes int `json:"total_routes"` 50 TotalServices int `json:"total_services"` 51 NonBasicServicesAllowed bool `json:"non_basic_services_allowed"` 52 AppInstanceLimit int `json:"app_instance_limit"` 53 TotalReservedRoutePorts int `json:"total_reserved_route_ports"` 54 } 55 56 // Space contains basic information about the targeted space. 57 type Space struct { 58 GUID string `json:"GUID"` 59 Name string `json:"Name"` 60 AllowSSH bool `json:"AllowSSH"` 61 } 62 63 // User represents the user information provided by the JWT access token. 64 type User struct { 65 Name string 66 } 67 68 // AccessToken returns the access token for making authenticated API calls. 69 func (config *Config) AccessToken() string { 70 return config.ConfigFile.AccessToken 71 } 72 73 // APIVersion returns the CC API Version. 74 func (config *Config) APIVersion() string { 75 return config.ConfigFile.APIVersion 76 } 77 78 // CurrentUser returns user information decoded from the JWT access token in 79 // .cf/config.json. 80 func (config *Config) CurrentUser() (User, error) { 81 return decodeUserFromJWT(config.ConfigFile.AccessToken) 82 } 83 84 // HasTargetedOrganization returns true if the organization is set. 85 func (config *Config) HasTargetedOrganization() bool { 86 return config.ConfigFile.TargetedOrganization.GUID != "" 87 } 88 89 // HasTargetedSpace returns true if the space is set. 90 func (config *Config) HasTargetedSpace() bool { 91 return config.ConfigFile.TargetedSpace.GUID != "" 92 } 93 94 // MinCLIVersion returns the minimum CLI version requried by the CC. 95 func (config *Config) MinCLIVersion() string { 96 return config.ConfigFile.MinCLIVersion 97 } 98 99 // OverallPollingTimeout returns the overall polling timeout for async 100 // operations. The time is based off of: 101 // 1. The config file's AsyncTimeout value (integer) is > 0 102 // 2. Defaults to the DefaultOverallPollingTimeout 103 func (config *Config) OverallPollingTimeout() time.Duration { 104 if config.ConfigFile.AsyncTimeout == 0 { 105 return DefaultOverallPollingTimeout 106 } 107 return time.Duration(config.ConfigFile.AsyncTimeout) * time.Minute 108 } 109 110 // RefreshToken returns the refresh token for getting a new access token. 111 func (config *Config) RefreshToken() string { 112 return config.ConfigFile.RefreshToken 113 } 114 115 // SetAccessToken sets the current access token. 116 func (config *Config) SetAccessToken(accessToken string) { 117 config.ConfigFile.AccessToken = accessToken 118 } 119 120 // SetUAAClientCredentials sets the client credentials. 121 func (config *Config) SetUAAClientCredentials(client string, clientSecret string) { 122 config.ConfigFile.UAAOAuthClient = client 123 config.ConfigFile.UAAOAuthClientSecret = clientSecret 124 } 125 126 // SetOrganizationInformation sets the currently targeted organization. 127 func (config *Config) SetOrganizationInformation(guid string, name string) { 128 config.ConfigFile.TargetedOrganization.GUID = guid 129 config.ConfigFile.TargetedOrganization.Name = name 130 config.ConfigFile.TargetedOrganization.QuotaDefinition = QuotaDefinition{} 131 } 132 133 // SetRefreshToken sets the current refresh token. 134 func (config *Config) SetRefreshToken(refreshToken string) { 135 config.ConfigFile.RefreshToken = refreshToken 136 } 137 138 // SetSpaceInformation sets the currently targeted space. 139 func (config *Config) SetSpaceInformation(guid string, name string, allowSSH bool) { 140 config.ConfigFile.TargetedSpace.GUID = guid 141 config.ConfigFile.TargetedSpace.Name = name 142 config.ConfigFile.TargetedSpace.AllowSSH = allowSSH 143 } 144 145 // SetTargetInformation sets the currently targeted CC API and related other 146 // related API URLs. 147 func (config *Config) SetTargetInformation(api string, apiVersion string, auth string, minCLIVersion string, doppler string, routing string, skipSSLValidation bool) { 148 config.ConfigFile.Target = api 149 config.ConfigFile.APIVersion = apiVersion 150 config.ConfigFile.AuthorizationEndpoint = auth 151 config.ConfigFile.MinCLIVersion = minCLIVersion 152 config.ConfigFile.DopplerEndpoint = doppler 153 config.ConfigFile.RoutingEndpoint = routing 154 config.ConfigFile.SkipSSLValidation = skipSSLValidation 155 156 config.UnsetOrganizationAndSpaceInformation() 157 } 158 159 // SetTokenInformation sets the current token/user information. 160 func (config *Config) SetTokenInformation(accessToken string, refreshToken string, sshOAuthClient string) { 161 config.ConfigFile.AccessToken = accessToken 162 config.ConfigFile.RefreshToken = refreshToken 163 config.ConfigFile.SSHOAuthClient = sshOAuthClient 164 } 165 166 // SetUAAGrantType sets the UAA grant type for logging in and refreshing the 167 // token. 168 func (config *Config) SetUAAGrantType(uaaGrantType string) { 169 config.ConfigFile.UAAGrantType = uaaGrantType 170 } 171 172 // SetUAAEndpoint sets the UAA endpoint that is obtained from hitting 173 // <AuthorizationEndpoint>/login. 174 func (config *Config) SetUAAEndpoint(uaaEndpoint string) { 175 config.ConfigFile.UAAEndpoint = uaaEndpoint 176 } 177 178 // SkipSSLValidation returns whether or not to skip SSL validation when 179 // targeting an API endpoint. 180 func (config *Config) SkipSSLValidation() bool { 181 return config.ConfigFile.SkipSSLValidation 182 } 183 184 // SSHOAuthClient returns the OAuth client id used for SSHing into 185 // application/process containers. 186 func (config *Config) SSHOAuthClient() string { 187 return config.ConfigFile.SSHOAuthClient 188 } 189 190 // Target returns the CC API URL. 191 func (config *Config) Target() string { 192 return config.ConfigFile.Target 193 } 194 195 // TargetedOrganization returns the currently targeted organization. 196 func (config *Config) TargetedOrganization() Organization { 197 return config.ConfigFile.TargetedOrganization 198 } 199 200 // TargetedSpace returns the currently targeted space. 201 func (config *Config) TargetedSpace() Space { 202 return config.ConfigFile.TargetedSpace 203 } 204 205 // UAAOAuthClient returns the CLI's UAA client ID. 206 func (config *Config) UAAOAuthClient() string { 207 return config.ConfigFile.UAAOAuthClient 208 } 209 210 // UAAOAuthClientSecret returns the CLI's UAA client secret. 211 func (config *Config) UAAOAuthClientSecret() string { 212 return config.ConfigFile.UAAOAuthClientSecret 213 } 214 215 // UAAGrantType returns the grant type of the supplied UAA credentials. 216 func (config *Config) UAAGrantType() string { 217 return config.ConfigFile.UAAGrantType 218 } 219 220 // UnsetUserInformation resets the access token, refresh token, UAA grant type, 221 // UAA client credentials, and targeted org/space information. 222 func (config *Config) UnsetUserInformation() { 223 config.SetAccessToken("") 224 config.SetRefreshToken("") 225 config.SetUAAGrantType("") 226 config.SetUAAClientCredentials(DefaultUAAOAuthClient, DefaultUAAOAuthClientSecret) 227 228 config.UnsetOrganizationAndSpaceInformation() 229 230 } 231 232 // UnsetOrganizationAndSpaceInformation resets the organization and space 233 // values to default. 234 func (config *Config) UnsetOrganizationAndSpaceInformation() { 235 config.SetOrganizationInformation("", "") 236 config.UnsetSpaceInformation() 237 } 238 239 // UnsetSpaceInformation resets the space values to default. 240 func (config *Config) UnsetSpaceInformation() { 241 config.SetSpaceInformation("", "", false) 242 } 243 244 func decodeUserFromJWT(accessToken string) (User, error) { 245 if accessToken == "" { 246 return User{}, nil 247 } 248 249 token, err := jws.ParseJWT([]byte(accessToken[7:])) 250 if err != nil { 251 return User{}, err 252 } 253 254 claims := token.Claims() 255 256 var ID string 257 if claims.Has("user_name") { 258 ID = claims.Get("user_name").(string) 259 } else { 260 ID = claims.Get("client_id").(string) 261 } 262 return User{ 263 Name: ID, 264 }, nil 265 }