@@ -95,17 +95,21 @@ type Services struct {
9595
9696// DiscoverServices fetches from the service discovery service for a given subdomain
9797// and parses the CyberArk Identity API URL and Inventory API URL.
98- func (c * Client ) DiscoverServices (ctx context.Context , subdomain string ) (* Services , error ) {
98+ // It also returns the Tenant ID UUID corresponding to the subdomain.
99+ func (c * Client ) DiscoverServices (ctx context.Context , subdomain string ) (* Services , string , error ) {
99100 u , err := url .Parse (c .baseURL )
100101 if err != nil {
101- return nil , fmt .Errorf ("invalid base URL for service discovery: %w" , err )
102+ return nil , "" , fmt .Errorf ("invalid base URL for service discovery: %w" , err )
102103 }
104+
103105 u .Path = path .Join (u .Path , "api/public/tenant-discovery" )
104106 u .RawQuery = url.Values {"bySubdomain" : []string {subdomain }}.Encode ()
107+
105108 endpoint := u .String ()
109+
106110 request , err := http .NewRequestWithContext (ctx , http .MethodGet , endpoint , nil )
107111 if err != nil {
108- return nil , fmt .Errorf ("failed to initialise request to %s: %s" , endpoint , err )
112+ return nil , "" , fmt .Errorf ("failed to initialise request to %s: %s" , endpoint , err )
109113 }
110114
111115 request .Header .Set ("Accept" , "application/json" )
@@ -114,7 +118,7 @@ func (c *Client) DiscoverServices(ctx context.Context, subdomain string) (*Servi
114118 arkapi .SetTelemetryRequestHeader (request )
115119 resp , err := c .client .Do (request )
116120 if err != nil {
117- return nil , fmt .Errorf ("failed to perform HTTP request: %s" , err )
121+ return nil , "" , fmt .Errorf ("failed to perform HTTP request: %s" , err )
118122 }
119123
120124 defer resp .Body .Close ()
@@ -123,19 +127,19 @@ func (c *Client) DiscoverServices(ctx context.Context, subdomain string) (*Servi
123127 // a 404 error is returned with an empty JSON body "{}" if the subdomain is unknown; at the time of writing, we haven't observed
124128 // any other errors and so we can't special case them
125129 if resp .StatusCode == http .StatusNotFound {
126- return nil , fmt .Errorf ("got an HTTP 404 response from service discovery; maybe the subdomain %q is incorrect or does not exist?" , subdomain )
130+ return nil , "" , fmt .Errorf ("got an HTTP 404 response from service discovery; maybe the subdomain %q is incorrect or does not exist?" , subdomain )
127131 }
128132
129- return nil , fmt .Errorf ("got unexpected status code %s from request to service discovery API" , resp .Status )
133+ return nil , "" , fmt .Errorf ("got unexpected status code %s from request to service discovery API" , resp .Status )
130134 }
131135
132136 var discoveryResp DiscoveryResponse
133137 err = json .NewDecoder (io .LimitReader (resp .Body , maxDiscoverBodySize )).Decode (& discoveryResp )
134138 if err != nil {
135139 if err == io .ErrUnexpectedEOF {
136- return nil , fmt .Errorf ("rejecting JSON response from server as it was too large or was truncated" )
140+ return nil , "" , fmt .Errorf ("rejecting JSON response from server as it was too large or was truncated" )
137141 }
138- return nil , fmt .Errorf ("failed to parse JSON from otherwise successful request to service discovery endpoint: %s" , err )
142+ return nil , "" , fmt .Errorf ("failed to parse JSON from otherwise successful request to service discovery endpoint: %s" , err )
139143 }
140144 var identityAPI , discoveryContextAPI string
141145 for _ , svc := range discoveryResp .Services {
@@ -158,13 +162,13 @@ func (c *Client) DiscoverServices(ctx context.Context, subdomain string) (*Servi
158162 }
159163
160164 if identityAPI == "" {
161- return nil , fmt .Errorf ("didn't find %s in service discovery response, " +
165+ return nil , "" , fmt .Errorf ("didn't find %s in service discovery response, " +
162166 "which may indicate a suspended tenant; unable to detect CyberArk Identity API URL" , IdentityServiceName )
163167 }
164168 //TODO: Should add a check for discoveryContextAPI too?
165169
166170 return & Services {
167171 Identity : ServiceEndpoint {API : identityAPI },
168172 DiscoveryContext : ServiceEndpoint {API : discoveryContextAPI },
169- }, nil
173+ }, discoveryResp . TenantID , nil
170174}
0 commit comments