|
| 1 | +package manual |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v9" |
| 8 | + "github.com/overmindtech/cli/go/discovery" |
| 9 | + "github.com/overmindtech/cli/go/sdp-go" |
| 10 | + "github.com/overmindtech/cli/go/sdpcache" |
| 11 | + "github.com/overmindtech/cli/sources" |
| 12 | + "github.com/overmindtech/cli/sources/azure/clients" |
| 13 | + azureshared "github.com/overmindtech/cli/sources/azure/shared" |
| 14 | + "github.com/overmindtech/cli/sources/shared" |
| 15 | + "github.com/overmindtech/cli/sources/stdlib" |
| 16 | +) |
| 17 | + |
| 18 | +var NetworkDefaultSecurityRuleLookupByUniqueAttr = shared.NewItemTypeLookup("uniqueAttr", azureshared.NetworkDefaultSecurityRule) |
| 19 | + |
| 20 | +type networkDefaultSecurityRuleWrapper struct { |
| 21 | + client clients.DefaultSecurityRulesClient |
| 22 | + *azureshared.MultiResourceGroupBase |
| 23 | +} |
| 24 | + |
| 25 | +// NewNetworkDefaultSecurityRule creates a new networkDefaultSecurityRuleWrapper instance (SearchableWrapper: child of network security group). |
| 26 | +func NewNetworkDefaultSecurityRule(client clients.DefaultSecurityRulesClient, resourceGroupScopes []azureshared.ResourceGroupScope) sources.SearchableWrapper { |
| 27 | + return &networkDefaultSecurityRuleWrapper{ |
| 28 | + client: client, |
| 29 | + MultiResourceGroupBase: azureshared.NewMultiResourceGroupBase( |
| 30 | + resourceGroupScopes, |
| 31 | + sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK, |
| 32 | + azureshared.NetworkDefaultSecurityRule, |
| 33 | + ), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (n networkDefaultSecurityRuleWrapper) Get(ctx context.Context, scope string, queryParts ...string) (*sdp.Item, *sdp.QueryError) { |
| 38 | + if len(queryParts) < 2 { |
| 39 | + return nil, &sdp.QueryError{ |
| 40 | + ErrorType: sdp.QueryError_OTHER, |
| 41 | + ErrorString: "Get requires 2 query parts: networkSecurityGroupName and defaultSecurityRuleName", |
| 42 | + Scope: scope, |
| 43 | + ItemType: n.Type(), |
| 44 | + } |
| 45 | + } |
| 46 | + nsgName := queryParts[0] |
| 47 | + ruleName := queryParts[1] |
| 48 | + if ruleName == "" { |
| 49 | + return nil, azureshared.QueryError(errors.New("default security rule name cannot be empty"), scope, n.Type()) |
| 50 | + } |
| 51 | + |
| 52 | + rgScope, err := n.ResourceGroupScopeFromScope(scope) |
| 53 | + if err != nil { |
| 54 | + return nil, azureshared.QueryError(err, scope, n.Type()) |
| 55 | + } |
| 56 | + resp, err := n.client.Get(ctx, rgScope.ResourceGroup, nsgName, ruleName, nil) |
| 57 | + if err != nil { |
| 58 | + return nil, azureshared.QueryError(err, scope, n.Type()) |
| 59 | + } |
| 60 | + |
| 61 | + return n.azureDefaultSecurityRuleToSDPItem(&resp.SecurityRule, nsgName, ruleName, scope) |
| 62 | +} |
| 63 | + |
| 64 | +func (n networkDefaultSecurityRuleWrapper) GetLookups() sources.ItemTypeLookups { |
| 65 | + return sources.ItemTypeLookups{ |
| 66 | + NetworkNetworkSecurityGroupLookupByName, |
| 67 | + NetworkDefaultSecurityRuleLookupByUniqueAttr, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (n networkDefaultSecurityRuleWrapper) Search(ctx context.Context, scope string, queryParts ...string) ([]*sdp.Item, *sdp.QueryError) { |
| 72 | + if len(queryParts) < 1 { |
| 73 | + return nil, &sdp.QueryError{ |
| 74 | + ErrorType: sdp.QueryError_OTHER, |
| 75 | + ErrorString: "Search requires 1 query part: networkSecurityGroupName", |
| 76 | + Scope: scope, |
| 77 | + ItemType: n.Type(), |
| 78 | + } |
| 79 | + } |
| 80 | + nsgName := queryParts[0] |
| 81 | + |
| 82 | + rgScope, err := n.ResourceGroupScopeFromScope(scope) |
| 83 | + if err != nil { |
| 84 | + return nil, azureshared.QueryError(err, scope, n.Type()) |
| 85 | + } |
| 86 | + pager := n.client.NewListPager(rgScope.ResourceGroup, nsgName, nil) |
| 87 | + |
| 88 | + var items []*sdp.Item |
| 89 | + for pager.More() { |
| 90 | + page, err := pager.NextPage(ctx) |
| 91 | + if err != nil { |
| 92 | + return nil, azureshared.QueryError(err, scope, n.Type()) |
| 93 | + } |
| 94 | + for _, rule := range page.Value { |
| 95 | + if rule == nil || rule.Name == nil { |
| 96 | + continue |
| 97 | + } |
| 98 | + item, sdpErr := n.azureDefaultSecurityRuleToSDPItem(rule, nsgName, *rule.Name, scope) |
| 99 | + if sdpErr != nil { |
| 100 | + return nil, sdpErr |
| 101 | + } |
| 102 | + items = append(items, item) |
| 103 | + } |
| 104 | + } |
| 105 | + return items, nil |
| 106 | +} |
| 107 | + |
| 108 | +func (n networkDefaultSecurityRuleWrapper) SearchStream(ctx context.Context, stream discovery.QueryResultStream, cache sdpcache.Cache, cacheKey sdpcache.CacheKey, scope string, queryParts ...string) { |
| 109 | + if len(queryParts) < 1 { |
| 110 | + stream.SendError(azureshared.QueryError(errors.New("Search requires 1 query part: networkSecurityGroupName"), scope, n.Type())) |
| 111 | + return |
| 112 | + } |
| 113 | + nsgName := queryParts[0] |
| 114 | + |
| 115 | + rgScope, err := n.ResourceGroupScopeFromScope(scope) |
| 116 | + if err != nil { |
| 117 | + stream.SendError(azureshared.QueryError(err, scope, n.Type())) |
| 118 | + return |
| 119 | + } |
| 120 | + pager := n.client.NewListPager(rgScope.ResourceGroup, nsgName, nil) |
| 121 | + for pager.More() { |
| 122 | + page, err := pager.NextPage(ctx) |
| 123 | + if err != nil { |
| 124 | + stream.SendError(azureshared.QueryError(err, scope, n.Type())) |
| 125 | + return |
| 126 | + } |
| 127 | + for _, rule := range page.Value { |
| 128 | + if rule == nil || rule.Name == nil { |
| 129 | + continue |
| 130 | + } |
| 131 | + item, sdpErr := n.azureDefaultSecurityRuleToSDPItem(rule, nsgName, *rule.Name, scope) |
| 132 | + if sdpErr != nil { |
| 133 | + stream.SendError(sdpErr) |
| 134 | + continue |
| 135 | + } |
| 136 | + cache.StoreItem(ctx, item, shared.DefaultCacheDuration, cacheKey) |
| 137 | + stream.SendItem(item) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (n networkDefaultSecurityRuleWrapper) SearchLookups() []sources.ItemTypeLookups { |
| 143 | + return []sources.ItemTypeLookups{ |
| 144 | + {NetworkNetworkSecurityGroupLookupByName}, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func (n networkDefaultSecurityRuleWrapper) azureDefaultSecurityRuleToSDPItem(rule *armnetwork.SecurityRule, nsgName, ruleName, scope string) (*sdp.Item, *sdp.QueryError) { |
| 149 | + attributes, err := shared.ToAttributesWithExclude(rule, "tags") |
| 150 | + if err != nil { |
| 151 | + return nil, azureshared.QueryError(err, scope, n.Type()) |
| 152 | + } |
| 153 | + |
| 154 | + err = attributes.Set("uniqueAttr", shared.CompositeLookupKey(nsgName, ruleName)) |
| 155 | + if err != nil { |
| 156 | + return nil, azureshared.QueryError(err, scope, n.Type()) |
| 157 | + } |
| 158 | + |
| 159 | + sdpItem := &sdp.Item{ |
| 160 | + Type: azureshared.NetworkDefaultSecurityRule.String(), |
| 161 | + UniqueAttribute: "uniqueAttr", |
| 162 | + Attributes: attributes, |
| 163 | + Scope: scope, |
| 164 | + } |
| 165 | + |
| 166 | + // Link to parent Network Security Group |
| 167 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 168 | + Query: &sdp.Query{ |
| 169 | + Type: azureshared.NetworkNetworkSecurityGroup.String(), |
| 170 | + Method: sdp.QueryMethod_GET, |
| 171 | + Query: nsgName, |
| 172 | + Scope: scope, |
| 173 | + }, |
| 174 | + }) |
| 175 | + |
| 176 | + if rule.Properties != nil { |
| 177 | + // Link to SourceApplicationSecurityGroups |
| 178 | + if rule.Properties.SourceApplicationSecurityGroups != nil { |
| 179 | + for _, asgRef := range rule.Properties.SourceApplicationSecurityGroups { |
| 180 | + if asgRef != nil && asgRef.ID != nil { |
| 181 | + asgName := azureshared.ExtractResourceName(*asgRef.ID) |
| 182 | + if asgName != "" { |
| 183 | + linkScope := scope |
| 184 | + if extractedScope := azureshared.ExtractScopeFromResourceID(*asgRef.ID); extractedScope != "" { |
| 185 | + linkScope = extractedScope |
| 186 | + } |
| 187 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 188 | + Query: &sdp.Query{ |
| 189 | + Type: azureshared.NetworkApplicationSecurityGroup.String(), |
| 190 | + Method: sdp.QueryMethod_GET, |
| 191 | + Query: asgName, |
| 192 | + Scope: linkScope, |
| 193 | + }, |
| 194 | + }) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // Link to DestinationApplicationSecurityGroups |
| 201 | + if rule.Properties.DestinationApplicationSecurityGroups != nil { |
| 202 | + for _, asgRef := range rule.Properties.DestinationApplicationSecurityGroups { |
| 203 | + if asgRef != nil && asgRef.ID != nil { |
| 204 | + asgName := azureshared.ExtractResourceName(*asgRef.ID) |
| 205 | + if asgName != "" { |
| 206 | + linkScope := scope |
| 207 | + if extractedScope := azureshared.ExtractScopeFromResourceID(*asgRef.ID); extractedScope != "" { |
| 208 | + linkScope = extractedScope |
| 209 | + } |
| 210 | + sdpItem.LinkedItemQueries = append(sdpItem.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 211 | + Query: &sdp.Query{ |
| 212 | + Type: azureshared.NetworkApplicationSecurityGroup.String(), |
| 213 | + Method: sdp.QueryMethod_GET, |
| 214 | + Query: asgName, |
| 215 | + Scope: linkScope, |
| 216 | + }, |
| 217 | + }) |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + // Link to stdlib.NetworkIP for source/destination address prefixes when they are IPs or CIDRs |
| 224 | + if rule.Properties.SourceAddressPrefix != nil { |
| 225 | + appendIPOrCIDRLinkIfValid(&sdpItem.LinkedItemQueries, *rule.Properties.SourceAddressPrefix) |
| 226 | + } |
| 227 | + for _, p := range rule.Properties.SourceAddressPrefixes { |
| 228 | + if p != nil { |
| 229 | + appendIPOrCIDRLinkIfValid(&sdpItem.LinkedItemQueries, *p) |
| 230 | + } |
| 231 | + } |
| 232 | + if rule.Properties.DestinationAddressPrefix != nil { |
| 233 | + appendIPOrCIDRLinkIfValid(&sdpItem.LinkedItemQueries, *rule.Properties.DestinationAddressPrefix) |
| 234 | + } |
| 235 | + for _, p := range rule.Properties.DestinationAddressPrefixes { |
| 236 | + if p != nil { |
| 237 | + appendIPOrCIDRLinkIfValid(&sdpItem.LinkedItemQueries, *p) |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + return sdpItem, nil |
| 243 | +} |
| 244 | + |
| 245 | +func (n networkDefaultSecurityRuleWrapper) PotentialLinks() map[shared.ItemType]bool { |
| 246 | + return shared.NewItemTypesSet( |
| 247 | + azureshared.NetworkNetworkSecurityGroup, |
| 248 | + azureshared.NetworkApplicationSecurityGroup, |
| 249 | + stdlib.NetworkIP, |
| 250 | + ) |
| 251 | +} |
| 252 | + |
| 253 | +// ref: https://learn.microsoft.com/en-us/rest/api/virtualnetwork/network-security-groups/get#defaultsecurityrules |
| 254 | +func (n networkDefaultSecurityRuleWrapper) IAMPermissions() []string { |
| 255 | + return []string{ |
| 256 | + "Microsoft.Network/networkSecurityGroups/defaultSecurityRules/read", |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +func (n networkDefaultSecurityRuleWrapper) PredefinedRole() string { |
| 261 | + return "Reader" |
| 262 | +} |
0 commit comments