Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 7071591

Browse files
Merge pull request #291 from stacklok/generate-certs
Adds cli command to generate certificate
2 parents 0e76be5 + 0304252 commit 7071591

File tree

12 files changed

+214
-257
lines changed

12 files changed

+214
-257
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ weaviate_data/
4747
codegate.db
4848

4949
# certificate directory
50-
certs/
50+
*certs/

cert_gen.py

Lines changed: 0 additions & 178 deletions
This file was deleted.

docs/cli.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ codegate serve [OPTIONS]
2929
- Overrides configuration file and environment variables
3030

3131
- `--log-level [ERROR|WARNING|INFO|DEBUG]`: Set the log level (default: INFO)
32+
- Optional
3233
- Case-insensitive
3334
- Overrides configuration file and environment variables
3435

3536
- `--log-format [JSON|TEXT]`: Set the log format (default: JSON)
37+
- Optional
3638
- Case-insensitive
3739
- Overrides configuration file and environment variables
3840

@@ -72,6 +74,10 @@ codegate serve [OPTIONS]
7274
- `--embedding-model TEXT`: Name of the model used for embeddings
7375
- Optional
7476

77+
- `--db-path TEXT`: Path to a SQLite DB. It will create one if it doesn't exist. (default: ./codegate_volume/db/codegate.db)
78+
- Optional
79+
- Overrides configuration file and environment variables
80+
7581
### show-prompts
7682

7783
Display the loaded system prompts:
@@ -87,6 +93,46 @@ codegate show-prompts [OPTIONS]
8793
- Must be a valid YAML file
8894
- If not provided, shows default prompts from prompts/default.yaml
8995

96+
### generate_certs
97+
98+
Generate certificates for the CodeGate server.
99+
100+
```bash
101+
codegate generate-certs [OPTIONS]
102+
```
103+
104+
#### Options
105+
106+
- `--certs-out-dir PATH`: Directory path where the certificates are going to be generated. (default: ./codegate_volume/certs)
107+
- Optional
108+
- Overrides configuration file and environment variables
109+
110+
- `--ca-cert-name TEXT`: Name that will be given to the created CA certificate. (default: ca.crt)
111+
- Optional
112+
- Overrides configuration file and environment variables
113+
114+
- `--ca-key-name TEXT`: Name that will be given to the created CA key. (default: ca.key)
115+
- Optional
116+
- Overrides configuration file and environment variables
117+
118+
- `--server-cert-name TEXT`: Name that will be given to the created server certificate. (default: server.crt)
119+
- Optional
120+
- Overrides configuration file and environment variables
121+
122+
- `--server-key-name TEXT`: Name that will be given to the created server key. (default: server.key)
123+
- Optional
124+
- Overrides configuration file and environment variables
125+
126+
- `--log-level [ERROR|WARNING|INFO|DEBUG]`: Set the log level (default: INFO)
127+
- Optional
128+
- Case-insensitive
129+
- Overrides configuration file and environment variables
130+
131+
- `--log-format [JSON|TEXT]`: Set the log format (default: JSON)
132+
- Optional
133+
- Case-insensitive
134+
- Overrides configuration file and environment variables
135+
90136
## Error Handling
91137

92138
The CLI provides user-friendly error messages for:
@@ -144,3 +190,9 @@ codegate show-prompts
144190
Show prompts from a custom file:
145191
```bash
146192
codegate show-prompts --prompts my-prompts.yaml
193+
```
194+
195+
Generate certificates with default settings:
196+
```bash
197+
codegate generate-certs
198+
```

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/entrypoint.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ BACKUP_PATH="/tmp/weaviate_backup"
77
BACKUP_NAME="backup"
88
MODEL_BASE_PATH="/app/codegate_volume/models"
99
CODEGATE_DB_FILE="/app/codegate_volume/db/codegate.db"
10+
CODEGATE_CERTS="/app/codegate_volume/certs"
1011

