Skip to content

Commit 1edcda1

Browse files
iruAlex
andauthored
Parse Azure trusted identity-internal (#134)
* add azure to valid benchmark schemas Co-authored-by: Alex <alex.qiu@sysdig.com>
1 parent b748e91 commit 1edcda1

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

.pre-commit-config.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: fmt
5+
pass_filenames: false
6+
name: fmt
7+
entry: make fmt
8+
language: system
9+
- id: fmtcheck
10+
pass_filenames: false
11+
name: fmtcheck
12+
entry: make fmtcheck
13+
language: system
14+
- id: lint
15+
pass_filenames: false
16+
name: lint
17+
entry: make lint
18+
language: system

GNUmakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ fmt:
3636
fmtcheck:
3737
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
3838

39+
lint:
40+
golangci-lint run --timeout 1h ./...
41+
3942
errcheck:
4043
@sh -c "'$(CURDIR)/scripts/errcheck.sh'"
4144

sysdig/data_source_sysdig_secure_trusted_cloud_identity.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,25 @@ func dataSourceSysdigSecureTrustedCloudIdentityRead(ctx context.Context, d *sche
5858
d.SetId(identity)
5959
d.Set("identity", identity)
6060

61-
// If identity is an ARN, attempt to extract certain fields
62-
parsedArn, err := arn.Parse(identity)
63-
if err == nil {
64-
d.Set("aws_account_id", parsedArn.AccountID)
61+
provider := d.Get("cloud_provider")
62+
switch provider {
63+
case "aws", "gcp":
64+
// If identity is an ARN, attempt to extract certain fields
65+
parsedArn, err := arn.Parse(identity)
66+
if err == nil {
67+
d.Set("aws_account_id", parsedArn.AccountID)
6568

66-
if parsedArn.Service == "iam" && strings.HasPrefix(parsedArn.Resource, "role/") {
67-
d.Set("aws_role_name", strings.TrimPrefix(parsedArn.Resource, "role/"))
69+
if parsedArn.Service == "iam" && strings.HasPrefix(parsedArn.Resource, "role/") {
70+
d.Set("aws_role_name", strings.TrimPrefix(parsedArn.Resource, "role/"))
71+
}
72+
}
73+
case "azure":
74+
// If identity is an Azure tenantID/clientID, separate into each part
75+
tenantID, clientID, err := parseAzureCreds(identity)
76+
if err == nil {
77+
d.Set("azure_tenant_id", tenantID)
78+
d.Set("azure_client_id", clientID)
6879
}
6980
}
70-
7181
return nil
7282
}

sysdig/helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package sysdig
22

33
import (
4+
"errors"
45
"fmt"
6+
"strings"
57

68
"github.com/hashicorp/go-cty/cty"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -31,3 +33,12 @@ func validateDiagFunc(validateFunc func(interface{}, string) ([]string, []error)
3133
return diags
3234
}
3335
}
36+
37+
// parseAzureCreds splits an Azure Trusted Identity into a tenantID and a clientID
38+
func parseAzureCreds(azureTrustedIdentity string) (tenantID string, clientID string, err error) {
39+
tokens := strings.Split(azureTrustedIdentity, ":")
40+
if len(tokens) != 2 {
41+
return "", "", errors.New("Not a valid Azure Trusted Identity")
42+
}
43+
return tokens[0], tokens[1], nil
44+
}

0 commit comments

Comments
 (0)