Skip to content

Commit e7656cd

Browse files
yeshwanth1993Yeshwanth Gunasekaran
andauthored
Validate and throw an error if wrong SA format is given (#71)
* Validate and throw an error if wrong SA format is given * Fix SA name --------- Co-authored-by: Yeshwanth Gunasekaran <yesh@google.com>
1 parent 39d79e9 commit e7656cd

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

devops-mcp-server/cloudbuild/cloudbuild.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package cloudbuild
1717
import (
1818
"context"
1919
"fmt"
20+
"regexp"
2021
"strings"
2122

2223
"github.com/modelcontextprotocol/go-sdk/mcp"
@@ -85,7 +86,7 @@ type CreateTriggerArgs struct {
8586
Location string `json:"location" jsonschema:"The Google Cloud location for the trigger."`
8687
TriggerID string `json:"trigger_id" jsonschema:"The ID of the trigger."`
8788
RepoLink string `json:"repo_link" jsonschema:"The Developer Connect repository link, use dev connect setup repo to create a connect and repo link"`
88-
ServiceAccount string `json:"service_account" jsonschema:"The service account to use for the build. E.g. serviceAccount:123-compute@developer.gserviceaccount.com"`
89+
ServiceAccount string `json:"service_account,omitempty" jsonschema:"The service account to use for the build. E.g. serviceAccount:name@project-id.iam.gserviceaccount.com optional"`
8990
Branch string `json:"branch,omitempty" jsonschema:"Create builds on push to branch. Should be regex e.g. '^main$'"`
9091
Tag string `json:"tag,omitempty" jsonschema:"Create builds on new tag push. Should be regex e.g. '^nightly$'"`
9192
}
@@ -94,6 +95,9 @@ var createTriggerToolFunc func(ctx context.Context, req *mcp.CallToolRequest, ar
9495

9596
func addCreateTriggerTool(server *mcp.Server, cbClient cloudbuildclient.CloudBuildClient, iamClient iamclient.IAMClient, rmClient resourcemanagerclient.ResourcemanagerClient) {
9697
createTriggerToolFunc = func(ctx context.Context, req *mcp.CallToolRequest, args CreateTriggerArgs) (*mcp.CallToolResult, any, error) {
98+
if args.ServiceAccount != "" && !IsValidServiceAccount(args.ServiceAccount) {
99+
return &mcp.CallToolResult{}, nil, fmt.Errorf("service account needs to be of the form serviceAccount:name@project-id.iam.gserviceaccount.com")
100+
}
97101
resolvedSA, err := setPermissionsForCloudBuildSA(ctx, args.ProjectID, args.ServiceAccount, rmClient, iamClient)
98102
if err != nil {
99103
return &mcp.CallToolResult{}, nil, fmt.Errorf("failed to grant necessary permissions for the Cloud build service account: %w", err)
@@ -133,3 +137,9 @@ func setPermissionsForCloudBuildSA(ctx context.Context, projectID, serviceAccoun
133137
}
134138
return resolvedSA, nil
135139
}
140+
141+
// IsValidServiceAccount checks if the string follows the specific GCP service account format.
142+
func IsValidServiceAccount(sa string) bool {
143+
var saRegex = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])@[a-z0-9-]+\.iam\.gserviceaccount\.com$`)
144+
return saRegex.MatchString(sa)
145+
}

0 commit comments

Comments
 (0)