-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[wip] SpiceDB reconnect behavior #18570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,14 @@ import { TrustedValue } from "@gitpod/gitpod-protocol/lib/util/scrubbing"; | |
import { getExperimentsClientForBackend } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server"; | ||
import { inject, injectable } from "inversify"; | ||
import { observeSpicedbClientLatency, spicedbClientLatency } from "../prometheus-metrics"; | ||
import { SpiceDBClient } from "./spicedb"; | ||
import { SpiceDBClientProvider } from "./spicedb"; | ||
import * as grpc from "@grpc/grpc-js"; | ||
|
||
@injectable() | ||
export class SpiceDBAuthorizer { | ||
constructor( | ||
@inject(SpiceDBClient) | ||
private client: SpiceDBClient, | ||
@inject(SpiceDBClientProvider) | ||
private readonly clientProvider: SpiceDBClientProvider, | ||
) {} | ||
|
||
async check( | ||
|
@@ -26,10 +27,6 @@ export class SpiceDBAuthorizer { | |
userId: string; | ||
}, | ||
): Promise<boolean> { | ||
if (!this.client) { | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was really confusing in testing 🙈 |
||
} | ||
|
||
const featureEnabled = await getExperimentsClientForBackend().getValueAsync("centralizedPermissions", false, { | ||
user: { | ||
id: experimentsFields.userId, | ||
|
@@ -42,7 +39,8 @@ export class SpiceDBAuthorizer { | |
const timer = spicedbClientLatency.startTimer(); | ||
let error: Error | undefined; | ||
try { | ||
const response = await this.client.checkPermission(req); | ||
const client = this.clientProvider.getClient(); | ||
const response = await client.checkPermission(req, this.callOptions); | ||
const permitted = response.permissionship === v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION; | ||
|
||
return permitted; | ||
|
@@ -58,17 +56,15 @@ export class SpiceDBAuthorizer { | |
} | ||
|
||
async writeRelationships(...updates: v1.RelationshipUpdate[]): Promise<v1.WriteRelationshipsResponse | undefined> { | ||
if (!this.client) { | ||
return undefined; | ||
} | ||
|
||
const timer = spicedbClientLatency.startTimer(); | ||
let error: Error | undefined; | ||
try { | ||
const response = await this.client.writeRelationships( | ||
const client = this.clientProvider.getClient(); | ||
const response = await client.writeRelationships( | ||
v1.WriteRelationshipsRequest.create({ | ||
updates, | ||
}), | ||
this.callOptions, | ||
); | ||
log.info("[spicedb] Successfully wrote relationships.", { response, updates }); | ||
|
||
|
@@ -82,17 +78,14 @@ export class SpiceDBAuthorizer { | |
} | ||
|
||
async deleteRelationships(req: v1.DeleteRelationshipsRequest): Promise<v1.ReadRelationshipsResponse[]> { | ||
if (!this.client) { | ||
return []; | ||
} | ||
|
||
const timer = spicedbClientLatency.startTimer(); | ||
let error: Error | undefined; | ||
try { | ||
const existing = await this.client.readRelationships(v1.ReadRelationshipsRequest.create(req)); | ||
const client = this.clientProvider.getClient(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could extract this into
|
||
const existing = await client.readRelationships(v1.ReadRelationshipsRequest.create(req), this.callOptions); | ||
if (existing.length > 0) { | ||
const response = await this.client.deleteRelationships(req); | ||
const after = await this.client.readRelationships(v1.ReadRelationshipsRequest.create(req)); | ||
const response = await client.deleteRelationships(req, this.callOptions); | ||
const after = await client.readRelationships(v1.ReadRelationshipsRequest.create(req), this.callOptions); | ||
if (after.length > 0) { | ||
log.error("[spicedb] Failed to delete relationships.", { existing, after, request: req }); | ||
} | ||
|
@@ -115,9 +108,21 @@ export class SpiceDBAuthorizer { | |
} | ||
|
||
async readRelationships(req: v1.ReadRelationshipsRequest): Promise<v1.ReadRelationshipsResponse[]> { | ||
if (!this.client) { | ||
const client = this.clientProvider.getClient(); | ||
if (!client) { | ||
return []; | ||
} | ||
return this.client.readRelationships(req); | ||
return client.readRelationships(req, this.callOptions); | ||
} | ||
|
||
/** | ||
* permission_service.grpc-client.d.ts has all methods overloaded with this pattern: | ||
* - xyzRelationships(input: Request, metadata?: grpc.Metadata | grpc.CallOptions, options?: grpc.CallOptions): grpc.ClientReadableStream<ReadRelationshipsResponse>; | ||
* But the promisified client somehow does not have the same overloads. Thus we convince it here that options may be passed as 2nd argument. | ||
*/ | ||
private get callOptions(): grpc.Metadata { | ||
return (<grpc.CallOptions>{ | ||
deadline: Date.now() + 8000, | ||
}) as any as grpc.Metadata; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ import { AuthJWT, SignInJWT } from "./auth/jwt"; | |
import { LoginCompletionHandler } from "./auth/login-completion-handler"; | ||
import { VerificationService } from "./auth/verification-service"; | ||
import { Authorizer, createInitializingAuthorizer } from "./authorization/authorizer"; | ||
import { SpiceDBClient, spicedbClientFromEnv } from "./authorization/spicedb"; | ||
import { CachingSpiceDBClientProvider, SpiceDBClientProvider, spiceDBConfigFromEnv } from "./authorization/spicedb"; | ||
import { BillingModes } from "./billing/billing-mode"; | ||
import { EntitlementService, EntitlementServiceImpl } from "./billing/entitlement-service"; | ||
import { EntitlementServiceUBP } from "./billing/entitlement-service-ubp"; | ||
|
@@ -302,8 +302,15 @@ export const productionContainerModule = new ContainerModule( | |
bind(IamSessionApp).toSelf().inSingletonScope(); | ||
|
||
// Authorization & Perms | ||
bind(SpiceDBClient) | ||
.toDynamicValue(() => spicedbClientFromEnv()) | ||
bind(SpiceDBClientProvider) | ||
.toDynamicValue((ctx) => { | ||
const config = spiceDBConfigFromEnv(); | ||
if (!config) { | ||
throw new Error("[spicedb] Missing configuration expected in env vars!"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We error now if the configuration is incomplete. |
||
} | ||
const clientCallMetrics = ctx.container.get<IClientCallMetrics>(IClientCallMetrics); | ||
return new CachingSpiceDBClientProvider(config, clientCallMetrics); | ||
}) | ||
.inSingletonScope(); | ||
bind(SpiceDBAuthorizer).toSelf().inSingletonScope(); | ||
bind(Authorizer) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,12 @@ | |
jsonpointer "^5.0.0" | ||
leven "^3.1.0" | ||
|
||
"@authzed/authzed-node@^0.10.0": | ||
version "0.10.0" | ||
resolved "https://registry.yarnpkg.com/@authzed/authzed-node/-/authzed-node-0.10.0.tgz#623e4911fde221bb526e7f2e9ca335d9f3b9072d" | ||
integrity sha512-TnAnatcU5dHvyGqrWoZzPNaO1opPpVU1y7P5LrJsV2j54y0xvx/OFhYtfeguMxHSz2kpbdCuIvIKJuB8WFbRRA== | ||
"@authzed/authzed-node@^0.12.1": | ||
version "0.12.1" | ||
resolved "https://registry.yarnpkg.com/@authzed/authzed-node/-/authzed-node-0.12.1.tgz#0c28395a64f9b1ecf33faf67259e32a9a3bce300" | ||
integrity sha512-BVHLaPfiHQw1Vz+199m9i4xltT3YyFhqVHtkYPIQ28q8a7iJpnXmFRZIWuTMJcxJI01wtAxJYFuRJq3ktFe6qw== | ||
dependencies: | ||
"@grpc/grpc-js" "^1.2.8" | ||
"@grpc/grpc-js" "^1.8.3" | ||
"@protobuf-ts/runtime" "^2.8.1" | ||
"@protobuf-ts/runtime-rpc" "^2.8.1" | ||
google-protobuf "^3.15.3" | ||
|
@@ -2222,22 +2222,14 @@ | |
qs "^6.10.1" | ||
xcase "^2.0.1" | ||
|
||
"@grpc/[email protected]": | ||
"@grpc/[email protected]", "@grpc/grpc-js@^1.8.3": | ||
version "1.8.8" | ||
resolved "https://registry.yarnpkg.com/@grpc/grpc-js/-/grpc-js-1.8.8.tgz#a7c6765d0302f47ba67c0ce3cb79718d6b028248" | ||
integrity sha512-4gfDqMLXTrorvYTKA1jL22zLvVwiHJ73t6Re1OHwdCFRjdGTDOVtSJuaWhtHaivyeDGg0LeCkmU77MTKoV3wPA== | ||
dependencies: | ||
"@grpc/proto-loader" "^0.7.0" | ||
"@types/node" ">=12.12.47" | ||
|
||
"@grpc/grpc-js@^1.2.8": | ||
version "1.8.7" | ||
resolved "https://registry.yarnpkg.com/@grpc/grpc-js/-/grpc-js-1.8.7.tgz#2154fc0134462ad45f4134e8b54682a25ed05956" | ||
integrity sha512-dRAWjRFN1Zy9mzPNLkFFIWT8T6C9euwluzCHZUKuhC+Bk3MayNPcpgDRyG+sg+n2sitEUySKxUynirVpu9ItKw== | ||
dependencies: | ||
"@grpc/proto-loader" "^0.7.0" | ||
"@types/node" ">=12.12.47" | ||
|
||
"@grpc/grpc-js@^1.6.1": | ||
version "1.7.0" | ||
resolved "https://registry.yarnpkg.com/@grpc/grpc-js/-/grpc-js-1.7.0.tgz#5a96bdbe51cce23faa38a4db6e43595a5c584849" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would hide the error and only log. Is that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, good catch.