Skip to content

Commit 95cf692

Browse files
feat(onboarding): Datasource for regulatory trusted identity (#561)
* feat(onboarding): Datasource for regulatory trusted identity Change summary: ----------------- - Adding a new datasource secure_trusted_cloud_regulation_assets with identities for onboarding regulatory workloads such as aws gov workloads. - added acc test and docs for the new datasource. * Update test assertions * Merge and use a single datasource
1 parent 24de192 commit 95cf692

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

sysdig/data_source_sysdig_secure_onboarding.go

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ func dataSourceSysdigSecureTrustedCloudIdentity() *schema.Resource {
5454
Type: schema.TypeString,
5555
Computed: true,
5656
},
57+
"gov_identity": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
},
61+
"aws_gov_account_id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
},
65+
"aws_gov_role_name": {
66+
Type: schema.TypeString,
67+
Computed: true,
68+
},
5769
},
5870
}
5971
}
@@ -65,18 +77,55 @@ func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *sche
6577
return diag.FromErr(err)
6678
}
6779

80+
// get trusted identity for commercial backend
6881
identity, err := client.GetTrustedCloudIdentitySecure(ctx, d.Get("cloud_provider").(string))
6982
if err != nil {
7083
return diag.FromErr(err)
7184
}
7285

86+
// get trusted identity for regulatory backend, such as govcloud
87+
// XXX: only supported for aws currently. update when supported for other providers
88+
var trustedRegulation map[string]string
89+
if d.Get("cloud_provider").(string) == "aws" {
90+
trustedRegulation, err = client.GetTrustedCloudRegulationAssetsSecure(ctx, d.Get("cloud_provider").(string))
91+
if err != nil {
92+
return diag.FromErr(err)
93+
}
94+
}
95+
7396
d.SetId(identity)
74-
_ = d.Set("identity", identity)
7597

