Skip to content

Commit

Permalink
Merge pull request #110 from huwshimi/updates-for-juju-3.2
Browse files Browse the repository at this point in the history
WD-3491 - 3.2 support
  • Loading branch information
huwshimi authored May 1, 2023
2 parents ed74fd7 + 229cf20 commit 0895f47
Show file tree
Hide file tree
Showing 31 changed files with 6,822 additions and 1,259 deletions.
264 changes: 136 additions & 128 deletions README.md

Large diffs are not rendered by default.

38 changes: 20 additions & 18 deletions api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import {
GenericFacade,
} from "./types.js";
import { createAsyncHandler } from "./utils.js";
import { Macaroon } from "./facades/admin/AdminV3";

export const CLIENT_VERSION = "3.2.0";

export interface ConnectOptions {
bakery?: Bakery | null;
closeCallback: Callback<number>;
Expand All @@ -52,13 +54,6 @@ export interface Credentials {
macaroons?: object;
}

export interface LoginArguments {
"auth-tag"?: string; // username prefixed with 'user-'
"client-version"?: string; // the client version in the format 3.0.0
credentials?: string; // password
macaroons?: object; // Macaroon object
}

/**
Connect to the Juju controller or model at the given URL.
Expand Down Expand Up @@ -163,15 +158,16 @@ function connect(
async function connectAndLogin(
url: string,
credentials: Credentials,
options: ConnectOptions
options: ConnectOptions,
clientVersion = CLIENT_VERSION
): Promise<{
conn?: Connection;
logout: typeof Client.prototype.logout;
}> {
// Connect to Juju.
const juju: Client = await connect(url, options);
try {
const conn = await juju.login(credentials);
const conn = await juju.login(credentials, clientVersion);
return { conn, logout: juju.logout.bind(juju) };
} catch (error: any) {
if (!juju || !juju.isRedirectionError(error)) {
Expand Down Expand Up @@ -200,7 +196,8 @@ async function connectAndLogin(
return await connectAndLogin(
generateURL(url, srv),
credentials,
options
options,
clientVersion
);
}
}
Expand Down Expand Up @@ -251,17 +248,22 @@ class Client {
Log in to Juju.
@param credentials An object with the user and password fields for
userpass authentication or the macaroons field for bakery authentication.
If an empty object is provided a full bakery discharge will be attempted
for logging in with macaroons. Any necessary third party discharges are
performed using the bakery instance originally provided to connect().
@return {Promise} This promise will be rejected if there is an error
userpass authentication or the macaroons field for bakery authentication.
If an empty object is provided a full bakery discharge will be attempted
for logging in with macaroons. Any necessary third party discharges are
performed using the bakery instance originally provided to connect().
@param clientVersion The Juju controller version to target.
@return This promise will be rejected if there is an error
connecting, or resolved with a new connection instance. Note that the
promise will not be resolved or rejected if a callback is provided.
*/
async login(credentials: Credentials): Promise<Connection | undefined> {
async login(
credentials: Credentials,
clientVersion = CLIENT_VERSION
): Promise<Connection | undefined> {
const args: LoginRequest = {
"auth-tag": "",
"client-version": clientVersion,
credentials: "",
macaroons: [],
nonce: "",
Expand Down Expand Up @@ -308,7 +310,7 @@ class Client {
this._bakery?.storage.set(origin, serialized, () => {});
// Send the login request again including the discharge macaroons.
credentials.macaroons = [macaroons];
return resolve(this.login(credentials));
return resolve(this.login(credentials, clientVersion));
};
const onFailure = (err: string | MacaroonError) => {
reject("macaroon discharge failed: " + err);
Expand Down
Loading

0 comments on commit 0895f47

Please sign in to comment.