Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions packages/amp/src/admin/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import * as Effect from "effect/Effect"
import { constUndefined } from "effect/Function"
import * as Layer from "effect/Layer"
import * as Option from "effect/Option"
import * as Schema from "effect/Schema"
import * as HttpClient from "effect/http/HttpClient"
import type * as HttpClientError from "effect/http/HttpClientError"
import * as HttpClientRequest from "effect/http/HttpClientRequest"
import * as HttpApiClient from "effect/http-api/HttpApiClient"
import type * as HttpApiError from "effect/http-api/HttpApiError"
import * as HttpApiError from "effect/http-api/HttpApiError"
import type * as KeyValueStore from "effect/persistence/KeyValueStore"
import * as Auth from "../auth/service.ts"
import type * as Models from "../core/domain.ts"
Expand Down Expand Up @@ -216,6 +217,20 @@ export interface MakeOptions {
readonly jwt?: string | null | undefined
}

/**
* Keeps HTTP client errors as typed failures, and turns 401 and 403 responses
* into `Unauthorized` and `Forbidden` whatever their body, since the Admin API's
* error bodies don't decode into them. Schema errors become defects.
*/
const handleHttpError = (
error: HttpClientError.HttpClientError | Schema.SchemaError
): Effect.Effect<never, HttpError> => {
if (Schema.isSchemaError(error)) return Effect.die(error)
if (error.response?.status === 401) return Effect.fail(new HttpApiError.Unauthorized({}))
if (error.response?.status === 403) return Effect.fail(new HttpApiError.Forbidden({}))
return Effect.fail(error)
}

const make = Effect.fnUntraced(function* (options: MakeOptions) {
type Service = typeof AdminApi.Service

Expand Down Expand Up @@ -255,7 +270,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ params })
return yield* client.dataset.getDatasetManifest({ params })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const getDatasetLineage: Service["getDatasetLineage"] = Effect.fn("AdminApi.getDatasetLineage")(
Expand All @@ -269,20 +284,20 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ params, query })
return yield* client.dataset.getDatasetLineage({ params, query })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const getDatasets: Service["getDatasets"] = client.dataset
.getDatasets({})
.pipe(Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die), Effect.withSpan("AdminApi.getDatasets"))
.pipe(Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError), Effect.withSpan("AdminApi.getDatasets"))

const getDatasetVersion: Service["getDatasetVersion"] = Effect.fn("AdminApi.getDatasetVersion")(
function* (namespace, name, revision) {
const params = { namespace, name, revision }
yield* Effect.annotateCurrentSpan({ params })
return yield* client.dataset.getDatasetVersion({ params })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const getDatasetVersions: Service["getDatasetVersions"] = Effect.fn("AdminApi.getDatasetVersions")(
Expand All @@ -291,7 +306,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ params })
return yield* client.dataset.getDatasetVersions({ params })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const registerDataset: Service["registerDataset"] = Effect.fn("AdminApi.registerDataset")(
Expand All @@ -300,7 +315,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ payload })
return yield* client.dataset.registerDataset({ payload })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

// Job Operations
Expand All @@ -315,7 +330,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ query })
return yield* client.job.getJobs({ query })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const getJobById: Service["getJobById"] = Effect.fn("AdminApi.getJobById")(
Expand All @@ -324,7 +339,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ params })
return yield* client.job.getJobById({ params })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const stopJob: Service["stopJob"] = Effect.fn("AdminApi.stopJob")(
Expand All @@ -333,7 +348,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ params })
return yield* client.job.stopJob({ params })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

const deleteJob: Service["deleteJob"] = Effect.fn("AdminApi.deleteJob")(
Expand All @@ -342,28 +357,31 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
yield* Effect.annotateCurrentSpan({ params })
return yield* client.job.deleteJob({ params })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

// Worker Operations

const getWorkers: Service["getWorkers"] = client.worker
.getWorkers({})
.pipe(Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die), Effect.withSpan("AdminApi.getWorkers"))
.pipe(Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError), Effect.withSpan("AdminApi.getWorkers"))

// Provider Operations

