Skip to content

Commit 05ccbb2

Browse files
hrntknrCopilot
andauthored
feat!: replace global secret parameter with auto-generated secret (#6)
* feat!: replace global secret parameter with auto-generated secret - Remove GLOBAL_SECRET environment variable and command line flag - Add LoadOrGenerateSecret function to automatically generate and persist secrets - Update documentation to remove global secret references - Add developer guidelines for conventional commits BREAKING CHANGE: GLOBAL_SECRET environment variable and --global-secret flag are no longer supported. Secrets are now automatically generated and persisted. * Update pkg/utils/keys.go Co-authored-by: Copilot <[email protected]> * refactor: extract secret size to named constant Replace magic number 32 with SecretSize constant for better maintainability. --------- Co-authored-by: Copilot <[email protected]>
1 parent 882d71d commit 05ccbb2

4 files changed

Lines changed: 77 additions & 11 deletions

File tree

README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ If this project saves you time, please give it a star — it really helps visibi
1010
docker run --rm -p 80:80 --net=host \
1111
-e EXTERNAL_URL=http://localhost \
1212
-e PROXY_URL=http://localhost:8080 \
13-
-e GLOBAL_SECRET=$(openssl rand -hex 32) \
1413
-e PASSWORD=changeme \
1514
-v ./data:/data \
1615
ghcr.io/sigbit/mcp-auth-proxy:latest
@@ -53,7 +52,6 @@ For a simpler approach to publish local MCP servers over OAuth, consider [MCP Wa
5352
| `DATA_PATH` | No | Data directory path | `./data` |
5453
| `EXTERNAL_URL` | No | External URL for OAuth callbacks | `http://localhost` |
5554
| `PROXY_URL` | No | Target MCP server URL | `http://localhost:8080` |
56-
| `GLOBAL_SECRET` | No | Global secret for session encryption | `supersecret` |
5755
| `GOOGLE_CLIENT_ID` | No | Google OAuth client ID | - |
5856
| `GOOGLE_CLIENT_SECRET` | No | Google OAuth client secret | - |
5957
| `GOOGLE_ALLOWED_USERS` | No | Comma-separated list of allowed Google emails | - |
@@ -87,7 +85,6 @@ Download the latest binary from [releases](https://github.com/sigbit/mcp-auth-pr
8785
./mcp-auth-proxy \
8886
--external-url "http://localhost:8081" \
8987
--proxy-url "http://localhost:8080" \
90-
--global-secret "$(openssl rand -hex 32)" \
9188
--google-client-id "your-google-client-id" \
9289
--google-client-secret "your-google-client-secret" \
9390
--google-allowed-users "[email protected],[email protected]" \
@@ -103,7 +100,6 @@ Download the latest binary from [releases](https://github.com/sigbit/mcp-auth-pr
103100
docker run --rm -p 8081:8081 --net=host \
104101
-e EXTERNAL_URL=http://localhost:8081 \
105102
-e PROXY_URL=http://localhost:8080 \
106-
-e GLOBAL_SECRET=$(openssl rand -hex 32) \
107103
-e GOOGLE_CLIENT_ID="your-google-client-id" \
108104
-e GOOGLE_CLIENT_SECRET="your-google-client-secret" \
109105
-e GOOGLE_ALLOWED_USERS="[email protected],[email protected]" \
@@ -114,3 +110,51 @@ docker run --rm -p 8081:8081 --net=host \
114110
-v ./data:/data \
115111
ghcr.io/sigbit/mcp-auth-proxy:latest
116112
```
113+
114+
## 👨‍💻 For Developers
115+
116+
### Commit Message Guidelines
117+
118+
This project follows [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification for commit messages. This helps with automated versioning, changelog generation, and makes the commit history more readable.
119+
120+
#### Format
121+
122+
```
123+
<type>[optional scope]: <description>
124+
125+
[optional body]
126+
127+
[optional footer(s)]
128+
```
129+
130+
#### Types
131+
132+
- **feat**: A new feature
133+
- **fix**: A bug fix
134+
- **docs**: Documentation only changes
135+
- **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
136+
- **refactor**: A code change that neither fixes a bug nor adds a feature
137+
- **perf**: A code change that improves performance
138+
- **test**: Adding missing tests or correcting existing tests
139+
- **build**: Changes that affect the build system or external dependencies
140+
- **ci**: Changes to our CI configuration files and scripts
141+
- **chore**: Other changes that don't modify src or test files
142+
- **revert**: Reverts a previous commit
143+
144+
#### Examples
145+
146+
```
147+
feat: add GitHub OAuth provider support
148+
fix: resolve token expiration handling
149+
docs: update OAuth setup instructions
150+
refactor: simplify authentication middleware
151+
ci: add automated release workflow
152+
```
153+
154+
#### Breaking Changes
155+
156+
Breaking changes should be indicated by a `!` after the type/scope:
157+
158+
```
159+
feat!: change authentication API to support multiple providers
160+
```

main.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func main() {
3434
var dataPath string
3535
var externalURL string
3636
var proxyURL string
37-
var globalSecret string
3837
var googleClientID string
3938
var googleClientSecret string
4039
var googleAllowedUsers string
@@ -72,7 +71,6 @@ func main() {
7271
dataPath,
7372
externalURL,
7473
proxyURL,
75-
globalSecret,
7674
googleClientID,
7775
googleClientSecret,
7876
googleAllowedUsersList,
@@ -95,7 +93,6 @@ func main() {
9593
rootCmd.Flags().StringVarP(&dataPath, "data", "d", getEnvWithDefault("DATA_PATH", "./data"), "Path to the data directory")
9694
rootCmd.Flags().StringVarP(&externalURL, "external-url", "e", getEnvWithDefault("EXTERNAL_URL", "http://localhost"), "External URL for the proxy")
9795
rootCmd.Flags().StringVarP(&proxyURL, "proxy-url", "p", getEnvWithDefault("PROXY_URL", "http://localhost:8080"), "Proxy URL for the proxy")
98-
rootCmd.Flags().StringVarP(&globalSecret, "global-secret", "s", getEnvWithDefault("GLOBAL_SECRET", "supersecret"), "Global secret for the proxy")
9996

10097
// Google OAuth configuration
10198
rootCmd.Flags().StringVar(&googleClientID, "google-client-id", getEnvWithDefault("GOOGLE_CLIENT_ID", ""), "Google OAuth client ID")

pkg/mcp-proxy/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package mcpproxy
22

33
import (
44
"context"
5-
"crypto/sha256"
65
"errors"
76
"fmt"
87
"net/http"
@@ -37,7 +36,6 @@ func Run(
3736
dataPath string,
3837
externalURL string,
3938
proxyURL string,
40-
globalSecret string,
4139
googleClientID string,
4240
googleClientSecret string,
4341
googleAllowedUsers []string,
@@ -54,8 +52,11 @@ func Run(
5452
if parsedExternalURL.Path != "" {
5553
return fmt.Errorf("external URL must not have a path, got: %s", parsedExternalURL.Path)
5654
}
57-
sha256Hash := sha256.Sum256([]byte(globalSecret))
58-
secret := sha256Hash[:]
55+
56+
secret, err := utils.LoadOrGenerateSecret(path.Join(dataPath, "secret"))
57+
if err != nil {
58+
return fmt.Errorf("failed to load or generate secret: %w", err)
59+
}
5960

6061
var config zap.Config
6162
if os.Getenv("MODE") == "debug" {

pkg/utils/keys.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@ import (
99
"os"
1010
)
1111

12+
const SecretSize = 32
13+
14+
func LoadOrGenerateSecret(secretPath string) ([]byte, error) {
15+
_, err := os.Stat(secretPath)
16+
if os.IsNotExist(err) {
17+
secret := make([]byte, SecretSize)
18+
if _, err := rand.Read(secret); err != nil {
19+
return nil, fmt.Errorf("failed to generate secret: %w", err)
20+
}
21+
if err := os.WriteFile(secretPath, secret, 0600); err != nil {
22+
return nil, fmt.Errorf("failed to save secret: %w", err)
23+
}
24+
return secret, nil
25+
}
26+
if err != nil {
27+
return nil, fmt.Errorf("failed to stat secret file: %w", err)
28+
}
29+
secret, err := os.ReadFile(secretPath)
30+
if err != nil {
31+
return nil, fmt.Errorf("failed to read secret file: %w", err)
32+
}
33+
return secret, nil
34+
}
35+
1236
func LoadOrGeneratePrivateKey(keyPath string) (*rsa.PrivateKey, error) {
1337
_, err := os.Stat(keyPath)
1438
if os.IsNotExist(err) {

0 commit comments

Comments
 (0)