@@ -2,6 +2,7 @@ package cfrestclient
22
33import (
44 "crypto/md5"
5+ "crypto/tls"
56 "encoding/hex"
67 "encoding/json"
78 "fmt"
@@ -11,16 +12,23 @@ import (
1112 "code.cloudfoundry.org/cli/plugin"
1213 "code.cloudfoundry.org/jsonry"
1314 "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
15+ "github.com/cloudfoundry-incubator/multiapps-cli-plugin/log"
1416)
1517
1618const cfBaseUrl = "v3/"
1719
1820type CloudFoundryRestClient struct {
19- cliConn plugin.CliConnection
21+ cliConn plugin.CliConnection
22+ isSslDisabled bool
2023}
2124
2225func NewCloudFoundryRestClient (cliConn plugin.CliConnection ) CloudFoundryOperationsExtended {
23- return & CloudFoundryRestClient {cliConn }
26+ isSslDisabled , err := cliConn .IsSSLDisabled ()
27+ if err != nil {
28+ log .Tracef ("Error while determining skip-ssl-validation: %v" , err )
29+ isSslDisabled = false
30+ }
31+ return & CloudFoundryRestClient {cliConn , isSslDisabled }
2432}
2533
2634func (c CloudFoundryRestClient ) GetApplications (mtaId , mtaNamespace , spaceGuid string ) ([]models.CloudFoundryApplication , error ) {
@@ -40,7 +48,7 @@ func (c CloudFoundryRestClient) GetApplications(mtaId, mtaNamespace, spaceGuid s
4048 } else {
4149 getAppsUrl = fmt .Sprintf ("%s,!mta_namespace" , getAppsUrl )
4250 }
43- return getPaginatedResources [models.CloudFoundryApplication ](getAppsUrl , token )
51+ return getPaginatedResources [models.CloudFoundryApplication ](getAppsUrl , token , c . isSslDisabled )
4452}
4553
4654func (c CloudFoundryRestClient ) GetAppProcessStatistics (appGuid string ) ([]models.ApplicationProcessStatistics , error ) {
@@ -51,7 +59,7 @@ func (c CloudFoundryRestClient) GetAppProcessStatistics(appGuid string) ([]model
5159 apiEndpoint , _ := c .cliConn .ApiEndpoint ()
5260
5361 getAppProcessStatsUrl := fmt .Sprintf ("%s/%sapps/%s/processes/web/stats" , apiEndpoint , cfBaseUrl , appGuid )
54- body , err := executeRequest (getAppProcessStatsUrl , token )
62+ body , err := executeRequest (getAppProcessStatsUrl , token , c . isSslDisabled )
5563 if err != nil {
5664 return nil , err
5765 }
@@ -70,7 +78,7 @@ func (c CloudFoundryRestClient) GetApplicationRoutes(appGuid string) ([]models.A
7078 apiEndpoint , _ := c .cliConn .ApiEndpoint ()
7179
7280 getAppRoutesUrl := fmt .Sprintf ("%s/%sapps/%s/routes" , apiEndpoint , cfBaseUrl , appGuid )
73- return getPaginatedResources [models.ApplicationRoute ](getAppRoutesUrl , token )
81+ return getPaginatedResources [models.ApplicationRoute ](getAppRoutesUrl , token , c . isSslDisabled )
7482}
7583
7684func (c CloudFoundryRestClient ) GetServiceInstances (mtaId , mtaNamespace , spaceGuid string ) ([]models.CloudFoundryServiceInstance , error ) {
@@ -91,7 +99,7 @@ func (c CloudFoundryRestClient) GetServiceInstances(mtaId, mtaNamespace, spaceGu
9199 } else {
92100 getServicesUrl = fmt .Sprintf ("%s,!mta_namespace" , getServicesUrl )
93101 }
94- return getPaginatedResourcesWithIncluded (getServicesUrl , token , buildServiceInstance )
102+ return getPaginatedResourcesWithIncluded (getServicesUrl , token , c . isSslDisabled , buildServiceInstance )
95103}
96104
97105func (c CloudFoundryRestClient ) GetServiceBindings (serviceName string ) ([]models.ServiceBinding , error ) {
@@ -102,13 +110,13 @@ func (c CloudFoundryRestClient) GetServiceBindings(serviceName string) ([]models
102110 apiEndpoint , _ := c .cliConn .ApiEndpoint ()
103111
104112 getServiceBindingsUrl := fmt .Sprintf ("%s/%sservice_credential_bindings?type=app&include=app&service_instance_names=%s" , apiEndpoint , cfBaseUrl , serviceName )
105- return getPaginatedResourcesWithIncluded (getServiceBindingsUrl , token , buildServiceBinding )
113+ return getPaginatedResourcesWithIncluded (getServiceBindingsUrl , token , c . isSslDisabled , buildServiceBinding )
106114}
107115
108- func getPaginatedResources [T any ](url , token string ) ([]T , error ) {
116+ func getPaginatedResources [T any ](url , token string , isSslDisabled bool ) ([]T , error ) {
109117 var result []T
110118 for url != "" {
111- body , err := executeRequest (url , token )
119+ body , err := executeRequest (url , token , isSslDisabled )
112120 if err != nil {
113121 return nil , err
114122 }
@@ -125,10 +133,10 @@ func getPaginatedResources[T any](url, token string) ([]T, error) {
125133 return result , nil
126134}
127135
128- func getPaginatedResourcesWithIncluded [T any , Auxiliary any ](url , token string , auxiliaryContentHandler func (T , Auxiliary ) T ) ([]T , error ) {
136+ func getPaginatedResourcesWithIncluded [T any , Auxiliary any ](url , token string , isSslDisabled bool , auxiliaryContentHandler func (T , Auxiliary ) T ) ([]T , error ) {
129137 var result []T
130138 for url != "" {
131- body , err := executeRequest (url , token )
139+ body , err := executeRequest (url , token , isSslDisabled )
132140 if err != nil {
133141 return nil , err
134142 }
@@ -145,10 +153,14 @@ func getPaginatedResourcesWithIncluded[T any, Auxiliary any](url, token string,
145153 return result , nil
146154}
147155
148- func executeRequest (url , token string ) ([]byte , error ) {
156+ func executeRequest (url , token string , isSslDisabled bool ) ([]byte , error ) {
149157 req , _ := http .NewRequest (http .MethodGet , url , nil )
150158 req .Header .Add ("Authorization" , token )
151- resp , err := http .DefaultClient .Do (req )
159+ httpTransport := http .DefaultTransport .(* http.Transport ).Clone ()
160+ httpTransport .TLSClientConfig = & tls.Config {InsecureSkipVerify : isSslDisabled }
161+ client := http .DefaultClient
162+ client .Transport = httpTransport
163+ resp , err := client .Do (req )
152164 if err != nil {
153165 return nil , err
154166 }
0 commit comments