const getProviders: Service["getProviders"] = client.provider
.getProviders({})
.pipe(Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die), Effect.withSpan("AdminApi.getProviders"))
.pipe(
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError),
Effect.withSpan("AdminApi.getProviders")
)

// Manifest Operations

const registerManifest: Service["registerManifest"] = Effect.fn("AdminApi.registerManifest")(
function* (manifest) {
return yield* client.manifest.registerManifest({ payload: manifest })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

// Schema Operations
Expand All @@ -372,7 +390,7 @@ const make = Effect.fnUntraced(function* (options: MakeOptions) {
function* (payload) {
return yield* client.schema.getOutputSchema({ payload })
},
Effect.catchTag(["HttpClientError", "SchemaError"], Effect.die)
Effect.catchTag(["HttpClientError", "SchemaError"], handleHttpError)
)

return AdminApi.of({
Expand Down
78 changes: 78 additions & 0 deletions packages/amp/test/admin/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import * as Redacted from "effect/Redacted"
import * as HttpClient from "effect/http/HttpClient"
import * as HttpClientError from "effect/http/HttpClientError"
import * as HttpClientResponse from "effect/http/HttpClientResponse"
import * as KeyValueStore from "effect/persistence/KeyValueStore"

Expand Down Expand Up @@ -351,6 +352,83 @@ describe("AdminApi", () => {
)
})

describe("AdminApi failures", () => {
const unauthorized = { error_code: "UNAUTHORIZED", error_message: "missing bearer token" }

it.effect("keeps transport failures as HttpClientError", () =>
Effect.gen(function* () {
const admin = yield* AdminApi.AdminApi
const reason = yield* admin.getDatasets.pipe(
Effect.map(() => "none"),
Effect.catchTag("HttpClientError", (error) => Effect.succeed(error.reason._tag))
)
assert.strictEqual(reason, "TransportError")
}).pipe(
Effect.provide(
AdminApi.layer({ url: "http://localhost:1610" }).pipe(
Layer.provide(
Layer.succeed(
HttpClient.HttpClient,
HttpClient.make((request) =>
Effect.fail(
new HttpClientError.HttpClientError({
reason: new HttpClientError.TransportError({ request, description: "connection refused" })
})
)
)
)
)
)
)
)
)

it.effect("keeps an undeclared status as HttpClientError", () =>
Effect.gen(function* () {
const admin = yield* AdminApi.AdminApi
const error = yield* admin.getJobs().pipe(Effect.flip)
assert.strictEqual(error._tag, "HttpClientError")
}).pipe(Effect.provide(makeLayer({ "GET /jobs": { status: 502, body: "bad gateway" } })))
)

it.effect("maps a 401 to Unauthorized on every endpoint, whatever its body", () =>
Effect.gen(function* () {
const admin = yield* AdminApi.AdminApi
const jobs = yield* admin.getJobs().pipe(Effect.flip)
assert.strictEqual(jobs._tag, "Unauthorized")
const datasets = yield* admin.getDatasets.pipe(Effect.flip)
assert.strictEqual(datasets._tag, "Unauthorized")
const workers = yield* admin.getWorkers.pipe(Effect.flip)
assert.strictEqual(workers._tag, "Unauthorized")
}).pipe(
Effect.provide(
makeLayer({
"GET /jobs": { status: 401, body: unauthorized },
"GET /datasets": { status: 401 },
"GET /workers": { status: 401, body: { _tag: "Unauthorized" } }
})
)
)
)

it.effect("maps a 403 to Forbidden on every endpoint, whatever its body", () =>
Effect.gen(function* () {
const admin = yield* AdminApi.AdminApi
const providers = yield* admin.getProviders.pipe(Effect.flip)
assert.strictEqual(providers._tag, "Forbidden")
const datasets = yield* admin.getDatasets.pipe(Effect.flip)
assert.strictEqual(datasets._tag, "Forbidden")
}).pipe(
Effect.provide(
makeLayer({
"GET /providers": { status: 403 },
"GET /datasets": { status: 403, body: { error_code: "FORBIDDEN", error_message: "not allowed" } }
})
)
)
)
})

describe("AdminApi authentication", () => {
const makeRecordingClient = (authorization: Array<string | undefined>) =>
Layer.succeed(
Expand Down
Loading