keypin 0.2.0 → 0.3.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-p51dggc1.js} +945 -54
- package/main.js +1 -1
- package/package.json +1 -1
|
@@ -736,6 +736,407 @@ function rotationDue(input) {
|
|
|
736
736
|
add("due-soon", `${label}: is due ${due} (rotated ${rotated}, every ${rotateEvery})`);
|
|
737
737
|
return due;
|
|
738
738
|
}
|
|
739
|
+
// ../../packages/core/src/discover.ts
|
|
740
|
+
var {TOML: TOML2 } = globalThis.Bun;
|
|
741
|
+
var SKIPPED_FOLDERS = new Set([
|
|
742
|
+
".git",
|
|
743
|
+
"node_modules",
|
|
744
|
+
".wrangler",
|
|
745
|
+
"dist",
|
|
746
|
+
"build",
|
|
747
|
+
"out",
|
|
748
|
+
".output",
|
|
749
|
+
".next",
|
|
750
|
+
".nuxt",
|
|
751
|
+
".svelte-kit",
|
|
752
|
+
".astro",
|
|
753
|
+
".turbo",
|
|
754
|
+
".vercel",
|
|
755
|
+
".netlify",
|
|
756
|
+
".cache",
|
|
757
|
+
".parcel-cache",
|
|
758
|
+
"coverage",
|
|
759
|
+
".venv",
|
|
760
|
+
"venv",
|
|
761
|
+
"__pycache__",
|
|
762
|
+
"target",
|
|
763
|
+
"vendor",
|
|
764
|
+
"Pods",
|
|
765
|
+
"DerivedData"
|
|
766
|
+
]);
|
|
767
|
+
var ENV_FILE = /^(\.env|\.dev\.vars)(\..+)?$/;
|
|
768
|
+
var EXPECTED_FILE = /\.(example|sample|template)$/;
|
|
769
|
+
function envFileKind(fileName) {
|
|
770
|
+
if (!ENV_FILE.test(fileName))
|
|
771
|
+
return null;
|
|
772
|
+
return EXPECTED_FILE.test(fileName) ? "expected" : "set";
|
|
773
|
+
}
|
|
774
|
+
var WRANGLER_FILES = [
|
|
775
|
+
"wrangler.toml",
|
|
776
|
+
"wrangler.json",
|
|
777
|
+
"wrangler.jsonc"
|
|
778
|
+
];
|
|
779
|
+
var DOTENV_LINE = /^\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?$/gm;
|
|
780
|
+
function envNames(text) {
|
|
781
|
+
const names = new Set;
|
|
782
|
+
for (const match of text.replace(/\r\n?/g, `
|
|
783
|
+
`).matchAll(DOTENV_LINE)) {
|
|
784
|
+
if (match[1])
|
|
785
|
+
names.add(match[1]);
|
|
786
|
+
}
|
|
787
|
+
return [...names];
|
|
788
|
+
}
|
|
789
|
+
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;
|
|
790
|
+
function workflowRefs(text) {
|
|
791
|
+
let data;
|
|
792
|
+
try {
|
|
793
|
+
data = Bun.YAML.parse(text);
|
|
794
|
+
} catch {
|
|
795
|
+
return null;
|
|
796
|
+
}
|
|
797
|
+
const refs = {
|
|
798
|
+
secrets: new Set,
|
|
799
|
+
vars: new Set,
|
|
800
|
+
environments: new Set,
|
|
801
|
+
hidesUses: false
|
|
802
|
+
};
|
|
803
|
+
const scan = (expression) => {
|
|
804
|
+
if (/toJSON\(\s*secrets\s*\)/.test(expression))
|
|
805
|
+
refs.hidesUses = true;
|
|
806
|
+
for (const match of expression.matchAll(CONTEXT_REF)) {
|
|
807
|
+
const name = (match[2] ?? match[3])?.toUpperCase();
|
|
808
|
+
if (!name)
|
|
809
|
+
continue;
|
|
810
|
+
(match[1] === "secrets" ? refs.secrets : refs.vars).add(name);
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
const visit = (value, key) => {
|
|
814
|
+
if (typeof value === "string") {
|
|
815
|
+
if (key === "if" && !value.includes("${{"))
|
|
816
|
+
scan(withoutStrings(value));
|
|
817
|
+
for (const expression of expressions(value))
|
|
818
|
+
scan(expression);
|
|
819
|
+
if (key === "secrets" && value.trim() === "inherit")
|
|
820
|
+
refs.hidesUses = true;
|
|
821
|
+
} else if (Array.isArray(value)) {
|
|
822
|
+
for (const item of value)
|
|
823
|
+
visit(item, null);
|
|
824
|
+
} else if (isRecord(value)) {
|
|
825
|
+
for (const [k, v] of Object.entries(value))
|
|
826
|
+
visit(v, k);
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
visit(data, null);
|
|
830
|
+
const jobs = isRecord(data) && isRecord(data.jobs) ? Object.values(data.jobs) : [];
|
|
831
|
+
for (const job of jobs) {
|
|
832
|
+
const environment = isRecord(job) ? job.environment : undefined;
|
|
833
|
+
const name = isRecord(environment) ? environment.name : environment;
|
|
834
|
+
if (typeof name === "string" && name.trim())
|
|
835
|
+
refs.environments.add(name);
|
|
836
|
+
}
|
|
837
|
+
return {
|
|
838
|
+
secrets: [...refs.secrets],
|
|
839
|
+
vars: [...refs.vars],
|
|
840
|
+
environments: [...refs.environments],
|
|
841
|
+
hidesUses: refs.hidesUses
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
function expressions(text) {
|
|
845
|
+
const found = [];
|
|
846
|
+
for (let start = text.indexOf("${{");start !== -1; start = text.indexOf("${{", start)) {
|
|
847
|
+
let i = start + 3;
|
|
848
|
+
while (i < text.length && !text.startsWith("}}", i))
|
|
849
|
+
i = text[i] === "'" ? endOfString(text, i) : i + 1;
|
|
850
|
+
found.push(withoutStrings(text.slice(start + 3, i)));
|
|
851
|
+
start = i + 2;
|
|
852
|
+
}
|
|
853
|
+
return found;
|
|
854
|
+
}
|
|
855
|
+
function withoutStrings(expression) {
|
|
856
|
+
let code = "";
|
|
857
|
+
for (let i = 0;i < expression.length; ) {
|
|
858
|
+
if (expression[i] !== "'") {
|
|
859
|
+
code += expression[i];
|
|
860
|
+
i++;
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
const end = endOfString(expression, i);
|
|
864
|
+
const literal = expression.slice(i, end);
|
|
865
|
+
code += /(?:secrets|vars)\s*\[\s*$/.test(code) ? literal : "''";
|
|
866
|
+
i = end;
|
|
867
|
+
}
|
|
868
|
+
return code;
|
|
869
|
+
}
|
|
870
|
+
function endOfString(text, open) {
|
|
871
|
+
let i = open + 1;
|
|
872
|
+
while (i < text.length) {
|
|
873
|
+
if (text[i] !== "'")
|
|
874
|
+
i++;
|
|
875
|
+
else if (text[i + 1] === "'")
|
|
876
|
+
i += 2;
|
|
877
|
+
else
|
|
878
|
+
return i + 1;
|
|
879
|
+
}
|
|
880
|
+
return i;
|
|
881
|
+
}
|
|
882
|
+
var ACCOUNT_ID2 = /^[0-9a-f]{32}$/;
|
|
883
|
+
function wranglerWorkers(text, config) {
|
|
884
|
+
let data;
|
|
885
|
+
try {
|
|
886
|
+
data = config.endsWith(".toml") ? TOML2.parse(text) : Bun.JSONC.parse(text);
|
|
887
|
+
} catch {
|
|
888
|
+
return { ok: false };
|
|
889
|
+
}
|
|
890
|
+
if (!isRecord(data))
|
|
891
|
+
return { ok: false };
|
|
892
|
+
const topName = stringOf(data.name);
|
|
893
|
+
const topAccount = stringOf(data.account_id);
|
|
894
|
+
const workers = [declaration(config, null, data, topName, topAccount)];
|
|
895
|
+
const envs = isRecord(data.env) ? data.env : {};
|
|
896
|
+
for (const [env, raw] of Object.entries(envs)) {
|
|
897
|
+
if (!isRecord(raw))
|
|
898
|
+
continue;
|
|
899
|
+
const name = stringOf(raw.name) ?? (topName ? `${topName}-${env}` : null);
|
|
900
|
+
workers.push(declaration(config, env, raw, name, stringOf(raw.account_id) ?? topAccount));
|
|
901
|
+
}
|
|
902
|
+
return { ok: true, workers };
|
|
903
|
+
}
|
|
904
|
+
function declaration(config, env, table, worker, account) {
|
|
905
|
+
const secrets = isRecord(table.secrets) ? table.secrets : null;
|
|
906
|
+
const required = Array.isArray(secrets?.required) ? secrets.required.filter((n) => typeof n === "string") : null;
|
|
907
|
+
return {
|
|
908
|
+
config,
|
|
909
|
+
env,
|
|
910
|
+
worker,
|
|
911
|
+
account: account && ACCOUNT_ID2.test(account) ? account : null,
|
|
912
|
+
required,
|
|
913
|
+
vars: isRecord(table.vars) ? Object.keys(table.vars) : []
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
function stringOf(value) {
|
|
917
|
+
return typeof value === "string" && value.trim() ? value : null;
|
|
918
|
+
}
|
|
919
|
+
var GITHUB_PROVIDED = "GITHUB_TOKEN";
|
|
920
|
+
var LOCAL_KINDS = ["env-file"];
|
|
921
|
+
var CLOUD_KINDS = ["github-secret", "worker-secret"];
|
|
922
|
+
var VARIABLE_KINDS = ["worker-var", "workflow-var"];
|
|
923
|
+
function buildDiscovery(input) {
|
|
924
|
+
const sources = new Map;
|
|
925
|
+
const add = (name, source) => {
|
|
926
|
+
const list = sources.get(name) ?? [];
|
|
927
|
+
list.push(source);
|
|
928
|
+
sources.set(name, list);
|
|
929
|
+
};
|
|
930
|
+
const problems = [];
|
|
931
|
+
const unknowns = [...input.unknowns];
|
|
932
|
+
for (const file of input.envFiles) {
|
|
933
|
+
for (const name of file.names) {
|
|
934
|
+
add(name, file.kind === "set" ? { kind: "env-file", path: file.path, tracked: file.tracked } : { kind: "example-file", path: file.path });
|
|
935
|
+
}
|
|
936
|
+
if (file.kind === "set" && file.tracked === true) {
|
|
937
|
+
problems.push({
|
|
938
|
+
kind: "tracked-env-file",
|
|
939
|
+
path: file.path,
|
|
940
|
+
message: `${file.path} is tracked by git, so its values may be committed`
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
const referenced = new Map;
|
|
945
|
+
let hidesUses = false;
|
|
946
|
+
for (const workflow of input.workflows) {
|
|
947
|
+
hidesUses ||= workflow.refs.hidesUses;
|
|
948
|
+
for (const name of workflow.refs.secrets) {
|
|
949
|
+
if (name === GITHUB_PROVIDED)
|
|
950
|
+
continue;
|
|
951
|
+
const environments = referenced.get(name) ?? new Set;
|
|
952
|
+
for (const environment of workflow.refs.environments)
|
|
953
|
+
environments.add(environment);
|
|
954
|
+
referenced.set(name, environments);
|
|
955
|
+
add(name, { kind: "workflow-secret", path: workflow.path });
|
|
956
|
+
}
|
|
957
|
+
for (const name of workflow.refs.vars)
|
|
958
|
+
add(name, { kind: "workflow-var", path: workflow.path });
|
|
959
|
+
}
|
|
960
|
+
const environments = new Set(input.workflows.flatMap((w) => w.refs.environments));
|
|
961
|
+
if (input.github && environments.size) {
|
|
962
|
+
unknowns.push({
|
|
963
|
+
what: `GitHub environment secrets (${[...environments].join(", ")})`,
|
|
964
|
+
reason: "discover reads repo secrets only",
|
|
965
|
+
cloud: true
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
if (input.github) {
|
|
969
|
+
const { repo, read } = input.github;
|
|
970
|
+
if (read.ok) {
|
|
971
|
+
const present = new Set(read.secrets.map((s) => s.name.toUpperCase()));
|
|
972
|
+
for (const secret of read.secrets) {
|
|
973
|
+
add(secret.name, { kind: "github-secret", repo, updatedAt: secret.updatedAt });
|
|
974
|
+
if (!referenced.has(secret.name.toUpperCase())) {
|
|
975
|
+
problems.push({
|
|
976
|
+
kind: "unreferenced-github-secret",
|
|
977
|
+
name: secret.name,
|
|
978
|
+
message: `no workflow here references this ${repo} secret${hidesUses ? " (a workflow passes secrets on, which can hide a use)" : ""}`
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
for (const [name, environments] of referenced) {
|
|
983
|
+
if (present.has(name))
|
|
984
|
+
continue;
|
|
985
|
+
const maybe = environments.size ? `; it may be a secret of the environment ${[...environments].join(" or ")}, which discover doesn't read` : "";
|
|
986
|
+
problems.push({
|
|
987
|
+
kind: "missing-github-secret",
|
|
988
|
+
name,
|
|
989
|
+
message: `a workflow references it, and ${repo} has no repo secret with this name${maybe}`
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
} else {
|
|
993
|
+
unknowns.push({
|
|
994
|
+
what: `GitHub Actions secrets of ${repo}`,
|
|
995
|
+
reason: read.reason,
|
|
996
|
+
cloud: true
|
|
997
|
+
});
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
for (const worker of input.workers) {
|
|
1001
|
+
const label = workerLabel(worker);
|
|
1002
|
+
for (const name of worker.required ?? []) {
|
|
1003
|
+
add(name, { kind: "worker-required", path: worker.config, worker: worker.worker });
|
|
1004
|
+
}
|
|
1005
|
+
for (const name of worker.vars) {
|
|
1006
|
+
add(name, { kind: "worker-var", path: worker.config, worker: worker.worker });
|
|
1007
|
+
}
|
|
1008
|
+
if (!worker.worker || !worker.account) {
|
|
1009
|
+
unknowns.push({
|
|
1010
|
+
what: `Worker secrets of ${label}`,
|
|
1011
|
+
cloud: true,
|
|
1012
|
+
reason: worker.worker ? `${worker.config} has no account_id, and discover doesn't guess an account` : `${worker.config} names no Worker`
|
|
1013
|
+
});
|
|
1014
|
+
continue;
|
|
1015
|
+
}
|
|
1016
|
+
const read = worker.read;
|
|
1017
|
+
if (!read)
|
|
1018
|
+
continue;
|
|
1019
|
+
if (!read.ok) {
|
|
1020
|
+
unknowns.push({ what: `Worker secrets of ${label}`, reason: read.reason, cloud: true });
|
|
1021
|
+
continue;
|
|
1022
|
+
}
|
|
1023
|
+
const present = new Set(read.secrets.map((s) => s.name));
|
|
1024
|
+
for (const secret of read.secrets) {
|
|
1025
|
+
add(secret.name, { kind: "worker-secret", worker: worker.worker, account: worker.account });
|
|
1026
|
+
}
|
|
1027
|
+
for (const name of worker.required ?? []) {
|
|
1028
|
+
if (present.has(name))
|
|
1029
|
+
continue;
|
|
1030
|
+
problems.push({
|
|
1031
|
+
kind: "missing-worker-secret",
|
|
1032
|
+
name,
|
|
1033
|
+
message: `${worker.config} requires ${name}, and the Worker ${worker.worker} doesn't have it`
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
for (const key of input.map?.keys ?? []) {
|
|
1038
|
+
add(key.env, { kind: "map", path: input.map?.file ?? "keypin.toml", keyId: key.id });
|
|
1039
|
+
}
|
|
1040
|
+
const cloudUnknown = unknowns.some((u) => u.cloud);
|
|
1041
|
+
const names = [...sources.entries()].map(([name, list]) => {
|
|
1042
|
+
const local = list.some((s) => LOCAL_KINDS.includes(s.kind));
|
|
1043
|
+
const cloud = list.some((s) => CLOUD_KINDS.includes(s.kind));
|
|
1044
|
+
const configured = list.filter((s) => s.kind !== "map");
|
|
1045
|
+
const variable = configured.length > 0 && configured.every((s) => VARIABLE_KINDS.includes(s.kind));
|
|
1046
|
+
return {
|
|
1047
|
+
name,
|
|
1048
|
+
where: local && cloud ? "both" : local ? "local" : cloud ? "cloud" : variable ? "variable" : "declared",
|
|
1049
|
+
cloudUnknown: !cloud && cloudUnknown,
|
|
1050
|
+
mapped: list.flatMap((s) => s.kind === "map" ? [s.keyId] : []),
|
|
1051
|
+
sources: list,
|
|
1052
|
+
problems: problems.filter((p) => p.name?.toUpperCase() === name.toUpperCase()).map((p) => p.message)
|
|
1053
|
+
};
|
|
1054
|
+
}).sort((a, b) => a.name.localeCompare(b.name));
|
|
1055
|
+
return { names, problems, unknowns };
|
|
1056
|
+
}
|
|
1057
|
+
function workerLabel(worker) {
|
|
1058
|
+
const name = worker.worker ?? "(unnamed)";
|
|
1059
|
+
return worker.env ? `${name} (${worker.config}, env ${worker.env})` : `${name} (${worker.config})`;
|
|
1060
|
+
}
|
|
1061
|
+
function draftMap(names, options) {
|
|
1062
|
+
const lines = [];
|
|
1063
|
+
const taken = new Set(options.takenIds);
|
|
1064
|
+
const vault = options.vault ?? {
|
|
1065
|
+
name: "personal",
|
|
1066
|
+
account: "<your 1Password account>",
|
|
1067
|
+
vault: "<your vault>"
|
|
1068
|
+
};
|
|
1069
|
+
for (const row of names) {
|
|
1070
|
+
if (row.mapped.length)
|
|
1071
|
+
continue;
|
|
1072
|
+
const local = row.sources.some((s) => s.kind === "env-file");
|
|
1073
|
+
const seen = new Set;
|
|
1074
|
+
const publish = row.sources.flatMap((s) => {
|
|
1075
|
+
const place = s.kind === "worker-secret" ? `${s.account}/${s.worker}` : s.kind === "github-secret" ? s.repo.toLowerCase() : "";
|
|
1076
|
+
if (!place || seen.has(place))
|
|
1077
|
+
return [];
|
|
1078
|
+
seen.add(place);
|
|
1079
|
+
if (s.kind === "worker-secret")
|
|
1080
|
+
return [
|
|
1081
|
+
['to = "cloudflare-worker"', `account = ${str(s.account)}`, `worker = ${str(s.worker)}`]
|
|
1082
|
+
];
|
|
1083
|
+
if (s.kind === "github-secret") {
|
|
1084
|
+
const account = options.githubAccount;
|
|
1085
|
+
const owner = s.repo.split("/")[0]?.toLowerCase();
|
|
1086
|
+
return [
|
|
1087
|
+
[
|
|
1088
|
+
'to = "github-actions"',
|
|
1089
|
+
`repo = ${str(s.repo)}`,
|
|
1090
|
+
...account && account.toLowerCase() !== owner ? [`account = ${str(account)}`] : []
|
|
1091
|
+
]
|
|
1092
|
+
];
|
|
1093
|
+
}
|
|
1094
|
+
return [];
|
|
1095
|
+
});
|
|
1096
|
+
if (!local && !publish.length)
|
|
1097
|
+
continue;
|
|
1098
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(row.name))
|
|
1099
|
+
continue;
|
|
1100
|
+
const id = freeId(keyIdFor(row.name), taken);
|
|
1101
|
+
const what = row.name.toLowerCase().replaceAll("_", " ").trim();
|
|
1102
|
+
lines.push("", `[keys.${id}]`, `env = ${str(row.name)}`, `owner = ${str(options.owner)}`);
|
|
1103
|
+
if (local) {
|
|
1104
|
+
lines.push("", `[keys.${id}.dev]`, `vault = ${str(vault.name)}`, `item = ${str(`${options.owner} dev ${what}`)}`);
|
|
1105
|
+
}
|
|
1106
|
+
if (publish.length) {
|
|
1107
|
+
lines.push("", `[keys.${id}.prod]`, `vault = ${str(vault.name)}`, `item = ${str(`${options.owner} prod ${what}`)}`);
|
|
1108
|
+
for (const target of publish)
|
|
1109
|
+
lines.push("", `[[keys.${id}.prod.publish]]`, ...target);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
if (!lines.length)
|
|
1113
|
+
return null;
|
|
1114
|
+
const header = [
|
|
1115
|
+
"# Draft from keypin discover; nothing was written. Vault item names are placeholders.",
|
|
1116
|
+
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.",
|
|
1117
|
+
"",
|
|
1118
|
+
`[vaults.${/^[A-Za-z_][A-Za-z0-9_-]*$/.test(vault.name) ? vault.name : str(vault.name)}]`,
|
|
1119
|
+
'kind = "1password"',
|
|
1120
|
+
`account = ${str(vault.account)}`,
|
|
1121
|
+
`vault = ${str(vault.vault)}`
|
|
1122
|
+
];
|
|
1123
|
+
return `${[...header, ...lines].join(`
|
|
1124
|
+
`)}
|
|
1125
|
+
`;
|
|
1126
|
+
}
|
|
1127
|
+
function keyIdFor(name) {
|
|
1128
|
+
return name.toLowerCase().replaceAll("_", "-").replace(/^-+|-+$/g, "").replace(/-{2,}/g, "-") || "key";
|
|
1129
|
+
}
|
|
1130
|
+
function freeId(base, taken) {
|
|
1131
|
+
let id = base;
|
|
1132
|
+
for (let n = 2;taken.has(id); n++)
|
|
1133
|
+
id = `${base}-${n}`;
|
|
1134
|
+
taken.add(id);
|
|
1135
|
+
return id;
|
|
1136
|
+
}
|
|
1137
|
+
function str(value) {
|
|
1138
|
+
return JSON.stringify(value);
|
|
1139
|
+
}
|
|
739
1140
|
// ../../packages/destination-cloudflare/src/token.ts
|
|
740
1141
|
import { execFile } from "child_process";
|
|
741
1142
|
import { readFile } from "fs/promises";
|
|
@@ -752,6 +1153,12 @@ async function wranglerToken(path = WRANGLER_CONFIG) {
|
|
|
752
1153
|
}
|
|
753
1154
|
return login.token;
|
|
754
1155
|
}
|
|
1156
|
+
async function currentWranglerToken(path = WRANGLER_CONFIG) {
|
|
1157
|
+
const login = await readLogin(path);
|
|
1158
|
+
if (login.expired)
|
|
1159
|
+
throw new Error("the wrangler login has expired; run `wrangler whoami`");
|
|
1160
|
+
return login.token;
|
|
1161
|
+
}
|
|
755
1162
|
async function readLogin(path) {
|
|
756
1163
|
let text;
|
|
757
1164
|
try {
|
|
@@ -787,7 +1194,7 @@ function refresh() {
|
|
|
787
1194
|
|
|
788
1195
|
// ../../packages/destination-cloudflare/src/cloudflare.ts
|
|
789
1196
|
var API = "https://api.cloudflare.com/client/v4";
|
|
790
|
-
var
|
|
1197
|
+
var ACCOUNT_ID3 = /^[0-9a-f]{32}$/;
|
|
791
1198
|
var WORKER = /^[A-Za-z0-9_-]+$/;
|
|
792
1199
|
|
|
793
1200
|
class WorkerSecretDestination {
|
|
@@ -799,7 +1206,7 @@ class WorkerSecretDestination {
|
|
|
799
1206
|
#token;
|
|
800
1207
|
#passed = false;
|
|
801
1208
|
constructor(options) {
|
|
802
|
-
if (!
|
|
1209
|
+
if (!ACCOUNT_ID3.test(options.account))
|
|
803
1210
|
throw new Error(`"${options.account}" isn't a Cloudflare account ID`);
|
|
804
1211
|
if (!WORKER.test(options.worker))
|
|
805
1212
|
throw new Error(`"${options.worker}" isn't a Worker name`);
|
|
@@ -931,6 +1338,85 @@ function objectField(data, field) {
|
|
|
931
1338
|
const value = data[field];
|
|
932
1339
|
return isRecord(value) ? value : {};
|
|
933
1340
|
}
|
|
1341
|
+
// ../../packages/destination-cloudflare/src/list.ts
|
|
1342
|
+
var API2 = "https://api.cloudflare.com/client/v4";
|
|
1343
|
+
var ACCOUNT_ID4 = /^[0-9a-f]{32}$/;
|
|
1344
|
+
var WORKER2 = /^[A-Za-z0-9_-]+$/;
|
|
1345
|
+
|
|
1346
|
+
class CloudflareReader {
|
|
1347
|
+
#tokenSource;
|
|
1348
|
+
#fetch;
|
|
1349
|
+
#token;
|
|
1350
|
+
#accounts;
|
|
1351
|
+
constructor(options) {
|
|
1352
|
+
this.#tokenSource = options.token;
|
|
1353
|
+
this.#fetch = options.fetch ?? globalThis.fetch;
|
|
1354
|
+
}
|
|
1355
|
+
async listWorkerSecrets(account, worker) {
|
|
1356
|
+
if (!ACCOUNT_ID4.test(account))
|
|
1357
|
+
return { ok: false, reason: `"${account}" isn't a Cloudflare account ID` };
|
|
1358
|
+
if (!WORKER2.test(worker))
|
|
1359
|
+
return { ok: false, reason: `"${worker}" isn't a Worker name` };
|
|
1360
|
+
const accounts = await (this.#accounts ??= this.#accountIds());
|
|
1361
|
+
if (!Array.isArray(accounts))
|
|
1362
|
+
return { ok: false, reason: accounts.reason };
|
|
1363
|
+
if (!accounts.includes(account)) {
|
|
1364
|
+
return {
|
|
1365
|
+
ok: false,
|
|
1366
|
+
reason: `the wrangler login reaches ${accounts.join(", ") || "no account"}, not ${account}`
|
|
1367
|
+
};
|
|
1368
|
+
}
|
|
1369
|
+
const response = await this.#get(`/accounts/${account}/workers/scripts/${worker}/secrets`);
|
|
1370
|
+
if ("reason" in response)
|
|
1371
|
+
return { ok: false, reason: response.reason };
|
|
1372
|
+
if (response.status === 404)
|
|
1373
|
+
return { ok: false, reason: `account ${account} has no Worker named ${worker}` };
|
|
1374
|
+
if (response.status !== 200)
|
|
1375
|
+
return { ok: false, reason: `Cloudflare returned HTTP ${response.status}` };
|
|
1376
|
+
const result = response.body.result;
|
|
1377
|
+
if (!Array.isArray(result))
|
|
1378
|
+
return { ok: false, reason: "Cloudflare's secrets response has no result list" };
|
|
1379
|
+
return {
|
|
1380
|
+
ok: true,
|
|
1381
|
+
secrets: result.flatMap((s) => isRecord(s) && typeof s.name === "string" ? [{ name: s.name, updatedAt: null }] : [])
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
async#accountIds() {
|
|
1385
|
+
const ids = [];
|
|
1386
|
+
for (let page = 1;; page++) {
|
|
1387
|
+
const response = await this.#get(`/accounts?per_page=50&page=${page}`);
|
|
1388
|
+
if ("reason" in response)
|
|
1389
|
+
return response;
|
|
1390
|
+
if (response.status !== 200)
|
|
1391
|
+
return { reason: `Cloudflare GET /accounts returned HTTP ${response.status}` };
|
|
1392
|
+
const result = response.body.result;
|
|
1393
|
+
if (!Array.isArray(result))
|
|
1394
|
+
return { reason: "Cloudflare's /accounts response has no list" };
|
|
1395
|
+
for (const account of result) {
|
|
1396
|
+
if (isRecord(account) && typeof account.id === "string")
|
|
1397
|
+
ids.push(account.id);
|
|
1398
|
+
}
|
|
1399
|
+
const info = response.body.result_info;
|
|
1400
|
+
const pages = isRecord(info) ? info.total_pages : undefined;
|
|
1401
|
+
if (typeof pages !== "number" || page >= pages)
|
|
1402
|
+
return ids;
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
async#get(path) {
|
|
1406
|
+
let token;
|
|
1407
|
+
try {
|
|
1408
|
+
token = await (this.#token ??= this.#tokenSource());
|
|
1409
|
+
} catch (error) {
|
|
1410
|
+
return { reason: error instanceof Error ? error.message : "no Cloudflare token" };
|
|
1411
|
+
}
|
|
1412
|
+
const response = await this.#fetch(`${API2}${path}`, {
|
|
1413
|
+
method: "GET",
|
|
1414
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
1415
|
+
});
|
|
1416
|
+
const body = await response.json().catch(() => null);
|
|
1417
|
+
return { status: response.status, body: isRecord(body) ? body : {} };
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
934
1420
|
// ../../packages/destination-github/src/github.ts
|
|
935
1421
|
import sodium from "libsodium-wrappers";
|
|
936
1422
|
|
|
@@ -953,7 +1439,7 @@ function ghToken(login) {
|
|
|
953
1439
|
}
|
|
954
1440
|
|
|
955
1441
|
// ../../packages/destination-github/src/github.ts
|
|
956
|
-
var
|
|
1442
|
+
var API3 = "https://api.github.com";
|
|
957
1443
|
var REPO2 = /^[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/;
|
|
958
1444
|
var SECRET_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
959
1445
|
|
|
@@ -1045,7 +1531,7 @@ class GitHubSecretDestination {
|
|
|
1045
1531
|
}
|
|
1046
1532
|
async#request(method, path, body) {
|
|
1047
1533
|
this.#token ??= this.#tokenSource();
|
|
1048
|
-
const response = await this.#fetch(`${
|
|
1534
|
+
const response = await this.#fetch(`${API3}${path}`, {
|
|
1049
1535
|
method,
|
|
1050
1536
|
headers: {
|
|
1051
1537
|
Authorization: `Bearer ${await this.#token}`,
|
|
@@ -1085,9 +1571,66 @@ function stringField(data, field) {
|
|
|
1085
1571
|
throw new Error(`GitHub's response has no "${field}"`);
|
|
1086
1572
|
return value;
|
|
1087
1573
|
}
|
|
1574
|
+
// ../../packages/destination-github/src/list.ts
|
|
1575
|
+
var API4 = "https://api.github.com";
|
|
1576
|
+
var REPO3 = /^[A-Za-z0-9-]+\/[A-Za-z0-9._-]+$/;
|
|
1577
|
+
async function listRepoSecrets(options) {
|
|
1578
|
+
if (!REPO3.test(options.repo))
|
|
1579
|
+
return {
|
|
1580
|
+
ok: false,
|
|
1581
|
+
reason: `"${options.repo}" isn't a GitHub repository in the form owner/name`
|
|
1582
|
+
};
|
|
1583
|
+
const fetch = options.fetch ?? globalThis.fetch;
|
|
1584
|
+
let token;
|
|
1585
|
+
try {
|
|
1586
|
+
token = await options.token();
|
|
1587
|
+
} catch (error) {
|
|
1588
|
+
return { ok: false, reason: error instanceof Error ? error.message : "no GitHub token" };
|
|
1589
|
+
}
|
|
1590
|
+
const secrets = [];
|
|
1591
|
+
for (let page = 1;; page++) {
|
|
1592
|
+
const path = `/repos/${options.repo}/actions/secrets?per_page=100&page=${page}`;
|
|
1593
|
+
const response = await fetch(`${API4}${path}`, {
|
|
1594
|
+
method: "GET",
|
|
1595
|
+
headers: {
|
|
1596
|
+
Authorization: `Bearer ${token}`,
|
|
1597
|
+
Accept: "application/vnd.github+json",
|
|
1598
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
1599
|
+
}
|
|
1600
|
+
});
|
|
1601
|
+
if (response.status !== 200) {
|
|
1602
|
+
await response.body?.cancel();
|
|
1603
|
+
return { ok: false, reason: refusal(response.status, options.repo) };
|
|
1604
|
+
}
|
|
1605
|
+
const body = await response.json().catch(() => null);
|
|
1606
|
+
const list = isRecord(body) && Array.isArray(body.secrets) ? body.secrets : null;
|
|
1607
|
+
if (!list)
|
|
1608
|
+
return { ok: false, reason: "GitHub's secrets response has no secrets list" };
|
|
1609
|
+
for (const secret of list) {
|
|
1610
|
+
if (!isRecord(secret) || typeof secret.name !== "string")
|
|
1611
|
+
continue;
|
|
1612
|
+
secrets.push({
|
|
1613
|
+
name: secret.name,
|
|
1614
|
+
updatedAt: typeof secret.updated_at === "string" ? secret.updated_at : null
|
|
1615
|
+
});
|
|
1616
|
+
}
|
|
1617
|
+
const total = isRecord(body) && typeof body.total_count === "number" ? body.total_count : 0;
|
|
1618
|
+
if (list.length < 100 || secrets.length >= total)
|
|
1619
|
+
return { ok: true, secrets };
|
|
1620
|
+
}
|
|
1621
|
+
}
|
|
1622
|
+
function refusal(status, repo) {
|
|
1623
|
+
if (status === 404)
|
|
1624
|
+
return `HTTP 404: the login can't see ${repo}, or it isn't an admin of it`;
|
|
1625
|
+
if (status === 403)
|
|
1626
|
+
return `HTTP 403: the login isn't allowed to list ${repo}'s secrets`;
|
|
1627
|
+
if (status === 401)
|
|
1628
|
+
return "HTTP 401: the gh login's token was refused";
|
|
1629
|
+
return `GitHub returned HTTP ${status}`;
|
|
1630
|
+
}
|
|
1088
1631
|
// ../../packages/provider-cloudflare/src/tokens.ts
|
|
1089
|
-
var
|
|
1090
|
-
var
|
|
1632
|
+
var API5 = "https://api.cloudflare.com/client/v4";
|
|
1633
|
+
var ACCOUNT_ID5 = /^[0-9a-f]{32}$/;
|
|
1091
1634
|
var TOKEN_ID = /^[0-9a-f]{32}$/;
|
|
1092
1635
|
|
|
1093
1636
|
class CloudflareAccountTokens {
|
|
@@ -1095,7 +1638,7 @@ class CloudflareAccountTokens {
|
|
|
1095
1638
|
#admin;
|
|
1096
1639
|
#fetch;
|
|
1097
1640
|
constructor(options) {
|
|
1098
|
-
if (!
|
|
1641
|
+
if (!ACCOUNT_ID5.test(options.account))
|
|
1099
1642
|
throw new Error(`"${options.account}" isn't a Cloudflare account ID`);
|
|
1100
1643
|
this.#account = options.account;
|
|
1101
1644
|
this.#admin = options.admin;
|
|
@@ -1162,7 +1705,7 @@ class CloudflareAccountTokens {
|
|
|
1162
1705
|
return `/accounts/${this.#account}/tokens`;
|
|
1163
1706
|
}
|
|
1164
1707
|
async#request(method, path, auth, body) {
|
|
1165
|
-
const response = await this.#fetch(`${
|
|
1708
|
+
const response = await this.#fetch(`${API5}${path}`, {
|
|
1166
1709
|
method,
|
|
1167
1710
|
headers: {
|
|
1168
1711
|
Authorization: `Bearer ${auth.reveal()}`,
|
|
@@ -1429,9 +1972,10 @@ function toDay(value, ref) {
|
|
|
1429
1972
|
function messageOf2(error) {
|
|
1430
1973
|
return error instanceof Error ? error.message : String(error);
|
|
1431
1974
|
}
|
|
1432
|
-
// src/commands/
|
|
1433
|
-
import { join as
|
|
1975
|
+
// src/commands/discover.ts
|
|
1976
|
+
import { basename, join as join3, relative, resolve } from "path";
|
|
1434
1977
|
import { parseArgs } from "util";
|
|
1978
|
+
var {TOML: TOML3 } = globalThis.Bun;
|
|
1435
1979
|
|
|
1436
1980
|
// src/deps.ts
|
|
1437
1981
|
class UsageError extends Error {
|
|
@@ -1441,31 +1985,12 @@ function localDay(date) {
|
|
|
1441
1985
|
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
|
|
1442
1986
|
}
|
|
1443
1987
|
|
|
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
1988
|
// src/project.ts
|
|
1464
|
-
import { dirname
|
|
1989
|
+
import { dirname, join as join2 } from "path";
|
|
1465
1990
|
var MAP_FILE = "keypin.toml";
|
|
1466
1991
|
async function findMap(files, cwd) {
|
|
1467
|
-
for (let folder = cwd;; folder =
|
|
1468
|
-
const file =
|
|
1992
|
+
for (let folder = cwd;; folder = dirname(folder)) {
|
|
1993
|
+
const file = join2(folder, MAP_FILE);
|
|
1469
1994
|
const text = await files.read(file);
|
|
1470
1995
|
if (text !== null) {
|
|
1471
1996
|
const result = parseMap(text, file);
|
|
@@ -1474,12 +1999,353 @@ async function findMap(files, cwd) {
|
|
|
1474
1999
|
`));
|
|
1475
2000
|
return result.map;
|
|
1476
2001
|
}
|
|
1477
|
-
if (
|
|
2002
|
+
if (dirname(folder) === folder) {
|
|
1478
2003
|
throw new Error(`no ${MAP_FILE} in ${cwd} or any folder above it; run "keypin init" first`);
|
|
1479
2004
|
}
|
|
1480
2005
|
}
|
|
1481
2006
|
}
|
|
1482
2007
|
|
|
2008
|
+
// src/commands/discover.ts
|
|
2009
|
+
async function discover(args, deps) {
|
|
2010
|
+
const { values, positionals } = parseArgs({
|
|
2011
|
+
args,
|
|
2012
|
+
options: { json: { type: "boolean", default: false } },
|
|
2013
|
+
allowPositionals: true
|
|
2014
|
+
});
|
|
2015
|
+
if (positionals.length > 1)
|
|
2016
|
+
throw new UsageError("keypin discover takes at most one folder");
|
|
2017
|
+
const folder = resolve(deps.cwd, positionals[0] ?? ".");
|
|
2018
|
+
if (!await deps.isFolder(folder))
|
|
2019
|
+
throw new Error(`${folder} isn't a folder`);
|
|
2020
|
+
const unknowns = [];
|
|
2021
|
+
const scan = await walk(deps, folder);
|
|
2022
|
+
const tracked = await trackedFiles(deps, folder, scan.envFiles);
|
|
2023
|
+
const envFiles = [];
|
|
2024
|
+
for (const path of scan.envFiles) {
|
|
2025
|
+
const kind = envFileKind(basename(path));
|
|
2026
|
+
if (!kind)
|
|
2027
|
+
continue;
|
|
2028
|
+
const text = await deps.files.read(join3(folder, path));
|
|
2029
|
+
if (text === null)
|
|
2030
|
+
continue;
|
|
2031
|
+
envFiles.push({ path, kind, names: envNames(text), tracked: tracked?.has(path) ?? null });
|
|
2032
|
+
}
|
|
2033
|
+
if (tracked === null && envFiles.some((f) => f.kind === "set")) {
|
|
2034
|
+
unknowns.push({
|
|
2035
|
+
what: "whether env files are tracked by git",
|
|
2036
|
+
reason: "the folder isn't in a git repository",
|
|
2037
|
+
cloud: false
|
|
2038
|
+
});
|
|
2039
|
+
}
|
|
2040
|
+
const top = (await deps.git(folder, ["rev-parse", "--show-toplevel"]))?.trim() || null;
|
|
2041
|
+
const workflows = await readWorkflows(deps, folder, top, unknowns);
|
|
2042
|
+
const workers = [];
|
|
2043
|
+
for (const config of scan.wrangler) {
|
|
2044
|
+
const text = await deps.files.read(join3(folder, config));
|
|
2045
|
+
if (text === null)
|
|
2046
|
+
continue;
|
|
2047
|
+
const parsed = wranglerWorkers(text, config);
|
|
2048
|
+
if (!parsed.ok) {
|
|
2049
|
+
unknowns.push({
|
|
2050
|
+
what: `the Workers in ${config}`,
|
|
2051
|
+
reason: "it couldn't be parsed",
|
|
2052
|
+
cloud: true
|
|
2053
|
+
});
|
|
2054
|
+
continue;
|
|
2055
|
+
}
|
|
2056
|
+
for (const worker of parsed.workers) {
|
|
2057
|
+
const read = worker.worker && worker.account ? await deps.workerSecrets(worker.account, worker.worker).catch((error) => ({
|
|
2058
|
+
ok: false,
|
|
2059
|
+
reason: messageOf(error)
|
|
2060
|
+
})) : null;
|
|
2061
|
+
workers.push({ ...worker, read });
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
const origin = top ? await githubOrigin(deps, folder) : null;
|
|
2065
|
+
let github = null;
|
|
2066
|
+
let login = null;
|
|
2067
|
+
if (!top) {
|
|
2068
|
+
unknowns.push({
|
|
2069
|
+
what: "GitHub Actions secrets",
|
|
2070
|
+
reason: "the folder isn't in a git repository",
|
|
2071
|
+
cloud: true
|
|
2072
|
+
});
|
|
2073
|
+
} else if (!origin || "reason" in origin) {
|
|
2074
|
+
unknowns.push({
|
|
2075
|
+
what: "GitHub Actions secrets",
|
|
2076
|
+
reason: origin?.reason ?? "the repository has no origin remote",
|
|
2077
|
+
cloud: true
|
|
2078
|
+
});
|
|
2079
|
+
} else {
|
|
2080
|
+
login = (await deps.git(folder, ["config", "--get", "github.account"]))?.trim() || origin.owner;
|
|
2081
|
+
const read = await deps.githubSecrets(origin.repo, login).catch((error) => ({ ok: false, reason: messageOf(error) }));
|
|
2082
|
+
github = {
|
|
2083
|
+
repo: origin.repo,
|
|
2084
|
+
read: read.ok ? read : { ok: false, reason: `as ${login}: ${read.reason}` }
|
|
2085
|
+
};
|
|
2086
|
+
}
|
|
2087
|
+
const mapText = await deps.files.read(join3(folder, MAP_FILE));
|
|
2088
|
+
let map = null;
|
|
2089
|
+
if (mapText !== null) {
|
|
2090
|
+
const parsed = parseMap(mapText, MAP_FILE);
|
|
2091
|
+
if (parsed.ok)
|
|
2092
|
+
map = parsed.map;
|
|
2093
|
+
else
|
|
2094
|
+
unknowns.push({
|
|
2095
|
+
what: `which names ${MAP_FILE} maps`,
|
|
2096
|
+
reason: parsed.errors.join("; "),
|
|
2097
|
+
cloud: false
|
|
2098
|
+
});
|
|
2099
|
+
}
|
|
2100
|
+
const discovery = buildDiscovery({ envFiles, workflows, workers, github, map, unknowns });
|
|
2101
|
+
const owner = map?.keys[0]?.owner ?? basename(folder);
|
|
2102
|
+
const draft = draftMap(discovery.names, {
|
|
2103
|
+
owner,
|
|
2104
|
+
githubAccount: login,
|
|
2105
|
+
vault: map && mapText !== null ? firstVault(mapText) : null,
|
|
2106
|
+
takenIds: map?.keys.map((k) => k.id) ?? []
|
|
2107
|
+
});
|
|
2108
|
+
if (draft !== null) {
|
|
2109
|
+
const check = parseMap(draft, "draft");
|
|
2110
|
+
if (!check.ok)
|
|
2111
|
+
throw new Error(`the draft doesn't parse: ${check.errors.join("; ")}`);
|
|
2112
|
+
}
|
|
2113
|
+
const report = {
|
|
2114
|
+
folder,
|
|
2115
|
+
repository: github?.repo ?? null,
|
|
2116
|
+
login,
|
|
2117
|
+
files: {
|
|
2118
|
+
env: envFiles.map(({ path, kind, tracked: t }) => ({ path, kind, tracked: t })),
|
|
2119
|
+
wrangler: scan.wrangler,
|
|
2120
|
+
workflows: workflows.map((w) => w.path)
|
|
2121
|
+
},
|
|
2122
|
+
nestedRepositories: scan.nested,
|
|
2123
|
+
...discovery,
|
|
2124
|
+
draft
|
|
2125
|
+
};
|
|
2126
|
+
deps.stdout.write(values.json ? `${JSON.stringify(report, null, 2)}
|
|
2127
|
+
` : render(report, discovery));
|
|
2128
|
+
return 0;
|
|
2129
|
+
}
|
|
2130
|
+
async function walk(deps, root) {
|
|
2131
|
+
const scan = { envFiles: [], wrangler: [], nested: [] };
|
|
2132
|
+
const visit = async (folder) => {
|
|
2133
|
+
const entries = (await deps.listFolder(folder)).toSorted((a, b) => a.name.localeCompare(b.name));
|
|
2134
|
+
if (folder !== root && entries.some((e) => e.name === ".git")) {
|
|
2135
|
+
scan.nested.push(relative(root, folder));
|
|
2136
|
+
return;
|
|
2137
|
+
}
|
|
2138
|
+
for (const entry of entries) {
|
|
2139
|
+
const path = join3(folder, entry.name);
|
|
2140
|
+
if (entry.kind === "folder") {
|
|
2141
|
+
if (!SKIPPED_FOLDERS.has(entry.name))
|
|
2142
|
+
await visit(path);
|
|
2143
|
+
} else if (entry.kind === "file") {
|
|
2144
|
+
if (envFileKind(entry.name))
|
|
2145
|
+
scan.envFiles.push(relative(root, path));
|
|
2146
|
+
else if (WRANGLER_FILES.includes(entry.name))
|
|
2147
|
+
scan.wrangler.push(relative(root, path));
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
};
|
|
2151
|
+
await visit(root);
|
|
2152
|
+
return scan;
|
|
2153
|
+
}
|
|
2154
|
+
async function trackedFiles(deps, folder, paths) {
|
|
2155
|
+
if (!paths.length)
|
|
2156
|
+
return new Set;
|
|
2157
|
+
const out = await deps.git(folder, ["ls-files", "-z", "--", ...paths]);
|
|
2158
|
+
if (out === null)
|
|
2159
|
+
return null;
|
|
2160
|
+
return new Set(out.split("\x00").filter(Boolean));
|
|
2161
|
+
}
|
|
2162
|
+
async function readWorkflows(deps, folder, top, unknowns) {
|
|
2163
|
+
const dir = join3(top ?? folder, ".github", "workflows");
|
|
2164
|
+
let entries;
|
|
2165
|
+
try {
|
|
2166
|
+
entries = await deps.listFolder(dir);
|
|
2167
|
+
} catch (error) {
|
|
2168
|
+
if (errorCode(error) !== "ENOENT") {
|
|
2169
|
+
unknowns.push({
|
|
2170
|
+
what: "the secrets the workflows use",
|
|
2171
|
+
reason: `${relative(folder, dir)} couldn't be read: ${messageOf(error)}`,
|
|
2172
|
+
cloud: false
|
|
2173
|
+
});
|
|
2174
|
+
}
|
|
2175
|
+
return [];
|
|
2176
|
+
}
|
|
2177
|
+
const workflows = [];
|
|
2178
|
+
for (const entry of entries.toSorted((a, b) => a.name.localeCompare(b.name))) {
|
|
2179
|
+
if (entry.kind !== "file" || !/\.ya?ml$/.test(entry.name))
|
|
2180
|
+
continue;
|
|
2181
|
+
const text = await deps.files.read(join3(dir, entry.name));
|
|
2182
|
+
if (text === null)
|
|
2183
|
+
continue;
|
|
2184
|
+
const path = relative(folder, join3(dir, entry.name));
|
|
2185
|
+
const refs = workflowRefs(text);
|
|
2186
|
+
if (refs)
|
|
2187
|
+
workflows.push({ path, refs });
|
|
2188
|
+
else
|
|
2189
|
+
unknowns.push({
|
|
2190
|
+
what: `the secrets ${path} uses`,
|
|
2191
|
+
reason: "it isn't valid YAML",
|
|
2192
|
+
cloud: false
|
|
2193
|
+
});
|
|
2194
|
+
}
|
|
2195
|
+
return workflows;
|
|
2196
|
+
}
|
|
2197
|
+
var GITHUB_REMOTE = /^(?:https:\/\/(?:[^@/]+@)?github\.com\/|git@github\.com:|ssh:\/\/git@github\.com\/)([A-Za-z0-9-]+)\/([A-Za-z0-9._-]+?)(?:\.git)?\/?$/;
|
|
2198
|
+
async function githubOrigin(deps, folder) {
|
|
2199
|
+
const url = (await deps.git(folder, ["remote", "get-url", "origin"]))?.trim();
|
|
2200
|
+
if (!url)
|
|
2201
|
+
return null;
|
|
2202
|
+
const match = GITHUB_REMOTE.exec(url);
|
|
2203
|
+
if (!match?.[1] || !match[2])
|
|
2204
|
+
return { reason: "the origin remote isn't on github.com" };
|
|
2205
|
+
return { repo: `${match[1]}/${match[2]}`, owner: match[1] };
|
|
2206
|
+
}
|
|
2207
|
+
function firstVault(text) {
|
|
2208
|
+
let data;
|
|
2209
|
+
try {
|
|
2210
|
+
data = TOML3.parse(text);
|
|
2211
|
+
} catch {
|
|
2212
|
+
return null;
|
|
2213
|
+
}
|
|
2214
|
+
const vaults = isRecord(data) && isRecord(data.vaults) ? data.vaults : {};
|
|
2215
|
+
for (const [name, raw] of Object.entries(vaults)) {
|
|
2216
|
+
if (!isRecord(raw))
|
|
2217
|
+
continue;
|
|
2218
|
+
const { account, vault } = raw;
|
|
2219
|
+
if (typeof account === "string" && typeof vault === "string")
|
|
2220
|
+
return { name, account, vault };
|
|
2221
|
+
}
|
|
2222
|
+
return null;
|
|
2223
|
+
}
|
|
2224
|
+
var WHERE = {
|
|
2225
|
+
both: "local and cloud",
|
|
2226
|
+
local: "local",
|
|
2227
|
+
cloud: "cloud",
|
|
2228
|
+
declared: "declared, not set",
|
|
2229
|
+
variable: "plain variable"
|
|
2230
|
+
};
|
|
2231
|
+
var LABELS = {
|
|
2232
|
+
"env-file": "local",
|
|
2233
|
+
"example-file": "expected",
|
|
2234
|
+
"github-secret": "cloud",
|
|
2235
|
+
"worker-secret": "cloud",
|
|
2236
|
+
"workflow-secret": "workflow",
|
|
2237
|
+
"workflow-var": "workflow var",
|
|
2238
|
+
"worker-required": "required by",
|
|
2239
|
+
"worker-var": "var in",
|
|
2240
|
+
map: "mapped"
|
|
2241
|
+
};
|
|
2242
|
+
function describe(source) {
|
|
2243
|
+
switch (source.kind) {
|
|
2244
|
+
case "env-file":
|
|
2245
|
+
return source.tracked ? `${source.path} (tracked by git)` : source.path;
|
|
2246
|
+
case "example-file":
|
|
2247
|
+
case "workflow-secret":
|
|
2248
|
+
case "workflow-var":
|
|
2249
|
+
return source.path;
|
|
2250
|
+
case "worker-required":
|
|
2251
|
+
case "worker-var":
|
|
2252
|
+
return source.worker ? `${source.path} (${source.worker})` : source.path;
|
|
2253
|
+
case "github-secret":
|
|
2254
|
+
return `GitHub ${source.repo}${source.updatedAt ? `, updated ${source.updatedAt.slice(0, 10)}` : ""}`;
|
|
2255
|
+
case "worker-secret":
|
|
2256
|
+
return `Worker ${source.worker} (account ${source.account.slice(0, 8)}\u2026)`;
|
|
2257
|
+
case "map":
|
|
2258
|
+
return `${source.path} key ${source.keyId}`;
|
|
2259
|
+
default: {
|
|
2260
|
+
const unknown = source;
|
|
2261
|
+
return JSON.stringify(unknown);
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
function render(report, discovery) {
|
|
2266
|
+
const lines = [`keypin discover ${report.folder}`];
|
|
2267
|
+
lines.push(` GitHub: ${report.repository ? `${report.repository} as ${report.login}` : "no GitHub origin"}`);
|
|
2268
|
+
lines.push(` read: ${count(report.files.env.length, "env file")}, ${count(report.files.wrangler.length, "wrangler config")}, ${count(report.files.workflows.length, "workflow")}`);
|
|
2269
|
+
if (report.nestedRepositories.length)
|
|
2270
|
+
lines.push(` not scanned (own git repository): ${report.nestedRepositories.join(", ")}`);
|
|
2271
|
+
const onlyMissing = (row) => row.sources.every((s) => s.kind === "workflow-secret") && discovery.problems.some((p) => p.kind === "missing-github-secret" && p.name === row.name);
|
|
2272
|
+
const missing = discovery.names.filter(onlyMissing);
|
|
2273
|
+
const shown = discovery.names.filter((row) => !onlyMissing(row));
|
|
2274
|
+
lines.push("", `Names (${discovery.names.length}):`);
|
|
2275
|
+
for (const row of shown) {
|
|
2276
|
+
const where = WHERE[row.where] + (row.cloudUnknown ? ", cloud unknown" : "");
|
|
2277
|
+
const mapped = row.mapped.length ? ", mapped" : "";
|
|
2278
|
+
lines.push(` ${row.name} [${where}${mapped}]`);
|
|
2279
|
+
const groups = new Map;
|
|
2280
|
+
for (const source of row.sources) {
|
|
2281
|
+
const label = LABELS[source.kind];
|
|
2282
|
+
groups.set(label, [...groups.get(label) ?? [], describe(source)]);
|
|
2283
|
+
}
|
|
2284
|
+
for (const [label, items] of groups)
|
|
2285
|
+
lines.push(` ${label}: ${[...new Set(items)].join(", ")}`);
|
|
2286
|
+
for (const problem of row.problems)
|
|
2287
|
+
lines.push(` ! ${problem}`);
|
|
2288
|
+
}
|
|
2289
|
+
if (missing.length) {
|
|
2290
|
+
const byWorkflow = new Map;
|
|
2291
|
+
for (const row of missing) {
|
|
2292
|
+
const files = row.sources.map(describe).join(", ");
|
|
2293
|
+
byWorkflow.set(files, [...byWorkflow.get(files) ?? [], row.name]);
|
|
2294
|
+
}
|
|
2295
|
+
const repo = report.repository ?? "the repository";
|
|
2296
|
+
lines.push("", `Referenced by a workflow, and not a repo secret of ${repo} (${missing.length}):`);
|
|
2297
|
+
for (const [files, names] of byWorkflow)
|
|
2298
|
+
lines.push(` ${files}: ${names.join(", ")}`);
|
|
2299
|
+
}
|
|
2300
|
+
if (!discovery.names.length)
|
|
2301
|
+
lines.push(" (none found)");
|
|
2302
|
+
const loose = discovery.problems.filter((p) => p.name === undefined);
|
|
2303
|
+
if (loose.length) {
|
|
2304
|
+
lines.push("", `Problems (${loose.length}):`);
|
|
2305
|
+
for (const p of loose)
|
|
2306
|
+
lines.push(` ${p.kind}: ${p.message}`);
|
|
2307
|
+
}
|
|
2308
|
+
if (discovery.unknowns.length) {
|
|
2309
|
+
lines.push("", `Unknown (${discovery.unknowns.length}):`);
|
|
2310
|
+
for (const u of discovery.unknowns)
|
|
2311
|
+
lines.push(` ${u.what}: ${u.reason}`);
|
|
2312
|
+
}
|
|
2313
|
+
lines.push("");
|
|
2314
|
+
if (report.draft === null)
|
|
2315
|
+
lines.push("No draft: every name set locally or in the cloud is already mapped.");
|
|
2316
|
+
else
|
|
2317
|
+
lines.push("Draft keypin.toml for the unmapped names (not written):", "", report.draft);
|
|
2318
|
+
return `${lines.join(`
|
|
2319
|
+
`).trimEnd()}
|
|
2320
|
+
`;
|
|
2321
|
+
}
|
|
2322
|
+
function count(n, noun) {
|
|
2323
|
+
return `${n} ${noun}${n === 1 ? "" : "s"}`;
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
// src/commands/init.ts
|
|
2327
|
+
import { join as join5, resolve as resolve2 } from "path";
|
|
2328
|
+
import { parseArgs as parseArgs2 } from "util";
|
|
2329
|
+
|
|
2330
|
+
// src/paths.ts
|
|
2331
|
+
import { dirname as dirname2, join as join4 } from "path";
|
|
2332
|
+
function registryPath(deps) {
|
|
2333
|
+
return join4(base(deps.env.XDG_CONFIG_HOME, deps.home, ".config"), "keypin", "registry.json");
|
|
2334
|
+
}
|
|
2335
|
+
function statePath(deps) {
|
|
2336
|
+
return join4(base(deps.env.XDG_STATE_HOME, deps.home, ".local/state"), "keypin", "state.json");
|
|
2337
|
+
}
|
|
2338
|
+
function base(xdg, home, fallback) {
|
|
2339
|
+
return xdg?.startsWith("/") ? xdg : join4(home, fallback);
|
|
2340
|
+
}
|
|
2341
|
+
function projectCount(registry, path) {
|
|
2342
|
+
const count = registry.projects.length;
|
|
2343
|
+
return `${count} ${count === 1 ? "project" : "projects"} in ${path}`;
|
|
2344
|
+
}
|
|
2345
|
+
function rotationsPath(deps) {
|
|
2346
|
+
return join4(dirname2(statePath(deps)), "rotations.json");
|
|
2347
|
+
}
|
|
2348
|
+
|
|
1483
2349
|
// src/commands/init.ts
|
|
1484
2350
|
var TEMPLATE = `# keypin.toml: this project's keys. It holds names and places, never values.
|
|
1485
2351
|
# Uncomment and edit. Every field is checked; an unknown one is an error.
|
|
@@ -1516,13 +2382,13 @@ var TEMPLATE = `# keypin.toml: this project's keys. It holds names and places, n
|
|
|
1516
2382
|
# repo = "owner/repo"
|
|
1517
2383
|
`;
|
|
1518
2384
|
async function init(args, deps) {
|
|
1519
|
-
const { positionals } =
|
|
2385
|
+
const { positionals } = parseArgs2({ args, options: {}, allowPositionals: true });
|
|
1520
2386
|
if (positionals.length > 1)
|
|
1521
2387
|
throw new UsageError("keypin init takes at most one folder");
|
|
1522
|
-
const folder =
|
|
2388
|
+
const folder = resolve2(deps.cwd, positionals[0] ?? ".");
|
|
1523
2389
|
if (!await deps.isFolder(folder))
|
|
1524
2390
|
throw new Error(`${folder} isn't a folder`);
|
|
1525
|
-
const file =
|
|
2391
|
+
const file = join5(folder, MAP_FILE);
|
|
1526
2392
|
const created = await deps.createFile(file, TEMPLATE);
|
|
1527
2393
|
if (created === "link")
|
|
1528
2394
|
throw new Error(`${file} is a symbolic link; keypin init doesn't follow one`);
|
|
@@ -1537,7 +2403,7 @@ async function init(args, deps) {
|
|
|
1537
2403
|
}
|
|
1538
2404
|
|
|
1539
2405
|
// src/commands/publish.ts
|
|
1540
|
-
import { parseArgs as
|
|
2406
|
+
import { parseArgs as parseArgs3 } from "util";
|
|
1541
2407
|
|
|
1542
2408
|
// src/publishing.ts
|
|
1543
2409
|
var NEEDS_APPROVAL = 2;
|
|
@@ -1684,7 +2550,7 @@ async function publish(args, deps) {
|
|
|
1684
2550
|
return locked(deps, (path) => publishLocked({ ...request, args }, map, path, deps));
|
|
1685
2551
|
}
|
|
1686
2552
|
function parseRequest(args) {
|
|
1687
|
-
const { values, positionals } =
|
|
2553
|
+
const { values, positionals } = parseArgs3({
|
|
1688
2554
|
args,
|
|
1689
2555
|
options: {
|
|
1690
2556
|
env: { type: "string", default: "prod" },
|
|
@@ -1808,10 +2674,10 @@ async function confirmed(deps, needsApproval, request) {
|
|
|
1808
2674
|
}
|
|
1809
2675
|
|
|
1810
2676
|
// src/commands/rotate.ts
|
|
1811
|
-
import { parseArgs as
|
|
2677
|
+
import { parseArgs as parseArgs4 } from "util";
|
|
1812
2678
|
var GRACE_DAYS = DUE_SOON_DAYS;
|
|
1813
2679
|
async function rotate(args, deps) {
|
|
1814
|
-
const { values, positionals } =
|
|
2680
|
+
const { values, positionals } = parseArgs4({
|
|
1815
2681
|
args,
|
|
1816
2682
|
options: {
|
|
1817
2683
|
env: { type: "string", default: "prod" },
|
|
@@ -2292,9 +3158,9 @@ function describeAccess(token) {
|
|
|
2292
3158
|
}
|
|
2293
3159
|
|
|
2294
3160
|
// src/commands/run.ts
|
|
2295
|
-
import { parseArgs as
|
|
3161
|
+
import { parseArgs as parseArgs5 } from "util";
|
|
2296
3162
|
async function run(args, deps) {
|
|
2297
|
-
const { positionals } =
|
|
3163
|
+
const { positionals } = parseArgs5({ args, options: {}, allowPositionals: true });
|
|
2298
3164
|
if (!positionals.length)
|
|
2299
3165
|
throw new UsageError("keypin run needs a command after --");
|
|
2300
3166
|
const map = await findMap(deps.files, deps.cwd);
|
|
@@ -2360,10 +3226,10 @@ async function run(args, deps) {
|
|
|
2360
3226
|
}
|
|
2361
3227
|
|
|
2362
3228
|
// src/commands/status.ts
|
|
2363
|
-
import { join as
|
|
2364
|
-
import { parseArgs as
|
|
3229
|
+
import { join as join6 } from "path";
|
|
3230
|
+
import { parseArgs as parseArgs6 } from "util";
|
|
2365
3231
|
async function status(args, deps) {
|
|
2366
|
-
const { values, positionals } =
|
|
3232
|
+
const { values, positionals } = parseArgs6({
|
|
2367
3233
|
args,
|
|
2368
3234
|
options: { json: { type: "boolean", default: false } },
|
|
2369
3235
|
allowPositionals: true
|
|
@@ -2375,7 +3241,7 @@ async function status(args, deps) {
|
|
|
2375
3241
|
const { rotations } = await loadRotations(deps.files, rotationsPath(deps));
|
|
2376
3242
|
const maps = new Map;
|
|
2377
3243
|
for (const project of registry.projects) {
|
|
2378
|
-
const file =
|
|
3244
|
+
const file = join6(project, MAP_FILE);
|
|
2379
3245
|
try {
|
|
2380
3246
|
const text = await deps.files.read(file);
|
|
2381
3247
|
if (text !== null)
|
|
@@ -2412,11 +3278,11 @@ async function status(args, deps) {
|
|
|
2412
3278
|
deps.stdout.write(`${JSON.stringify({ today: localDay(deps.now()), ...result }, null, 2)}
|
|
2413
3279
|
`);
|
|
2414
3280
|
} else {
|
|
2415
|
-
deps.stdout.write(
|
|
3281
|
+
deps.stdout.write(render2(result, registry.projects.length));
|
|
2416
3282
|
}
|
|
2417
3283
|
return result.problems.some((p) => p.severity === "problem") ? 1 : 0;
|
|
2418
3284
|
}
|
|
2419
|
-
function
|
|
3285
|
+
function render2(result, projects) {
|
|
2420
3286
|
if (!projects)
|
|
2421
3287
|
return `No projects registered. Run "keypin init" in a project folder.
|
|
2422
3288
|
`;
|
|
@@ -2483,13 +3349,13 @@ function shown(problems) {
|
|
|
2483
3349
|
}
|
|
2484
3350
|
|
|
2485
3351
|
// src/commands/unregister.ts
|
|
2486
|
-
import { resolve as
|
|
2487
|
-
import { parseArgs as
|
|
3352
|
+
import { resolve as resolve3 } from "path";
|
|
3353
|
+
import { parseArgs as parseArgs7 } from "util";
|
|
2488
3354
|
async function unregister2(args, deps) {
|
|
2489
|
-
const { positionals } =
|
|
3355
|
+
const { positionals } = parseArgs7({ args, options: {}, allowPositionals: true });
|
|
2490
3356
|
if (positionals.length > 1)
|
|
2491
3357
|
throw new UsageError("keypin unregister takes at most one folder");
|
|
2492
|
-
const folder =
|
|
3358
|
+
const folder = resolve3(deps.cwd, positionals[0] ?? ".");
|
|
2493
3359
|
const path = registryPath(deps);
|
|
2494
3360
|
const before = await loadRegistry(deps.files, path);
|
|
2495
3361
|
const registry = await unregister(deps.files, path, folder);
|
|
@@ -2505,7 +3371,7 @@ async function unregister2(args, deps) {
|
|
|
2505
3371
|
return 0;
|
|
2506
3372
|
}
|
|
2507
3373
|
// ../../package.json
|
|
2508
|
-
var version = "0.
|
|
3374
|
+
var version = "0.3.0";
|
|
2509
3375
|
|
|
2510
3376
|
// src/cli.ts
|
|
2511
3377
|
var USAGE = `Usage:
|
|
@@ -2513,6 +3379,8 @@ var USAGE = `Usage:
|
|
|
2513
3379
|
keypin unregister [folder]
|
|
2514
3380
|
stop listing a project; its keypin.toml stays
|
|
2515
3381
|
keypin status [--json] every key in every registered project, with problems
|
|
3382
|
+
keypin discover [folder] [--json]
|
|
3383
|
+
every key name a project uses, local and in the cloud, and a draft map
|
|
2516
3384
|
keypin run -- <command> run a command with this project's dev keys in its environment
|
|
2517
3385
|
keypin publish <key-id> [--env prod|dev] [--yes]
|
|
2518
3386
|
check accounts, plan, confirm, then write the key to every destination
|
|
@@ -2525,6 +3393,7 @@ var COMMANDS = new Map([
|
|
|
2525
3393
|
["init", init],
|
|
2526
3394
|
["unregister", unregister2],
|
|
2527
3395
|
["status", status],
|
|
3396
|
+
["discover", discover],
|
|
2528
3397
|
["run", run],
|
|
2529
3398
|
["publish", publish],
|
|
2530
3399
|
["rotate", rotate]
|
|
@@ -2562,7 +3431,8 @@ ${USAGE}`);
|
|
|
2562
3431
|
|
|
2563
3432
|
// src/disk.ts
|
|
2564
3433
|
import { constants, rmSync } from "fs";
|
|
2565
|
-
import {
|
|
3434
|
+
import { execFile as execFile3 } from "child_process";
|
|
3435
|
+
import { chmod, lstat, mkdir, open, readdir, readFile as readFile2, rename, rm, stat } from "fs/promises";
|
|
2566
3436
|
import { dirname as dirname3 } from "path";
|
|
2567
3437
|
|
|
2568
3438
|
// src/launch.ts
|
|
@@ -2647,6 +3517,20 @@ async function isFolder(path) {
|
|
|
2647
3517
|
return false;
|
|
2648
3518
|
}
|
|
2649
3519
|
}
|
|
3520
|
+
async function listFolder(path) {
|
|
3521
|
+
const entries = await readdir(path, { withFileTypes: true });
|
|
3522
|
+
return entries.map((entry) => ({
|
|
3523
|
+
name: entry.name,
|
|
3524
|
+
kind: entry.isFile() ? "file" : entry.isDirectory() ? "folder" : "other"
|
|
3525
|
+
}));
|
|
3526
|
+
}
|
|
3527
|
+
function git(folder, args) {
|
|
3528
|
+
return new Promise((resolve) => {
|
|
3529
|
+
execFile3("git", ["-C", folder, ...args], { maxBuffer: 64 * 1024 * 1024 }, (error, stdout) => {
|
|
3530
|
+
resolve(error ? null : String(stdout));
|
|
3531
|
+
});
|
|
3532
|
+
});
|
|
3533
|
+
}
|
|
2650
3534
|
var diskLock = async (path) => {
|
|
2651
3535
|
await privateFolder(dirname3(path));
|
|
2652
3536
|
if (await take(path))
|
|
@@ -2696,6 +3580,7 @@ function held(path) {
|
|
|
2696
3580
|
|
|
2697
3581
|
// src/start.ts
|
|
2698
3582
|
var vault;
|
|
3583
|
+
var cloudflare;
|
|
2699
3584
|
function destination(target) {
|
|
2700
3585
|
switch (target.to) {
|
|
2701
3586
|
case "cloudflare-worker":
|
|
@@ -2738,6 +3623,12 @@ function main() {
|
|
|
2738
3623
|
home: homedir2(),
|
|
2739
3624
|
files: diskFiles,
|
|
2740
3625
|
isFolder,
|
|
3626
|
+
listFolder,
|
|
3627
|
+
git,
|
|
3628
|
+
githubSecrets: (repo, login) => listRepoSecrets({ repo, token: () => ghToken(login) }),
|
|
3629
|
+
workerSecrets: (account, worker) => (cloudflare ??= new CloudflareReader({
|
|
3630
|
+
token: () => currentWranglerToken()
|
|
3631
|
+
})).listWorkerSecrets(account, worker),
|
|
2741
3632
|
createFile,
|
|
2742
3633
|
vault: () => vault ??= onePasswordBackend(connect),
|
|
2743
3634
|
now: () => new Date,
|