keypin 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -104
- package/{chunk-mjxb8yj8.js → chunk-dfr5e2r6.js} +1062 -60
- package/chunk-j6kpw8ge.js +65 -0
- package/main.js +6 -28
- package/package.json +1 -1
- package/chunk-8agfxsqt.js +0 -31
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
import {
|
|
3
3
|
messageOf,
|
|
4
4
|
errorCode,
|
|
5
|
-
installProblem
|
|
6
|
-
|
|
5
|
+
installProblem,
|
|
6
|
+
selfCommand
|
|
7
|
+
} from "./chunk-j6kpw8ge.js";
|
|
7
8
|
|
|
8
9
|
// src/start.ts
|
|
9
10
|
import { homedir as homedir2 } from "os";
|
|
@@ -736,6 +737,407 @@ function rotationDue(input) {
|
|
|
736
737
|
add("due-soon", `${label}: is due ${due} (rotated ${rotated}, every ${rotateEvery})`);
|
|
737
738
|
return due;
|
|
738
739
|
}
|
|
740
|
+
// ../../packages/core/src/discover.ts
|
|
741
|
+
var {TOML: TOML2 } = globalThis.Bun;
|
|
742
|
+
var SKIPPED_FOLDERS = new Set([
|
|
743
|
+
".git",
|
|
744
|
+
"node_modules",
|
|
745
|
+
".wrangler",
|
|
746
|
+
"dist",
|
|
747
|
+
"build",
|
|
748
|
+
"out",
|
|
749
|
+
".output",
|
|
750
|
+
".next",
|
|
751
|
+
".nuxt",
|
|
752
|
+
".svelte-kit",
|
|
753
|
+
".astro",
|
|
754
|
+
".turbo",
|
|
755
|
+
".vercel",
|
|
756
|
+
".netlify",
|
|
757
|
+
".cache",
|
|
758
|
+
".parcel-cache",
|
|
759
|
+
"coverage",
|
|
760
|
+
".venv",
|
|
761
|
+
"venv",
|
|
762
|
+
"__pycache__",
|
|
763
|
+
"target",
|
|
764
|
+
"vendor",
|
|
765
|
+
"Pods",
|
|
766
|
+
"DerivedData"
|
|
767
|
+
]);
|
|
768
|
+
var ENV_FILE = /^(\.env|\.dev\.vars)(\..+)?$/;
|
|
769
|
+
var EXPECTED_FILE = /\.(example|sample|template)$/;
|
|
770
|
+
function envFileKind(fileName) {
|
|
771
|
+
if (!ENV_FILE.test(fileName))
|
|
772
|
+
return null;
|
|
773
|
+
return EXPECTED_FILE.test(fileName) ? "expected" : "set";
|
|
774
|
+
}
|
|
775
|
+
var WRANGLER_FILES = [
|
|
776
|
+
"wrangler.toml",
|
|
777
|
+
"wrangler.json",
|
|
778
|
+
"wrangler.jsonc"
|
|
779
|
+
];
|
|
780
|
+
var DOTENV_LINE = /^\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?$/gm;
|
|
781
|
+
function envNames(text) {
|
|
782
|
+
const names = new Set;
|
|
783
|
+
for (const match of text.replace(/\r\n?/g, `
|
|
784
|
+
`).matchAll(DOTENV_LINE)) {
|
|
785
|
+
if (match[1])
|
|
786
|
+
names.add(match[1]);
|
|
787
|
+
}
|
|
788
|
+
return [...names];
|
|
789
|
+
}
|
|
790
|
+
var CONTEXT_REF = /(?<![\w.])(secrets|vars)\s*(?:\.\s*([A-Za-z_][A-Za-z0-9_]*)|\[\s*'([A-Za-z_][A-Za-z0-9_]*)'\s*\])/g;
|
|
791
|
+
function workflowRefs(text) {
|
|
792
|
+
let data;
|
|
793
|
+
try {
|
|
794
|
+
data = Bun.YAML.parse(text);
|
|
795
|
+
} catch {
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
const refs = {
|
|
799
|
+
secrets: new Set,
|
|
800
|
+
vars: new Set,
|
|
801
|
+
environments: new Set,
|
|
802
|
+
hidesUses: false
|
|
803
|
+
};
|
|
804
|
+
const scan = (expression) => {
|
|
805
|
+
if (/toJSON\(\s*secrets\s*\)/.test(expression))
|
|
806
|
+
refs.hidesUses = true;
|
|
807
|
+
for (const match of expression.matchAll(CONTEXT_REF)) {
|
|
808
|
+
const name = (match[2] ?? match[3])?.toUpperCase();
|
|
809
|
+
if (!name)
|
|
810
|
+
continue;
|
|
811
|
+
(match[1] === "secrets" ? refs.secrets : refs.vars).add(name);
|
|
812
|
+
}
|
|
813
|
+
};
|
|
814
|
+
const visit = (value, key) => {
|
|
815
|
+
if (typeof value === "string") {
|
|
816
|
+
if (key === "if" && !value.includes("${{"))
|
|
817
|
+
scan(withoutStrings(value));
|
|
818
|
+
for (const expression of expressions(value))
|
|
819
|
+
scan(expression);
|
|
820
|
+
if (key === "secrets" && value.trim() === "inherit")
|
|
821
|
+
refs.hidesUses = true;
|
|
822
|
+
} else if (Array.isArray(value)) {
|
|
823
|
+
for (const item of value)
|
|
824
|
+
visit(item, null);
|
|
825
|
+
} else if (isRecord(value)) {
|
|
826
|
+
for (const [k, v] of Object.entries(value))
|
|
827
|
+
visit(v, k);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
visit(data, null);
|
|
831
|
+
const jobs = isRecord(data) && isRecord(data.jobs) ? Object.values(data.jobs) : [];
|
|
832
|
+
for (const job of jobs) {
|
|
833
|
+
const environment = isRecord(job) ? job.environment : undefined;
|
|
834
|
+
const name = isRecord(environment) ? environment.name : environment;
|
|
835
|
+
if (typeof name === "string" && name.trim())
|
|
836
|
+
refs.environments.add(name);
|
|
837
|
+
}
|
|
838
|
+
return {
|
|
839
|
+
secrets: [...refs.secrets],
|
|
840
|
+
vars: [...refs.vars],
|
|
841
|
+
environments: [...refs.environments],
|
|
842
|
+
hidesUses: refs.hidesUses
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
function expressions(text) {
|
|
846
|
+
const found = [];
|
|
847
|
+
for (let start = text.indexOf("${{");start !== -1; start = text.indexOf("${{", start)) {
|
|
848
|
+
let i = start + 3;
|
|
849
|
+
while (i < text.length && !text.startsWith("}}", i))
|
|
850
|
+
i = text[i] === "'" ? endOfString(text, i) : i + 1;
|
|
851
|
+
found.push(withoutStrings(text.slice(start + 3, i)));
|
|
852
|
+
start = i + 2;
|
|
853
|
+
}
|
|
854
|
+
return found;
|
|
855
|
+
}
|
|
856
|
+
function withoutStrings(expression) {
|
|
857
|
+
let code = "";
|
|
858
|
+
for (let i = 0;i < expression.length; ) {
|
|
859
|
+
if (expression[i] !== "'") {
|
|
860
|
+
code += expression[i];
|
|
861
|
+
i++;
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
const end = endOfString(expression, i);
|
|
865
|
+
const literal = expression.slice(i, end);
|
|
866
|
+
code += /(?:secrets|vars)\s*\[\s*$/.test(code) ? literal : "''";
|
|
867
|
+
i = end;
|
|
868
|
+
}
|
|
869
|
+
return code;
|
|
870
|
+
}
|
|
871
|
+
function endOfString(text, open) {
|
|
872
|
+
let i = open + 1;
|
|
873
|
+
while (i < text.length) {
|
|
874
|
+
if (text[i] !== "'")
|
|
875
|
+
i++;
|
|
876
|
+
else if (text[i + 1] === "'")
|
|
877
|
+
i += 2;
|
|
878
|
+
else
|
|
879
|
+
return i + 1;
|
|
880
|
+
}
|
|
881
|
+
return i;
|
|
882
|
+
}
|
|
883
|
+
var ACCOUNT_ID2 = /^[0-9a-f]{32}$/;
|
|
884
|
+
function wranglerWorkers(text, config) {
|
|
885
|
+
let data;
|
|
886
|
+
try {
|
|
887
|
+
data = config.endsWith(".toml") ? TOML2.parse(text) : Bun.JSONC.parse(text);
|
|
888
|
+
} catch {
|
|
889
|
+
return { ok: false };
|
|
890
|
+
}
|
|
891
|
+
if (!isRecord(data))
|
|
892
|
+
return { ok: false };
|
|
893
|
+
const topName = stringOf(data.name);
|
|
894
|
+
const topAccount = stringOf(data.account_id);
|
|
895
|
+
const workers = [declaration(config, null, data, topName, topAccount)];
|
|
896
|
+
const envs = isRecord(data.env) ? data.env : {};
|
|
897
|
+
for (const [env, raw] of Object.entries(envs)) {
|
|
898
|
+
if (!isRecord(raw))
|
|
899
|
+
continue;
|
|
900
|
+
const name = stringOf(raw.name) ?? (topName ? `${topName}-${env}` : null);
|
|
901
|
+
workers.push(declaration(config, env, raw, name, stringOf(raw.account_id) ?? topAccount));
|
|
902
|
+
}
|
|
903
|
+
return { ok: true, workers };
|
|
904
|
+
}
|
|
905
|
+
function declaration(config, env, table, worker, account) {
|
|
906
|
+
const secrets = isRecord(table.secrets) ? table.secrets : null;
|
|
907
|
+
const required = Array.isArray(secrets?.required) ? secrets.required.filter((n) => typeof n === "string") : null;
|
|
908
|
+
return {
|
|
909
|
+
config,
|
|
910
|
+
env,
|
|
911
|
+
worker,
|
|
912
|
+
account: account && ACCOUNT_ID2.test(account) ? account : null,
|
|
913
|
+
required,
|
|
914
|
+
vars: isRecord(table.vars) ? Object.keys(table.vars) : []
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
function stringOf(value) {
|
|
918
|
+
return typeof value === "string" && value.trim() ? value : null;
|
|
919
|
+
}
|
|
920
|
+
var GITHUB_PROVIDED = "GITHUB_TOKEN";
|
|
921
|
+
var LOCAL_KINDS = ["env-file"];
|
|
922
|
+
var CLOUD_KINDS = ["github-secret", "worker-secret"];
|
|
923
|
+
var VARIABLE_KINDS = ["worker-var", "workflow-var"];
|
|
924
|
+
function buildDiscovery(input) {
|
|
925
|
+
const sources = new Map;
|
|
926
|
+
const add = (name, source) => {
|
|
927
|
+
const list = sources.get(name) ?? [];
|
|
928
|
+
list.push(source);
|
|
929
|
+
sources.set(name, list);
|
|
930
|
+
};
|
|
931
|
+
const problems = [];
|
|
932
|
+
const unknowns = [...input.unknowns];
|
|
933
|
+
for (const file of input.envFiles) {
|
|
934
|
+
for (const name of file.names) {
|
|
935
|
+
add(name, file.kind === "set" ? { kind: "env-file", path: file.path, tracked: file.tracked } : { kind: "example-file", path: file.path });
|
|
936
|
+
}
|
|
937
|
+
if (file.kind === "set" && file.tracked === true) {
|
|
938
|
+
problems.push({
|
|
939
|
+
kind: "tracked-env-file",
|
|
940
|
+
path: file.path,
|
|
941
|
+
message: `${file.path} is tracked by git, so its values may be committed`
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
const referenced = new Map;
|
|
946
|
+
let hidesUses = false;
|
|
947
|
+
for (const workflow of input.workflows) {
|
|
948
|
+
hidesUses ||= workflow.refs.hidesUses;
|
|
949
|
+
for (const name of workflow.refs.secrets) {
|
|
950
|
+
if (name === GITHUB_PROVIDED)
|
|
951
|
+
continue;
|
|
952
|
+
const environments = referenced.get(name) ?? new Set;
|
|
953
|
+
for (const environment of workflow.refs.environments)
|
|
954
|
+
environments.add(environment);
|
|
955
|
+
referenced.set(name, environments);
|
|
956
|
+
add(name, { kind: "workflow-secret", path: workflow.path });
|
|
957
|
+
}
|
|
958
|
+
for (const name of workflow.refs.vars)
|
|
959
|
+
add(name, { kind: "workflow-var", path: workflow.path });
|
|
960
|
+
}
|
|
961
|
+
const environments = new Set(input.workflows.flatMap((w) => w.refs.environments));
|
|
962
|
+
if (input.github && environments.size) {
|
|
963
|
+
unknowns.push({
|
|
964
|
+
what: `GitHub environment secrets (${[...environments].join(", ")})`,
|
|
965
|
+
reason: "discover reads repo secrets only",
|
|
966
|
+
cloud: true
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
if (input.github) {
|
|
970
|
+
const { repo, read } = input.github;
|
|
971
|
+
if (read.ok) {
|
|
972
|
+
const present = new Set(read.secrets.map((s) => s.name.toUpperCase()));
|
|
973
|
+
for (const secret of read.secrets) {
|
|
974
|
+
add(secret.name, { kind: "github-secret", repo, updatedAt: secret.updatedAt });
|
|
975
|
+
if (!referenced.has(secret.name.toUpperCase())) {
|
|
976
|
+
problems.push({
|
|
977
|
+
kind: "unreferenced-github-secret",
|
|
978
|
+
name: secret.name,
|
|
979
|
+
message: `no workflow here references this ${repo} secret${hidesUses ? " (a workflow passes secrets on, which can hide a use)" : ""}`
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
for (const [name, environments] of referenced) {
|
|
984
|
+
if (present.has(name))
|
|
985
|
+
continue;
|
|
986
|
+
const maybe = environments.size ? `; it may be a secret of the environment ${[...environments].join(" or ")}, which discover doesn't read` : "";
|
|
987
|
+
problems.push({
|
|
988
|
+
kind: "missing-github-secret",
|
|
989
|
+
name,
|
|
990
|
+
message: `a workflow references it, and ${repo} has no repo secret with this name${maybe}`
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
} else {
|
|
994
|
+
unknowns.push({
|
|
995
|
+
what: `GitHub Actions secrets of ${repo}`,
|
|
996
|
+
reason: read.reason,
|
|
997
|
+
cloud: true
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
for (const worker of input.workers) {
|
|
1002
|
+
const label = workerLabel(worker);
|
|
1003
|
+
for (const name of worker.required ?? []) {
|
|
1004
|
+
add(name, { kind: "worker-required", path: worker.config, worker: worker.worker });
|
|
1005
|
+
}
|
|
1006
|
+
for (const name of worker.vars) {
|
|
1007
|
+
add(name, { kind: "worker-var", path: worker.config, worker: worker.worker });
|
|
1008
|
+
}
|
|
1009
|
+
if (!worker.worker || !worker.account) {
|
|
1010
|
+
unknowns.push({
|
|
1011
|
+
what: `Worker secrets of ${label}`,
|
|
1012
|
+
cloud: true,
|
|
1013
|
+
reason: worker.worker ? `${worker.config} has no account_id, and discover doesn't guess an account` : `${worker.config} names no Worker`
|
|
1014
|
+
});
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
const read = worker.read;
|
|
1018
|
+
if (!read)
|
|
1019
|
+
continue;
|
|
1020
|
+
if (!read.ok) {
|
|
1021
|
+
unknowns.push({ what: `Worker secrets of ${label}`, reason: read.reason, cloud: true });
|
|
1022
|
+
continue;
|
|
1023
|
+
}
|
|
1024
|
+
const present = new Set(read.secrets.map((s) => s.name));
|
|
1025
|
+
for (const secret of read.secrets) {
|
|
1026
|
+
add(secret.name, { kind: "worker-secret", worker: worker.worker, account: worker.account });
|
|
1027
|
+
}
|
|
1028
|
+
for (const name of worker.required ?? []) {
|
|
1029
|
+
if (present.has(name))
|
|
1030
|
+
continue;
|
|
1031
|
+
problems.push({
|
|
1032
|
+
kind: "missing-worker-secret",
|
|
1033
|
+
name,
|
|
1034
|
+
message: `${worker.config} requires ${name}, and the Worker ${worker.worker} doesn't have it`
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
for (const key of input.map?.keys ?? []) {
|
|
1039
|
+
add(key.env, { kind: "map", path: input.map?.file ?? "keypin.toml", keyId: key.id });
|
|
1040
|
+
}
|
|
1041
|
+
const cloudUnknown = unknowns.some((u) => u.cloud);
|
|
1042
|
+
const names = [...sources.entries()].map(([name, list]) => {
|
|
1043
|
+
const local = list.some((s) => LOCAL_KINDS.includes(s.kind));
|
|
1044
|
+
const cloud = list.some((s) => CLOUD_KINDS.includes(s.kind));
|
|
1045
|
+
const configured = list.filter((s) => s.kind !== "map");
|
|
1046
|
+
const variable = configured.length > 0 && configured.every((s) => VARIABLE_KINDS.includes(s.kind));
|
|
1047
|
+
return {
|
|
1048
|
+
name,
|
|
1049
|
+
where: local && cloud ? "both" : local ? "local" : cloud ? "cloud" : variable ? "variable" : "declared",
|
|
1050
|
+
cloudUnknown: !cloud && cloudUnknown,
|
|
1051
|
+
mapped: list.flatMap((s) => s.kind === "map" ? [s.keyId] : []),
|
|
1052
|
+
sources: list,
|
|
1053
|
+
problems: problems.filter((p) => p.name?.toUpperCase() === name.toUpperCase()).map((p) => p.message)
|
|
1054
|
+
};
|
|
1055
|
+
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
1056
|
+
return { names, problems, unknowns };
|
|
1057
|
+
}
|
|
1058
|
+
function workerLabel(worker) {
|
|
1059
|
+
const name = worker.worker ?? "(unnamed)";
|
|
1060
|
+
return worker.env ? `${name} (${worker.config}, env ${worker.env})` : `${name} (${worker.config})`;
|
|
1061
|
+
}
|
|
1062
|
+
function draftMap(names, options) {
|
|
1063
|
+
const lines = [];
|
|
1064
|
+
const taken = new Set(options.takenIds);
|
|
1065
|
+
const vault = options.vault ?? {
|
|
1066
|
+
name: "personal",
|
|
1067
|
+
account: "<your 1Password account>",
|
|
1068
|
+
vault: "<your vault>"
|
|
1069
|
+
};
|
|
1070
|
+
for (const row of names) {
|
|
1071
|
+
if (row.mapped.length)
|
|
1072
|
+
continue;
|
|
1073
|
+
const local = row.sources.some((s) => s.kind === "env-file");
|
|
1074
|
+
const seen = new Set;
|
|
1075
|
+
const publish = row.sources.flatMap((s) => {
|
|
1076
|
+
const place = s.kind === "worker-secret" ? `${s.account}/${s.worker}` : s.kind === "github-secret" ? s.repo.toLowerCase() : "";
|
|
1077
|
+
if (!place || seen.has(place))
|
|
1078
|
+
return [];
|
|
1079
|
+
seen.add(place);
|
|
1080
|
+
if (s.kind === "worker-secret")
|
|
1081
|
+
return [
|
|
1082
|
+
['to = "cloudflare-worker"', `account = ${str(s.account)}`, `worker = ${str(s.worker)}`]
|
|
1083
|
+
];
|
|
1084
|
+
if (s.kind === "github-secret") {
|
|
1085
|
+
const account = options.githubAccount;
|
|
1086
|
+
const owner = s.repo.split("/")[0]?.toLowerCase();
|
|
1087
|
+
return [
|
|
1088
|
+
[
|
|
1089
|
+
'to = "github-actions"',
|
|
1090
|
+
`repo = ${str(s.repo)}`,
|
|
1091
|
+
...account && account.toLowerCase() !== owner ? [`account = ${str(account)}`] : []
|
|
1092
|
+
]
|
|
1093
|
+
];
|
|
1094
|
+
}
|
|
1095
|
+
return [];
|
|
1096
|
+
});
|
|
1097
|
+
if (!local && !publish.length)
|
|
1098
|
+
continue;
|
|
1099
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(row.name))
|
|
1100
|
+
continue;
|
|
1101
|
+
const id = freeId(keyIdFor(row.name), taken);
|
|
1102
|
+
const what = row.name.toLowerCase().replaceAll("_", " ").trim();
|
|
1103
|
+
lines.push("", `[keys.${id}]`, `env = ${str(row.name)}`, `owner = ${str(options.owner)}`);
|
|
1104
|
+
if (local) {
|
|
1105
|
+
lines.push("", `[keys.${id}.dev]`, `vault = ${str(vault.name)}`, `item = ${str(`${options.owner} dev ${what}`)}`);
|
|
1106
|
+
}
|
|
1107
|
+
if (publish.length) {
|
|
1108
|
+
lines.push("", `[keys.${id}.prod]`, `vault = ${str(vault.name)}`, `item = ${str(`${options.owner} prod ${what}`)}`);
|
|
1109
|
+
for (const target of publish)
|
|
1110
|
+
lines.push("", `[[keys.${id}.prod.publish]]`, ...target);
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
if (!lines.length)
|
|
1114
|
+
return null;
|
|
1115
|
+
const header = [
|
|
1116
|
+
"# Draft from keypin discover; nothing was written. Vault item names are placeholders.",
|
|
1117
|
+
options.vault ? "# The vault is the one in this project's keypin.toml; copy only the [keys.*] tables." : "# Fill in the vault, then save this as keypin.toml.",
|
|
1118
|
+
"",
|
|
1119
|
+
`[vaults.${/^[A-Za-z_][A-Za-z0-9_-]*$/.test(vault.name) ? vault.name : str(vault.name)}]`,
|
|
1120
|
+
'kind = "1password"',
|
|
1121
|
+
`account = ${str(vault.account)}`,
|
|
1122
|
+
`vault = ${str(vault.vault)}`
|
|
1123
|
+
];
|
|
1124
|
+
return `${[...header, ...lines].join(`
|
|
1125
|
+
`)}
|
|
1126
|
+
`;
|
|
1127
|
+
}
|
|
1128
|
+
function keyIdFor(name) {
|
|
1129
|
+
return name.toLowerCase().replaceAll("_", "-").replace(/^-+|-+$/g, "").replace(/-{2,}/g, "-") || "key";
|
|
1130
|
+
}
|
|
1131
|
+
function freeId(base, taken) {
|
|
1132
|
+
let id = base;
|
|
1133
|
+
for (let n = 2;taken.has(id); n++)
|
|
1134
|
+
id = `${base}-${n}`;
|
|
1135
|
+
taken.add(id);
|
|
1136
|
+
return id;
|
|
1137
|
+
}
|
|
1138
|
+
function str(value) {
|
|
1139
|
+
return JSON.stringify(value);
|
|
1140
|
+
}
|
|
739
1141
|
// ../../packages/destination-cloudflare/src/token.ts
|
|
740
1142
|
import { execFile } from "child_process";
|
|
741
1143
|
import { readFile } from "fs/promises";
|
|
@@ -752,6 +1154,12 @@ async function wranglerToken(path = WRANGLER_CONFIG) {
|
|
|
752
1154
|
}
|
|
753
1155
|
return login.token;
|
|
754
1156
|
}
|
|
1157
|
+
async function currentWranglerToken(path = WRANGLER_CONFIG) {
|
|
1158
|
+
const login = await readLogin(path);
|
|
1159
|
+
if (login.expired)
|
|
1160
|
+
throw new Error("the wrangler login has expired; run `wrangler whoami`");
|
|
1161
|
+
return login.token;
|
|
1162
|
+
}
|
|
755
1163
|
async function readLogin(path) {
|
|
756
1164
|
let text;
|
|
757
1165
|
try {
|
|
@@ -787,7 +1195,7 @@ function refresh() {
|
|
|
787
1195
|
|
|
788
1196
|
// ../../packages/destination-cloudflare/src/cloudflare.ts
|
|
789
1197
|
var API = "https://api.cloudflare.com/client/v4";
|
|
790
|
-
var
|
|
1198
|
+
var ACCOUNT_ID3 = /^[0-9a-f]{32}$/;
|
|
791
1199
|
var WORKER = /^[A-Za-z0-9_-]+$/;
|
|
792
1200
|
|
|
793
1201
|
class WorkerSecretDestination {
|
|
@@ -799,7 +1207,7 @@ class WorkerSecretDestination {
|
|
|
799
1207
|
#token;
|
|
800
1208
|
#passed = false;
|
|
801
1209
|
constructor(options) {
|
|
802
|
-
if (!
|
|
1210
|
+
if (!ACCOUNT_ID3.test(options.account))
|
|
803
1211
|
throw new Error(`"${options.account}" isn't a Cloudflare account ID`);
|
|
804
1212
|
if (!WORKER.test(options.worker))
|
|
805
1213
|
throw new Error(`"${options.worker}" isn't a Worker name`);
|
|
@@ -931,6 +1339,85 @@ function objectField(data, field) {
|
|
|
931
1339
|
const value = data[field];
|
|
932
1340
|
return isRecord(value) ? value : {};
|
|
933
1341
|
}
|
|
1342
|
+
// ../../packages/destination-cloudflare/src/list.ts
|
|
1343
|
+
var API2 = "https://api.cloudflare.com/client/v4";
|
|
1344
|
+
var ACCOUNT_ID4 = /^[0-9a-f]{32}$/;
|
|
1345
|
+
var WORKER2 = /^[A-Za-z0-9_-]+$/;
|
|
1346
|
+
|
|
1347
|
+
class CloudflareReader {
|
|
1348
|
+
#tokenSource;
|
|
1349
|
+
#fetch;
|
|
1350
|
+
#token;
|
|
1351
|
+
#accounts;
|
|
1352
|
+
constructor(options) {
|
|
1353
|
+
this.#tokenSource = options.token;
|
|
1354
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
1355
|
+
}
|
|
1356
|
+
async listWorkerSecrets(account, worker) {
|
|
1357
|
+
if (!ACCOUNT_ID4.test(account))
|
|
1358
|
+
return { ok: false, reason: `"${account}" isn't a Cloudflare account ID` };
|
|
1359
|
+
if (!WORKER2.test(worker))
|
|
1360
|
+
return { ok: false, reason: `"${worker}" isn't a Worker name` };
|
|
1361
|
+
const accounts = await (this.#accounts ??= this.#accountIds());
|
|
1362
|
+
if (!Array.isArray(accounts))
|
|
1363
|
+
return { ok: false, reason: accounts.reason };
|
|
1364
|
+
if (!accounts.includes(account)) {
|
|
1365
|
+
return {
|
|
1366
|
+
ok: false,
|
|
1367
|
+
reason: `the wrangler login reaches ${accounts.join(", ") || "no account"}, not ${account}`
|
|
1368
|
+
};
|
|
1369
|
+
}
|
|
1370
|
+
const response = await this.#get(`/accounts/${account}/workers/scripts/${worker}/secrets`);
|
|
1371
|
+
if ("reason" in response)
|
|
1372
|
+
return { ok: false, reason: response.reason };
|
|
1373
|
+
if (response.status === 404)
|
|
1374
|
+
return { ok: false, reason: `account ${account} has no Worker named ${worker}` };
|
|
1375
|
+
if (response.status !== 200)
|
|
1376
|
+
return { ok: false, reason: `Cloudflare returned HTTP ${response.status}` };
|
|
1377
|
+
const result = response.body.result;
|
|
1378
|
+
if (!Array.isArray(result))
|
|
1379
|
+
return { ok: false, reason: "Cloudflare's secrets response has no result list" };
|
|
1380
|
+
return {
|
|
1381
|
+
ok: true,
|
|
1382
|
+
secrets: result.flatMap((s) => isRecord(s) && typeof s.name === "string" ? [{ name: s.name, updatedAt: null }] : [])
|
|
1383
|
+
};
|
|
1384
|
+
}
|
|
1385
|
+
async#accountIds() {
|
|
1386
|
+
const ids = [];
|
|
1387
|
+
for (let page = 1;; page++) {
|
|
1388
|
+
const response = await this.#get(`/accounts?per_page=50&page=${page}`);
|
|
1389
|
+
if ("reason" in response)
|
|
1390
|
+
return response;
|
|
1391
|
+
if (response.status !== 200)
|
|
1392
|
+
return { reason: `Cloudflare GET /accounts returned HTTP ${response.status}` };
|
|
1393
|
+
const result = response.body.result;
|
|
1394
|
+
if (!Array.isArray(result))
|
|
1395
|
+
return { reason: "Cloudflare's /accounts response has no list" };
|
|
1396
|
+
for (const account of result) {
|
|
1397
|
+
if (isRecord(account) && typeof account.id === "string")
|
|
1398
|
+
ids.push(account.id);
|
|
1399
|
+
}
|
|
1400
|
+
const info = response.body.result_info;
|
|
1401
|
+
const pages = isRecord(info) ? info.total_pages : undefined;
|
|
1402
|
+
if (typeof pages !== "number" || page >= pages)
|
|
1403
|
+
return ids;
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
async#get(path) {
|
|
1407
|
+
let token;
|
|
1408
|
+
try {
|
|
1409
|
+
token = await (this.#token ??= this.#tokenSource());
|
|
1410
|
+
} catch (error) {
|
|
1411
|
+
return { reason: error instanceof Error ? error.message : "no Cloudflare token" };
|
|
1412
|
+
}
|
|
1413
|
+
const response = await this.#fetch(`${API2}${path}`, {
|
|
1414
|
+
method: "GET",
|
|
1415
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
1416
|
+
});
|
|
1417
|
+
const body = await response.json().catch(() => null);
|
|
1418
|
+
return { status: response.status, body: isRecord(body) ? body : {} };
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
934
1421
|
// ../../packages/destination-github/src/github.ts
|
|
935
1422
|
import sodium from "libsodium-wrappers";
|
|
936
1423
|
|
|
@@ -953,7 +1440,7 @@ function ghToken(login) {
|
|
|
953
1440
|
}
|
|
954
1441
|
|
|
955
1442
|
// ../../packages/destination-github/src/github.ts
|
|
956
|
-
var
|
|
1443
|
+
var API3 = "https://api.github.com";
|
|
957
1444
|
var REPO2 = /^[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/;
|
|
958
1445
|
var SECRET_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
959
1446
|
|
|
@@ -1045,7 +1532,7 @@ class GitHubSecretDestination {
|
|
|
1045
1532
|
}
|
|
1046
1533
|
async#request(method, path, body) {
|
|
1047
1534
|
this.#token ??= this.#tokenSource();
|
|
1048
|
-
const response = await this.#fetch(`${
|
|
1535
|
+
const response = await this.#fetch(`${API3}${path}`, {
|
|
1049
1536
|
method,
|
|
1050
1537
|
headers: {
|
|
1051
1538
|
Authorization: `Bearer ${await this.#token}`,
|
|
@@ -1085,9 +1572,66 @@ function stringField(data, field) {
|
|
|
1085
1572
|
throw new Error(`GitHub's response has no "${field}"`);
|
|
1086
1573
|
return value;
|
|
1087
1574
|
}
|
|
1575
|
+
// ../../packages/destination-github/src/list.ts
|
|
1576
|
+
var API4 = "https://api.github.com";
|
|
1577
|
+
var REPO3 = /^[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/;
|
|
1578
|
+
async function listRepoSecrets(options) {
|
|
1579
|
+
if (!REPO3.test(options.repo))
|
|
1580
|
+
return {
|
|
1581
|
+
ok: false,
|
|
1582
|
+
reason: `"${options.repo}" isn't a GitHub repository in the form owner/name`
|
|
1583
|
+
};
|
|
1584
|
+
const fetch = options.fetch ?? globalThis.fetch;
|
|
1585
|
+
let token;
|
|
1586
|
+
try {
|
|
1587
|
+
token = await options.token();
|
|
1588
|
+
} catch (error) {
|
|
1589
|
+
return { ok: false, reason: error instanceof Error ? error.message : "no GitHub token" };
|
|
1590
|
+
}
|
|
1591
|
+
const secrets = [];
|
|
1592
|
+
for (let page = 1;; page++) {
|
|
1593
|
+
const path = `/repos/${options.repo}/actions/secrets?per_page=100&page=${page}`;
|
|
1594
|
+
const response = await fetch(`${API4}${path}`, {
|
|
1595
|
+
method: "GET",
|
|
1596
|
+
headers: {
|
|
1597
|
+
Authorization: `Bearer ${token}`,
|
|
1598
|
+
Accept: "application/vnd.github+json",
|
|
1599
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
1600
|
+
}
|
|
1601
|
+
});
|
|
1602
|
+
if (response.status !== 200) {
|
|
1603
|
+
await response.body?.cancel();
|
|
1604
|
+
return { ok: false, reason: refusal(response.status, options.repo) };
|
|
1605
|
+
}
|
|
1606
|
+
const body = await response.json().catch(() => null);
|
|
1607
|
+
const list = isRecord(body) && Array.isArray(body.secrets) ? body.secrets : null;
|
|
1608
|
+
if (!list)
|
|
1609
|
+
return { ok: false, reason: "GitHub's secrets response has no secrets list" };
|
|
1610
|
+
for (const secret of list) {
|
|
1611
|
+
if (!isRecord(secret) || typeof secret.name !== "string")
|
|
1612
|
+
continue;
|
|
1613
|
+
secrets.push({
|
|
1614
|
+
name: secret.name,
|
|
1615
|
+
updatedAt: typeof secret.updated_at === "string" ? secret.updated_at : null
|
|
1616
|
+
});
|
|
1617
|
+
}
|
|
1618
|
+
const total = isRecord(body) && typeof body.total_count === "number" ? body.total_count : 0;
|
|
1619
|
+
if (list.length < 100 || secrets.length >= total)
|
|
1620
|
+
return { ok: true, secrets };
|
|
1621
|
+
}
|
|
1622
|
+
}
|
|
1623
|
+
function refusal(status, repo) {
|
|
1624
|
+
if (status === 404)
|
|
1625
|
+
return `HTTP 404: the login can't see ${repo}, or it isn't an admin of it`;
|
|
1626
|
+
if (status === 403)
|
|
1627
|
+
return `HTTP 403: the login isn't allowed to list ${repo}'s secrets`;
|
|
1628
|
+
if (status === 401)
|
|
1629
|
+
return "HTTP 401: the gh login's token was refused";
|
|
1630
|
+
return `GitHub returned HTTP ${status}`;
|
|
1631
|
+
}
|
|
1088
1632
|
// ../../packages/provider-cloudflare/src/tokens.ts
|
|
1089
|
-
var
|
|
1090
|
-
var
|
|
1633
|
+
var API5 = "https://api.cloudflare.com/client/v4";
|
|
1634
|
+
var ACCOUNT_ID5 = /^[0-9a-f]{32}$/;
|
|
1091
1635
|
var TOKEN_ID = /^[0-9a-f]{32}$/;
|
|
1092
1636
|
|
|
1093
1637
|
class CloudflareAccountTokens {
|
|
@@ -1095,7 +1639,7 @@ class CloudflareAccountTokens {
|
|
|
1095
1639
|
#admin;
|
|
1096
1640
|
#fetch;
|
|
1097
1641
|
constructor(options) {
|
|
1098
|
-
if (!
|
|
1642
|
+
if (!ACCOUNT_ID5.test(options.account))
|
|
1099
1643
|
throw new Error(`"${options.account}" isn't a Cloudflare account ID`);
|
|
1100
1644
|
this.#account = options.account;
|
|
1101
1645
|
this.#admin = options.admin;
|
|
@@ -1162,7 +1706,7 @@ class CloudflareAccountTokens {
|
|
|
1162
1706
|
return `/accounts/${this.#account}/tokens`;
|
|
1163
1707
|
}
|
|
1164
1708
|
async#request(method, path, auth, body) {
|
|
1165
|
-
const response = await this.#fetch(`${
|
|
1709
|
+
const response = await this.#fetch(`${API5}${path}`, {
|
|
1166
1710
|
method,
|
|
1167
1711
|
headers: {
|
|
1168
1712
|
Authorization: `Bearer ${auth.reveal()}`,
|
|
@@ -1429,9 +1973,10 @@ function toDay(value, ref) {
|
|
|
1429
1973
|
function messageOf2(error) {
|
|
1430
1974
|
return error instanceof Error ? error.message : String(error);
|
|
1431
1975
|
}
|
|
1432
|
-
// src/commands/
|
|
1433
|
-
import { join as
|
|
1976
|
+
// src/commands/discover.ts
|
|
1977
|
+
import { basename, join as join3, relative, resolve } from "path";
|
|
1434
1978
|
import { parseArgs } from "util";
|
|
1979
|
+
var {TOML: TOML3 } = globalThis.Bun;
|
|
1435
1980
|
|
|
1436
1981
|
// src/deps.ts
|
|
1437
1982
|
class UsageError extends Error {
|
|
@@ -1441,31 +1986,12 @@ function localDay(date) {
|
|
|
1441
1986
|
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
1442
1987
|
}
|
|
1443
1988
|
|
|
1444
|
-
// src/paths.ts
|
|
1445
|
-
import { dirname, join as join2 } from "path";
|
|
1446
|
-
function registryPath(deps) {
|
|
1447
|
-
return join2(base(deps.env.XDG_CONFIG_HOME, deps.home, ".config"), "keypin", "registry.json");
|
|
1448
|
-
}
|
|
1449
|
-
function statePath(deps) {
|
|
1450
|
-
return join2(base(deps.env.XDG_STATE_HOME, deps.home, ".local/state"), "keypin", "state.json");
|
|
1451
|
-
}
|
|
1452
|
-
function base(xdg, home, fallback) {
|
|
1453
|
-
return xdg?.startsWith("/") ? xdg : join2(home, fallback);
|
|
1454
|
-
}
|
|
1455
|
-
function projectCount(registry, path) {
|
|
1456
|
-
const count = registry.projects.length;
|
|
1457
|
-
return `${count} ${count === 1 ? "project" : "projects"} in ${path}`;
|
|
1458
|
-
}
|
|
1459
|
-
function rotationsPath(deps) {
|
|
1460
|
-
return join2(dirname(statePath(deps)), "rotations.json");
|
|
1461
|
-
}
|
|
1462
|
-
|
|
1463
1989
|
// src/project.ts
|
|
1464
|
-
import { dirname
|
|
1990
|
+
import { dirname, join as join2 } from "path";
|
|
1465
1991
|
var MAP_FILE = "keypin.toml";
|
|
1466
1992
|
async function findMap(files, cwd) {
|
|
1467
|
-
for (let folder = cwd;; folder =
|
|
1468
|
-
const file =
|
|
1993
|
+
for (let folder = cwd;; folder = dirname(folder)) {
|
|
1994
|
+
const file = join2(folder, MAP_FILE);
|
|
1469
1995
|
const text = await files.read(file);
|
|
1470
1996
|
if (text !== null) {
|
|
1471
1997
|
const result = parseMap(text, file);
|
|
@@ -1474,12 +2000,353 @@ async function findMap(files, cwd) {
|
|
|
1474
2000
|
`));
|
|
1475
2001
|
return result.map;
|
|
1476
2002
|
}
|
|
1477
|
-
if (
|
|
2003
|
+
if (dirname(folder) === folder) {
|
|
1478
2004
|
throw new Error(`no ${MAP_FILE} in ${cwd} or any folder above it; run "keypin init" first`);
|
|
1479
2005
|
}
|
|
1480
2006
|
}
|
|
1481
2007
|
}
|
|
1482
2008
|
|
|
2009
|
+
// src/commands/discover.ts
|
|
2010
|
+
async function discover(args, deps) {
|
|
2011
|
+
const { values, positionals } = parseArgs({
|
|
2012
|
+
args,
|
|
2013
|
+
options: { json: { type: "boolean", default: false } },
|
|
2014
|
+
allowPositionals: true
|
|
2015
|
+
});
|
|
2016
|
+
if (positionals.length > 1)
|
|
2017
|
+
throw new UsageError("keypin discover takes at most one folder");
|
|
2018
|
+
const folder = resolve(deps.cwd, positionals[0] ?? ".");
|
|
2019
|
+
if (!await deps.isFolder(folder))
|
|
2020
|
+
throw new Error(`${folder} isn't a folder`);
|
|
2021
|
+
const unknowns = [];
|
|
2022
|
+
const scan = await walk(deps, folder);
|
|
2023
|
+
const tracked = await trackedFiles(deps, folder, scan.envFiles);
|
|
2024
|
+
const envFiles = [];
|
|
2025
|
+
for (const path of scan.envFiles) {
|
|
2026
|
+
const kind = envFileKind(basename(path));
|
|
2027
|
+
if (!kind)
|
|
2028
|
+
continue;
|
|
2029
|
+
const text = await deps.files.read(join3(folder, path));
|
|
2030
|
+
if (text === null)
|
|
2031
|
+
continue;
|
|
2032
|
+
envFiles.push({ path, kind, names: envNames(text), tracked: tracked?.has(path) ?? null });
|
|
2033
|
+
}
|
|
2034
|
+
if (tracked === null && envFiles.some((f) => f.kind === "set")) {
|
|
2035
|
+
unknowns.push({
|
|
2036
|
+
what: "whether env files are tracked by git",
|
|
2037
|
+
reason: "the folder isn't in a git repository",
|
|
2038
|
+
cloud: false
|
|
2039
|
+
});
|
|
2040
|
+
}
|
|
2041
|
+
const top = (await deps.git(folder, ["rev-parse", "--show-toplevel"]))?.trim() || null;
|
|
2042
|
+
const workflows = await readWorkflows(deps, folder, top, unknowns);
|
|
2043
|
+
const workers = [];
|
|
2044
|
+
for (const config of scan.wrangler) {
|
|
2045
|
+
const text = await deps.files.read(join3(folder, config));
|
|
2046
|
+
if (text === null)
|
|
2047
|
+
continue;
|
|
2048
|
+
const parsed = wranglerWorkers(text, config);
|
|
2049
|
+
if (!parsed.ok) {
|
|
2050
|
+
unknowns.push({
|
|
2051
|
+
what: `the Workers in ${config}`,
|
|
2052
|
+
reason: "it couldn't be parsed",
|
|
2053
|
+
cloud: true
|
|
2054
|
+
});
|
|
2055
|
+
continue;
|
|
2056
|
+
}
|
|
2057
|
+
for (const worker of parsed.workers) {
|
|
2058
|
+
const read = worker.worker && worker.account ? await deps.workerSecrets(worker.account, worker.worker).catch((error) => ({
|
|
2059
|
+
ok: false,
|
|
2060
|
+
reason: messageOf(error)
|
|
2061
|
+
})) : null;
|
|
2062
|
+
workers.push({ ...worker, read });
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
const origin = top ? await githubOrigin(deps, folder) : null;
|
|
2066
|
+
let github = null;
|
|
2067
|
+
let login = null;
|
|
2068
|
+
if (!top) {
|
|
2069
|
+
unknowns.push({
|
|
2070
|
+
what: "GitHub Actions secrets",
|
|
2071
|
+
reason: "the folder isn't in a git repository",
|
|
2072
|
+
cloud: true
|
|
2073
|
+
});
|
|
2074
|
+
} else if (!origin || "reason" in origin) {
|
|
2075
|
+
unknowns.push({
|
|
2076
|
+
what: "GitHub Actions secrets",
|
|
2077
|
+
reason: origin?.reason ?? "the repository has no origin remote",
|
|
2078
|
+
cloud: true
|
|
2079
|
+
});
|
|
2080
|
+
} else {
|
|
2081
|
+
login = (await deps.git(folder, ["config", "--get", "github.account"]))?.trim() || origin.owner;
|
|
2082
|
+
const read = await deps.githubSecrets(origin.repo, login).catch((error) => ({ ok: false, reason: messageOf(error) }));
|
|
2083
|
+
github = {
|
|
2084
|
+
repo: origin.repo,
|
|
2085
|
+
read: read.ok ? read : { ok: false, reason: `as ${login}: ${read.reason}` }
|
|
2086
|
+
};
|
|
2087
|
+
}
|
|
2088
|
+
const mapText = await deps.files.read(join3(folder, MAP_FILE));
|
|
2089
|
+
let map = null;
|
|
2090
|
+
if (mapText !== null) {
|
|
2091
|
+
const parsed = parseMap(mapText, MAP_FILE);
|
|
2092
|
+
if (parsed.ok)
|
|
2093
|
+
map = parsed.map;
|
|
2094
|
+
else
|
|
2095
|
+
unknowns.push({
|
|
2096
|
+
what: `which names ${MAP_FILE} maps`,
|
|
2097
|
+
reason: parsed.errors.join("; "),
|
|
2098
|
+
cloud: false
|
|
2099
|
+
});
|
|
2100
|
+
}
|
|
2101
|
+
const discovery = buildDiscovery({ envFiles, workflows, workers, github, map, unknowns });
|
|
2102
|
+
const owner = map?.keys[0]?.owner ?? basename(folder);
|
|
2103
|
+
const draft = draftMap(discovery.names, {
|
|
2104
|
+
owner,
|
|
2105
|
+
githubAccount: login,
|
|
2106
|
+
vault: map && mapText !== null ? firstVault(mapText) : null,
|
|
2107
|
+
takenIds: map?.keys.map((k) => k.id) ?? []
|
|
2108
|
+
});
|
|
2109
|
+
if (draft !== null) {
|
|
2110
|
+
const check = parseMap(draft, "draft");
|
|
2111
|
+
if (!check.ok)
|
|
2112
|
+
throw new Error(`the draft doesn't parse: ${check.errors.join("; ")}`);
|
|
2113
|
+
}
|
|
2114
|
+
const report = {
|
|
2115
|
+
folder,
|
|
2116
|
+
repository: github?.repo ?? null,
|
|
2117
|
+
login,
|
|
2118
|
+
files: {
|
|
2119
|
+
env: envFiles.map(({ path, kind, tracked: t }) => ({ path, kind, tracked: t })),
|
|
2120
|
+
wrangler: scan.wrangler,
|
|
2121
|
+
workflows: workflows.map((w) => w.path)
|
|
2122
|
+
},
|
|
2123
|
+
nestedRepositories: scan.nested,
|
|
2124
|
+
...discovery,
|
|
2125
|
+
draft
|
|
2126
|
+
};
|
|
2127
|
+
deps.stdout.write(values.json ? `${JSON.stringify(report, null, 2)}
|
|
2128
|
+
` : render(report, discovery));
|
|
2129
|
+
return 0;
|
|
2130
|
+
}
|
|
2131
|
+
async function walk(deps, root) {
|
|
2132
|
+
const scan = { envFiles: [], wrangler: [], nested: [] };
|
|
2133
|
+
const visit = async (folder) => {
|
|
2134
|
+
const entries = (await deps.listFolder(folder)).toSorted((a, b) => a.name.localeCompare(b.name));
|
|
2135
|
+
if (folder !== root && entries.some((e) => e.name === ".git")) {
|
|
2136
|
+
scan.nested.push(relative(root, folder));
|
|
2137
|
+
return;
|
|
2138
|
+
}
|
|
2139
|
+
for (const entry of entries) {
|
|
2140
|
+
const path = join3(folder, entry.name);
|
|
2141
|
+
if (entry.kind === "folder") {
|
|
2142
|
+
if (!SKIPPED_FOLDERS.has(entry.name))
|
|
2143
|
+
await visit(path);
|
|
2144
|
+
} else if (entry.kind === "file") {
|
|
2145
|
+
if (envFileKind(entry.name))
|
|
2146
|
+
scan.envFiles.push(relative(root, path));
|
|
2147
|
+
else if (WRANGLER_FILES.includes(entry.name))
|
|
2148
|
+
scan.wrangler.push(relative(root, path));
|
|
2149
|
+
}
|
|
2150
|
+
}
|
|
2151
|
+
};
|
|
2152
|
+
await visit(root);
|
|
2153
|
+
return scan;
|
|
2154
|
+
}
|
|
2155
|
+
async function trackedFiles(deps, folder, paths) {
|
|
2156
|
+
if (!paths.length)
|
|
2157
|
+
return new Set;
|
|
2158
|
+
const out = await deps.git(folder, ["ls-files", "-z", "--", ...paths]);
|
|
2159
|
+
if (out === null)
|
|
2160
|
+
return null;
|
|
2161
|
+
return new Set(out.split("\x00").filter(Boolean));
|
|
2162
|
+
}
|
|
2163
|
+
async function readWorkflows(deps, folder, top, unknowns) {
|
|
2164
|
+
const dir = join3(top ?? folder, ".github", "workflows");
|
|
2165
|
+
let entries;
|
|
2166
|
+
try {
|
|
2167
|
+
entries = await deps.listFolder(dir);
|
|
2168
|
+
} catch (error) {
|
|
2169
|
+
if (errorCode(error) !== "ENOENT") {
|
|
2170
|
+
unknowns.push({
|
|
2171
|
+
what: "the secrets the workflows use",
|
|
2172
|
+
reason: `${relative(folder, dir)} couldn't be read: ${messageOf(error)}`,
|
|
2173
|
+
cloud: false
|
|
2174
|
+
});
|
|
2175
|
+
}
|
|
2176
|
+
return [];
|
|
2177
|
+
}
|
|
2178
|
+
const workflows = [];
|
|
2179
|
+
for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
|
|
2180
|
+
if (entry.kind !== "file" || !/\.ya?ml$/.test(entry.name))
|
|
2181
|
+
continue;
|
|
2182
|
+
const text = await deps.files.read(join3(dir, entry.name));
|
|
2183
|
+
if (text === null)
|
|
2184
|
+
continue;
|
|
2185
|
+
const path = relative(folder, join3(dir, entry.name));
|
|
2186
|
+
const refs = workflowRefs(text);
|
|
2187
|
+
if (refs)
|
|
2188
|
+
workflows.push({ path, refs });
|
|
2189
|
+
else
|
|
2190
|
+
unknowns.push({
|
|
2191
|
+
what: `the secrets ${path} uses`,
|
|
2192
|
+
reason: "it isn't valid YAML",
|
|
2193
|
+
cloud: false
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
return workflows;
|
|
2197
|
+
}
|
|
2198
|
+
var GITHUB_REMOTE = /^(?:https:\/\/(?:[^@/]+@)?github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([A-Za-z0-9-]+)\/([A-Za-z0-9._-]+?)(?:\.git)?\/?$/;
|
|
2199
|
+
async function githubOrigin(deps, folder) {
|
|
2200
|
+
const url = (await deps.git(folder, ["remote", "get-url", "origin"]))?.trim();
|
|
2201
|
+
if (!url)
|
|
2202
|
+
return null;
|
|
2203
|
+
const match = GITHUB_REMOTE.exec(url);
|
|
2204
|
+
if (!match?.[1] || !match[2])
|
|
2205
|
+
return { reason: "the origin remote isn't on github.com" };
|
|
2206
|
+
return { repo: `${match[1]}/${match[2]}`, owner: match[1] };
|
|
2207
|
+
}
|
|
2208
|
+
function firstVault(text) {
|
|
2209
|
+
let data;
|
|
2210
|
+
try {
|
|
2211
|
+
data = TOML3.parse(text);
|
|
2212
|
+
} catch {
|
|
2213
|
+
return null;
|
|
2214
|
+
}
|
|
2215
|
+
const vaults = isRecord(data) && isRecord(data.vaults) ? data.vaults : {};
|
|
2216
|
+
for (const [name, raw] of Object.entries(vaults)) {
|
|
2217
|
+
if (!isRecord(raw))
|
|
2218
|
+
continue;
|
|
2219
|
+
const { account, vault } = raw;
|
|
2220
|
+
if (typeof account === "string" && typeof vault === "string")
|
|
2221
|
+
return { name, account, vault };
|
|
2222
|
+
}
|
|
2223
|
+
return null;
|
|
2224
|
+
}
|
|
2225
|
+
var WHERE = {
|
|
2226
|
+
both: "local and cloud",
|
|
2227
|
+
local: "local",
|
|
2228
|
+
cloud: "cloud",
|
|
2229
|
+
declared: "declared, not set",
|
|
2230
|
+
variable: "plain variable"
|
|
2231
|
+
};
|
|
2232
|
+
var LABELS = {
|
|
2233
|
+
"env-file": "local",
|
|
2234
|
+
"example-file": "expected",
|
|
2235
|
+
"github-secret": "cloud",
|
|
2236
|
+
"worker-secret": "cloud",
|
|
2237
|
+
"workflow-secret": "workflow",
|
|
2238
|
+
"workflow-var": "workflow var",
|
|
2239
|
+
"worker-required": "required by",
|
|
2240
|
+
"worker-var": "var in",
|
|
2241
|
+
map: "mapped"
|
|
2242
|
+
};
|
|
2243
|
+
function describe(source) {
|
|
2244
|
+
switch (source.kind) {
|
|
2245
|
+
case "env-file":
|
|
2246
|
+
return source.tracked ? `${source.path} (tracked by git)` : source.path;
|
|
2247
|
+
case "example-file":
|
|
2248
|
+
case "workflow-secret":
|
|
2249
|
+
case "workflow-var":
|
|
2250
|
+
return source.path;
|
|
2251
|
+
case "worker-required":
|
|
2252
|
+
case "worker-var":
|
|
2253
|
+
return source.worker ? `${source.path} (${source.worker})` : source.path;
|
|
2254
|
+
case "github-secret":
|
|
2255
|
+
return `GitHub ${source.repo}${source.updatedAt ? `, updated ${source.updatedAt.slice(0, 10)}` : ""}`;
|
|
2256
|
+
case "worker-secret":
|
|
2257
|
+
return `Worker ${source.worker} (account ${source.account.slice(0, 8)}\u2026)`;
|
|
2258
|
+
case "map":
|
|
2259
|
+
return `${source.path} key ${source.keyId}`;
|
|
2260
|
+
default: {
|
|
2261
|
+
const unknown = source;
|
|
2262
|
+
return JSON.stringify(unknown);
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
function render(report, discovery) {
|
|
2267
|
+
const lines = [`keypin discover ${report.folder}`];
|
|
2268
|
+
lines.push(` GitHub: ${report.repository ? `${report.repository} as ${report.login}` : "no GitHub origin"}`);
|
|
2269
|
+
lines.push(` read: ${count(report.files.env.length, "env file")}, ${count(report.files.wrangler.length, "wrangler config")}, ${count(report.files.workflows.length, "workflow")}`);
|
|
2270
|
+
if (report.nestedRepositories.length)
|
|
2271
|
+
lines.push(` not scanned (own git repository): ${report.nestedRepositories.join(", ")}`);
|
|
2272
|
+
const onlyMissing = (row) => row.sources.every((s) => s.kind === "workflow-secret") && discovery.problems.some((p) => p.kind === "missing-github-secret" && p.name === row.name);
|
|
2273
|
+
const missing = discovery.names.filter(onlyMissing);
|
|
2274
|
+
const shown = discovery.names.filter((row) => !onlyMissing(row));
|
|
2275
|
+
lines.push("", `Names (${discovery.names.length}):`);
|
|
2276
|
+
for (const row of shown) {
|
|
2277
|
+
const where = WHERE[row.where] + (row.cloudUnknown ? ", cloud unknown" : "");
|
|
2278
|
+
const mapped = row.mapped.length ? ", mapped" : "";
|
|
2279
|
+
lines.push(` ${row.name} [${where}${mapped}]`);
|
|
2280
|
+
const groups = new Map;
|
|
2281
|
+
for (const source of row.sources) {
|
|
2282
|
+
const label = LABELS[source.kind];
|
|
2283
|
+
groups.set(label, [...groups.get(label) ?? [], describe(source)]);
|
|
2284
|
+
}
|
|
2285
|
+
for (const [label, items] of groups)
|
|
2286
|
+
lines.push(` ${label}: ${[...new Set(items)].join(", ")}`);
|
|
2287
|
+
for (const problem of row.problems)
|
|
2288
|
+
lines.push(` ! ${problem}`);
|
|
2289
|
+
}
|
|
2290
|
+
if (missing.length) {
|
|
2291
|
+
const byWorkflow = new Map;
|
|
2292
|
+
for (const row of missing) {
|
|
2293
|
+
const files = row.sources.map(describe).join(", ");
|
|
2294
|
+
byWorkflow.set(files, [...byWorkflow.get(files) ?? [], row.name]);
|
|
2295
|
+
}
|
|
2296
|
+
const repo = report.repository ?? "the repository";
|
|
2297
|
+
lines.push("", `Referenced by a workflow, and not a repo secret of ${repo} (${missing.length}):`);
|
|
2298
|
+
for (const [files, names] of byWorkflow)
|
|
2299
|
+
lines.push(` ${files}: ${names.join(", ")}`);
|
|
2300
|
+
}
|
|
2301
|
+
if (!discovery.names.length)
|
|
2302
|
+
lines.push(" (none found)");
|
|
2303
|
+
const loose = discovery.problems.filter((p) => p.name === undefined);
|
|
2304
|
+
if (loose.length) {
|
|
2305
|
+
lines.push("", `Problems (${loose.length}):`);
|
|
2306
|
+
for (const p of loose)
|
|
2307
|
+
lines.push(` ${p.kind}: ${p.message}`);
|
|
2308
|
+
}
|
|
2309
|
+
if (discovery.unknowns.length) {
|
|
2310
|
+
lines.push("", `Unknown (${discovery.unknowns.length}):`);
|
|
2311
|
+
for (const u of discovery.unknowns)
|
|
2312
|
+
lines.push(` ${u.what}: ${u.reason}`);
|
|
2313
|
+
}
|
|
2314
|
+
lines.push("");
|
|
2315
|
+
if (report.draft === null)
|
|
2316
|
+
lines.push("No draft: every name set locally or in the cloud is already mapped.");
|
|
2317
|
+
else
|
|
2318
|
+
lines.push("Draft keypin.toml for the unmapped names (not written):", "", report.draft);
|
|
2319
|
+
return `${lines.join(`
|
|
2320
|
+
`).trimEnd()}
|
|
2321
|
+
`;
|
|
2322
|
+
}
|
|
2323
|
+
function count(n, noun) {
|
|
2324
|
+
return `${n} ${noun}${n === 1 ? "" : "s"}`;
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
// src/commands/init.ts
|
|
2328
|
+
import { join as join5, resolve as resolve2 } from "path";
|
|
2329
|
+
import { parseArgs as parseArgs2 } from "util";
|
|
2330
|
+
|
|
2331
|
+
// src/paths.ts
|
|
2332
|
+
import { dirname as dirname2, join as join4 } from "path";
|
|
2333
|
+
function registryPath(deps) {
|
|
2334
|
+
return join4(base(deps.env.XDG_CONFIG_HOME, deps.home, ".config"), "keypin", "registry.json");
|
|
2335
|
+
}
|
|
2336
|
+
function statePath(deps) {
|
|
2337
|
+
return join4(base(deps.env.XDG_STATE_HOME, deps.home, ".local/state"), "keypin", "state.json");
|
|
2338
|
+
}
|
|
2339
|
+
function base(xdg, home, fallback) {
|
|
2340
|
+
return xdg?.startsWith("/") ? xdg : join4(home, fallback);
|
|
2341
|
+
}
|
|
2342
|
+
function projectCount(registry, path) {
|
|
2343
|
+
const count = registry.projects.length;
|
|
2344
|
+
return `${count} ${count === 1 ? "project" : "projects"} in ${path}`;
|
|
2345
|
+
}
|
|
2346
|
+
function rotationsPath(deps) {
|
|
2347
|
+
return join4(dirname2(statePath(deps)), "rotations.json");
|
|
2348
|
+
}
|
|
2349
|
+
|
|
1483
2350
|
// src/commands/init.ts
|
|
1484
2351
|
var TEMPLATE = `# keypin.toml: this project's keys. It holds names and places, never values.
|
|
1485
2352
|
# Uncomment and edit. Every field is checked; an unknown one is an error.
|
|
@@ -1516,13 +2383,13 @@ var TEMPLATE = `# keypin.toml: this project's keys. It holds names and places, n
|
|
|
1516
2383
|
# repo = "owner/repo"
|
|
1517
2384
|
`;
|
|
1518
2385
|
async function init(args, deps) {
|
|
1519
|
-
const { positionals } =
|
|
2386
|
+
const { positionals } = parseArgs2({ args, options: {}, allowPositionals: true });
|
|
1520
2387
|
if (positionals.length > 1)
|
|
1521
2388
|
throw new UsageError("keypin init takes at most one folder");
|
|
1522
|
-
const folder =
|
|
2389
|
+
const folder = resolve2(deps.cwd, positionals[0] ?? ".");
|
|
1523
2390
|
if (!await deps.isFolder(folder))
|
|
1524
2391
|
throw new Error(`${folder} isn't a folder`);
|
|
1525
|
-
const file =
|
|
2392
|
+
const file = join5(folder, MAP_FILE);
|
|
1526
2393
|
const created = await deps.createFile(file, TEMPLATE);
|
|
1527
2394
|
if (created === "link")
|
|
1528
2395
|
throw new Error(`${file} is a symbolic link; keypin init doesn't follow one`);
|
|
@@ -1537,10 +2404,10 @@ async function init(args, deps) {
|
|
|
1537
2404
|
}
|
|
1538
2405
|
|
|
1539
2406
|
// src/commands/publish.ts
|
|
1540
|
-
import { parseArgs as
|
|
2407
|
+
import { parseArgs as parseArgs3 } from "util";
|
|
1541
2408
|
|
|
1542
2409
|
// src/publishing.ts
|
|
1543
|
-
var NEEDS_APPROVAL =
|
|
2410
|
+
var NEEDS_APPROVAL = 3;
|
|
1544
2411
|
async function locked(deps, body) {
|
|
1545
2412
|
const path = statePath(deps);
|
|
1546
2413
|
const lockFile = `${path}.lock`;
|
|
@@ -1684,7 +2551,7 @@ async function publish(args, deps) {
|
|
|
1684
2551
|
return locked(deps, (path) => publishLocked({ ...request, args }, map, path, deps));
|
|
1685
2552
|
}
|
|
1686
2553
|
function parseRequest(args) {
|
|
1687
|
-
const { values, positionals } =
|
|
2554
|
+
const { values, positionals } = parseArgs3({
|
|
1688
2555
|
args,
|
|
1689
2556
|
options: {
|
|
1690
2557
|
env: { type: "string", default: "prod" },
|
|
@@ -1808,10 +2675,11 @@ async function confirmed(deps, needsApproval, request) {
|
|
|
1808
2675
|
}
|
|
1809
2676
|
|
|
1810
2677
|
// src/commands/rotate.ts
|
|
1811
|
-
import {
|
|
2678
|
+
import { join as join6 } from "path";
|
|
2679
|
+
import { parseArgs as parseArgs4 } from "util";
|
|
1812
2680
|
var GRACE_DAYS = DUE_SOON_DAYS;
|
|
1813
2681
|
async function rotate(args, deps) {
|
|
1814
|
-
const { values, positionals } =
|
|
2682
|
+
const { values, positionals } = parseArgs4({
|
|
1815
2683
|
args,
|
|
1816
2684
|
options: {
|
|
1817
2685
|
env: { type: "string", default: "prod" },
|
|
@@ -1841,6 +2709,7 @@ async function rotate(args, deps) {
|
|
|
1841
2709
|
`);
|
|
1842
2710
|
return locked(deps, async (statePath) => {
|
|
1843
2711
|
const run = {
|
|
2712
|
+
map,
|
|
1844
2713
|
key,
|
|
1845
2714
|
entry,
|
|
1846
2715
|
issue,
|
|
@@ -2113,16 +2982,34 @@ async function revokeOld(run, data, rotation) {
|
|
|
2113
2982
|
say(`Stopped: run keypin publish ${key.id}${environment === "prod" ? "" : ` --env ${environment}`} first. Nothing was revoked.`);
|
|
2114
2983
|
return 1;
|
|
2115
2984
|
}
|
|
2116
|
-
say(`Every destination
|
|
2985
|
+
say(`Every destination of ${key.id} (${environment}) has generation ${generation}.`);
|
|
2117
2986
|
for (const removed of retired) {
|
|
2118
2987
|
say(` ${removed.destinationId} left the map and its receipt has the old token (generation ${removed.lastReceipt.generation}); revoking breaks it unless that secret is gone`);
|
|
2119
2988
|
}
|
|
2989
|
+
const others = await otherHolders(run, state.receipts, rotation.old.generation, retired);
|
|
2990
|
+
if (others === null)
|
|
2991
|
+
return 1;
|
|
2992
|
+
if (others.length) {
|
|
2993
|
+
say(`Stopped: these destinations still have the old token. Put the new value in each one's vault item, then run its keypin publish. Nothing was revoked.`);
|
|
2994
|
+
for (const { receipt, owner } of others) {
|
|
2995
|
+
say(` ${describeHolder(receipt)}: ${publishCommand(owner)}`);
|
|
2996
|
+
}
|
|
2997
|
+
return 1;
|
|
2998
|
+
}
|
|
2120
2999
|
if (!deps.isTerminal) {
|
|
2121
3000
|
say("Stopped: revoking needs yes typed in a terminal, and --yes can't give it. Nothing was revoked.");
|
|
2122
3001
|
say(rerun(deps, "rotate", run.args));
|
|
2123
3002
|
return NEEDS_APPROVAL;
|
|
2124
3003
|
}
|
|
2125
|
-
announce(deps.stderr, "the admin token", [
|
|
3004
|
+
announce(deps.stderr, "the current value, to check it's the new token, and the admin token", [
|
|
3005
|
+
run.entry.ref,
|
|
3006
|
+
issue.admin
|
|
3007
|
+
]);
|
|
3008
|
+
const holds = await generationOf(await deps.vault().read(run.entry.ref));
|
|
3009
|
+
if (holds !== generation) {
|
|
3010
|
+
say(`Stopped: item "${run.entry.ref.item}" holds generation ${holds}, not the new ${generation}. Something else changed it, and revoking would leave it with a dead token. Nothing was revoked.`);
|
|
3011
|
+
return 1;
|
|
3012
|
+
}
|
|
2126
3013
|
const provider = deps.provider(issue, await deps.vault().read(issue.admin));
|
|
2127
3014
|
const current = await provider.details(rotation.new.id);
|
|
2128
3015
|
if (current?.status !== "active") {
|
|
@@ -2173,6 +3060,21 @@ async function abandon(run, data, rotation) {
|
|
|
2173
3060
|
return 1;
|
|
2174
3061
|
}
|
|
2175
3062
|
}
|
|
3063
|
+
if (rotation.new.generation) {
|
|
3064
|
+
const state = await loadState(deps.files, run.statePath);
|
|
3065
|
+
const holders = holdersOf(state.receipts, rotation.new.generation);
|
|
3066
|
+
if (holders.length) {
|
|
3067
|
+
say(`Stopped: the new token ${rotation.new.id} was published, so keypin won't delete it. Nothing was changed. Either put the new value back in item "${entry.ref.item}" and run ${again(run)} again to finish, or publish the old value to each destination that has the new one:`);
|
|
3068
|
+
const owners = await listedOwners(run);
|
|
3069
|
+
if (owners === null)
|
|
3070
|
+
return 1;
|
|
3071
|
+
for (const r of holders) {
|
|
3072
|
+
const owner = ownerOf(r, owners);
|
|
3073
|
+
say(` ${describeHolder(r)}: ${owner ? publishCommand(owner) : "no registered map lists it; set that secret to the old value by hand"}`);
|
|
3074
|
+
}
|
|
3075
|
+
return 1;
|
|
3076
|
+
}
|
|
3077
|
+
}
|
|
2176
3078
|
say(`It stopped before the new value was stored, so the old token ${rotation.old.id} still works everywhere.`);
|
|
2177
3079
|
if (!deps.isTerminal) {
|
|
2178
3080
|
say("Stopped: cleaning it up needs yes typed in a terminal. Nothing was changed.");
|
|
@@ -2229,6 +3131,69 @@ async function recheck(run, rotation, value) {
|
|
|
2229
3131
|
return `the old token ${rotation.old.id} is gone, so its policies can't be compared`;
|
|
2230
3132
|
return checkNew(provider, rotation.new.id, value, old);
|
|
2231
3133
|
}
|
|
3134
|
+
function holdersOf(receipts, generation) {
|
|
3135
|
+
return latestPerSecret(receipts).filter((r) => r.generation === generation);
|
|
3136
|
+
}
|
|
3137
|
+
function describeHolder(receipt) {
|
|
3138
|
+
return `${receipt.destinationId} (${receipt.keyId}, ${receipt.environment})`;
|
|
3139
|
+
}
|
|
3140
|
+
function publishCommand(owner) {
|
|
3141
|
+
const env = owner.environment === "prod" ? "" : ` --env ${owner.environment}`;
|
|
3142
|
+
const where = owner.project === null ? "" : `cd ${shellWord(owner.project)} && `;
|
|
3143
|
+
return `${where}keypin publish ${owner.keyId}${env}`;
|
|
3144
|
+
}
|
|
3145
|
+
async function listedOwners(run) {
|
|
3146
|
+
const { deps, say } = run;
|
|
3147
|
+
const owners = new Map;
|
|
3148
|
+
const list = (map, project) => {
|
|
3149
|
+
for (const k of map.keys) {
|
|
3150
|
+
for (const [name, entry] of Object.entries(k.environments)) {
|
|
3151
|
+
for (const target of entry.publish) {
|
|
3152
|
+
const secret = secretKey(target, k.env);
|
|
3153
|
+
owners.set(secret, [
|
|
3154
|
+
...owners.get(secret) ?? [],
|
|
3155
|
+
{ project, keyId: k.id, environment: name }
|
|
3156
|
+
]);
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
};
|
|
3161
|
+
list(run.map, null);
|
|
3162
|
+
const registry = await loadRegistry(deps.files, registryPath(deps));
|
|
3163
|
+
for (const project of registry.projects) {
|
|
3164
|
+
const file = join6(project, MAP_FILE);
|
|
3165
|
+
if (file === run.map.file)
|
|
3166
|
+
continue;
|
|
3167
|
+
const text = await deps.files.read(file);
|
|
3168
|
+
const result = text === null ? null : parseMap(text, file);
|
|
3169
|
+
if (!result?.ok) {
|
|
3170
|
+
say(`Stopped: ${file} ${result ? "has errors" : "is missing"}, so keypin can't tell which keys use these destinations. Fix it or run keypin unregister ${shellWord(project)}. Nothing was changed.`);
|
|
3171
|
+
return null;
|
|
3172
|
+
}
|
|
3173
|
+
list(result.map, project);
|
|
3174
|
+
}
|
|
3175
|
+
return owners;
|
|
3176
|
+
}
|
|
3177
|
+
function ownerOf(receipt, owners) {
|
|
3178
|
+
const found = owners.get(receiptSecretKey(receipt)) ?? [];
|
|
3179
|
+
return found.find((o) => o.keyId === receipt.keyId && o.environment === receipt.environment) ?? found[0];
|
|
3180
|
+
}
|
|
3181
|
+
var UNLISTED = "no registered map lists it any more; revoking breaks it unless that secret is gone";
|
|
3182
|
+
async function otherHolders(run, receipts, generation, retired) {
|
|
3183
|
+
const owners = await listedOwners(run);
|
|
3184
|
+
if (owners === null)
|
|
3185
|
+
return null;
|
|
3186
|
+
const shown = new Set(retired.map((r) => r.destinationId));
|
|
3187
|
+
return holdersOf(receipts, generation).flatMap((receipt) => {
|
|
3188
|
+
const owner = ownerOf(receipt, owners);
|
|
3189
|
+
if (owner)
|
|
3190
|
+
return [{ receipt, owner }];
|
|
3191
|
+
if (!shown.has(receipt.destinationId)) {
|
|
3192
|
+
run.say(` ${describeHolder(receipt)} has the old token, and ${UNLISTED}`);
|
|
3193
|
+
}
|
|
3194
|
+
return [];
|
|
3195
|
+
});
|
|
3196
|
+
}
|
|
2232
3197
|
function again(run) {
|
|
2233
3198
|
return rotateCommand(run.key.id, run.environment);
|
|
2234
3199
|
}
|
|
@@ -2292,9 +3257,9 @@ function describeAccess(token) {
|
|
|
2292
3257
|
}
|
|
2293
3258
|
|
|
2294
3259
|
// src/commands/run.ts
|
|
2295
|
-
import { parseArgs as
|
|
3260
|
+
import { parseArgs as parseArgs5 } from "util";
|
|
2296
3261
|
async function run(args, deps) {
|
|
2297
|
-
const { positionals } =
|
|
3262
|
+
const { positionals } = parseArgs5({ args, options: {}, allowPositionals: true });
|
|
2298
3263
|
if (!positionals.length)
|
|
2299
3264
|
throw new UsageError("keypin run needs a command after --");
|
|
2300
3265
|
const map = await findMap(deps.files, deps.cwd);
|
|
@@ -2360,10 +3325,10 @@ async function run(args, deps) {
|
|
|
2360
3325
|
}
|
|
2361
3326
|
|
|
2362
3327
|
// src/commands/status.ts
|
|
2363
|
-
import { join as
|
|
2364
|
-
import { parseArgs as
|
|
3328
|
+
import { join as join7 } from "path";
|
|
3329
|
+
import { parseArgs as parseArgs6 } from "util";
|
|
2365
3330
|
async function status(args, deps) {
|
|
2366
|
-
const { values, positionals } =
|
|
3331
|
+
const { values, positionals } = parseArgs6({
|
|
2367
3332
|
args,
|
|
2368
3333
|
options: { json: { type: "boolean", default: false } },
|
|
2369
3334
|
allowPositionals: true
|
|
@@ -2375,7 +3340,7 @@ async function status(args, deps) {
|
|
|
2375
3340
|
const { rotations } = await loadRotations(deps.files, rotationsPath(deps));
|
|
2376
3341
|
const maps = new Map;
|
|
2377
3342
|
for (const project of registry.projects) {
|
|
2378
|
-
const file =
|
|
3343
|
+
const file = join7(project, MAP_FILE);
|
|
2379
3344
|
try {
|
|
2380
3345
|
const text = await deps.files.read(file);
|
|
2381
3346
|
if (text !== null)
|
|
@@ -2412,11 +3377,11 @@ async function status(args, deps) {
|
|
|
2412
3377
|
deps.stdout.write(`${JSON.stringify({ today: localDay(deps.now()), ...result }, null, 2)}
|
|
2413
3378
|
`);
|
|
2414
3379
|
} else {
|
|
2415
|
-
deps.stdout.write(
|
|
3380
|
+
deps.stdout.write(render2(result, registry.projects.length));
|
|
2416
3381
|
}
|
|
2417
3382
|
return result.problems.some((p) => p.severity === "problem") ? 1 : 0;
|
|
2418
3383
|
}
|
|
2419
|
-
function
|
|
3384
|
+
function render2(result, projects) {
|
|
2420
3385
|
if (!projects)
|
|
2421
3386
|
return `No projects registered. Run "keypin init" in a project folder.
|
|
2422
3387
|
`;
|
|
@@ -2483,13 +3448,13 @@ function shown(problems) {
|
|
|
2483
3448
|
}
|
|
2484
3449
|
|
|
2485
3450
|
// src/commands/unregister.ts
|
|
2486
|
-
import { resolve as
|
|
2487
|
-
import { parseArgs as
|
|
3451
|
+
import { resolve as resolve3 } from "path";
|
|
3452
|
+
import { parseArgs as parseArgs7 } from "util";
|
|
2488
3453
|
async function unregister2(args, deps) {
|
|
2489
|
-
const { positionals } =
|
|
3454
|
+
const { positionals } = parseArgs7({ args, options: {}, allowPositionals: true });
|
|
2490
3455
|
if (positionals.length > 1)
|
|
2491
3456
|
throw new UsageError("keypin unregister takes at most one folder");
|
|
2492
|
-
const folder =
|
|
3457
|
+
const folder = resolve3(deps.cwd, positionals[0] ?? ".");
|
|
2493
3458
|
const path = registryPath(deps);
|
|
2494
3459
|
const before = await loadRegistry(deps.files, path);
|
|
2495
3460
|
const registry = await unregister(deps.files, path, folder);
|
|
@@ -2505,7 +3470,7 @@ async function unregister2(args, deps) {
|
|
|
2505
3470
|
return 0;
|
|
2506
3471
|
}
|
|
2507
3472
|
// ../../package.json
|
|
2508
|
-
var version = "0.
|
|
3473
|
+
var version = "0.4.0";
|
|
2509
3474
|
|
|
2510
3475
|
// src/cli.ts
|
|
2511
3476
|
var USAGE = `Usage:
|
|
@@ -2513,6 +3478,8 @@ var USAGE = `Usage:
|
|
|
2513
3478
|
keypin unregister [folder]
|
|
2514
3479
|
stop listing a project; its keypin.toml stays
|
|
2515
3480
|
keypin status [--json] every key in every registered project, with problems
|
|
3481
|
+
keypin discover [folder] [--json]
|
|
3482
|
+
every key name a project uses, local and in the cloud, and a draft map
|
|
2516
3483
|
keypin run -- <command> run a command with this project's dev keys in its environment
|
|
2517
3484
|
keypin publish <key-id> [--env prod|dev] [--yes]
|
|
2518
3485
|
check accounts, plan, confirm, then write the key to every destination
|
|
@@ -2520,11 +3487,19 @@ var USAGE = `Usage:
|
|
|
2520
3487
|
replace a token with a new one that has the same permissions, publish it,
|
|
2521
3488
|
and revoke the old one on the next run
|
|
2522
3489
|
keypin --version print the version
|
|
3490
|
+
|
|
3491
|
+
Exit codes:
|
|
3492
|
+
0 done
|
|
3493
|
+
1 failed, or status found a problem
|
|
3494
|
+
2 usage error
|
|
3495
|
+
3 stopped for approval: run the printed command in a terminal and type yes
|
|
3496
|
+
keypin run exits 1 if it can't start the command, then with the command's own code.
|
|
2523
3497
|
`;
|
|
2524
3498
|
var COMMANDS = new Map([
|
|
2525
3499
|
["init", init],
|
|
2526
3500
|
["unregister", unregister2],
|
|
2527
3501
|
["status", status],
|
|
3502
|
+
["discover", discover],
|
|
2528
3503
|
["run", run],
|
|
2529
3504
|
["publish", publish],
|
|
2530
3505
|
["rotate", rotate]
|
|
@@ -2546,6 +3521,11 @@ async function cli(argv, deps) {
|
|
|
2546
3521
|
${USAGE}`);
|
|
2547
3522
|
return 2;
|
|
2548
3523
|
}
|
|
3524
|
+
const own = args.includes("--") ? args.slice(0, args.indexOf("--")) : args;
|
|
3525
|
+
if (own.includes("--help") || own.includes("-h")) {
|
|
3526
|
+
deps.stdout.write(USAGE);
|
|
3527
|
+
return 0;
|
|
3528
|
+
}
|
|
2549
3529
|
try {
|
|
2550
3530
|
return await command(args, deps);
|
|
2551
3531
|
} catch (error) {
|
|
@@ -2562,7 +3542,8 @@ ${USAGE}`);
|
|
|
2562
3542
|
|
|
2563
3543
|
// src/disk.ts
|
|
2564
3544
|
import { constants, rmSync } from "fs";
|
|
2565
|
-
import {
|
|
3545
|
+
import { execFile as execFile3 } from "child_process";
|
|
3546
|
+
import { chmod, lstat, mkdir, open, readdir, readFile as readFile2, rename, rm, stat } from "fs/promises";
|
|
2566
3547
|
import { dirname as dirname3 } from "path";
|
|
2567
3548
|
|
|
2568
3549
|
// src/launch.ts
|
|
@@ -2647,6 +3628,20 @@ async function isFolder(path) {
|
|
|
2647
3628
|
return false;
|
|
2648
3629
|
}
|
|
2649
3630
|
}
|
|
3631
|
+
async function listFolder(path) {
|
|
3632
|
+
const entries = await readdir(path, { withFileTypes: true });
|
|
3633
|
+
return entries.map((entry) => ({
|
|
3634
|
+
name: entry.name,
|
|
3635
|
+
kind: entry.isFile() ? "file" : entry.isDirectory() ? "folder" : "other"
|
|
3636
|
+
}));
|
|
3637
|
+
}
|
|
3638
|
+
function git(folder, args) {
|
|
3639
|
+
return new Promise((resolve) => {
|
|
3640
|
+
execFile3("git", ["-C", folder, ...args], { maxBuffer: 64 * 1024 * 1024 }, (error, stdout) => {
|
|
3641
|
+
resolve(error ? null : String(stdout));
|
|
3642
|
+
});
|
|
3643
|
+
});
|
|
3644
|
+
}
|
|
2650
3645
|
var diskLock = async (path) => {
|
|
2651
3646
|
await privateFolder(dirname3(path));
|
|
2652
3647
|
if (await take(path))
|
|
@@ -2696,6 +3691,7 @@ function held(path) {
|
|
|
2696
3691
|
|
|
2697
3692
|
// src/start.ts
|
|
2698
3693
|
var vault;
|
|
3694
|
+
var cloudflare;
|
|
2699
3695
|
function destination(target) {
|
|
2700
3696
|
switch (target.to) {
|
|
2701
3697
|
case "cloudflare-worker":
|
|
@@ -2733,11 +3729,17 @@ async function ask(question) {
|
|
|
2733
3729
|
function main() {
|
|
2734
3730
|
return cli(process.argv.slice(2), {
|
|
2735
3731
|
cwd: process.cwd(),
|
|
2736
|
-
self: process.argv
|
|
3732
|
+
self: selfCommand(process.argv[0] ?? "bun", process.argv[1] ?? "keypin"),
|
|
2737
3733
|
env: process.env,
|
|
2738
3734
|
home: homedir2(),
|
|
2739
3735
|
files: diskFiles,
|
|
2740
3736
|
isFolder,
|
|
3737
|
+
listFolder,
|
|
3738
|
+
git,
|
|
3739
|
+
githubSecrets: (repo, login) => listRepoSecrets({ repo, token: () => ghToken(login) }),
|
|
3740
|
+
workerSecrets: (account, worker) => (cloudflare ??= new CloudflareReader({
|
|
3741
|
+
token: () => currentWranglerToken()
|
|
3742
|
+
})).listWorkerSecrets(account, worker),
|
|
2741
3743
|
createFile,
|
|
2742
3744
|
vault: () => vault ??= onePasswordBackend(connect),
|
|
2743
3745
|
now: () => new Date,
|