7698
provider := d.Get("cloud_provider")
7799
switch provider {
78-
case "aws", "gcp":
79-
// If identity is an ARN, attempt to extract certain fields
100+
case "aws":
101+
// set the commercial identity
102+
_ = d.Set("identity", identity)
103+
// if identity is an ARN, attempt to extract certain fields
104+
parsedArn, err := arn.Parse(identity)
105+
if err == nil {
106+
_ = d.Set("aws_account_id", parsedArn.AccountID)
107+
if parsedArn.Service == "iam" && strings.HasPrefix(parsedArn.Resource, "role/") {
108+
_ = d.Set("aws_role_name", strings.TrimPrefix(parsedArn.Resource, "role/"))
109+
}
110+
}
111+
112+
// set the gov regulation based identity (only supported for aws currently)
113+
err = d.Set("gov_identity", trustedRegulation["trustedIdentityGov"])
114+
if err != nil {
115+
return diag.FromErr(err)
116+
}
117+
// if identity is an ARN, attempt to extract certain fields
118+
parsedArn, err = arn.Parse(trustedRegulation["trustedIdentityGov"])
119+
if err == nil {
120+
_ = d.Set("aws_gov_account_id", parsedArn.AccountID)
121+
if parsedArn.Service == "iam" && strings.HasPrefix(parsedArn.Resource, "role/") {
122+
_ = d.Set("aws_gov_role_name", strings.TrimPrefix(parsedArn.Resource, "role/"))
123+
}
124+
}
125+
case "gcp":
126+
// set the commercial identity
127+
_ = d.Set("identity", identity)
128+
// if identity is an ARN, attempt to extract certain fields
80129
parsedArn, err := arn.Parse(identity)
81130
if err == nil {
82131
_ = d.Set("aws_account_id", parsedArn.AccountID)
@@ -85,7 +134,9 @@ func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *sche
85134
}
86135
}
87136
case "azure":
88-
// If identity is an Azure tenantID/clientID, separate into each part
137+
// set the commercial identity
138+
_ = d.Set("identity", identity)
139+
// if identity is an Azure tenantID/clientID, separate into each part
89140
tenantID, spID, err := parseAzureCreds(identity)
90141
if err == nil {
91142
_ = d.Set("azure_tenant_id", tenantID)

sysdig/data_source_sysdig_secure_onboarding_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ func TestAccTrustedCloudIdentityDataSource(t *testing.T) {
2626
},
2727
},
2828
Steps: []resource.TestStep{
29+
{
30+
Config: `data "sysdig_secure_trusted_cloud_identity" "trusted_identity" { cloud_provider = "invalid" }`,
31+
ExpectError: regexp.MustCompile(`.*expected cloud_provider to be one of.*`),
32+
},
2933
{
3034
Config: `data "sysdig_secure_trusted_cloud_identity" "trusted_identity" { cloud_provider = "aws" }`,
3135
Check: resource.ComposeTestCheckFunc(
3236
resource.TestCheckResourceAttr("data.sysdig_secure_trusted_cloud_identity.trusted_identity", "cloud_provider", "aws"),
3337
resource.TestCheckResourceAttrSet("data.sysdig_secure_trusted_cloud_identity.trusted_identity", "aws_account_id"),
3438
resource.TestCheckResourceAttrSet("data.sysdig_secure_trusted_cloud_identity.trusted_identity", "aws_role_name"),
39+
// not asserting the gov exported fields because not every backend environment is gov supported and will have non-empty values returned
3540
),
3641
},
3742
{

sysdig/internal/client/v2/onboarding.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const (
1212
onboardingTenantExternaIDPath = "%s/api/secure/onboarding/v2/externalID"
1313
onboardingAgentlessScanningAssetsPath = "%s/api/secure/onboarding/v2/agentlessScanningAssets"
1414
onboardingCloudIngestionAssetsPath = "%s/api/secure/onboarding/v2/cloudIngestionAssets"
15+
onboardingTrustedRegulationAssetsPath = "%s/api/secure/onboarding/v2/trustedRegulationAssets?provider=%s"
1516
)
1617

1718
type OnboardingSecureInterface interface {
@@ -21,6 +22,7 @@ type OnboardingSecureInterface interface {
2122
GetTenantExternalIDSecure(ctx context.Context) (string, error)
2223
GetAgentlessScanningAssetsSecure(ctx context.Context) (map[string]any, error)
2324
GetCloudIngestionAssetsSecure(ctx context.Context) (map[string]any, error)
25+
GetTrustedCloudRegulationAssetsSecure(ctx context.Context, provider string) (map[string]string, error)
2426
}
2527

2628
func (client *Client) GetTrustedCloudIdentitySecure(ctx context.Context, provider string) (string, error) {
@@ -92,3 +94,17 @@ func (client *Client) GetCloudIngestionAssetsSecure(ctx context.Context) (map[st
9294

9395
return Unmarshal[map[string]interface{}](response.Body)
9496
}
97+
98+
func (client *Client) GetTrustedCloudRegulationAssetsSecure(ctx context.Context, provider string) (map[string]string, error) {
99+
response, err := client.requester.Request(ctx, http.MethodGet, fmt.Sprintf(onboardingTrustedRegulationAssetsPath, client.config.url, provider), nil)
100+
if err != nil {
101+
return nil, err
102+
}
103+
defer response.Body.Close()
104+
105+
if response.StatusCode != http.StatusOK {
106+
return nil, client.ErrorFromResponse(response)
107+
}
108+
109+
return Unmarshal[map[string]string](response.Body)
110+
}

website/docs/d/secure_trusted_cloud_identity.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ In addition to all arguments above, the following attributes are exported:
3939

4040
* `azure_service_principal_id` - If `identity` contains credentials for an Azure Service Principal, this attribute contains the service principal's ID. `cloud_provider` must be equal to `azure`.
4141

42+
* `gov_identity` - Sysdig's identity for regulatory workloads (User/Role/etc) that should be used to create a trust relationship allowing Sysdig access to your regulated cloud account. Currently supported on `aws`.
43+
44+
* `aws_gov_account_id` - If `gov_identity` is an AWS GOV IAM Role ARN, this attribute contains the AWS GOV Account ID to which the ARN belongs, otherwise it contains the empty string. Currently supported on `aws`.
45+
46+
* `aws_gov_role_name` - If `gov_identity` is a AWS GOV IAM Role ARN, this attribute contains the name of the GOV role, otherwise it contains the empty string. Currently supported on `aws`.

0 commit comments

Comments
 (0)