2026-05-11 22:40:22 -07:00
|
|
|
// Copyright IBM Corp. 2024, 2026
|
2024-04-02 15:47:49 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2024-09-16 14:28:54 -04:00
|
|
|
package host
|
2024-04-02 15:47:49 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2024-10-18 10:35:05 -04:00
|
|
|
"strings"
|
2024-04-02 15:47:49 -04:00
|
|
|
|
|
|
|
|
computepb "cloud.google.com/go/compute/apiv1/computepb"
|
2026-10-12 10:28:05 -07:00
|
|
|
cred "repo.anhonesthost.net/CyberCoveLLC/threshold-plugin-gcp/internal/credential"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold-plugin-gcp/internal/errors"
|
|
|
|
|
"repo.anhonesthost.net/CyberCoveLLC/threshold-plugin-gcp/internal/values"
|
2024-04-02 15:47:49 -04:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
|
|
)
|
|
|
|
|
|
2024-08-06 11:51:00 -04:00
|
|
|
// CatalogAttributes defines a set of attributes for the host catalog
|
2024-04-02 15:47:49 -04:00
|
|
|
type CatalogAttributes struct {
|
|
|
|
|
*cred.CredentialAttributes
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getCatalogAttributes(in *structpb.Struct) (*CatalogAttributes, error) {
|
|
|
|
|
unknownFields := values.StructFields(in)
|
|
|
|
|
badFields := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
credAttributes, err := cred.GetCredentialAttributes(in)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for s := range unknownFields {
|
|
|
|
|
switch s {
|
|
|
|
|
// Ignore knownFields from CredentialAttributes
|
2024-09-17 10:50:10 -04:00
|
|
|
case cred.ConstProjectId:
|
2024-04-02 15:47:49 -04:00
|
|
|
continue
|
|
|
|
|
case cred.ConstZone:
|
|
|
|
|
continue
|
2024-09-17 10:50:10 -04:00
|
|
|
case cred.ConstDisableCredentialRotation:
|
|
|
|
|
continue
|
|
|
|
|
case cred.ConstClientEmail:
|
|
|
|
|
continue
|
|
|
|
|
case cred.ConstTargetServiceAccountID:
|
|
|
|
|
continue
|
2024-04-02 15:47:49 -04:00
|
|
|
default:
|
|
|
|
|
badFields[fmt.Sprintf("attributes.%s", s)] = "unrecognized field"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(badFields) > 0 {
|
|
|
|
|
return nil, errors.InvalidArgumentError("Invalid arguments in catalog attributes", badFields)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &CatalogAttributes{
|
|
|
|
|
CredentialAttributes: credAttributes,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 11:51:00 -04:00
|
|
|
// SetAttributes defines attributes fro the host set
|
2024-04-02 15:47:49 -04:00
|
|
|
type SetAttributes struct {
|
2024-10-28 12:50:14 -04:00
|
|
|
Filters []string `mapstructure:"filters"`
|
2024-04-02 15:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSetAttributes(in *structpb.Struct) (*SetAttributes, error) {
|
|
|
|
|
var setAttrs SetAttributes
|
|
|
|
|
|
|
|
|
|
badFields := make(map[string]string)
|
|
|
|
|
unknownFields := values.StructFields(in)
|
|
|
|
|
|
|
|
|
|
delete(unknownFields, ConstListInstancesFilter)
|
|
|
|
|
|
|
|
|
|
for a := range unknownFields {
|
|
|
|
|
badFields[fmt.Sprintf("attributes.%s", a)] = "unrecognized field"
|
|
|
|
|
}
|
|
|
|
|
if len(badFields) > 0 {
|
|
|
|
|
return nil, errors.InvalidArgumentError("Error in the attributes provided", badFields)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mapstructure complains if it expects a slice as output and sees a scalar
|
|
|
|
|
// value. Rather than use WeakDecode and risk unintended consequences, I'm
|
|
|
|
|
// manually making this change if necessary.
|
|
|
|
|
inMap := in.AsMap()
|
|
|
|
|
if filtersRaw, ok := inMap[ConstListInstancesFilter]; ok {
|
|
|
|
|
switch filterVal := filtersRaw.(type) {
|
|
|
|
|
case string:
|
2024-10-18 10:35:05 -04:00
|
|
|
inMap[ConstListInstancesFilter] = []string{filterVal}
|
2024-04-02 15:47:49 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-06 11:51:00 -04:00
|
|
|
|
2024-04-02 15:47:49 -04:00
|
|
|
if err := mapstructure.Decode(inMap, &setAttrs); err != nil {
|
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "error decoding set attributes: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &setAttrs, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 12:50:14 -04:00
|
|
|
func buildListInstancesRequest(attributes *SetAttributes, catalog *CatalogAttributes) (*computepb.ListInstancesRequest, error) {
|
2024-04-02 15:47:49 -04:00
|
|
|
request := &computepb.ListInstancesRequest{
|
2024-09-17 10:50:10 -04:00
|
|
|
Project: catalog.ProjectId,
|
2024-04-02 15:47:49 -04:00
|
|
|
Zone: catalog.Zone,
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 12:50:14 -04:00
|
|
|
filters, err := buildFilters(attributes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error building filters: %w", err)
|
2024-04-02 15:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
2024-12-18 15:27:52 -05:00
|
|
|
if len(filters) > 0 {
|
2024-10-28 12:50:14 -04:00
|
|
|
filters := strings.Join(filters, " AND ")
|
2024-10-18 10:35:05 -04:00
|
|
|
request.Filter = &filters
|
2024-04-02 15:47:49 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 12:50:14 -04:00
|
|
|
return request, nil
|
2024-04-02 15:47:49 -04:00
|
|
|
}
|