Skip to content

Commit

Permalink
improvement(cloud): more informative error message on CA cert problems (
Browse files Browse the repository at this point in the history
#5941)

* improvement(cloud): more informative error message on CA cert problems

* fix(cloud): rethrow original request errors in not CA cert

Some error-code specific logic is implemented in caller-functions.
  • Loading branch information
vvagaytsev committed Apr 17, 2024
1 parent 63abb40 commit 1d92962
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { LogLevel } from "../logger/logger.js"
import { makeAuthHeader } from "./auth.js"
import type { StringMap } from "../config/common.js"
import { styles } from "../logger/styles.js"
import { RequestError } from "got"

const gardenClientName = "garden-core"
const gardenClientVersion = getPackageVersion()
Expand Down Expand Up @@ -554,24 +555,50 @@ export class CloudApi {
requestOptions.retry = undefined // Disables retry
}

const res = await got<T>(url.href, requestOptions)
try {
const res = await got<T>(url.href, requestOptions)

if (!isObject(res.body)) {
throw new CloudApiError({
message: dedent`
Unexpected API response: Expected object.
if (!isObject(res.body)) {
throw new CloudApiError({
message: dedent`
Unexpected response from Garden Cloud: Expected object.
Request ID: ${res.headers["x-request-id"]}
Request url: ${url}
Response code: ${res?.statusCode}
Response body: ${JSON.stringify(res?.body)}
`,
responseStatusCode: res?.statusCode,
})
}
responseStatusCode: res?.statusCode,
})
}

return {
...res.body,
headers: res.headers,
}
} catch (e: unknown) {
if (!(e instanceof RequestError)) {
throw e
}

// The assumption here is that Garden Enterprise is self-hosted.
// This error should only be thrown if the Garden Enterprise instance is not hosted by us (i.e. Garden Inc.)
if (e.code === "DEPTH_ZERO_SELF_SIGNED_CERT" && getCloudDistributionName(this.domain) === "Garden Enterprise") {
throw new CloudApiError({
message: dedent`
SSL error when communicating to Garden Cloud: ${e}
If your Garden Cloud instance is self-hosted and you are using a self-signed certificate, Garden will not trust your system's CA certificates.
In case if you need to trust extra certificate authorities, consider exporting the environment variable NODE_EXTRA_CA_CERTS. See https://nodejs.org/api/cli.html#node_extra_ca_certsfile
Request url: ${url}
Error code: ${e.code}
`,
})
}

return {
...res.body,
headers: res.headers,
throw e
}
}

Expand Down

0 comments on commit 1d92962

Please sign in to comment.