Skip to content

Commit d5cdcd3

Browse files
Copilotiloveitaly
andcommitted
Refine op integration toggle handling
Co-authored-by: iloveitaly <150855+iloveitaly@users.noreply.github.com>
1 parent 8840a4a commit d5cdcd3

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

.envrc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,35 @@ layout uv
4444
# debugging much more challenging. It's easy for obscure error messages to get swallowed up and waste development time
4545
# forgetting that this error path exists.
4646
op_inject_source() {
47-
local tmpfile
48-
tmpfile=$(mktemp)
47+
local input_file injected_file op_reference_pattern
48+
op_reference_pattern='op://[[:alnum:].~_-]+/[[:alnum:].~_-]+/[[:alnum:].~_/-]+'
49+
# simplified matcher for op://vault/item/field references with alnum, dot, underscore, tilde, dash, or slash in the field
50+
# references using other characters or additional path segments will bypass replacement
51+
52+
input_file=$(umask 077; mktemp)
53+
injected_file=$(umask 077; mktemp)
54+
55+
if ! cat > "$input_file"; then
56+
just _banner_echo "Failed to capture inject input"
57+
rm -f "$input_file" "$injected_file"
58+
return 1
59+
fi
4960

5061
if [ -n "${OP_INTEGRATION_DISABLED:-}" ]; then
51-
cat > "$tmpfile"
52-
sed -E 's@op://[^[:space:]]+@fake-op-value@g' "$tmpfile" > "${tmpfile}.fake"
53-
source "${tmpfile}.fake"
54-
rm -f "$tmpfile" "${tmpfile}.fake"
62+
if grep -Eq "${op_reference_pattern}" "$input_file"; then
63+
# intentionally uses a single placeholder for all secrets while OP integration is disabled
64+
sed -E "s|${op_reference_pattern}|fake-op-value|g" "$input_file" > "$injected_file"
65+
else
66+
just _banner_echo "OP_INTEGRATION_DISABLED set but no op references matched fallback pattern; leaving values unchanged"
67+
cp "$input_file" "$injected_file"
68+
fi
69+
source "$injected_file"
70+
rm -f "$input_file" "$injected_file"
5571
return 0
5672
fi
5773

5874
# `inject` consumes stdin
59-
if ! op inject > "$tmpfile"; then
75+
if ! op inject < "$input_file" > "$injected_file"; then
6076
just _banner_echo "1Password injection failed"
6177
cat >&2 << EOF
6278
Make sure you're authenticated with 1Password, have access to referenced secrets, and are using valid secret references.
@@ -72,12 +88,12 @@ Some tips on how to resolve this issue:
7288
- If you move a 1P entry out of a vault, this will cause a missing entry error.
7389
7490
EOF
75-
rm -f "$tmpfile"
91+
rm -f "$input_file" "$injected_file"
7692
return 1
7793
fi
7894

79-
source "$tmpfile"
80-
rm -f "$tmpfile"
95+
source "$injected_file"
96+
rm -f "$input_file" "$injected_file"
8197
}
8298

8399
# this is hack to allow us to render a specific .env file

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ There are a couple of dependencies which are not managed by the project:
7777
* Latest macOS
7878
* VS Code
7979

80-
(If you want to bootstrap without 1Password on first run, set `OP_INTEGRATION_DISABLED=1` to load fake secret values instead of running `op inject`.)
80+
(If you want to bootstrap without 1Password on first run, set `OP_INTEGRATION_DISABLED=1` to load fake secret values instead of running `op inject`).
8181

8282
(you **could** use a different setup (bash, vim, etc), but this is not the golden path for this project.)
8383

0 commit comments

Comments
 (0)