import (
"context"
"fmt"
"io"
securitycenter "cloud.google.com/go/securitycenter/apiv1"
securitycenterpb "google.golang.org/genproto/googleapis/cloud/securitycenter/v1"
"google.golang.org/genproto/protobuf/field_mask"
)
// addSecurityMarks adds/updates the security marks for the assetName.
// Specifically, it sets "key_a" and "key_b" to "value_a" and "value_b"
// respectively. assetName is the resource path for an asset.
func addSecurityMarks(w io.Writer, assetName string) error {
// assetName := "organizations/123123342/assets/12312321"
// Instantiate a context and a security service client to make API calls.
ctx := context.Background()
client, err := securitycenter.NewClient(ctx)
if err != nil {
return fmt.Errorf("securitycenter.NewClient: %v", err)
}
defer client.Close() // Closing the client safely cleans up background resources.
req := &securitycenterpb.UpdateSecurityMarksRequest{
// If not set or empty, all marks would be cleared before
// adding the new marks below.
UpdateMask: &field_mask.FieldMask{
Paths: []string{"marks.key_a", "marks.key_b"},
},
SecurityMarks: &securitycenterpb.SecurityMarks{
Name: fmt.Sprintf("%s/securityMarks", assetName),
// Note keys correspond to the last part of each path.
Marks: map[string]string{"key_a": "value_a", "key_b": "value_b"},
},
}
updatedMarks, err := client.UpdateSecurityMarks(ctx, req)
if err != nil {
return fmt.Errorf("UpdateSecurityMarks: %v", err)
}
fmt.Fprintf(w, "Updated marks: %s\n", updatedMarks.Name)
for k, v := range updatedMarks.Marks {
fmt.Fprintf(w, "%s = %s\n", k, v)
}
return nil
}
// Imports the Google Cloud client library.
const {SecurityCenterClient} = require('@google-cloud/security-center');
// Creates a new client.
const client = new SecurityCenterClient();
async function addSecurityMarks() {
// assetName is the full resource path for the asset to update.
/*
* TODO(developer): Uncomment the following lines
*/
// const assetName = "organizations/123123342/assets/12312321";
const [newMarks] = await client.updateSecurityMarks({
securityMarks: {
name: `${assetName}/securityMarks`,
marks: {key_a: 'value_a', key_b: 'value_b'},
},
// Only update the marks with these keys.
updateMask: {paths: ['marks.key_a', 'marks.key_b']},
});
console.log('New marks: %', newMarks);
}
addSecurityMarks();
from google.cloud import securitycenter
from google.protobuf import field_mask_pb2
# Create a new client.
client = securitycenter.SecurityCenterClient()
# asset_name is the resource path for an asset that exists in CSCC.
# Its format is "organization/{organization_id}/assets/{asset_id}
# e.g.:
# asset_name = organizations/123123342/assets/12312321
marks_name = "{}/securityMarks".format(asset_name)
# Notice the suffix after "marks." in the field mask matches the keys
# in marks.
field_mask = field_mask_pb2.FieldMask(paths=["marks.key_a", "marks.key_b"])
marks = {"key_a": "value_a", "key_b": "value_b"}
updated_marks = client.update_security_marks(
request={
"security_marks": {"name": marks_name, "marks": marks},
"update_mask": field_mask,
}
)
print(updated_marks)