Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/22-add-cli #22

Merged
merged 8 commits into from
Oct 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Document/how-to-generate-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# How-To

This document describes how to develop for `BSN.IpTables.Cli`

## Some considration

* warning | PreCheck/AllOfWhenYouMeantRef _is using an 'allOf' instead of a $ref. This creates a wasteful anonymous type when generating code._
* Resolve warning: as you can see in <https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1488> and <https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/issues/13> we must to do not use `options.UseAllOfToExtendReferenceSchemas();` in my code.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# IpTables.Api

API for local iptables management

IpTables.Api is the RESTful server which applies iptables rules to block or allow IP addresses on request from a remote client.
@@ -9,10 +10,10 @@ This project is powered on [IPTables.Net](https://github.com/splitice/IPTables.N

## How iptables Work

**Note: This below article is just copy from https://phoenixnap.com/kb/iptables-tutorial-linux-firewall**
**Note: This below article is just copy from <https://phoenixnap.com/kb/iptables-tutorial-linux-firewall>**

![IpTablesFlow](Document/IpTablesWorkFlow.webp "IpTables Work Flow")
*Source: https://www.booleanworld.com/depth-guide-iptables-linux-firewall/*
*Source: <https://www.booleanworld.com/depth-guide-iptables-linux-firewall/>*

Network traffic is made up of packets. Data is broken up into smaller pieces (called packets), sent over a network, then put back together. Iptables identifies the packets received and then uses a set of rules to decide what to do with them.

@@ -36,7 +37,7 @@ The Filter table is the most frequently used one. It acts as a bouncer, deciding
* Input: the rules in this chain control the packets received by the server.
* Output: this chain controls the packets for outbound traffic.
* Forward: this set of rules controls the packets that are routed through the server.

#### 2. Network Address Translation (NAT)

This table contains NAT (Network Address Translation) rules for routing packets to networks that cannot be accessed directly. When the destination or source of the packet has to be altered, the NAT table is used. It includes the following chains:
@@ -78,6 +79,7 @@ To run, follow below steps
1. simply run web service with `Source/BSN.IpTables.Api/bin/Release/net6.0/BSN.IpTables.Api --urls=http://localhost:8002`

### Kamailio Example

```kamailio
loadmodule "http_client.so"
loadmodule "htable.so"
17 changes: 16 additions & 1 deletion Source/BSN.IpTables.Api/ConfigureSwaggerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
@@ -40,7 +41,21 @@ public void Configure(SwaggerGenOptions options)
Type = "string",
Example = new OpenApiString("00:00:00")
});
options.UseAllOfToExtendReferenceSchemas();

// Because capable to use autorest we must to diable AllOf
// for more information please see https://stackoverflow.com/q/59788412/1539100
// and https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/issues/13
// options.UseAllOfToExtendReferenceSchemas();

// operationId is an optional unique string used to identify an operation.
// If provided, these IDs must be unique among all operations described in your API.
//
// However, AutoRest seems to use that to identify each method.
// I found a Github question / issue: <see href:https://github.com/Azure/autorest/issues/2647/>
// where people addressed this by configuring AutoRest to use tags instead of operation ID to identify method.
//
// <see href:https://stackoverflow.com/a/60875558/1539100/>
options.CustomOperationIds(description => (description.ActionDescriptor as ControllerActionDescriptor)?.ActionName);
}

/// <summary>
1 change: 0 additions & 1 deletion Source/BSN.IpTables.Api/Controllers/V1/HomeController.cs
Original file line number Diff line number Diff line change
@@ -72,7 +72,6 @@ public async Task<ActionResult<Response>> Append([FromQuery] RulesCommandService

[HttpDelete]
[Route("Delete")]
[Route("")]
[ProducesResponseType(typeof(Response), (int)HttpStatusCode.OK)]
public async Task<ActionResult<Response>> Delete([FromQuery] RulesCommandServiceDeleteRequest request)
{
316 changes: 19 additions & 297 deletions Source/BSN.IpTables.Api/swagger.json

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Source/BSN.IpTables.Cli/Generate-PowerShellClient.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ITNOA

# TODO: Make sure nodejs install correctlly
# nvm list available
# nvm install 18.16.1
# nvm use 18.16.1

npm install -g "autorest"
autorest configuration.yaml

# Build Module
Write-Host $PSScriptRoot
& $PSScriptRoot\generated\build-module.ps1
272 changes: 272 additions & 0 deletions Source/BSN.IpTables.Cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<!-- region Generated -->
# BSN.IpTables.Cli

This directory contains the PowerShell module for the BSN.IpTables.Api service.

---

## Status

[![BSN.IpTables.Cli](https://img.shields.io/powershellgallery/v/BSN.IpTables.Cli.svg?style=flat-square&label=BSN.IpTables.Cli "BSN.IpTables.Cli")](https://www.powershellgallery.com/packages/BSN.IpTables.Cli/)

## Info

- Modifiable: yes
- Generated: all
- Committed: yes
- Packaged: yes

---

## Detail

This module was primarily generated via [AutoRest](https://github.com/Azure/autorest) using the [PowerShell](https://github.com/Azure/autorest.powershell) extension.

## Module Requirements

- None

## Development

For information on how to develop for `Az.Databricks`, see [how-to-generate-cli.md](../../Document/how-to-generate-cli.md).
<!-- endregion -->

---

### AutoRest Configuration

> see <https://aka.ms/autorest>
``` yaml
branch: a078cebc3964c8968d141906c613794ca0453861
require:
- $(this-folder)/../readme.azure.noprofile.md
- $(repo)/specification/databricks/resource-manager/readme.md
try-require:
- $(repo)/specification/databricks/resource-manager/readme.powershell.md

module-version: 1.2.0
title: Databricks
subject-prefix: $(service-name)

inlining-threshold: 100

resourcegroup-append: true
identity-correction-for-post: true
nested-object-to-string: true

directive:
# Remove cmdlet, Private link related resource should be ignored.
- where:
subject: PrivateEndpointConnection|PrivateLinkResource
remove: true
# Remove the unexpanded parameter set
- where:
variant: ^Create$|^CreateViaIdentityExpanded$|^Update$|^UpdateViaIdentity$
remove: true
# Hide CreateViaIdentity for customization
- where:
variant: ^CreateViaIdentity$
hide: true

# Rename the parameter name to follow Azure PowerShell best practice
- where:
parameter-name: SkuName
set:
parameter-name: Sku
- where:
parameter-name: CustomVirtualNetworkIdValue
set:
parameter-name: VirtualNetworkId
- where:
parameter-name: CustomPublicSubnetNameValue
set:
parameter-name: PublicSubnetName
- where:
parameter-name: CustomPrivateSubnetNameValue
set:
parameter-name: PrivateSubnetName
- where:
parameter-name: PrepareEncryptionValue
set:
parameter-name: PrepareEncryption
- where:
parameter-name: ValueKeySource
set:
parameter-name: EncryptionKeySource
- where:
parameter-name: ValueKeyName
set:
parameter-name: EncryptionKeyName
- where:
parameter-name: ValueKeyVersion
set:
parameter-name: EncryptionKeyVersion
- where:
parameter-name: ValueKeyVaultUri
set:
parameter-name: EncryptionKeyVaultUri
- where:
parameter-name: RequireInfrastructureEncryptionValue
set:
parameter-name: RequireInfrastructureEncryption
- where:
parameter-name: PeeringName
set:
parameter-name: Name
- where:
parameter-name: AmlWorkspaceIdValue
set:
parameter-name: AmlWorkspaceId

- where:
parameter-name: EnableNoPublicIPValue
set:
parameter-name: EnableNoPublicIP
- where:
parameter-name: PublicIPNameValue
set:
parameter-name: PublicIPName

- where:
parameter-name: KeyVaultPropertyKeyName
set:
parameter-name: KeyVaultKeyName
- where:
parameter-name: KeyVaultPropertyKeyVaultUri
set:
parameter-name: KeyVaultUri
- where:
parameter-name: KeyVaultPropertyKeyVersion
set:
parameter-name: KeyVaultKeyVersion

- where:
parameter-name: LoadBalancerBackendPoolNameValue
set:
parameter-name: LoadBalancerBackendPoolName
- where:
parameter-name: LoadBalancerIdValue
set:
parameter-name: LoadBalancerId

- where:
parameter-name: NatGatewayNameValue
set:
parameter-name: NatGatewayName

- where:
parameter-name: StorageAccountNameValue
set:
parameter-name: StorageAccountName

- where:
parameter-name: StorageAccountSkuNameValue
set:
parameter-name: StorageAccountSku

- where:
parameter-name: VnetAddressPrefixValue
set:
parameter-name: VnetAddressPrefix

# Update property names related to CMK
- where:
model-name: Workspace
property-name: ValueKeyName
set:
property-name: EncryptionKeyName
- where:
model-name: Workspace
property-name: ValueKeySource
set:
property-name: EncryptionKeySource
- where:
model-name: Workspace
property-name: ValueKeyVaultUri
set:
property-name: EncryptionKeyVaultUri
- where:
model-name: Workspace
property-name: ValueKeyVersion
set:
property-name: EncryptionKeyVersion
- where:
model-name: Workspace
property-name: PrepareEncryptionValue
set:
property-name: PrepareEncryption
- where:
model-name: Workspace
property-name: RequireInfrastructureEncryptionValue
set:
property-name: RequireInfrastructureEncryption
- where:
model-name: Workspace
property-name: EnableNoPublicIPValue
set:
property-name: EnableNoPublicIP

# Rename parameters of VNetPeering cmdlet
- where:
parameter-name: DatabrickAddressSpaceAddressPrefix
set:
parameter-name: DatabricksAddressSpacePrefix
- where:
parameter-name: RemoteAddressSpaceAddressPrefix
set:
parameter-name: RemoteAddressSpacePrefix
- where:
parameter-name: DatabrickVirtualNetworkId
set:
parameter-name: DatabricksVirtualNetworkId

- where:
subject: AccessConnector
parameter-name: ConnectorName
set:
parameter-name: Name
- where:
verb: New
subject: AccessConnector
parameter-name: IdentityUserAssignedIdentity
set:
parameter-name: UserAssignedIdentity

# Remove the set Workspace cmdlet
- where:
verb: Set
subject: Workspace
remove: true

# Remove the set AccessConnector cmdlet
- where:
verb: Set
subject: AccessConnector
remove: true

# Hide the New/Update Workspace cmdlet for customization
- where:
verb: New|Update
subject: Workspace
hide: true
# Hide the Set VNetPeering cmdlet for customization
- where:
verb: Set
subject: VNetPeering
hide: true
set:
verb: Update

- where:
model-name: Workspace
set:
format-table:
properties:
- Name
- ResourceGroupName
- Location
- ManagedResourceGroupId
labels:
ManagedResourceGroupId: Managed Resource Group ID
```
20 changes: 20 additions & 0 deletions Source/BSN.IpTables.Cli/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
branch: master

module-version: 1.2.0
title: BSN.IpTables.Cli
subject-prefix: $(service-name)

inlining-threshold: 100

tag: v1
input-file: ../BSN.IpTables.Api/swagger.json
powershell: true

use:
- '@autorest/powershell@4.0.648'

namespace: BSN.IpTables.V1
verbose: true
resourcegroup-append: true
identity-correction-for-post: true
nested-object-to-string: true
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using AutoMapper;
using BSN.IpTables.Presentation.Dto.V1.ViewModels;
using IPTables.Net.Iptables;
using IPTables.Net.Iptables.Modules;
using IPTables.Net.Iptables.Modules.Core;
using System;
using System.Linq;

@@ -44,9 +46,15 @@ private class IpTablesRuleViewModelConverter : ITypeConverter<IpTablesRule, IpTa
{
public IpTablesRuleViewModel Convert(IpTablesRule source, IpTablesRuleViewModel destination, ResolutionContext context)
{
string rule = source.ToString();
// TODO: Check if does not CoreModule
var detailedSource = (source.ModuleData.FirstOrDefault() as CoreModule) ?? throw new NullReferenceException("Source must be not null");
return new IpTablesRuleViewModel()
{
SourceIp = detailedSource.Source.Null ? string.Empty : detailedSource.Source.Value.Address.ToString(),
DestinationIp = detailedSource.Destination.Null ? string.Empty : detailedSource.Destination.Value.Address.ToString(),
Protocol = detailedSource.Protocol.Null ? string.Empty : detailedSource.Protocol.Value.ToString(),
Target = detailedSource.Target.ToString(),
InterfaceName = (detailedSource.InInterface.Value ?? detailedSource.OutInterface.Value)?.ToString() ?? string.Empty,
};
}
}