Giter Club home page Giter Club logo

flush-nsg's Introduction

Flush Connections on a Network Security Group

REST example of how to flush an Network Security Group (NSG) rules to allow for immediate re-evaluation

Rationale

Network Security Group (NSG) rules are executed against new network flows. If there is an on-going network flow and a new Deny rule is added, then this will not have an affect on the network flow. In some circumstances, it is useful to flush the connection to allow the rule to be re-evaluated.

Solution

There is a REST interface that allows the update of an NSG. This may be used to update the NSG. This sample uses the Rest Client from Visual Studio Code.

Authenticate

This uses an Azure service principal that has enough permissions to do the job.

az ad sp create-for-rbac --scopes /subscriptions/your-subscription-id  --name "sp-name" --role "Contributor"

This returns a clientId and secret. These can be used below

### variables for my personal subscription  - you will need to change these
@directoryid = your-tenant-id
@clientid = your-service-principal-client-id
@secret = your-service-principal-client-secret
@subscriptionid = your-subscription-id

### get a bearer token
# @name login
POST https://login.microsoftonline.com/{{directoryid}}/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

client_id={{clientid}}
&scope=https://management.core.windows.net/.default
&client_secret={{secret}}
&grant_type=client_credentials

### grab the access token
@authToken = {{login.response.body.access_token}}

The variable authToken should contain the bearer token

List the Network Secrurity Groups in your subscription

### get list of NSGs for this subscription
GET https://management.azure.com/subscriptions/{{subscriptionid}}/providers/Microsoft.Network/networkSecurityGroups?api-version=2022-11-01
Authorization: Bearer {{authToken}}

Get the details of a specific NSG

### set these for later queries
@resourceGroupName = your-nsg-resource-group
@networkSecurityGroupName = your-nsg-name
@location = westeurope

### get a specific NSG
GET https://management.azure.com/subscriptions/{{subscriptionid}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Network/networkSecurityGroups/{{networkSecurityGroupName}}?api-version=2022-11-01
Authorization: Bearer {{authToken}}

Update a specific Network Security Group

### update a specic NSG to flush connections
PUT https://management.azure.com/subscriptions/{{subscriptionid}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Network/networkSecurityGroups/{{networkSecurityGroupName}}?api-version=2022-11-01
Authorization: Bearer {{authToken}}
Content-Type: application/json

{
    "id": "/subscriptions/{{subscriptionid}}/resourceGroups/{{resourceGroupName}}/providers/Microsoft.Network/networkSecurityGroups/{{networkSecurityGroupName}}",
    "location": "{{location}}",
    "properties": {
        "flushConnection": true
    }
}

The response from this should contain "provisioningState": "Succeeded".

The REST client code

This is contained in the file rest-flush-nsg-connection.

flush-nsg's People

Contributors

jometzg avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.