Skip to content
Merged
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
34 changes: 34 additions & 0 deletions docs/self-host/knowledge-sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,40 @@ CoderHelm asks for `https://www.googleapis.com/auth/drive.readonly`. A knowledge

On a hybrid install that chose CoderHelm's Google app, Drive is offered only while the hosted service has its own Drive app; otherwise the choice is unavailable and the install falls back to its own keys.

## Bedrock knowledge bases, connected in place

A knowledge base in Amazon Bedrock can be connected in place instead of copied. CoderHelm searches it live in your AWS account each time an agent or the brain chat needs it, and nothing is copied out: no documents, no passages, no vectors. Bedrock turns the question into a search itself, so it doesn't matter which embedding model, chunking or vector store the knowledge base uses. Both kinds of Bedrock knowledge base work, the classic ones whose vector store you run and the managed ones.

To connect one, open **Knowledge**, **New knowledge base**, **Bedrock knowledge base**. On a self-hosted install the dialog lists the knowledge bases in the install's own account and region; pick one, or paste the ARN of any knowledge base (`arn:aws:bedrock:<region>:<account>:knowledge-base/<id>`). Before anything is saved, CoderHelm runs one test search. When it fails, the dialog says why in one sentence and what to do. The base's page then shows **Connected in place: searched live in your AWS account, nothing copied** instead of a list of files, and it takes no uploads or synced apps.

How it is searched:

- **Hybrid search.** CoderHelm asks for Bedrock's hybrid search (vectors and keywords) where the vector store supports it, and plain vector search where it doesn't (the test search finds out). No reranking.
- **Merged with your other bases.** Bedrock's ranked list joins the same rank fusion as CoderHelm's own lists, so answers can cite a connected base and an uploaded file side by side. At most three passages come from one document.
- **Citations.** A citation names the document and links to it: the original page for a web, Confluence or SharePoint source, and the S3 console for an S3 object (which opens only for people who can sign in to that account).
- **The region is the ARN's.** A knowledge base in another region than the install is searched in its own region.
- **When it doesn't answer.** The other bases still answer, and the agent or chat is told that this one couldn't be searched, and why.

### Who searches it

A knowledge base in the install's own account is searched with the install's own gateway and worker roles. Their read-only policy comes from the same update template as the rest of the install's own-account reads, and for the install it names every knowledge base in its account (`bedrock:Retrieve` and `bedrock:GetDocumentContent` on `arn:aws:bedrock:*:<account>:knowledge-base/*`, and `bedrock:ListKnowledgeBases` for the list). Admins still choose which ones a team connects. Each version update brings this policy along.

A knowledge base in another account is searched with that account's `CoderHelmLogReader` role, the one the account was connected with on **Settings**, **Integrations**, **AWS**. Connect that account first. The role may search only the knowledge bases named in its stack's `KnowledgeBases` parameter (their ARNs separated by commas, or `all` for every knowledge base in that account). When the test search finds the role can't, the dialog says "The role in your account does not allow Retrieve on this knowledge base" and offers a one-click update stack in that account with the parameter filled in. That stack carries everything the role's other update stacks do, so you can delete the older `CoderHelmLogReaderUpdate` stacks once it is in place. If AWS says the role's policies are too large, delete them first.

### The permissions boundary: one admin step

Searching needs `bedrock:Retrieve`, and the install's permissions boundary allows it only from this release on. Listing and reading a cited document were already allowed (`bedrock:List*`, `bedrock:Get*`). An update never changes the boundary: the parent stack keeps the boundary installs have, and the new one reaches an install only through the access stack an AWS admin applies (**Settings**, **System**, **Version**, **Permissions**, one link to the AWS console). See [Updates](./updates.md).

Until an admin applies it, connecting a knowledge base in the install's own account says so plainly: "This install's permission limit doesn't allow searching Bedrock knowledge bases yet: the release that searches them needs a permission change an AWS admin hasn't applied." Nothing else waits on it, and knowledge bases in other accounts work without it.

