-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathsecurity.go
238 lines (210 loc) · 9.23 KB
/
security.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package github
import (
"context"
"encoding/json"
"fmt"
"github.com/github/github-mcp-server/pkg/translations"
"github.com/google/go-github/v69/github"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
// SecurityAndAnalysis represents the security and analysis settings for a repository
type SecurityAndAnalysis struct {
AdvancedSecurity struct {
Status string `json:"status"`
} `json:"advanced_security"`
SecretScanning struct {
Status string `json:"status"`
} `json:"secret_scanning"`
SecretScanningPushProtection struct {
Status string `json:"status"`
} `json:"secret_scanning_push_protection"`
}
// GetSecuritySettings retrieves security settings for a repository
func GetSecuritySettings(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("get_security_settings",
mcp.WithDescription(t("TOOL_GET_SECURITY_SETTINGS_DESCRIPTION", "Get security settings for a repository")),
mcp.WithString("owner",
mcp.Required(),
mcp.Description(t("PARAM_OWNER_DESCRIPTION", "Repository owner")),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description(t("PARAM_REPO_DESCRIPTION", "Repository name")),
),
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner, ok := request.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("missing required parameter: owner")
}
repo, ok := request.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("missing required parameter: repo")
}
repository, _, err := client.Repositories.Get(ctx, owner, repo)
if err != nil {
return nil, fmt.Errorf("failed to get repository settings: %w", err)
}
response, err := json.Marshal(repository.SecurityAndAnalysis)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
return mcp.NewToolResultText(string(response)), nil
}
}
// UpdateSecuritySettings updates security settings for a repository
func UpdateSecuritySettings(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("update_security_settings",
mcp.WithDescription(t("TOOL_UPDATE_SECURITY_SETTINGS_DESCRIPTION", "Update security settings for a repository")),
mcp.WithString("owner",
mcp.Required(),
mcp.Description(t("PARAM_OWNER_DESCRIPTION", "Repository owner")),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description(t("PARAM_REPO_DESCRIPTION", "Repository name")),
),
mcp.WithObject("settings",
mcp.Required(),
mcp.Description(t("PARAM_SETTINGS_DESCRIPTION", "Security settings to update")),
),
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner, ok := request.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("missing required parameter: owner")
}
repo, ok := request.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("missing required parameter: repo")
}
settings, ok := request.Params.Arguments["settings"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("missing required parameter: settings")
}
// Get current repository settings
repository, _, err := client.Repositories.Get(ctx, owner, repo)
if err != nil {
return nil, fmt.Errorf("failed to get repository settings: %w", err)
}
// Initialize security settings if nil
if repository.SecurityAndAnalysis == nil {
repository.SecurityAndAnalysis = &github.SecurityAndAnalysis{}
}
// Update vulnerability alerts if specified
if vulnerabilityAlerts, ok := settings["vulnerability_alerts"].(bool); ok {
if repository.SecurityAndAnalysis.AdvancedSecurity == nil {
repository.SecurityAndAnalysis.AdvancedSecurity = &github.AdvancedSecurity{}
}
if vulnerabilityAlerts {
repository.SecurityAndAnalysis.AdvancedSecurity.Status = github.Ptr("enabled")
} else {
repository.SecurityAndAnalysis.AdvancedSecurity.Status = github.Ptr("disabled")
}
}
// Update other security settings
settingsJSON, err := json.Marshal(settings)
if err != nil {
return nil, fmt.Errorf("failed to marshal settings: %w", err)
}
var securitySettings github.SecurityAndAnalysis
if err := json.Unmarshal(settingsJSON, &securitySettings); err != nil {
return nil, fmt.Errorf("failed to unmarshal settings: %w", err)
}
// Merge the new settings with existing ones
if securitySettings.AdvancedSecurity != nil {
if repository.SecurityAndAnalysis.AdvancedSecurity == nil || repository.SecurityAndAnalysis.AdvancedSecurity.Status == "" {
repository.SecurityAndAnalysis.AdvancedSecurity = securitySettings.AdvancedSecurity
}
}
if securitySettings.SecretScanning != nil {
repository.SecurityAndAnalysis.SecretScanning = securitySettings.SecretScanning
}
if securitySettings.SecretScanningPushProtection != nil {
repository.SecurityAndAnalysis.SecretScanningPushProtection = securitySettings.SecretScanningPushProtection
}
// Update the repository
updatedRepo, _, err := client.Repositories.Edit(ctx, owner, repo, &github.Repository{
SecurityAndAnalysis: repository.SecurityAndAnalysis,
})
if err != nil {
return nil, fmt.Errorf("failed to update repository settings: %w", err)
}
// Return complete security settings
response, err := json.Marshal(updatedRepo.SecurityAndAnalysis)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
return mcp.NewToolResultText(string(response)), nil
}
}
// GetDependabotSecurityUpdatesStatus checks if Dependabot security updates are enabled
func GetDependabotSecurityUpdatesStatus(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("get_dependabot_security_updates_status",
mcp.WithDescription(t("TOOL_GET_DEPENDABOT_SECURITY_UPDATES_STATUS_DESCRIPTION", "Check if Dependabot security updates are enabled for a repository")),
mcp.WithString("owner",
mcp.Required(),
mcp.Description(t("PARAM_OWNER_DESCRIPTION", "Repository owner")),
),
mcp.WithString("repo",
mcp.Required(),
mcp.Description(t("PARAM_REPO_DESCRIPTION", "Repository name")),
),
), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
owner, ok := request.Params.Arguments["owner"].(string)
if !ok {
return nil, fmt.Errorf("missing required parameter: owner")
}
repo, ok := request.Params.Arguments["repo"].(string)
if !ok {
return nil, fmt.Errorf("missing required parameter: repo")
}
status, _, err := client.Repositories.GetAutomatedSecurityFixes(ctx, owner, repo)
if err != nil {
return nil, fmt.Errorf("failed to get Dependabot security updates status: %w", err)
}
response, err := json.Marshal(status)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
return mcp.NewToolResultText(string(response)), nil
}
}
// EnableDependabotSecurityUpdates and DisableDependabotSecurityUpdates are currently disabled.
// Issue: There is a discrepancy in GitHub's API behavior regarding Dependabot security updates:
// 1. Public repositories should have Dependabot alerts enabled by default
// 2. However, the API still requires explicit enabling of vulnerability alerts
// 3. This creates a confusing user experience where the system says one thing but behaves differently
// 4. The functionality needs to be investigated and fixed before being re-enabled
// See: https://github.com/github/github-mcp-server/issues/176
// EnableDependabotSecurityUpdates enables Dependabot security updates for a repository
// func EnableDependabotSecurityUpdates(client *github.Client, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) {
// return mcp.NewTool("enable_dependabot_security_updates",
// mcp.WithDescription(t("TOOL_ENABLE_DEPENDABOT_SECURITY_UPDATES_DESCRIPTION", "Enable Dependabot security updates for a repository")),
// mcp.WithString("owner",
// mcp.Required(),
// mcp.Description(t("PARAM_OWNER_DESCRIPTION", "Repository owner")),
// ),
// mcp.WithString("repo",
// mcp.Required(),
// mcp.Description(t("PARAM_REPO_DESCRIPTION", "Repository name")),
// ),
// ), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// return nil, fmt.Errorf("this functionality is currently disabled due to GitHub API behavior discrepancy")
// }
// }
// DisableDependabotSecurityUpdates disables Dependabot security updates for a repository
// func DisableDependabotSecurityUpdates(client *github.Client, t translations.TranslationHelperFunc) (mcp.Tool, server.ToolHandlerFunc) {
// return mcp.NewTool("disable_dependabot_security_updates",
// mcp.WithDescription(t("TOOL_DISABLE_DEPENDABOT_SECURITY_UPDATES_DESCRIPTION", "Disable Dependabot security updates for a repository")),
// mcp.WithString("owner",
// mcp.Required(),
// mcp.Description(t("PARAM_OWNER_DESCRIPTION", "Repository owner")),
// ),
// mcp.WithString("repo",
// mcp.Required(),
// mcp.Description(t("PARAM_REPO_DESCRIPTION", "Repository name")),
// ),
// ), func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// return nil, fmt.Errorf("this functionality is currently disabled due to GitHub API behavior discrepancy")
// }
// }