Skip to content
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

Rename hpke configs to collector credentials #563

Merged
merged 1 commit into from
Oct 14, 2023
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
50 changes: 32 additions & 18 deletions app/src/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface Task {
max_batch_size: number | null;
report_count: number;
aggregate_collection_count: number;
hpke_config_id: string;
collector_credential_id: string;
}

export interface CollectorAuthToken {
Expand Down Expand Up @@ -153,7 +153,7 @@ export interface ApiToken {
last_used_at?: string;
}

export interface HpkeConfig {
export interface CollectorCredential {
id: string;
contents: {
id: number;
Expand Down Expand Up @@ -436,35 +436,45 @@ export class ApiClient {
return res.data as QueueJob;
}

async deleteHpkeConfig(hpkeConfigId: string) {
await this.delete(`/api/hpke_configs/${hpkeConfigId}`);
async deleteCollectorCredential(collectorCredentialId: string) {
await this.delete(`/api/collector_credentials/${collectorCredentialId}`);
return null;
}

async updateHpkeConfig(hpkeConfigId: string, hpkeConfig: { name: string }) {
await this.patch(`/api/hpke_configs/${hpkeConfigId}`, hpkeConfig);
async updateCollectorCredential(
collectorCredentialId: string,
collectorCredential: { name: string },
) {
await this.patch(
`/api/collector_credentials/${collectorCredentialId}`,
collectorCredential,
);
return null;
}

async hpkeConfig(hpkeConfigId: string): Promise<HpkeConfig> {
const res = await this.get(`/api/hpke_configs/${hpkeConfigId}`);
return res.data as HpkeConfig;
async collectorCredential(
collectorCredentialId: string,
): Promise<CollectorCredential> {
const res = await this.get(
`/api/collector_credentials/${collectorCredentialId}`,
);
return res.data as CollectorCredential;
}

async createHpkeConfig(
async createCollectorCredential(
accountId: string,
hpkeConfig: { contents: string; name: string },
collectorCredential: { contents: string; name: string },
): Promise<
| HpkeConfig
| CollectorCredential
| { error: ValidationErrorsFor<{ contents: string; name: string }> }
> {
const res = await this.post(
`/api/accounts/${accountId}/hpke_configs`,
hpkeConfig,
`/api/accounts/${accountId}/collector_credentials`,
collectorCredential,
);
switch (res.status) {
case 201:
return res.data as HpkeConfig;
return res.data as CollectorCredential;
case 400:
return { error: res.data } as {
error: ValidationErrorsFor<{ contents: string; name: string }>;
Expand All @@ -474,9 +484,13 @@ export class ApiClient {
}
}

async accountHpkeConfigs(accountId: string): Promise<HpkeConfig[]> {
const res = await this.get(`/api/accounts/${accountId}/hpke_configs`);
return res.data as HpkeConfig[];
async accountCollectorCredentials(
accountId: string,
): Promise<CollectorCredential[]> {
const res = await this.get(
`/api/accounts/${accountId}/collector_credentials`,
);
return res.data as CollectorCredential[];
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/accounts/AccountSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default function AccountSummary() {
</ListGroup.Item>
</LinkContainer>

<LinkContainer to="hpke_configs">
<LinkContainer to="collector_credentials">
<ListGroup.Item action>
<KeyFill /> HPKE Configs
</ListGroup.Item>
Expand Down
21 changes: 12 additions & 9 deletions app/src/accounts/NextSteps/InlineCollectorCredentials.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,27 @@ function SaveApiToken({ onToken }: { onToken: (token: string) => void }) {
export default function InlineCollectorCredentials() {
const apiClient = React.useContext(ApiClientContext);
const { accountId } = useParams() as { accountId: string };
const [anyHpkeConfigs, setAnyHpkeConfigs] = React.useState(false);
const [anyCollectorCredentials, setAnyCollectorCredentials] =
React.useState(false);
const [inFlight, setInFlight] = React.useState(false);
useInterval(
React.useCallback(() => {
if (inFlight || anyHpkeConfigs) return;
if (inFlight || anyCollectorCredentials) return;
setInFlight(true);
apiClient.accountHpkeConfigs(accountId).then((hpkeConfigs) => {
setAnyHpkeConfigs(hpkeConfigs.length > 0);
setInFlight(false);
});
}, [apiClient, setInFlight, setAnyHpkeConfigs]),
apiClient
.accountCollectorCredentials(accountId)
.then((collectorCredentials) => {
setAnyCollectorCredentials(collectorCredentials.length > 0);
setInFlight(false);
});
}, [apiClient, setInFlight, setAnyCollectorCredentials]),
1000,
);

const [token, setToken] = React.useState<string>("«TOKEN»");
const { revalidate, state } = useRevalidator();

if (anyHpkeConfigs) {
if (anyCollectorCredentials) {
return (
<>
<h1>Success</h1>
Expand All @@ -84,7 +87,7 @@ export default function InlineCollectorCredentials() {
</>
);
} else {
const command = `divviup -t ${token} hpke-config generate`;
const command = `divviup -t ${token} collector-credential generate`;
return (
<>
<ol className="list-unstyled">
Expand Down
34 changes: 20 additions & 14 deletions app/src/accounts/NextSteps/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Steps } from "primereact/steps";
import { Button, Card, Col, Row } from "react-bootstrap";
import { useLoaderData } from "react-router-dom";
import { Account, Aggregator, HpkeConfig, Task } from "../../ApiClient";
import {
Account,
Aggregator,
CollectorCredential,
Task,
} from "../../ApiClient";
import { LinkContainer } from "react-router-bootstrap";
import AggregatorTypeSelection from "./AggregatorTypeSelection";
import InlineCollectorCredentials from "./InlineCollectorCredentials";
Expand All @@ -17,20 +22,20 @@ const STEPS = [

function determineModel({
account,
hpkeConfigs,
collectorCredentials,
aggregators,
}: {
account: Account;
hpkeConfigs: HpkeConfig[];
collectorCredentials: CollectorCredential[];
aggregators: Aggregator[];
}): number {
const aggregatorStepComplete =
account.intends_to_use_shared_aggregators === true ||
!!aggregators.find((a) => !!a.account_id);
const hpkeConfigStepComplete = hpkeConfigs.length > 0;
const collectorCredentialStepComplete = collectorCredentials.length > 0;

const nextStepIndex =
[aggregatorStepComplete, hpkeConfigStepComplete, false].findIndex(
[aggregatorStepComplete, collectorCredentialStepComplete, false].findIndex(
(x) => !x,
) + 1;

Expand All @@ -50,18 +55,19 @@ function NextInner({ activeIndex }: { activeIndex: number | undefined }) {
}

export default function NextSteps() {
const { account, hpkeConfigs, aggregators, tasks } = useLoaderData() as {
account: Promise<Account>;
hpkeConfigs: Promise<HpkeConfig[]>;
aggregators: Promise<Aggregator[]>;
tasks: Promise<Task[]>;
};
const { account, collectorCredentials, aggregators, tasks } =
useLoaderData() as {
account: Promise<Account>;
collectorCredentials: Promise<CollectorCredential[]>;
aggregators: Promise<Aggregator[]>;
tasks: Promise<Task[]>;
};

const loadedTasks = usePromise(tasks, []);
const activeIndex = usePromiseAll(
[account, hpkeConfigs, aggregators],
([account, hpkeConfigs, aggregators]) =>
determineModel({ account, hpkeConfigs, aggregators }),
[account, collectorCredentials, aggregators],
([account, collectorCredentials, aggregators]) =>
determineModel({ account, collectorCredentials, aggregators }),
1,
);

Expand Down
3 changes: 2 additions & 1 deletion app/src/accounts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export default function accounts(
return defer({
apiTokens: apiClient.accountApiTokens(accountId),
tasks: apiClient.accountTasks(accountId),
hpkeConfigs: apiClient.accountHpkeConfigs(accountId),
collectorCredentials:
apiClient.accountCollectorCredentials(accountId),
aggregators: apiClient.accountAggregators(accountId),
account: apiClient.account(accountId),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import { KeyFill } from "react-bootstrap-icons";
import { useFetcher } from "react-router-dom";
import { formikErrors } from "../ApiClient";

export default function HpkeConfigForm() {
export default function CollectorCredentialForm() {
const fetcher = useFetcher();
const [name, setName] = React.useState("");
const [hpke, setHpke] = React.useState("");
const [collectorCredential, setCollectorCredential] = React.useState("");
const reader = React.useMemo(() => {
const reader = new FileReader();
reader.addEventListener("load", () => {
if (typeof reader.result === "string") {
setHpke(reader.result.split(",")[1]);
setCollectorCredential(reader.result.split(",")[1]);
}
});
return reader;
}, [setHpke]);
}, [setCollectorCredential]);
const onChange: ChangeEventHandler<HTMLInputElement> = React.useCallback(
(event) => {
const files = event.target.files;
Expand All @@ -44,12 +44,12 @@ export default function HpkeConfigForm() {
useEffect(() => {
if (typeof fetcher.data === "object" && !("error" in fetcher.data)) {
setName("");
setHpke("");
setCollectorCredential("");
if (ref.current) {
ref.current.value = "";
}
}
}, [fetcher, setName, setHpke, ref]);
}, [fetcher, setName, setCollectorCredential, ref]);

const errors = formikErrors<{ contents?: string; name?: string }>(
fetcher.data && "error" in fetcher.data
Expand Down Expand Up @@ -97,7 +97,7 @@ export default function HpkeConfigForm() {
</FormControl.Feedback>
) : null}
</FormGroup>
<input type="hidden" name="contents" value={hpke} />
<input type="hidden" name="contents" value={collectorCredential} />
</Col>
<Col sm="2">
<FormGroup controlId="submit" className="my-3">
Expand Down
Loading
Loading