Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svidstore gcp region #5718

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/plugin_agent_svidstore_gcp_secretmanager.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ Selectors are used on `storable` entries to describe metadata that is needed by
| `gcp_secretmanager:projectid` | `gcp_secretmanager:projectid:some-project` | x | The Google Cloud project ID which the plugin will use Secret Manager |
| `gcp_secretmanager:role` | `gcp_secretmanager:role:roles/secretmanager.viewer` | - | The Google Cloud role id for IAM policy (serviceaccount required when set) |
| `gcp_secretmanager:serviceaccount` | `gcp_secretmanager:serviceaccount:[email protected]` | - | The Google Cloud Service account for IAM policy (role required when set) |
| `gcp_secretmanager:regions` | `gcp_secretmanager:serviceaccount:europe-north1,europe-west1` | - | List of Google Cloud Region to create the secret in |
40 changes: 35 additions & 5 deletions pkg/agent/plugin/svidstore/gcpsecretmanager/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ func (p *SecretManagerPlugin) PutX509SVID(ctx context.Context, req *svidstorev1.
Parent: opt.parent(),
SecretId: opt.name,
Secret: &secretmanagerpb.Secret{
Replication: &secretmanagerpb.Replication{
Replication: &secretmanagerpb.Replication_Automatic_{
Automatic: &secretmanagerpb.Replication_Automatic{},
},
},
Replication: opt.replication,
Labels: map[string]string{
"spire-svid": p.tdHash,
},
Expand Down Expand Up @@ -302,6 +298,7 @@ type secretOptions struct {
name string
roleName string
serviceAccount string
replication *secretmanagerpb.Replication
}

// parent gets parent in the format `projects/*`
Expand Down Expand Up @@ -346,11 +343,44 @@ func optionsFromSecretData(selectorData []string) (*secretOptions, error) {
return nil, status.Error(codes.InvalidArgument, "service account is required when role is set")
}

regions, ok := data["regions"]

var replica *secretmanagerpb.Replication

if !ok {
replica = &secretmanagerpb.Replication{
Replication: &secretmanagerpb.Replication_Automatic_{
Automatic: &secretmanagerpb.Replication_Automatic{},
},
}
} else {
regionsSlice := strings.Split(regions, ",")

var replicas []*secretmanagerpb.Replication_UserManaged_Replica

for _, region := range regionsSlice {
replica := &secretmanagerpb.Replication_UserManaged_Replica{
Location: region,
}

replicas = append(replicas, replica)
}

replica = &secretmanagerpb.Replication{
Replication: &secretmanagerpb.Replication_UserManaged_{
UserManaged: &secretmanagerpb.Replication_UserManaged{
Replicas: replicas,
},
},
}
}

return &secretOptions{
name: name,
projectID: projectID,
roleName: roleName,
serviceAccount: serviceAccount,
replication: replica,
}, nil
}

Expand Down
38 changes: 37 additions & 1 deletion pkg/agent/plugin/svidstore/gcpsecretmanager/gcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func TestPutX509SVID(t *testing.T) {
"federated1": {federatedBundle},
},
}

secret := &svidstore.Data{
SPIFFEID: "spiffe://example.org/foh",
X509SVID: x509CertPem,
Expand Down Expand Up @@ -460,6 +460,42 @@ func TestPutX509SVID(t *testing.T) {
getSecretErr: status.Error(codes.NotFound, "secret not found"),
},
},
{
name: "Add payload and create regional secret",
req: successReq,
expectCreateSecretReq: &secretmanagerpb.CreateSecretRequest{
Parent: "projects/project1",
SecretId: "secret1",
Secret: &secretmanagerpb.Secret{
Replication: &secretmanagerpb.Replication{
Replication: &secretmanagerpb.Replication_UserManaged_{
UserManaged: &secretmanagerpb.Replication_UserManaged{
Replicas: []*secretmanagerpb.Replication_UserManaged_Replica{
{
Location: "europe-north1",
},
},
},
},
},
Labels: map[string]string{
"spire-svid": tdHash,
},
},
},
expectGetSecretReq: &secretmanagerpb.GetSecretRequest{
Name: "projects/project1/secrets/secret1",
},
expectAddSecretVersionReq: &secretmanagerpb.AddSecretVersionRequest{
Parent: "projects/project1/secrets/secret1",
Payload: &secretmanagerpb.SecretPayload{
Data: payload,
},
},
clientConfig: &clientConfig{
getSecretErr: status.Error(codes.NotFound, "secret not found"),
},
},
{
name: "Add IAM policy when creating",
req: &svidstore.X509SVID{
Expand Down