### Private networks

An install whose functions have no internet route needs interface VPC endpoints for `bedrock-agent-runtime` (search) and `bedrock-agent` (the list), in each region a connected knowledge base lives in. Without them the dialog says CoderHelm couldn't reach Bedrock.

### Who sees what

Everyone who can read the CoderHelm knowledge base sees what the search returns, whatever their rights in the systems the Bedrock knowledge base was built from. Agents search the bases attached to them, so anyone who can start a run reaches everything the role can search in those bases. Connect only knowledge bases that everyone with that access may read.

## What runs

- **Schedule.** An EventBridge rule (`<prefix>-knowledge-sync`) drops `knowledge_sync_sweep` on the ticket queue every 15 minutes. The worker queues a sync for each source that is due: every 15 minutes for a Sanity sign-in source, every hour for other sources, or every 6 hours as a backstop for sources that get instant updates.
Expand Down
41 changes: 40 additions & 1 deletion infra/cfn/coderhelm-log-reader-update.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: >
Brings the existing CoderHelmLogReader role up to date with the read-only
permissions the current CoderHelm release uses. Every permission here is
permissions the current CoderHelm release uses, and lets it search the
Bedrock knowledge bases named in KnowledgeBases. Every permission here is
read-only: CoderHelm can list, describe and query, never change anything.
It only adds: to drop a permission an older release granted, update the
CoderHelmLogReader stack itself with the current role template.
Expand All @@ -15,6 +16,20 @@ Parameters:
Description: The CoderHelm role to update (do not change this value).
AllowedPattern: "^CoderHelmLogReader$"

KnowledgeBases:
Type: String
Default: ""
Description: >
Bedrock knowledge bases CoderHelm may search, read-only: their ARNs
separated by commas, or "all" for every knowledge base in this account.
Empty: none (the role reads no knowledge base).
AllowedPattern: "^(|all|arn:aws:bedrock:[a-z0-9-]+:[0-9]{12}:knowledge-base/[A-Z0-9]{10}(,arn:aws:bedrock:[a-z0-9-]+:[0-9]{12}:knowledge-base/[A-Z0-9]{10})*)$"
ConstraintDescription: Knowledge base ARNs separated by commas, "all", or empty.

Conditions:
ReadsKnowledgeBases: !Not [!Equals [!Ref KnowledgeBases, ""]]
ReadsEveryKnowledgeBase: !Equals [!Ref KnowledgeBases, "all"]

Resources:
CoderHelmReadOnlyUpdate:
Type: AWS::IAM::Policy
Expand Down Expand Up @@ -104,3 +119,27 @@ Resources:
- "kms:DescribeKey"
- "kms:GetKeyPolicy"
Resource: "*"
# Bedrock knowledge bases connected in place: searched live in
# this account, nothing copied. Only the knowledge bases named in
# KnowledgeBases (or every one in this account with "all"):
# search and read a cited document, never change one.
- !If
- ReadsKnowledgeBases
- Sid: ReadKnowledgeBases
Effect: Allow
Action:
- "bedrock:Retrieve"
- "bedrock:GetDocumentContent"
Resource: !If
- ReadsEveryKnowledgeBase
- !Sub "arn:aws:bedrock:*:${AWS::AccountId}:knowledge-base/*"
- !Split [",", !Ref KnowledgeBases]
- !Ref AWS::NoValue
- !If
- ReadsEveryKnowledgeBase
- Sid: ListKnowledgeBases
Effect: Allow
Action:
- "bedrock:ListKnowledgeBases"
Resource: "*"
- !Ref AWS::NoValue
46 changes: 44 additions & 2 deletions infra/cfn/coderhelm-log-reader.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: >
Creates a read-only IAM role that allows Coderhelm to query your CloudWatch Logs and metrics
and read Airflow (MWAA) as a Viewer.
Creates a read-only IAM role that allows Coderhelm to query your CloudWatch Logs and metrics,
read Airflow (MWAA) as a Viewer, and search the Bedrock knowledge bases you name.
This role uses an External ID to prevent confused deputy attacks.

