Skip to content

Commit a8a34c2

Browse files
authored
Add subscription_policy column in table azure_tenant close #956 (#957)
1 parent a27c4c9 commit a8a34c2

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

azure/table_azure_tenant.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/Azure/azure-sdk-for-go/profiles/latest/resources/mgmt/subscriptions"
7+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
78
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
89
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
910
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
@@ -62,13 +63,20 @@ func tableAzureTenant(_ context.Context) *plugin.Table {
6263
Type: proto.ColumnType_STRING,
6364
Description: "The list of domains for the tenant.",
6465
},
65-
{
66-
Name: "domains",
67-
Type: proto.ColumnType_JSON,
68-
Description: "The list of domains for the tenant.",
69-
},
66+
{
67+
Name: "domains",
68+
Type: proto.ColumnType_JSON,
69+
Description: "The list of domains for the tenant.",
70+
},
71+
{
72+
Name: "subscription_policy",
73+
Type: proto.ColumnType_JSON,
74+
Description: "The subscription policy for the tenant, including properties like BlockSubscriptionsLeavingTenant, BlockSubscriptionsIntoTenant, and ExemptedPrincipals.",
75+
Hydrate: getTenantSubscriptionPolicy,
76+
Transform: transform.FromValue(),
77+
},
7078

71-
// Steampipe standard columns
79+
// Steampipe standard columns
7280
{
7381
Name: "title",
7482
Description: ColumnDescriptionTitle,
@@ -111,6 +119,33 @@ func listTenants(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData
111119
return nil, nil
112120
}
113121

122+
//// HYDRATE FUNCTION
123+
124+
func getTenantSubscriptionPolicy(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
125+
// Get the session with credentials
126+
session, err := GetNewSessionUpdated(ctx, d)
127+
if err != nil {
128+
plugin.Logger(ctx).Error("azure_tenant.getTenantSubscriptionPolicy", "session_error", err)
129+
return nil, err
130+
}
131+
132+
// Create the policy client
133+
client, err := armsubscription.NewPolicyClient(session.Cred, session.ClientOptions)
134+
if err != nil {
135+
plugin.Logger(ctx).Error("azure_tenant.getTenantSubscriptionPolicy", "client_error", err)
136+
return nil, err
137+
}
138+
139+
// Get the tenant policy
140+
result, err := client.GetPolicyForTenant(ctx, nil)
141+
if err != nil {
142+
plugin.Logger(ctx).Error("azure_tenant.getTenantSubscriptionPolicy", "api_error", err)
143+
return nil, err
144+
}
145+
146+
return result.GetTenantPolicyResponse, nil
147+
}
148+
114149
//// TRANSFORM FUNCTION
115150

116151
func getNameOrID(ctx context.Context, d *transform.TransformData) (interface{}, error) {

docs/tables/azure_tenant.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,31 @@ select
4343
domains
4444
from
4545
azure_tenant;
46+
```
47+
48+
### Get subscription policy settings for tenants
49+
Retrieve the subscription policy configuration to understand restrictions on subscription transfers. This helps identify whether subscriptions are blocked from leaving or entering the tenant, and which principals are exempted from these policies.
50+
51+
```sql+postgres
52+
select
53+
tenant_id,
54+
display_name,
55+
subscription_policy ->> 'id' as policy_id,
56+
subscription_policy -> 'properties' ->> 'blockSubscriptionsLeavingTenant' as block_leaving_tenant,
57+
subscription_policy -> 'properties' ->> 'blockSubscriptionsIntoTenant' as block_into_tenant,
58+
subscription_policy -> 'properties' -> 'exemptedPrincipals' as exempted_principals
59+
from
60+
azure_tenant;
61+
```
62+
63+
```sql+sqlite
64+
select
65+
tenant_id,
66+
display_name,
67+
json_extract(subscription_policy, '$.id') as policy_id,
68+
json_extract(subscription_policy, '$.properties.blockSubscriptionsLeavingTenant') as block_leaving_tenant,
69+
json_extract(subscription_policy, '$.properties.blockSubscriptionsIntoTenant') as block_into_tenant,
70+
json_extract(subscription_policy, '$.properties.exemptedPrincipals') as exempted_principals
71+
from
72+
azure_tenant;
4673
```

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/security/armsecurity v0.14.0
2525
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/sql/armsql v1.2.0
2626
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.5.0
27+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.2.0
2728
github.com/Azure/azure-storage-blob-go v0.12.0
2829
github.com/Azure/go-autorest/autorest v0.11.17
2930
github.com/Azure/go-autorest/autorest/azure/auth v0.5.6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/sql/armsql v1.2.0 h1:S087d
655655
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/sql/armsql v1.2.0/go.mod h1:B4cEyXrWBmbfMDAPnpJ1di7MAt5DKP57jPEObAvZChg=
656656
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.5.0 h1:AifHbc4mg0x9zW52WOpKbsHaDKuRhlI7TVl47thgQ70=
657657
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.5.0/go.mod h1:T5RfihdXtBDxt1Ch2wobif3TvzTdumDy29kahv6AV9A=
658+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.2.0 h1:UrGzkHueDwAWDdjQxC+QaXHd4tVCkISYE9j7fSSXF8k=
659+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription v1.2.0/go.mod h1:qskvSQeW+cxEE2bcKYyKimB1/KiQ9xpJ99bcHY0BX6c=
658660
github.com/Azure/azure-storage-blob-go v0.12.0 h1:7bFXA1QB+lOK2/ASWHhp6/vnxjaeeZq6t8w1Jyp0Iaw=
659661
github.com/Azure/azure-storage-blob-go v0.12.0/go.mod h1:A0u4VjtpgZJ7Y7um/+ix2DHBuEKFC6sEIlj0xc13a4Q=
660662
github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=

0 commit comments

Comments
 (0)