Skip to content

Commit

Permalink
WD-8431 - Fetch and store secrets (#1687)
Browse files Browse the repository at this point in the history
* Fetch and store secrets.
  • Loading branch information
huwshimi authored Jan 29, 2024
1 parent 20707b6 commit 0d0ae5f
Show file tree
Hide file tree
Showing 16 changed files with 1,181 additions and 21 deletions.
32 changes: 32 additions & 0 deletions src/juju/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
stopModelWatcher,
findAuditEvents,
crossModelQuery,
connectToModel,
} from "./api";
import type { AllWatcherDelta } from "./types";
import { DeltaChangeTypes, DeltaEntityTypes } from "./types";
Expand Down Expand Up @@ -1054,6 +1055,37 @@ describe("Juju API", () => {
});
});

describe("connectToModel", () => {
it("can connect and log in", async () => {
const credentials = credentialFactory.build();
const conn = {
facades: {},
} as unknown as Connection;
const connectAndLogin = jest
.spyOn(jujuLib, "connectAndLogin")
.mockImplementation(async () => ({
logout: jest.fn(),
conn,
}));
const response = await connectToModel(
"abc123",
"wss://example.com/api",
credentials,
false,
);
expect(connectAndLogin).toHaveBeenCalledWith(
"wss://example.com/model/abc123/api",
{
username: credentials.user,
password: credentials.password,
},
expect.any(Object),
CLIENT_VERSION,
);
expect(response).toMatchObject(conn);
});
});

describe("getApplicationConfig", () => {
it("can get app config", async () => {
const config = applicationGetFactory.build();
Expand Down
34 changes: 28 additions & 6 deletions src/juju/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Cloud from "@canonical/jujulib/dist/api/facades/cloud";
import Controller from "@canonical/jujulib/dist/api/facades/controller";
import ModelManager from "@canonical/jujulib/dist/api/facades/model-manager";
import Pinger from "@canonical/jujulib/dist/api/facades/pinger";
import Secrets from "@canonical/jujulib/dist/api/facades/secrets";
import { jujuUpdateAvailable } from "@canonical/jujulib/dist/api/versions";
import type { ValueOf } from "@canonical/react-components";
import Limiter from "async-limiter";
Expand Down Expand Up @@ -89,6 +90,7 @@ export function generateConnectionOptions(
Controller,
ModelManager,
JIMM,
Secrets,
];
if (usePinger) {
facades.push(Pinger);
Expand Down Expand Up @@ -495,6 +497,28 @@ export function disableControllerUUIDMasking(conn: ConnectionWithFacades) {
});
}

/**
Connect to the model representing the supplied modelUUID.
@param modelUUID
@param appState
@returns conn The connection.
*/
export async function connectToModel(
modelUUID: string,
wsControllerURL: string,
credentials?: Credential,
identityProviderAvailable = false,
) {
const modelURL = wsControllerURL.replace("/api", `/model/${modelUUID}/api`);
const response = await connectAndLoginWithTimeout(
modelURL,
credentials,
generateConnectionOptions(true),
identityProviderAvailable,
);
return response.conn;
}

/**
Connect to the model representing the supplied modelUUID.
@param modelUUID
Expand All @@ -511,14 +535,12 @@ export async function connectAndLoginToModel(
}
const config = getConfig(appState);
const credentials = getUserPass(appState, wsControllerURL);
const modelURL = wsControllerURL.replace("/api", `/model/${modelUUID}/api`);
const response = await connectAndLoginWithTimeout(
modelURL,
return connectToModel(
modelUUID,
wsControllerURL,
credentials,
generateConnectionOptions(true),
config?.identityProviderAvailable ?? false,
config?.identityProviderAvailable,
);
return response.conn;
}

/**
Expand Down
Loading

0 comments on commit 0d0ae5f

Please sign in to comment.