Skip to content

Commit 1a14b97

Browse files
authored
Create gha-secret-extract.yaml
1 parent 132db16 commit 1a14b97

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# .github/workflows/upload-gh-secrets.yml
2+
name: Upload GH Secrets to 1Password
3+
4+
on:
5+
pull_request:
6+
workflow_dispatch:
7+
8+
jobs:
9+
upload-secrets:
10+
runs-on: ubuntu-latest
11+
env:
12+
GH_SECRETS: ${{ secrets }}
13+
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.OP_SERVICE_ACCOUNT_TOKEN }}
14+
GITHUB_REPOSITORY: ${{ github.repository }}
15+
GITHUB_SECRET_SOURCE: ${{ github.secret_source }}
16+
steps:
17+
- name: Check out repository
18+
uses: actions/checkout@v3
19+
20+
- name: Set up Python 3.x
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.x'
24+
25+
- name: Install dependencies
26+
run: |
27+
python -m pip install --upgrade pip
28+
pip install onepassword-sdk
29+
30+
- name: Run GH‐Secrets → 1Password loader
31+
run: |
32+
cat > gh_secrets_loader.py << 'EOF'
33+
import asyncio
34+
import os
35+
import json
36+
import secrets
37+
38+
from onepassword.client import Client
39+
from onepassword import *
40+
41+
async def main():
42+
# --- Load inputs from environment ---
43+
raw = os.getenv("GH_SECRETS", "{}")
44+
try:
45+
secrets_map = json.loads(raw)
46+
except json.JSONDecodeError:
47+
print("Failed to parse GH_SECRETS, exiting.")
48+
return
49+
50+
if not secrets_map:
51+
print("No secrets found, exiting.")
52+
return
53+
54+
# 1Password service-account token
55+
op_token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
56+
57+
# GitHub repo, e.g. "owner/repo"
58+
repo_full = os.getenv("GITHUB_REPOSITORY", "unknown/unknown")
59+
secret_source = os.getenv("GITHUB_SECRET_SOURCE", "nosource")
60+
61+
62+
# Single 6-digit hash for this execution
63+
run_hash = f"{secrets.randbelow(10**6):06}"
64+
65+
# --- Authenticate to 1Password ---
66+
client = await Client.authenticate(
67+
auth=op_token,
68+
integration_name="Extrtact GitHub Secrets",
69+
integration_version="v1.0.0"
70+
)
71+
72+
# --- Find the target vault by name ---
73+
vaults = await client.vaults.list()
74+
target = next((v for v in vaults if v.title == "GitHub Secrets Extraction"), None)
75+
if not target:
76+
raise ValueError("Vault 'GitHub Secrets Extraction' not found")
77+
vault_id = target.id
78+
79+
80+
title = f"{run_hash}-{repo_full}-{secret_source}"
81+
params = ItemCreateParams(
82+
title=title,
83+
category=ItemCategory.SECURENOTE,
84+
vault_id=vault_id,
85+
fields=[
86+
ItemField(
87+
id='json',
88+
title='json',
89+
field_type=ItemFieldType.CONCEALED,
90+
value=json.dumps(secrets_map)
91+
)
92+
]
93+
)
94+
95+
created = await client.items.create(params)
96+
print(f"✔ Created item {created.id!r} with title {title} in vault '{target.title}'")
97+
98+
if __name__ == "__main__":
99+
asyncio.run(main())
100+
EOF
101+
102+
python gh_secrets_loader.py

0 commit comments

Comments
 (0)