go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/id/awsec2/awsec2.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package awsec2
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/aws/aws-sdk-go-v2/config"
    10  	"github.com/cockroachdb/errors"
    11  	"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
    12  	"go.mondoo.com/cnquery/providers/os/connection"
    13  	"go.mondoo.com/cnquery/providers/os/connection/mock"
    14  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    15  )
    16  
    17  type Identity struct {
    18  	InstanceID   string
    19  	InstanceName string
    20  	AccountID    string
    21  }
    22  type InstanceIdentifier interface {
    23  	Identify() (Identity, error)
    24  }
    25  
    26  func Resolve(conn shared.Connection, pf *inventory.Platform) (InstanceIdentifier, error) {
    27  	cfg, err := config.LoadDefaultConfig(context.Background())
    28  	if err != nil {
    29  		// for local environments we must have a config, or it won't work
    30  		if conn.Type() == connection.Local {
    31  			return nil, errors.Wrap(err, "cannot not determine AWS environment")
    32  		}
    33  
    34  		// over a remote connection, we can try without the config
    35  		return NewCommandInstanceMetadata(conn, pf, nil), nil
    36  	}
    37  
    38  	if conn.Type() == connection.Local {
    39  		// TODO: Dom: Since a mocked local is not considered local in the original
    40  		// code, we are not testing this code path. Also the original only had
    41  		// mock and non-mock, where the v9 plugin system introduces hybrid modes.
    42  		// We have to revisit this part of the code...
    43  		if _, ok := conn.(*mock.Connection); !ok {
    44  			return NewLocal(cfg), nil
    45  		}
    46  	}
    47  	return NewCommandInstanceMetadata(conn, pf, &cfg), nil
    48  }