Skip to content

Commit 7e69b5e

Browse files
committed
Initial socket basics commit
0 parents  commit 7e69b5e

76 files changed

Lines changed: 19068 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @SocketDev/customer-success

.gitignore

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
2+
.idea
3+
venv
4+
.venv
5+
build
6+
dist
7+
*.build
8+
*.dist
9+
*.egg-info
10+
test
11+
*.env
12+
run_container.sh
13+
*.zip
14+
bin
15+
scripts/*.py
16+
*.json
17+
markdown_overview_temp.md
18+
markdown_security_temp.md
19+
.DS_Store
20+
*.pyc
21+
test.py
22+
23+
# Note: requirements.txt is no longer needed - using pyproject.toml + uv.lock instead
24+
# Version files are auto-managed by .hooks/version-check.py
25+
*.cpython-312.pyc
26+
file_generator.py
27+
.env
28+
*.md
29+
test_results
30+
local_tests/
31+
32+
# Common Python ignores
33+
__pycache__/
34+
*.py[cod]
35+
*$py.class
36+
.pytest_cache/
37+
.mypy_cache/
38+
.coverage
39+
.coverage.*
40+
htmlcov/
41+
pip-wheel-metadata/
42+
pip-log.txt
43+
pip-delete-this-directory.txt
44+
45+
# Virtual environments
46+
env/
47+
ENV/
48+
env.bak/
49+
venv.bak/
50+
51+
# IDEs and editors
52+
.vscode/
53+
.idea/
54+
*.sublime-workspace
55+
*.sublime-project
56+
*.swp
57+
*~
58+
59+
# Node
60+
node_modules/
61+
npm-debug.log*
62+
yarn-debug.log*
63+
yarn-error.log*
64+
.pnp/
65+
66+
# Build and distribution
67+
.eggs/
68+
*.egg
69+
dist/
70+
build/
71+
72+
# Coverage and test output
73+
coverage/
74+
coverage.xml
75+
nosetests.xml
76+
test-results/
77+
78+
# Logs and runtime files
79+
logs/
80+
81+
*.pid
82+
*.sock
83+
84+
# OS files
85+
.DS_Store
86+
87+
# Binary and compiled
88+
*.exe
89+
*.dll
90+
*.so
91+
*.dylib
92+
93+
# Jupyter
94+
.ipynb_checkpoints/
95+
96+
# Local temporary files
97+
*.tmp
98+
*.temp
99+
# Ignore output logs and generated src files
100+
*.log
101+
102+
.python-version

.hooks/setup.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Setup script to install pre-commit hooks for version management.
4+
"""
5+
import pathlib
6+
import subprocess
7+
import sys
8+
9+
def setup_pre_commit_hook():
10+
"""Set up the pre-commit hook for version checking."""
11+
git_hooks_dir = pathlib.Path(".git/hooks")
12+
pre_commit_hook = git_hooks_dir / "pre-commit"
13+
14+
if not git_hooks_dir.exists():
15+
print("❌ .git/hooks directory not found. Are you in a git repository?")
16+
sys.exit(1)
17+
18+
hook_content = '''#!/bin/bash
19+
# Version check pre-commit hook
20+
python3 .hooks/version-check.py
21+
'''
22+
23+
# Create or update the pre-commit hook
24+
if pre_commit_hook.exists():
25+
print("⚠️ Pre-commit hook already exists.")
26+
response = input("Do you want to overwrite it? (y/N): ")
27+
if response.lower() != 'y':
28+
print("❌ Aborted.")
29+
sys.exit(1)
30+
31+
pre_commit_hook.write_text(hook_content)
32+
pre_commit_hook.chmod(0o755)
33+
34+
print("✅ Pre-commit hook installed successfully!")
35+
print("Now version changes will be automatically checked on each commit.")
36+
print("")
37+
print("Usage:")
38+
print(" Normal commit: Will auto-bump patch version if unchanged")
39+
print(" Dev mode: python3 .hooks/version-check.py --dev")
40+
41+
def main():
42+
if "--install-hook" in sys.argv:
43+
setup_pre_commit_hook()
44+
else:
45+
print("Version management setup script")
46+
print("")
47+
print("Options:")
48+
print(" --install-hook Install pre-commit hook for version checking")
49+
print("")
50+
print("Manual usage:")
51+
print(" python3 .hooks/version-check.py # Check and auto-bump if needed")
52+
print(" python3 .hooks/version-check.py --dev # Use dev versioning")
53+
54+
if __name__ == "__main__":
55+
main()

.hooks/version-check.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env python3
2+
import subprocess
3+
import pathlib
4+
import re
5+
import sys
6+
import urllib.request
7+
import json
8+
9+
VERSION_FILE = pathlib.Path("socket_basics/version.py")
10+
PYPROJECT_FILE = pathlib.Path("pyproject.toml")
11+
12+
VERSION_PATTERN = re.compile(r"__version__\s*=\s*['\"]([^'\"]+)['\"]")
13+
PYPROJECT_PATTERN = re.compile(r'^version\s*=\s*"([^"]+)"$', re.MULTILINE)
14+
# Update this URL to match your actual PyPI package if you publish it
15+
PYPI_API = "https://pypi.org/pypi/security-wrapper/json"
16+
17+
def read_version_from_version_file(path: pathlib.Path) -> str:
18+
if not path.exists():
19+
print(f"❌ Version file {path} does not exist")
20+
sys.exit(1)
21+
content = path.read_text()
22+
match = VERSION_PATTERN.search(content)
23+
if not match:
24+
print(f"❌ Could not find __version__ in {path}")
25+
sys.exit(1)
26+
return match.group(1)
27+
28+
def read_version_from_pyproject(path: pathlib.Path) -> str:
29+
if not path.exists():
30+
print(f"❌ pyproject.toml file {path} does not exist")
31+
sys.exit(1)
32+
content = path.read_text()
33+
match = PYPROJECT_PATTERN.search(content)
34+
if not match:
35+
print(f"❌ Could not find version in {path}")
36+
sys.exit(1)
37+
return match.group(1)
38+
39+
def read_version_from_git(path: str) -> str:
40+
try:
41+
output = subprocess.check_output(["git", "show", f"HEAD:{path}"], text=True)
42+
match = VERSION_PATTERN.search(output)
43+
if not match:
44+
return None
45+
return match.group(1)
46+
except subprocess.CalledProcessError:
47+
return None
48+
49+
def bump_patch_version(version: str) -> str:
50+
if ".dev" in version:
51+
version = version.split(".dev")[0]
52+
parts = version.split(".")
53+
parts[-1] = str(int(parts[-1]) + 1)
54+
return ".".join(parts)
55+
56+
def fetch_existing_versions() -> set:
57+
try:
58+
with urllib.request.urlopen(PYPI_API) as response:
59+
data = json.load(response)
60+
return set(data.get("releases", {}).keys())
61+
except Exception as e:
62+
print(f"⚠️ Warning: Failed to fetch existing versions from PyPI: {e}")
63+
return set()
64+
65+
def find_next_available_dev_version(base_version: str) -> str:
66+
existing_versions = fetch_existing_versions()
67+
for i in range(1, 100):
68+
candidate = f"{base_version}.dev{i}"
69+
if candidate not in existing_versions:
70+
return candidate
71+
print("❌ Could not find available .devN slot after 100 attempts.")
72+
sys.exit(1)
73+
74+
def inject_version(version: str):
75+
print(f"🔁 Updating version to: {version}")
76+
77+
# Update version.py
78+
VERSION_FILE.write_text(f'__version__ = "{version}"\n')
79+
80+
# Update pyproject.toml
81+
pyproject = PYPROJECT_FILE.read_text()
82+
if PYPROJECT_PATTERN.search(pyproject):
83+
new_pyproject = PYPROJECT_PATTERN.sub(f'version = "{version}"', pyproject)
84+
PYPROJECT_FILE.write_text(new_pyproject)
85+
print(f"✅ Updated {PYPROJECT_FILE}")
86+
else:
87+
print(f"⚠️ Could not find version field in {PYPROJECT_FILE}")
88+
89+
def check_version_sync():
90+
"""Ensure version.py and pyproject.toml are in sync"""
91+
version_py = read_version_from_version_file(VERSION_FILE)
92+
version_toml = read_version_from_pyproject(PYPROJECT_FILE)
93+
94+
if version_py != version_toml:
95+
print(f"❌ Version mismatch: {VERSION_FILE} has {version_py}, {PYPROJECT_FILE} has {version_toml}")
96+
print("🔁 Syncing versions...")
97+
inject_version(version_toml) # Use pyproject.toml as source of truth
98+
return version_toml
99+
100+
return version_py
101+
102+
def main():
103+
dev_mode = "--dev" in sys.argv
104+
105+
# Ensure versions are synced
106+
current_version = check_version_sync()
107+
previous_version = read_version_from_git("src/version.py")
108+
109+
print(f"Current: {current_version}, Previous: {previous_version}")
110+
111+
if current_version == previous_version:
112+
if dev_mode:
113+
base_version = current_version.split(".dev")[0] if ".dev" in current_version else current_version
114+
new_version = find_next_available_dev_version(base_version)
115+
inject_version(new_version)
116+
print("⚠️ Version was unchanged — auto-bumped. Please git add + commit again.")
117+
sys.exit(0)
118+
else:
119+
new_version = bump_patch_version(current_version)
120+
inject_version(new_version)
121+
print("⚠️ Version was unchanged — auto-bumped. Please git add + commit again.")
122+
sys.exit(1)
123+
else:
124+
print("✅ Version already bumped — proceeding.")
125+
sys.exit(0)
126+
127+
if __name__ == "__main__":
128+
main()

Dockerfile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Use the official Python image as a base
2+
FROM python:3.12
3+
4+
# Create application directory
5+
WORKDIR /socket-security-tools
6+
ENV PATH=$PATH:/usr/local/go/bin
7+
8+
# Install uv
9+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
10+
11+
# Install Trivy
12+
RUN curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.18.3
13+
14+
# Install Trufflehog
15+
RUN curl -sSfL https://raw.githubusercontent.com/trufflesecurity/trufflehog/main/scripts/install.sh | sh -s -- -b /usr/local/bin
16+
17+
18+
RUN apt-get update && apt-get install -y curl git wget
19+
20+
# Install Trivy
21+
RUN curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.18.3
22+
23+
# Install Trufflehog
24+
RUN curl -sSfL https://raw.githubusercontent.com/trufflesecurity/trufflehog/main/scripts/install.sh | sh -s -- -b /usr/local/bin
25+
26+
# Install OpenGrep (connector/runtime dependency)
27+
RUN curl -fsSL https://raw.githubusercontent.com/opengrep/opengrep/main/install.sh | bash
28+
29+
# Copy socket_basics package so we can install the CLI entrypoint
30+
COPY socket_basics /socket-security-tools/socket_basics
31+
# Also copy the project root so editable install has access to all files
32+
COPY . /socket-security-tools/
33+
34+
COPY pyproject.toml uv.lock LICENSE README.md /scripts/
35+
# Install Python dependencies using uv
36+
WORKDIR /scripts
37+
RUN uv sync --frozen && uv pip install light-s3-client
38+
ENV PATH="/scripts/.venv/bin:/root/.opengrep/cli/latest:$PATH"
39+
40+
# Install this package so the `socket-basics` CLI entrypoint is available
41+
WORKDIR /socket-security-tools
42+
# Ensure python can import package if install doesn't run; prefer installed package
43+
ENV PYTHONPATH="/socket-security-tools:${PYTHONPATH}"
44+
# Ensure pyproject is present for editable install; fail loudly if install fails
45+
RUN uv pip install -e . || pip install -e .
46+
47+
# Use socket-basics as the default entrypoint
48+
ENTRYPOINT ["socket-basics"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Douglas
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)