Parameters:
Expand All @@ -27,6 +27,20 @@ Parameters:
allows any role in that account (do not change this value).
AllowedPattern: "^[A-Za-z0-9_-]{0,41}$"

KnowledgeBases:
Type: String
Default: ""
Description: >
Bedrock knowledge bases CoderHelm may search, read-only: their ARNs
separated by commas, or "all" for every knowledge base in this account.
Empty: none (the role reads no knowledge base).
AllowedPattern: "^(|all|arn:aws:bedrock:[a-z0-9-]+:[0-9]{12}:knowledge-base/[A-Z0-9]{10}(,arn:aws:bedrock:[a-z0-9-]+:[0-9]{12}:knowledge-base/[A-Z0-9]{10})*)$"
ConstraintDescription: Knowledge base ARNs separated by commas, "all", or empty.

Conditions:
ReadsKnowledgeBases: !Not [!Equals [!Ref KnowledgeBases, ""]]
ReadsEveryKnowledgeBase: !Equals [!Ref KnowledgeBases, "all"]

Resources:
CoderHelmLogReaderRole:
Type: AWS::IAM::Role
Expand Down Expand Up @@ -172,6 +186,34 @@ Resources:
- "kms:DescribeKey"
- "kms:GetKeyPolicy"
Resource: "*"
# Bedrock knowledge bases connected in place: searched live in this
# account, nothing copied. Only the knowledge bases named in
# KnowledgeBases (or every one in this account with "all"): search
# and read a cited document, never change one.
- !If
- ReadsKnowledgeBases
- PolicyName: KnowledgeBasesReadOnly
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ReadKnowledgeBases
Effect: Allow
Action:
- "bedrock:Retrieve"
- "bedrock:GetDocumentContent"
Resource: !If
- ReadsEveryKnowledgeBase
- !Sub "arn:aws:bedrock:*:${AWS::AccountId}:knowledge-base/*"
- !Split [",", !Ref KnowledgeBases]
- !If
- ReadsEveryKnowledgeBase
- Sid: ListKnowledgeBases
Effect: Allow
Action:
- "bedrock:ListKnowledgeBases"
Resource: "*"
- !Ref AWS::NoValue
- !Ref AWS::NoValue

