Skip to content

Commit

Permalink
Provide utility method in non-internal package to retrieve providers …
Browse files Browse the repository at this point in the history
…configuration (closes #117)
  • Loading branch information
christophetd committed May 25, 2022
1 parent b9abe6e commit a044663
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 1 addition & 2 deletions examples/custom/detonate_custom_technique.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/datadog/stratus-red-team/internal/providers"
"github.com/datadog/stratus-red-team/pkg/stratus"
_ "github.com/datadog/stratus-red-team/pkg/stratus/loader" // Note: This import is needed
"github.com/datadog/stratus-red-team/pkg/stratus/mitreattack"
Expand Down Expand Up @@ -34,7 +33,7 @@ func buildCustomAttackTechnique() *stratus.AttackTechnique {

func detonate(params map[string]string) error {
iamUserName := params["iam_user_name"]
iamClient := iam.NewFromConfig(providers.AWS().GetConnection())
iamClient := iam.NewFromConfig(stratus.AWSProvider().GetConnection())

userResponse, err := iamClient.GetUser(context.Background(), &iam.GetUserInput{
UserName: &iamUserName,
Expand Down
35 changes: 35 additions & 0 deletions pkg/stratus/providers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package stratus

import (
"errors"
"github.com/datadog/stratus-red-team/internal/providers"
)

func AWSProvider() *providers.AWSProvider {
return providers.AWS()
}

func K8sProvider() *providers.K8sProvider {
return providers.K8s()
}

// EnsureAuthenticated ensures that the current user is properly authenticated against a specific platform
func EnsureAuthenticated(platform Platform) error {
switch platform {
case AWS:
if !providers.AWS().IsAuthenticatedAgainstAWS() {
return errors.New("you are not authenticated against AWS, or you have not set your region. " +
"Make sure you are authenticated against AWS, and you have a default region set in your AWS config " +
"or environment (export AWS_DEFAULT_REGION=us-east-1)")
}
case Kubernetes:
if !providers.K8s().IsAuthenticated() {
return errors.New("You do not have a kubeconfig set up, or you do not have proper permissions for " +
"this cluster. Make sure you have proper credentials set in " + providers.GetKubeConfigPath())
}
default:
return errors.New("unhandled platform " + string(platform))
}

return nil
}

0 comments on commit a044663

Please sign in to comment.