1112
# Function to restore backup if paths are provided
1213
restore_backup() {
@@ -22,6 +23,11 @@ restore_backup() {
2223
fi
2324
}
2425

26+
genrerate_certs() {
27+
echo "Generating certificates..."
28+
python -m src.codegate.cli generate-certs --certs-out-dir "$CODEGATE_CERTS"
29+
}
30+
2531
# Function to start Nginx server for the dashboard
2632
start_dashboard() {
2733
echo "Starting the dashboard..."
@@ -54,8 +60,11 @@ echo "Initializing entrypoint script..."
5460
# Step 1: Restore backup if applicable
5561
restore_backup
5662

57-
# Step 2: Start the dashboard
63+
# Step 2: Generate certificates
64+
genrerate_certs
65+
66+
# Step 3: Start the dashboard
5867
start_dashboard
5968

60-
# Step 3: Start the main application
69+
# Step 4: Start the main application
6170
start_application

src/codegate/ca/codegate_ca.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def generate_certificates(self) -> Tuple[str, str]:
297297

298298
# CA generated, now generate server certificate
299299

300-
# Generate new certificate for domain
300+
## Generate new certificate for domain
301301
logger.debug("Generating private key for server")
302302
server_key = rsa.generate_private_key(
303303
public_exponent=65537,
@@ -369,31 +369,34 @@ def generate_certificates(self) -> Tuple[str, str]:
369369
)
370370

371371
# Print instructions for trusting the certificates
372-
print("Certificates generated successfully in the 'certs' directory")
373-
print("\nTo trust these certificates:")
374-
print("\nOn macOS:")
375-
print(
376-
"`sudo security add-trusted-cert -d -r trustRoot "
377-
"-k /Library/Keychains/System.keychain certs/ca.crt"
378-
)
379-
print("\nOn Windows (PowerShell as Admin):")
380-
print(
381-
'Import-Certificate -FilePath "certs\\ca.crt" '
382-
'-CertStoreLocation Cert:\\LocalMachine\\Root'
383-
)
384-
print("\nOn Linux:")
385-
print("sudo cp certs/ca.crt /usr/local/share/ca-certificates/codegate.crt")
386-
print("sudo update-ca-certificates")
387-
print("\nFor VSCode, add to settings.json:")
388-
print(
389-
"""{
372+
logger.info(
373+
"""
374+
Certificates generated successfully in the 'certs' directory
375+
To trust these certificates:
376+
377+
On macOS:
378+
`sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain certs/ca.crt`
379+
380+
On Windows (PowerShell as Admin):
381+
`Import-Certificate -FilePath "certs\\ca.crt" -CertStoreLocation Cert:\\LocalMachine\\Root`
382+
383+
On Linux:
384+
`sudo cp certs/ca.crt /usr/local/share/ca-certificates/codegate.crt`
385+
`sudo update-ca-certificates`
386+
387+
For VSCode, add to settings.json:
388+
{
390389
"http.proxy": "https://localhost:8990",
390+
"http.proxyStrictSSL": true,
391391
"http.proxySupport": "on",
392392
"github.copilot.advanced": {
393+
"debug.useNodeFetcher": true,
394+
"debug.useElectronFetcher": true,
393395
"debug.testOverrideProxyUrl": "https://localhost:8990",
394396
"debug.overrideProxyUrl": "https://localhost:8990"
395-
}
396-
}"""
397+
},
398+
}
399+
"""
397400
)
398401
logger.debug("Certificates generated successfully")
399402
return server_cert, server_key
@@ -434,10 +437,9 @@ def ensure_certificates_exist(self) -> None:
434437
logger.debug("Certificates not found, generating new certificates")
435438
self.generate_certificates()
436439
else:
437-
logger.debug(
438-
f"Certificates found at: {Config.get_config().server_cert} "
439-
"and {Config.get_config().server_key}"
440-
)
440+
server_cert = Config.get_config().server_cert
441+
server_key = Config.get_config().server_key
442+
logger.debug(f"Certificates found at: {server_cert} and {server_key}.")
441443

442444
def get_ssl_context(self) -> ssl.SSLContext:
443445
"""Get SSL context with certificates"""

0 commit comments

Comments
 (0)