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
37 changes: 34 additions & 3 deletions apps/server/src/cli/policy.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as NetService from "@t3tools/shared/Net";
import { assert, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { Command } from "effect/cli";

import { cli } from "../binCli.ts";
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
import {
makePersistedServerRuntimeState,
Expand All @@ -16,7 +20,7 @@ import { describeRunningServer } from "./policy.ts";

const encodeState = Schema.encodeEffect(Schema.fromJsonString(PersistedServerRuntimeState));

it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))((it) =>
it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))((it) => {
it.effect("tells whether the running server loaded the installed policy", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
Expand Down Expand Up @@ -53,5 +57,32 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))((it) =>
assert.include(yield* running(null, null), "runs without a policy, as installed");
assert.include(yield* running(undefined), "does not report which policy it loaded");
}),
),
);
);

it.effect("templates reads a ~/ --policy file from the home directory", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-policy-templates-" });
yield* fs.writeFileString(
path.join(home, "policy.json"),
JSON.stringify({ schemaVersion: 1, organization: "Tilde Corp" }),
);

yield* Command.runWith(cli, { version: "0.0.0" })([
"policy",
"templates",
home,
"--policy=~/policy.json",
]).pipe(
Effect.provideService(HostProcess.HomeDirectory, home),
Effect.provide(NetService.layer),
);

assert.include(
yield* fs.readFileString(path.join(home, "macos", "t3code.mobileconfig")),
"Tilde Corp",
);
}),
);
});
6 changes: 4 additions & 2 deletions apps/server/src/cli/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as SecureRandom from "effect/crypto/SecureRandom";
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
import { listManageableSettings } from "../policy/managedSettings.ts";
import { macosProfile, windowsAdml, windowsAdmx } from "../policy/policyTemplates.ts";
import { expandHomePath } from "../os-jank.ts";
import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";

Expand Down Expand Up @@ -132,13 +133,14 @@ const templatesCommand = Command.make("templates", {
const path = yield* Path.Path;
let document = SAMPLE_POLICY;
if (Option.isSome(policy)) {
if (!(yield* fs.exists(policy.value))) {
const file = yield* expandHomePath(policy.value);
if (!(yield* fs.exists(file))) {
return yield* new CliError.UserError({
cause: policy.value,
userMessage: `No policy file at ${policy.value}.`,
});
}
const loaded = (yield* ManagedPolicy.load(policy.value)).document;
const loaded = (yield* ManagedPolicy.load(file)).document;
if (loaded !== undefined) document = loaded;
}
const random = yield* SecureRandom.SecureRandom;
Expand Down
Loading