Outputs:
RoleArn:
Expand Down
7 changes: 7 additions & 0 deletions infra/lib/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ export const BOUNDARY_SERVICES = [
"bedrock:InvokeModelWithResponseStream",
"bedrock:Get*",
"bedrock:List*",
// Searching a Bedrock knowledge base connected in place, in the install's own
// account (own-account-read.ts; GetDocumentContent and ListKnowledgeBases are
// under Get* and List*). It reaches an install only through the access stack
// an AWS admin applies (native/access-stack.ts): the parent keeps its pinned
// boundary (access-pinned.json), and until the admin step the knowledge base
// page says so (services/common/src/knowledge/remote.rs, refusal).
"bedrock:Retrieve",
"cloudformation:*",
"cloudfront:*",
"cloudwatch:*",
Expand Down
39 changes: 35 additions & 4 deletions infra/lib/own-account-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
* Connecting the account still proves the team's own CoderHelmLogReader role
* with its private ID (routes/log_analyzer.rs); only reading uses the
* install's role (services/common/src/live/aws.rs, install_target).
*
* Bedrock knowledge bases connected in place (services/common/src/knowledge/remote.rs):
* the install is the customer's alone, so its role may search every knowledge
* base in its own account (the template's KnowledgeBases "all"), and list them
* for the picker. Admins still choose which ones a team connects. Retrieve also
* needs the permissions boundary to allow bedrock:Retrieve, which reaches an
* install only through the access stack an AWS admin applies (native/access-stack.ts).
*/
import * as cdk from "aws-cdk-lib";
import * as iam from "aws-cdk-lib/aws-iam";
Expand All @@ -24,15 +31,39 @@ export const OWN_ROLE_ENV = "INSTALL_OWN_ROLE_ARN";
const UPDATE_TEMPLATE = "infra/cfn/coderhelm-log-reader-update.yaml";
const UPDATE_POLICY_ID = "CoderHelmReadOnlyUpdate";
const ROLE_NAME_MARK = "__CODERHELM_ROLE__";
/** The install's own role searches every knowledge base in its own account. */
const OWN_KNOWLEDGE_BASES = "all";
/** The template's conditions for that value. A condition not listed here fails the build. */
const OWN_CONDITIONS: Record<string, boolean> = { ReadsKnowledgeBases: true, ReadsEveryKnowledgeBase: true };

type Statement = { Sid?: string; Effect: string; Action: string | string[]; Resource: unknown };

/** The update template's statements, as the template has them. */
/**
* `v` with every `Fn::If` decided by `conditions` and every `AWS::NoValue`
* dropped, as CloudFormation would for those values (CfnInclude keeps them,
* and the policy is written into another stack that has no such conditions).
*/
export function decideConditions(v: unknown, conditions: Record<string, boolean>): unknown {
const noValue = (x: unknown) => !!x && typeof x === "object" && (x as any).Ref === "AWS::NoValue";
if (Array.isArray(v)) return v.map((x) => decideConditions(x, conditions)).filter((x) => !noValue(x));
if (v && typeof v === "object") {
const o = v as Record<string, unknown>;
if (Object.keys(o).length === 1 && Array.isArray(o["Fn::If"])) {
const [name, yes, no] = o["Fn::If"] as [string, unknown, unknown];
if (!(name in conditions)) throw new Error(`${UPDATE_TEMPLATE}: condition ${name} isn't decided for the install's own role (own-account-read.ts)`);
return decideConditions(conditions[name] ? yes : no, conditions);
}
return Object.fromEntries(Object.entries(o).map(([k, x]) => [k, decideConditions(x, conditions)]).filter(([, x]) => !noValue(x)));
}
return v;
}

/** The update template's statements for the install's own role: its knowledge bases are every one in its account. */
export function updateTemplateStatements(): Statement[] {
const scratch = new cdk.Stack(new cdk.App(), "Scratch");
const inc = new CfnInclude(scratch, "T", { templateFile: repoPath(UPDATE_TEMPLATE), parameters: { RoleName: ROLE_NAME_MARK } });
const inc = new CfnInclude(scratch, "T", { templateFile: repoPath(UPDATE_TEMPLATE), parameters: { RoleName: ROLE_NAME_MARK, KnowledgeBases: OWN_KNOWLEDGE_BASES } });
const p = inc.getResource(UPDATE_POLICY_ID) as iam.CfnPolicy;
return scratch.resolve(p.policyDocument).Statement as Statement[];
return decideConditions(scratch.resolve(p.policyDocument).Statement, OWN_CONDITIONS) as Statement[];
}

/**
Expand All @@ -49,7 +80,7 @@ export function ownAccountStatements(roleArn: string): Statement[] {
export function grantOwnAccountRead(scope: Construct, id: string, fn: cdk.aws_lambda.Function): void {
const role = fn.role!;
new iam.CfnManagedPolicy(scope, id, {
description: "CoderHelm: reads this install's own AWS account (logs, metrics, Airflow, ECS), read-only; kept current by each version update",
description: "CoderHelm: reads this install's own AWS account (logs, metrics, Airflow, ECS, Bedrock knowledge bases), read-only; kept current by each version update",
roles: [role.roleName],
policyDocument: { Version: "2012-10-17", Statement: ownAccountStatements(role.roleArn) },
});
Expand Down
Loading
Loading